/*
 * 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.XQueryNCName;
import uk.co.reecedunn.intellij.plugin.xquery.ast.xquery.XQuerySchemaImport;
import uk.co.reecedunn.intellij.plugin.xquery.ast.xquery.XQuerySchemaPrefix;
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 XQuerySchemaImportPsiImpl extends ASTWrapperPsiElement implements XQuerySchemaImport, XQueryNamespaceResolver {
    public XQuerySchemaImportPsiImpl(@NotNull ASTNode node) {
        super(node);
    }

    @Override
    public Option<XQueryNamespace> resolveNamespace(CharSequence prefix) {
        XQuerySchemaPrefix schema = findChildByType(XQueryElementType.SCHEMA_PREFIX);
        if (schema == null) {
            return Option.none();
        }

        PsiElement name = schema.getFirstChild();
        while (name != null) {
            if (name instanceof XQueryNCName && ((XQueryNCName)name).getLocalNameElement().getText().equals(prefix)) {
                PsiElement element = findChildByType(XQueryElementType.URI_LITERAL);
                return Option.some(new XQueryNamespace(((XQueryNCName)name).getLocalNameElement(), element, this));
            }
            name = name.getNextSibling();
        }
        return Option.none();
    }
}

@@ -1919,54 +1919,151 @@
 Name
- && ((XQueryNCName)name).getLocalNameElement()
+) %7B%0A                Option%3CXQueryNamespace%3E ns = ((XQueryNCName)name).getLocalName().flatMap((localName) -%3E %7B%0A                    if (localName
 .get
@@ -2083,24 +2083,32 @@
 (prefix)) %7B%0A
+        
             
@@ -2188,32 +2188,40 @@
                 
+       
+ 
 return Option.so
@@ -2247,68 +2247,207 @@
 ace(
-((XQueryNCName)name).getLocalNameElement(), element, this));
+localName, element, this));%0A                    %7D%0A                    return Option.none();%0A                %7D);%0A%0A                if (ns.isDefined()) %7B%0A                    return ns;%0A                %7D
 %0A   

Use the Option version of getLocalName.