Search in sources :

Example 6 with XmlNSDescriptorImpl

use of com.intellij.xml.impl.schema.XmlNSDescriptorImpl in project intellij-community by JetBrains.

the class MavenDomElementDescriptorHolder method doCreateDescriptor.

@Nullable
private XmlNSDescriptorImpl doCreateDescriptor(FileKind kind) {
    String schemaUrl = kind.getSchemaUrl();
    String location = ExternalResourceManager.getInstance().getResourceLocation(schemaUrl);
    if (schemaUrl.equals(location))
        return null;
    VirtualFile schema;
    try {
        schema = VfsUtil.findFileByURL(new URL(location));
    } catch (MalformedURLException ignore) {
        return null;
    }
    if (schema == null)
        return null;
    PsiFile psiFile = PsiManager.getInstance(myProject).findFile(schema);
    if (!(psiFile instanceof XmlFile))
        return null;
    XmlNSDescriptorImpl result = new XmlNSDescriptorImpl();
    result.init(psiFile);
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) MalformedURLException(java.net.MalformedURLException) XmlFile(com.intellij.psi.xml.XmlFile) PsiFile(com.intellij.psi.PsiFile) XmlNSDescriptorImpl(com.intellij.xml.impl.schema.XmlNSDescriptorImpl) URL(java.net.URL) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with XmlNSDescriptorImpl

use of com.intellij.xml.impl.schema.XmlNSDescriptorImpl in project intellij-community by JetBrains.

the class XsltContextProviderBase method fillFromSchema.

private static void fillFromSchema(PsiFile file, ElementNames names) {
    if (!(file instanceof XmlFile))
        return;
    final XmlFile f = (XmlFile) file;
    final XmlDocument d = f.getDocument();
    if (d == null)
        return;
    final XmlTag rootTag = d.getRootTag();
    if (rootTag == null)
        return;
    //noinspection unchecked
    names.dependencies.add(new NSDeclTracker(rootTag));
    try {
        final Map<String, String> namespaceDeclarations = rootTag.getLocalNamespaceDeclarations();
        final Collection<String> prefixes = namespaceDeclarations.keySet();
        final XmlElementFactory ef = XmlElementFactory.getInstance(file.getProject());
        int noSchemaNamespaces = 0;
        for (String prefix : prefixes) {
            final String namespace = namespaceDeclarations.get(prefix);
            if (isIgnoredNamespace(prefix, namespace))
                continue;
            final XmlTag tag = ef.createTagFromText("<dummy-tag xmlns='" + namespace + "' />", XMLLanguage.INSTANCE);
            final XmlDocument document = PsiTreeUtil.getParentOfType(tag, XmlDocument.class);
            final XmlNSDescriptor rootDescriptor = tag.getNSDescriptor(tag.getNamespace(), true);
            if (rootDescriptor == null || (rootDescriptor instanceof XmlNSDescriptorImpl && ((XmlNSDescriptorImpl) rootDescriptor).getTag() == null) || !rootDescriptor.getDeclaration().isPhysical()) {
                final QName any = QNameUtil.createAnyLocalName(namespace);
                names.elementNames.add(any);
                names.attributeNames.add(any);
                noSchemaNamespaces++;
                continue;
            }
            //noinspection unchecked
            names.dependencies.add(rootDescriptor.getDescriptorFile());
            //noinspection unchecked
            final Set<XmlElementDescriptor> history = new THashSet<>(150);
            final XmlElementDescriptor[] e = rootDescriptor.getRootElementsDescriptors(document);
            try {
                for (XmlElementDescriptor descriptor : e) {
                    processElementDescriptors(descriptor, tag, names, history, 0);
                }
            } catch (StopProcessingException e1) {
                Logger.getInstance(XsltContextProviderBase.class).error("Maximum recursion depth reached. Missing equals()/hashCode() implementation?", StringUtil.join(history, descriptor -> descriptor.getClass().getName() + "[" + descriptor.getQualifiedName() + "]", ", "));
            }
        }
        names.validateNames = names.elementNames.size() > noSchemaNamespaces;
    //            final QName any = QNameUtil.createAnyLocalName("");
    //            names.elementNames.add(any);
    //            names.attributeNames.add(any);
    } catch (IncorrectOperationException e) {
        Logger.getInstance(XsltContextProvider.class.getName()).error(e);
    }
}
Also used : NSDeclTracker(org.intellij.lang.xpath.xslt.util.NSDeclTracker) QName(javax.xml.namespace.QName) THashSet(gnu.trove.THashSet) XmlNSDescriptor(com.intellij.xml.XmlNSDescriptor) IncorrectOperationException(com.intellij.util.IncorrectOperationException) XmlNSDescriptorImpl(com.intellij.xml.impl.schema.XmlNSDescriptorImpl) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor)

Example 8 with XmlNSDescriptorImpl

use of com.intellij.xml.impl.schema.XmlNSDescriptorImpl in project intellij-community by JetBrains.

the class XmlSchemaTest method testAny2.

//public void testAny1() throws Exception {
//  XmlDocumentDescriptor documentDescriptor = createDescriptorImpl(
//      "<xsd:schema targetNamespace=\"http://foo\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" >" +
//      "  <xsd:element name=\"root\">" +
//      "    <xsd:complexType>" +
//      "      <xsd:sequence minOccurs=\"1\" maxOccurs=\"1\">" +
//      "        <xsd:any namespace=\"##other\" minOccurs=\"0\" maxOccurs=\"unbounded\" processContents=\"skip\"/>" +
//      "      </xsd:sequence>" +
//      "    </xsd:complexType>" +
//      "  </xsd:element>" +
//      "</xsd:schema>"
//  );
//
//  XmlFile xmlFile = (XmlFile)createFile("file.xml",
//      "<root xmlns=\"http://foo\">" +
//      "  <a:a xmlns:a=\"http://bar\" />" +
//      "</root>"
//  );
//
//  XmlElementDescriptor rootDescriptor = documentDescriptor.getElementDescriptor(xmlFile.saveToString().getRootTag());
//  assertNotNull(rootDescriptor);
//
//  XmlTag aTag = xmlFile.saveToString().getRootTag().findSubTag("a:a");
//  assertNotNull(aTag);
//  XmlElementDescriptor aDescriptor = documentDescriptor.getElementDescriptor(aTag);
//  assertNotNull(aDescriptor);
//}
public void testAny2() throws Exception {
    PsiFile dtdFile = createFile("test.xml", "<xsd:schema targetNamespace=\"http://foo\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" >" + "  <xsd:element name=\"root\">" + "    <xsd:complexType>" + "      <xsd:sequence minOccurs=\"1\" maxOccurs=\"1\">" + "        <xsd:any namespace=\"##other\" minOccurs=\"0\" maxOccurs=\"unbounded\" processContents=\"skip\"/>" + "      </xsd:sequence>" + "    </xsd:complexType>" + "  </xsd:element>" + "</xsd:schema>");
    XmlNSDescriptor NSDescriptor = new XmlNSDescriptorImpl((XmlFile) dtdFile);
    XmlFile xmlFile = (XmlFile) createFile("file.xml", "<foo:root xmlns:foo=\"http://foo\">" + "  <foo:a xmlns:a=\"http://bar\" />" + "</foo:root>");
    XmlElementDescriptor rootDescriptor = NSDescriptor.getElementDescriptor(xmlFile.getDocument().getRootTag());
    assertNotNull(rootDescriptor);
    XmlTag aTag = xmlFile.getDocument().getRootTag().findFirstSubTag("foo:a");
    assertNotNull(aTag);
//XmlElementDescriptor aDescriptor = NSDescriptor.getElementDescriptor(aTag);
//assertNull(aDescriptor);
}
Also used : XmlNSDescriptor(com.intellij.xml.XmlNSDescriptor) XmlFile(com.intellij.psi.xml.XmlFile) PsiFile(com.intellij.psi.PsiFile) XmlNSDescriptorImpl(com.intellij.xml.impl.schema.XmlNSDescriptorImpl) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) XmlTag(com.intellij.psi.xml.XmlTag)

Example 9 with XmlNSDescriptorImpl

use of com.intellij.xml.impl.schema.XmlNSDescriptorImpl in project intellij-community by JetBrains.

the class TypeOrElementOrAttributeReference method getDescriptor.

XmlNSDescriptorImpl getDescriptor(final XmlTag tag, String text, boolean[] redefined) {
    if (myType != ReferenceType.ElementReference && myType != ReferenceType.AttributeReference) {
        final PsiElement parentElement = myElement.getContext();
        final PsiElement grandParentElement = parentElement != null ? parentElement.getParent() : null;
        boolean doRedefineCheck = false;
        if (parentElement instanceof XmlAttribute && grandParentElement instanceof XmlTag) {
            final String attrName = ((XmlAttribute) parentElement).getName();
            final String tagLocalName = ((XmlTag) grandParentElement).getLocalName();
            doRedefineCheck = (SchemaReferencesProvider.REF_ATTR_NAME.equals(attrName) && (SchemaReferencesProvider.GROUP_TAG_NAME.equals(tagLocalName) || SchemaReferencesProvider.ATTRIBUTE_GROUP_TAG_NAME.equals(tagLocalName))) || (SchemaReferencesProvider.BASE_ATTR_NAME.equals(attrName) || SchemaReferencesProvider.MEMBER_TYPES_ATTR_NAME.equals(attrName));
        }
        if (doRedefineCheck) {
            XmlNSDescriptorImpl redefinedDescriptor = SchemaReferencesProvider.findRedefinedDescriptor(tag, text);
            if (redefinedDescriptor != null) {
                redefined[0] = true;
                return redefinedDescriptor;
            }
        }
    }
    final String namespace = getNamespace(tag, text);
    XmlNSDescriptor nsDescriptor = tag.getNSDescriptor(namespace, true);
    final PsiFile file = tag.getContainingFile();
    if (!(file instanceof XmlFile))
        return null;
    final XmlDocument document = ((XmlFile) file).getDocument();
    if (nsDescriptor == null) {
        // import
        nsDescriptor = (XmlNSDescriptor) document.getMetaData();
    }
    if (nsDescriptor == null) {
        final XmlNSDescriptor[] descrs = new XmlNSDescriptor[1];
        URLReference.processWsdlSchemas(document.getRootTag(), xmlTag -> {
            if (namespace.equals(xmlTag.getAttributeValue(TARGET_NAMESPACE))) {
                descrs[0] = (XmlNSDescriptor) xmlTag.getMetaData();
                return false;
            }
            return true;
        });
        if (descrs[0] instanceof XmlNSDescriptorImpl)
            return (XmlNSDescriptorImpl) descrs[0];
    }
    return nsDescriptor instanceof XmlNSDescriptorImpl ? (XmlNSDescriptorImpl) nsDescriptor : null;
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlNSDescriptor(com.intellij.xml.XmlNSDescriptor) XmlFile(com.intellij.psi.xml.XmlFile) PsiFile(com.intellij.psi.PsiFile) XmlDocument(com.intellij.psi.xml.XmlDocument) XmlNSDescriptorImpl(com.intellij.xml.impl.schema.XmlNSDescriptorImpl) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 10 with XmlNSDescriptorImpl

use of com.intellij.xml.impl.schema.XmlNSDescriptorImpl in project intellij-community by JetBrains.

the class XmlUtil method findXmlDescriptorByType.

@Nullable
public static XmlElementDescriptor findXmlDescriptorByType(final XmlTag xmlTag, @Nullable XmlTag context) {
    String type = xmlTag.getAttributeValue("type", XML_SCHEMA_INSTANCE_URI);
    if (type == null) {
        String ns = xmlTag.getNamespace();
        if (ourSchemaUrisList.indexOf(ns) >= 0) {
            type = xmlTag.getAttributeValue("type", null);
        }
    }
    XmlElementDescriptor elementDescriptor = null;
    if (type != null) {
        final String namespaceByPrefix = findNamespaceByPrefix(findPrefixByQualifiedName(type), xmlTag);
        XmlNSDescriptor typeDecr = xmlTag.getNSDescriptor(namespaceByPrefix, true);
        if (typeDecr == null && namespaceByPrefix.isEmpty()) {
            if (context != null)
                typeDecr = context.getNSDescriptor("", true);
            if (typeDecr == null) {
                final PsiFile containingFile = xmlTag.getContainingFile();
                if (containingFile instanceof XmlFile) {
                    final XmlDocument document = ((XmlFile) containingFile).getDocument();
                    if (document != null)
                        typeDecr = (XmlNSDescriptor) document.getMetaData();
                }
            }
        }
        if (typeDecr instanceof XmlNSDescriptorImpl) {
            final XmlNSDescriptorImpl schemaDescriptor = (XmlNSDescriptorImpl) typeDecr;
            elementDescriptor = schemaDescriptor.getDescriptorByType(type, xmlTag);
        }
    }
    return elementDescriptor;
}
Also used : XmlNSDescriptorImpl(com.intellij.xml.impl.schema.XmlNSDescriptorImpl) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

XmlNSDescriptorImpl (com.intellij.xml.impl.schema.XmlNSDescriptorImpl)12 XmlTag (com.intellij.psi.xml.XmlTag)5 XmlNSDescriptor (com.intellij.xml.XmlNSDescriptor)5 XmlFile (com.intellij.psi.xml.XmlFile)4 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)4 PsiFile (com.intellij.psi.PsiFile)3 XmlDocument (com.intellij.psi.xml.XmlDocument)3 Nullable (org.jetbrains.annotations.Nullable)3 PsiMetaData (com.intellij.psi.meta.PsiMetaData)2 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)2 ExternalResourceManager (com.intellij.javaee.ExternalResourceManager)1 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiElement (com.intellij.psi.PsiElement)1 XmlAttribute (com.intellij.psi.xml.XmlAttribute)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 ComplexTypeDescriptor (com.intellij.xml.impl.schema.ComplexTypeDescriptor)1 TypeDescriptor (com.intellij.xml.impl.schema.TypeDescriptor)1 THashSet (gnu.trove.THashSet)1 MalformedURLException (java.net.MalformedURLException)1