Search in sources :

Example 1 with NamespaceContext

use of org.intellij.lang.xpath.context.NamespaceContext in project intellij-community by JetBrains.

the class CompletionLists method addContextNames.

private static XPathNodeTest.PrincipalType addContextNames(XPathNodeTest element, ContextProvider contextProvider, PrefixedName prefixedName, Set<Lookup> list) {
    final NamespaceContext namespaceContext = contextProvider.getNamespaceContext();
    final XmlElement context = contextProvider.getContextElement();
    final XPathNodeTest.PrincipalType principalType = element.getPrincipalType();
    if (principalType == XPathNodeTest.PrincipalType.ELEMENT) {
        final Set<QName> elementNames = contextProvider.getElements(false);
        if (elementNames != null) {
            for (QName pair : elementNames) {
                if ("*".equals(pair.getLocalPart()))
                    continue;
                if (namespaceMatches(prefixedName, pair.getNamespaceURI(), namespaceContext, context, true)) {
                    if (prefixedName.getPrefix() == null && namespaceContext != null) {
                        final String p = namespaceContext.getPrefixForURI(pair.getNamespaceURI(), context);
                        list.add(new NodeLookup(makePrefix(p) + pair.getLocalPart(), XPathNodeTest.PrincipalType.ELEMENT));
                    } else {
                        list.add(new NodeLookup(pair.getLocalPart(), XPathNodeTest.PrincipalType.ELEMENT));
                    }
                }
            }
        }
    } else if (principalType == XPathNodeTest.PrincipalType.ATTRIBUTE) {
        final Set<QName> attributeNames = contextProvider.getAttributes(false);
        if (attributeNames != null) {
            for (QName pair : attributeNames) {
                if ("*".equals(pair.getLocalPart()))
                    continue;
                if (namespaceMatches(prefixedName, pair.getNamespaceURI(), namespaceContext, context, false)) {
                    if (prefixedName.getPrefix() == null && namespaceContext != null) {
                        final String p = namespaceContext.getPrefixForURI(pair.getNamespaceURI(), context);
                        list.add(new NodeLookup(makePrefix(p) + pair.getLocalPart(), XPathNodeTest.PrincipalType.ATTRIBUTE));
                    } else {
                        list.add(new NodeLookup(pair.getLocalPart(), XPathNodeTest.PrincipalType.ATTRIBUTE));
                    }
                }
            }
        }
    }
    return principalType;
}
Also used : NamespaceContext(org.intellij.lang.xpath.context.NamespaceContext) QName(javax.xml.namespace.QName) XmlElement(com.intellij.psi.xml.XmlElement)

Example 2 with NamespaceContext

use of org.intellij.lang.xpath.context.NamespaceContext in project intellij-community by JetBrains.

the class PrefixReferenceImpl method resolve.

@Nullable
public PsiElement resolve() {
    final ContextProvider provider = getElement().getXPathContext();
    final NamespaceContext namespaceContext = provider.getNamespaceContext();
    if (namespaceContext != null) {
        return namespaceContext.resolve(getCanonicalText(), provider.getContextElement());
    } else {
        return null;
    }
}
Also used : NamespaceContext(org.intellij.lang.xpath.context.NamespaceContext) ContextProvider(org.intellij.lang.xpath.context.ContextProvider) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with NamespaceContext

use of org.intellij.lang.xpath.context.NamespaceContext in project intellij-community by JetBrains.

the class PrefixReferenceImpl method isUnresolved.

public boolean isUnresolved() {
    if (!canResolve())
        return true;
    if (resolve() != null) {
        return false;
    }
    final ContextProvider provider = getElement().getXPathContext();
    final NamespaceContext namespaceContext = provider.getNamespaceContext();
    assert namespaceContext != null;
    final String prefix = getCanonicalText();
    return !namespaceContext.getKnownPrefixes(provider.getContextElement()).contains(prefix);
}
Also used : NamespaceContext(org.intellij.lang.xpath.context.NamespaceContext) ContextProvider(org.intellij.lang.xpath.context.ContextProvider)

Example 4 with NamespaceContext

use of org.intellij.lang.xpath.context.NamespaceContext in project intellij-community by JetBrains.

the class XsltExtractFunctionAction method getSettings.

protected RefactoringOptions getSettings(XPathExpression expression, Set<XPathExpression> matchingExpressions) {
    final String name = Messages.showInputDialog(expression.getProject(), "Function Name: ", getRefactoringName(), Messages.getQuestionIcon());
    final boolean[] b = new boolean[] { false };
    if (name != null) {
        final String[] parts = name.split(":", 2);
        if (parts.length < 2) {
            Messages.showMessageDialog(expression.getProject(), "Custom functions require a prefixed name", "Error", Messages.getErrorIcon());
            b[0] = true;
        }
        final XmlElement context = PsiTreeUtil.getContextOfType(expression, XmlElement.class);
        final NamespaceContext namespaceContext = expression.getXPathContext().getNamespaceContext();
        if (namespaceContext != null && context != null && namespaceContext.resolve(parts[0], context) == null) {
            Messages.showMessageDialog(expression.getProject(), "Prefix '" + parts[0] + "' is not defined", "Error", Messages.getErrorIcon());
            b[0] = true;
        }
    }
    return new RefactoringOptions() {

        @Override
        public boolean isCanceled() {
            return b[0];
        }

        @Override
        public String getName() {
            return name;
        }
    };
}
Also used : NamespaceContext(org.intellij.lang.xpath.context.NamespaceContext) XmlElement(com.intellij.psi.xml.XmlElement)

Example 5 with NamespaceContext

use of org.intellij.lang.xpath.context.NamespaceContext in project intellij-community by JetBrains.

the class TestNamespaceContext method install.

@TestOnly
public static void install(Disposable parent) {
    final NamespaceContext old = ContextProvider.DefaultProvider.NULL_NAMESPACE_CONTEXT;
    ContextProvider.DefaultProvider.NULL_NAMESPACE_CONTEXT = TestNamespaceContext.INSTANCE;
    Disposer.register(parent, new Disposable() {

        @Override
        public void dispose() {
            ContextProvider.DefaultProvider.NULL_NAMESPACE_CONTEXT = old;
        }
    });
}
Also used : Disposable(com.intellij.openapi.Disposable) NamespaceContext(org.intellij.lang.xpath.context.NamespaceContext) TestOnly(org.jetbrains.annotations.TestOnly)

Aggregations

NamespaceContext (org.intellij.lang.xpath.context.NamespaceContext)7 XmlElement (com.intellij.psi.xml.XmlElement)3 ContextProvider (org.intellij.lang.xpath.context.ContextProvider)3 QName (javax.xml.namespace.QName)2 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)1 Annotation (com.intellij.lang.annotation.Annotation)1 Disposable (com.intellij.openapi.Disposable)1 Pair (com.intellij.openapi.util.Pair)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiReference (com.intellij.psi.PsiReference)1 XPathFile (org.intellij.lang.xpath.XPathFile)1 Function (org.intellij.lang.xpath.context.functions.Function)1 Nullable (org.jetbrains.annotations.Nullable)1 TestOnly (org.jetbrains.annotations.TestOnly)1