/*
 * Copyright (C) 2016 Reece H. Dunn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package uk.co.reecedunn.intellij.plugin.xquery.psi.impl.xquery;

import com.intellij.extapi.psi.ASTWrapperPsiElement;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import uk.co.reecedunn.intellij.plugin.xquery.ast.xquery.XQueryDirAttributeList;
import uk.co.reecedunn.intellij.plugin.xquery.ast.xquery.XQueryQName;
import uk.co.reecedunn.intellij.plugin.xquery.functional.Option;
import uk.co.reecedunn.intellij.plugin.xquery.parser.XQueryElementType;
import uk.co.reecedunn.intellij.plugin.xquery.psi.XQueryNamespace;
import uk.co.reecedunn.intellij.plugin.xquery.psi.XQueryNamespaceResolver;

public class XQueryDirAttributeListPsiImpl extends ASTWrapperPsiElement implements XQueryDirAttributeList, XQueryNamespaceResolver {
    public XQueryDirAttributeListPsiImpl(@NotNull ASTNode node) {
        super(node);
    }

    @Override
    public Option<XQueryNamespace> resolveNamespace(CharSequence prefix) {
        PsiElement element = getFirstChild();
        while (element != null) {
            if (element instanceof XQueryQName) {
                XQueryQName name = (XQueryQName) element;
                if (name.getLocalNameElement().getText().equals(prefix)) {
                    PsiElement uri = element.getNextSibling();
                    while (uri != null) {
                        if (uri.getNode().getElementType() == XQueryElementType.QNAME) {
                            break;
                        } else if (uri.getNode().getElementType() == XQueryElementType.DIR_ATTRIBUTE_VALUE) {
                            return Option.some(new XQueryNamespace(name.getLocalNameElement(), uri, this));
                        }
                        uri = uri.getNextSibling();
                    }
                    return Option.some(new XQueryNamespace(name.getLocalNameElement(), null, this));
                }
            }
            element = element.getNextSibling();
        }
        return Option.none();
    }
}

@@ -1745,17 +1745,16 @@
 ryQName)
- 
 element;
@@ -1774,38 +1774,115 @@
     
-if (name.getLocalNameElement()
+Option%3CXQueryNamespace%3E ns = name.getLocalName().flatMap((localName) -%3E %7B%0A                    if (localName
 .get
@@ -1918,32 +1918,36 @@
                 
+  
+  
 PsiElement uri =
@@ -1947,23 +1947,20 @@
 t uri = 
-element
+name
 .getNext
@@ -1986,24 +1986,28 @@
             
+ 
+   
 while (uri !
@@ -2008,32 +2008,36 @@
 (uri != null) %7B%0A
+    
                 
@@ -2105,24 +2105,28 @@
 pe.QNAME) %7B%0A
+    
             
@@ -2172,16 +2172,20 @@
         
+    
 %7D else i
@@ -2282,32 +2282,36 @@
                 
+    
 return Option.so
@@ -2337,34 +2337,17 @@
 ace(
-name.getLocalNameElement()
+localName
 , ur
@@ -2349,32 +2349,36 @@
 e, uri, this));%0A
+    
                 
@@ -2411,16 +2411,20 @@
         
+    
 uri = ur
@@ -2455,34 +2455,40 @@
                 
-%7D%0A
+    %7D%0A  
                 
@@ -2483,32 +2483,34 @@
                 
+  
 return Option.so
@@ -2536,48 +2536,185 @@
 ace(
-name.getLocalNameElement(), null, this))
+localName, null, this));%0A                    %7D%0A                    return Option.none();%0A                %7D);%0A%0A                if (ns.isDefined()) %7B%0A                    return ns
 ;%0A  

Use the Option version of getLocalName.