public Object[] get(final PsiElement context, CompletionContext completionContext) {
      XmlFile containingFile = null;
      XmlFile descriptorFile = null;
      final XmlTag tag = PsiTreeUtil.getParentOfType(context, XmlTag.class);

      if (tag != null) {
        containingFile = (XmlFile) tag.getContainingFile();
        descriptorFile = findDescriptorFile(tag, containingFile);
      } else {
        final XmlDocument document = PsiTreeUtil.getParentOfType(context, XmlDocument.class);

        if (document != null) {
          containingFile = (XmlFile) document.getContainingFile();

          final FileType ft = containingFile.getFileType();

          if (ft != StdFileTypes.XML) {
            final String namespace =
                ft == StdFileTypes.XHTML || ft == StdFileTypes.JSPX
                    ? XmlUtil.XHTML_URI
                    : XmlUtil.HTML_URI;
            final XmlNSDescriptor nsDescriptor = document.getDefaultNSDescriptor(namespace, true);

            if (nsDescriptor != null) {
              descriptorFile = nsDescriptor.getDescriptorFile();
            }
          }
        }
      }

      if (descriptorFile != null) {
        final List<Object> results = new ArrayList<Object>();
        final boolean acceptSystemEntities = containingFile.getFileType() == StdFileTypes.XML;

        final PsiElementProcessor processor =
            new PsiElementProcessor() {
              public boolean execute(@NotNull final PsiElement element) {
                if (element instanceof XmlEntityDecl) {
                  final XmlEntityDecl xmlEntityDecl = (XmlEntityDecl) element;
                  if (xmlEntityDecl.isInternalReference() || acceptSystemEntities) {
                    final String name = xmlEntityDecl.getName();
                    final Object _item = getLookupItem(xmlEntityDecl);
                    results.add(_item == null ? name : _item);
                  }
                }
                return true;
              }
            };

        XmlUtil.processXmlElements(descriptorFile, processor, true);
        if (descriptorFile != containingFile && containingFile.getFileType() == StdFileTypes.XML) {
          final XmlProlog element = containingFile.getDocument().getProlog();
          if (element != null) XmlUtil.processXmlElements(element, processor, true);
        }

        return ArrayUtil.toObjectArray(results);
      }

      return ArrayUtil.EMPTY_OBJECT_ARRAY;
    }
 private static boolean checkSchemaNamespace(String name, XmlTag context) {
   final String namespace = context.getNamespaceByPrefix(XmlUtil.findPrefixByQualifiedName(name));
   if (namespace.length() > 0) {
     return checkSchemaNamespace(namespace);
   }
   return XSD_PREFIX.equals(XmlUtil.findPrefixByQualifiedName(name));
 }
  public XmlAttributeDescriptor getAttributeDescriptor(String attributeName, final XmlTag context) {
    String caseSensitiveAttributeName =
        !myCaseSensitive ? attributeName.toLowerCase() : attributeName;
    XmlAttributeDescriptor descriptor =
        super.getAttributeDescriptor(caseSensitiveAttributeName, context);
    if (descriptor == null)
      descriptor =
          RelaxedHtmlFromSchemaElementDescriptor.getAttributeDescriptorFromFacelets(
              attributeName, context);

    if (descriptor == null) {
      String prefix = XmlUtil.findPrefixByQualifiedName(attributeName);

      if ("xml"
          .equals(
              prefix)) { // todo this is not technically correct dtd document references namespaces
                         // but we should handle it at least for xml stuff
        XmlNSDescriptor nsdescriptor = context.getNSDescriptor(XmlUtil.XML_NAMESPACE_URI, true);
        if (nsdescriptor instanceof XmlNSDescriptorImpl) {
          descriptor =
              ((XmlNSDescriptorImpl) nsdescriptor)
                  .getAttribute(
                      XmlUtil.findLocalNameByQualifiedName(caseSensitiveAttributeName),
                      XmlUtil.XML_NAMESPACE_URI,
                      context);
        }
      }
    }
    if (descriptor == null && HtmlUtil.isHtml5Context(context)) {
      descriptor = myDelegate.getAttributeDescriptor(attributeName, context);
    }
    return descriptor;
  }
 private boolean isCertainTypeElement(
     XmlTagImpl xml, final String typeName, final String nsPrefix) {
   if (!isTypeElement(xml)) return false;
   final XmlAttribute name = getNameAttr(xml);
   if (name == null) return false;
   final String value = name.getValue();
   if (value == null) return false;
   final String localName = XmlUtil.findLocalNameByQualifiedName(value);
   return typeName.equals(localName) && nsPrefix.equals(XmlUtil.findPrefixByQualifiedName(value));
 }
 @Nullable
 private XmlFile retrieveFile(final String fileLocation, String version) {
   final String targetNs = XmlUtil.getTargetSchemaNsFromTag(this);
   if (fileLocation.equals(targetNs)) {
     return null;
   } else {
     final XmlFile file = XmlUtil.getContainingFile(this);
     final PsiFile psiFile =
         ExternalResourceManager.getInstance().getResourceLocation(fileLocation, file, version);
     return psiFile instanceof XmlFile ? (XmlFile) psiFile : null;
   }
 }
    public Object[] get(final PsiElement context, CompletionContext completionContext) {
      XmlTag tag = PsiTreeUtil.getParentOfType(context, XmlTag.class, false);
      if (tag != null) {
        final XmlTag simpleContent = XmlUtil.getSchemaSimpleContent(tag);
        if (simpleContent != null) {
          final HashSet<String> variants = new HashSet<String>();
          XmlUtil.collectEnumerationValues(simpleContent, variants);
          return ArrayUtil.toObjectArray(variants);
        }
      }

      return ArrayUtil.EMPTY_OBJECT_ARRAY;
    }
 @Nullable
 private PsiMetaOwner retrieveOwner(final XmlFile file, final String namespace) {
   if (file == null) {
     return namespace.equals(XmlUtil.getTargetSchemaNsFromTag(this)) ? this : null;
   }
   return file.getDocument();
 }
  public XmlNSDescriptor getNSDescriptor(final String namespace, boolean strict) {
    final XmlTag parentTag = getParentTag();

    if (parentTag == null && namespace.equals(XmlUtil.XHTML_URI)) {
      final XmlNSDescriptor descriptor = getDtdDescriptor(XmlUtil.getContainingFile(this));
      if (descriptor != null) {
        return descriptor;
      }
    }

    Map<String, CachedValue<XmlNSDescriptor>> map = initNSDescriptorsMap();
    final CachedValue<XmlNSDescriptor> descriptor = map.get(namespace);
    if (descriptor != null) {
      final XmlNSDescriptor value = descriptor.getValue();
      if (value != null) {
        return value;
      }
    }

    if (parentTag == null) {
      final XmlDocument parentOfType = PsiTreeUtil.getParentOfType(this, XmlDocument.class);
      if (parentOfType == null) {
        return null;
      }
      return parentOfType.getDefaultNSDescriptor(namespace, strict);
    }

    return parentTag.getNSDescriptor(namespace, strict);
  }
  @NotNull
  public PsiReference[] getReferences() {
    ProgressManager.checkCanceled();
    final ASTNode startTagName = XmlChildRole.START_TAG_NAME_FINDER.findChild(this);
    if (startTagName == null) return PsiReference.EMPTY_ARRAY;
    final ASTNode endTagName = XmlChildRole.CLOSING_TAG_NAME_FINDER.findChild(this);
    List<PsiReference> refs = new ArrayList<PsiReference>();
    String prefix = getNamespacePrefix();

    TagNameReference startTagRef =
        TagNameReference.createTagNameReference(this, startTagName, true);
    refs.add(startTagRef);
    if (prefix.length() > 0) {
      refs.add(createPrefixReference(startTagName, prefix, startTagRef));
    }
    if (endTagName != null) {
      TagNameReference endTagRef = TagNameReference.createTagNameReference(this, endTagName, false);
      refs.add(endTagRef);
      prefix = XmlUtil.findPrefixByQualifiedName(endTagName.getText());
      if (StringUtil.isNotEmpty(prefix)) {
        refs.add(createPrefixReference(endTagName, prefix, endTagRef));
      }
    }

    // ArrayList.addAll() makes a clone of the collection
    //noinspection ManualArrayToCollectionCopy
    for (PsiReference ref :
        ReferenceProvidersRegistry.getReferencesFromProviders(this, XmlTag.class)) {
      refs.add(ref);
    }

    return ContainerUtil.toArray(refs, new PsiReference[refs.size()]);
  }
 public static boolean shouldBeValidated(@NotNull XmlTag tag) {
   PsiElement parent = tag.getParent();
   if (parent instanceof XmlTag) {
     return !skipValidation(parent) && !XmlUtil.tagFromTemplateFramework(tag);
   }
   return true;
 }
  public String[] getEnumeratedValues(XmlElement context) {
    final XmlElementDescriptorImpl elementDescriptor =
        (XmlElementDescriptorImpl)
            XmlUtil.findXmlDescriptorByType(
                myTag,
                context != null ? PsiTreeUtil.getContextOfType(context, XmlTag.class, true) : null);

    if (elementDescriptor != null && elementDescriptor.getType() instanceof ComplexTypeDescriptor) {
      final EnumerationData data =
          getEnumeratedValuesImpl(
              ((ComplexTypeDescriptor) elementDescriptor.getType()).getDeclaration());
      final String s = getDefaultValue();

      if (s != null && s.length() > 0 && data == null) {
        return new String[] {s};
      }
      return data != null ? data.enumeratedValues : ArrayUtil.EMPTY_STRING_ARRAY;
    }

    final String namespacePrefix = myTag.getNamespacePrefix();
    XmlTag type =
        myTag.findFirstSubTag(
            ((namespacePrefix.length() > 0) ? namespacePrefix + ":" : "") + "simpleType");

    if (type != null) {
      final EnumerationData data = getEnumeratedValuesImpl(type);
      return data != null ? data.enumeratedValues : ArrayUtil.EMPTY_STRING_ARRAY;
    }

    return ArrayUtil.EMPTY_STRING_ARRAY;
  }
  private static @Nullable XmlTag findSpecialTag(
      @NonNls String name,
      @NonNls String specialName,
      XmlTag rootTag,
      XmlNSDescriptorImpl descriptor,
      HashSet<XmlTag> visited) {
    XmlNSDescriptorImpl nsDescriptor = getNSDescriptorToSearchIn(rootTag, name, descriptor);

    if (nsDescriptor != descriptor) {
      final XmlDocument document =
          nsDescriptor.getDescriptorFile() != null
              ? nsDescriptor.getDescriptorFile().getDocument()
              : null;
      if (document == null) return null;

      return findSpecialTag(
          XmlUtil.findLocalNameByQualifiedName(name),
          specialName,
          document.getRootTag(),
          nsDescriptor,
          visited);
    }

    if (visited == null) visited = new HashSet<XmlTag>(1);
    else if (visited.contains(rootTag)) return null;
    visited.add(rootTag);

    XmlTag[] tags = rootTag.getSubTags();

    return findSpecialTagIn(tags, specialName, name, rootTag, descriptor, visited);
  }
  @NotNull
  private static XmlFileHeader calcXmlFileHeader(final XmlFile file) {

    if (file instanceof PsiFileEx
        && ((PsiFileEx) file).isContentsLoaded()
        && file.getNode().isParsed()) {
      return computeHeaderByPsi(file);
    }

    if (!XmlUtil.isStubBuilding() && file.getFileType() == XmlFileType.INSTANCE) {
      VirtualFile virtualFile = file.getVirtualFile();
      if (virtualFile instanceof VirtualFileWithId) {
        ObjectStubTree tree =
            StubTreeLoader.getInstance().readFromVFile(file.getProject(), virtualFile);
        if (tree != null) {
          Stub root = tree.getRoot();
          if (root instanceof FileStub) {
            return ((FileStub) root).getHeader();
          }
        }
      }
    }

    if (!file.isValid()) return XmlFileHeader.EMPTY;

    return NanoXmlUtil.parseHeader(file);
  }
  private boolean checkElementNameEquivalence(
      String localName, String namespace, String fqn, XmlTag context) {
    final String localAttrName = XmlUtil.findLocalNameByQualifiedName(fqn);
    if (!localAttrName.equals(localName)) return false;
    final String attrNamespace =
        context.getNamespaceByPrefix(XmlUtil.findPrefixByQualifiedName(fqn));
    if (attrNamespace.equals(namespace)) return true;

    if (myTargetNamespace == null) {
      if (XmlUtil.EMPTY_URI.equals(attrNamespace)) return true;
    } else {
      final boolean b = myTargetNamespace.equals(namespace);
      if (b) return b;
      return context.getNSDescriptor(namespace, true)
          == this; // schema's targetNamespace could be different from file systemId
    }
    return false;
  }
  private void checkTagByDescriptor(final XmlTag tag) {
    String name = tag.getName();

    XmlElementDescriptor elementDescriptor;

    final PsiElement parent = tag.getParent();
    if (parent instanceof XmlTag) {
      XmlTag parentTag = (XmlTag) parent;

      elementDescriptor = XmlUtil.getDescriptorFromContext(tag);

      final XmlElementDescriptor parentDescriptor = parentTag.getDescriptor();

      if (parentDescriptor != null && elementDescriptor == null && shouldBeValidated(tag)) {
        if (tag instanceof HtmlTag) {
          // XmlEntitiesInspection inspection = getInspectionProfile(tag,
          // HtmlStyleLocalInspection.SHORT_NAME);
          // if (inspection != null /*&&
          // isAdditionallyDeclared(inspection.getAdditionalEntries(XmlEntitiesInspection.UNKNOWN_TAG), name)*/) {
          return;
          // }
        }

        addElementsForTag(
            tag,
            XmlErrorMessages.message("element.is.not.allowed.here", name),
            getTagProblemInfoType(tag),
            null);
        return;
      }

      if (elementDescriptor instanceof AnyXmlElementDescriptor || elementDescriptor == null) {
        elementDescriptor = tag.getDescriptor();
      }

      if (elementDescriptor == null) return;
    } else {
      // root tag
      elementDescriptor = tag.getDescriptor();

      if (elementDescriptor == null) {
        addElementsForTag(
            tag,
            XmlErrorMessages.message("element.must.be.declared", name),
            HighlightInfoType.WRONG_REF,
            null);
        return;
      }
    }

    checkRequiredAttributes(tag, name, elementDescriptor);

    if (elementDescriptor instanceof Validator) {
      //noinspection unchecked
      ((Validator<XmlTag>) elementDescriptor).validate(tag, this);
    }
  }
  private Collection<SchemaTypeInfo> gatherInheritors(XmlTagImpl xml) {
    XmlAttribute name = getNameAttr(xml);
    if (name == null || StringUtil.isEmptyOrSpaces(name.getValue())) return null;
    String localName = name.getValue();
    final boolean hasPrefix = localName.contains(":");
    localName = hasPrefix ? localName.substring(localName.indexOf(':') + 1) : localName;
    final String nsPrefix =
        hasPrefix ? name.getValue().substring(0, name.getValue().indexOf(':')) : null;

    final XmlFile file = XmlUtil.getContainingFile(xml);
    if (file == null) return null;
    final Project project = file.getProject();
    if (project == null) return null;

    final Set<SchemaTypeInfo> result = new HashSet<SchemaTypeInfo>();
    final ArrayDeque<SchemaTypeInfo> queue = new ArrayDeque<SchemaTypeInfo>();

    String nsUri;
    if (!hasPrefix) {
      nsUri = getDefaultNs(file);
    } else {
      nsUri = XmlUtil.findNamespaceByPrefix(nsPrefix, file.getRootTag());
    }
    if (nsUri == null) return null;

    queue.add(new SchemaTypeInfo(localName, true, nsUri));

    final PairConvertor<String, String, List<Set<SchemaTypeInfo>>> worker =
        SchemaTypeInheritanceIndex.getWorker(project, file.getContainingFile().getVirtualFile());
    while (!queue.isEmpty()) {
      final SchemaTypeInfo info = queue.removeFirst();
      final List<Set<SchemaTypeInfo>> childrenOfType =
          worker.convert(info.getNamespaceUri(), info.getTagName());
      for (Set<SchemaTypeInfo> infos : childrenOfType) {
        for (SchemaTypeInfo typeInfo : infos) {
          if (typeInfo.isIsTypeName()) {
            queue.add(typeInfo);
          }
          result.add(typeInfo);
        }
      }
    }
    return result;
  }
 private boolean isGeneratedFromDtd(XmlNSDescriptor defaultNSDescriptorInner) {
   if (defaultNSDescriptorInner == null) {
     return false;
   }
   XmlFile descriptorFile = defaultNSDescriptorInner.getDescriptorFile();
   if (descriptorFile == null) {
     return false;
   }
   @NonNls String otherName = XmlUtil.getContainingFile(this).getName() + ".dtd";
   return descriptorFile.getName().equals(otherName);
 }
  @Nullable
  private XmlNSDescriptor getNsDescriptorFormDocType(
      final XmlDoctype doctype, final XmlFile containingFile, final boolean forHtml) {
    XmlNSDescriptor descriptor = getNSDescriptorFromMetaData(doctype.getMarkupDecl(), true);

    final String filePath = getFilePathForLogging(containingFile);

    final String dtdUri = XmlUtil.getDtdUri(doctype);
    LOG.debug(
        "DTD url for doctype " + doctype.getText() + " in file " + filePath + " is " + dtdUri);

    if (dtdUri != null && !dtdUri.isEmpty()) {
      XmlFile xmlFile = XmlUtil.findNamespace(containingFile, dtdUri);
      if (xmlFile == null) {
        // try to auto-detect it
        xmlFile = XmlNamespaceIndex.guessDtd(dtdUri, containingFile);
      }
      final String schemaFilePath = getFilePathForLogging(xmlFile);

      LOG.debug("Schema file for " + filePath + " is " + schemaFilePath);

      XmlNSDescriptor descriptorFromDtd =
          getNSDescriptorFromMetaData(xmlFile == null ? null : xmlFile.getDocument(), forHtml);

      LOG.debug(
          "Descriptor from meta data for schema file "
              + schemaFilePath
              + " is "
              + (descriptorFromDtd != null
                  ? descriptorFromDtd.getClass().getCanonicalName()
                  : "NULL"));

      if (descriptor != null && descriptorFromDtd != null) {
        descriptor =
            new XmlNSDescriptorSequence(new XmlNSDescriptor[] {descriptor, descriptorFromDtd});
      } else if (descriptorFromDtd != null) {
        descriptor = descriptorFromDtd;
      }
    }
    return descriptor;
  }
Example #19
0
 @Nullable
 private static XmlNSDescriptor getDtdDescriptor(@NotNull XmlFile containingFile) {
   final XmlDocument document = containingFile.getDocument();
   if (document == null) {
     return null;
   }
   final String url = XmlUtil.getDtdUri(document);
   if (url == null) {
     return null;
   }
   return document.getDefaultNSDescriptor(url, true);
 }
 public static boolean isElementWithEmbeddedType(
     XmlTagImpl xml, final String typeName, final String typeNsPrefix) {
   final String localName = xml.getLocalName();
   if (!(XmlUtil.XML_SCHEMA_URI.equals(xml.getNamespace()) && "element".equals(localName))) {
     return false;
   }
   final XmlAttribute nameAttr = getNameAttr(xml);
   if (nameAttr == null || nameAttr.getValue() == null) return false;
   final String localTypeName = XmlUtil.findLocalNameByQualifiedName(nameAttr.getValue());
   final String prefix = XmlUtil.findPrefixByQualifiedName(nameAttr.getValue());
   if (!typeName.equals(localTypeName) || !typeNsPrefix.equals(prefix)) {
     return false;
   }
   final XmlTag[] tags = xml.getSubTags();
   for (XmlTag tag : tags) {
     if (isTypeElement((XmlTagImpl) tag)) {
       return true;
     }
   }
   return false;
 }
  public TypeDescriptor getTypeDescriptor(final String name, XmlTag context) {
    if (checkSchemaNamespace(name, context)) {
      final String localNameByQualifiedName = XmlUtil.findLocalNameByQualifiedName(name);

      if (STD_TYPES.contains(localNameByQualifiedName)
          && (name.length() == localNameByQualifiedName.length()
              || UNDECLARED_STD_TYPES.contains(localNameByQualifiedName)))
        return new StdTypeDescriptor(localNameByQualifiedName);
    }

    return findTypeDescriptor(name, context);
  }
  private static EnumerationData getEnumeratedValuesImpl(final XmlTag declaration) {
    if ("boolean".equals(declaration.getAttributeValue("name"))) {
      return new EnumerationData(new String[] {"true", "false"}, true);
    }

    final HashSet<String> variants = new HashSet<String>();
    final boolean exaustive = XmlUtil.collectEnumerationValues(declaration, variants);

    if (variants.size() > 0) {
      return new EnumerationData(ArrayUtil.toStringArray(variants), exaustive);
    }
    return null;
  }
  private static void collectDependencies(
      @Nullable XmlTag myTag, @NotNull XmlFile myFile, @NotNull Set<PsiFile> visited) {
    if (visited.contains(myFile)) return;
    visited.add(myFile);

    if (myTag == null) return;
    XmlTag[] tags = myTag.getSubTags();

    for (final XmlTag tag : tags) {
      if (equalsToSchemaName(tag, INCLUDE_TAG_NAME) || equalsToSchemaName(tag, IMPORT_TAG_NAME)) {
        final XmlAttribute schemaLocation = tag.getAttribute("schemaLocation", null);
        if (schemaLocation != null) {
          final XmlFile xmlFile =
              XmlUtil.findNamespaceByLocation(myFile, schemaLocation.getValue());
          addDependency(xmlFile, visited);
        }
      } else if (equalsToSchemaName(tag, REDEFINE_TAG_NAME)) {
        myRedefinedDescriptorsInProcessing.set(visited);
        try {
          final XmlFile file = getRedefinedElementDescriptorFile(tag);
          addDependency(file, visited);
        } finally {
          myRedefinedDescriptorsInProcessing.set(null);
        }
      }
    }

    final String schemaLocationDeclaration =
        myTag.getAttributeValue("schemaLocation", XmlUtil.XML_SCHEMA_INSTANCE_URI);
    if (schemaLocationDeclaration != null) {
      final StringTokenizer tokenizer = new StringTokenizer(schemaLocationDeclaration);

      while (tokenizer.hasMoreTokens()) {
        final String uri = tokenizer.nextToken();

        if (tokenizer.hasMoreTokens()) {
          PsiFile resourceLocation =
              ExternalResourceManager.getInstance()
                  .getResourceLocation(tokenizer.nextToken(), myFile, null);
          if (resourceLocation == null && uri != null)
            resourceLocation =
                ExternalResourceManager.getInstance().getResourceLocation(uri, myFile, null);

          if (resourceLocation instanceof XmlFile)
            addDependency((XmlFile) resourceLocation, visited);
        }
      }
    }
  }
  private static boolean processTagsInNamespaceInner(
      @NotNull final XmlTag rootTag,
      final String[] tagNames,
      final PsiElementProcessor<XmlTag> processor,
      Set<XmlTag> visitedTags) {
    if (visitedTags == null) visitedTags = new HashSet<XmlTag>(3);
    else if (visitedTags.contains(rootTag)) return true;

    visitedTags.add(rootTag);
    XmlTag[] tags = rootTag.getSubTags();

    NextTag:
    for (XmlTag tag : tags) {
      for (String tagName : tagNames) {
        if (equalsToSchemaName(tag, tagName)) {
          final String name = tag.getAttributeValue("name");

          if (name != null) {
            if (!processor.execute(tag)) {
              return false;
            }
          }

          continue NextTag;
        }
      }

      if (equalsToSchemaName(tag, INCLUDE_TAG_NAME)) {
        final String schemaLocation = tag.getAttributeValue("schemaLocation");

        if (schemaLocation != null) {
          final XmlFile xmlFile =
              XmlUtil.findNamespaceByLocation(rootTag.getContainingFile(), schemaLocation);

          if (xmlFile != null) {
            final XmlDocument includedDocument = xmlFile.getDocument();

            if (includedDocument != null) {
              if (!processTagsInNamespaceInner(
                  includedDocument.getRootTag(), tagNames, processor, visitedTags)) return false;
            }
          }
        }
      }
    }

    return true;
  }
  public boolean isEnumerated(@Nullable XmlElement context) {
    final XmlElementDescriptorImpl elementDescriptor =
        (XmlElementDescriptorImpl)
            XmlUtil.findXmlDescriptorByType(
                myTag,
                context != null ? PsiTreeUtil.getContextOfType(context, XmlTag.class, true) : null);

    if (elementDescriptor != null && elementDescriptor.getType() instanceof ComplexTypeDescriptor) {
      final EnumerationData data =
          getEnumeratedValuesImpl(
              ((ComplexTypeDescriptor) elementDescriptor.getType()).getDeclaration());
      return data != null && data.exaustive;
    }

    return false;
  }
  static @NotNull XmlNSDescriptorImpl getNSDescriptorToSearchIn(
      XmlTag rootTag, final String name, XmlNSDescriptorImpl defaultNSDescriptor) {
    if (name == null) return defaultNSDescriptor;
    final String namespacePrefix = XmlUtil.findPrefixByQualifiedName(name);

    if (namespacePrefix != null && namespacePrefix.length() > 0) {
      final String namespace = rootTag.getNamespaceByPrefix(namespacePrefix);
      final XmlNSDescriptor nsDescriptor = rootTag.getNSDescriptor(namespace, true);

      if (nsDescriptor instanceof XmlNSDescriptorImpl) {
        return (XmlNSDescriptorImpl) nsDescriptor;
      }
    }

    return defaultNSDescriptor;
  }
Example #27
0
  @Nullable
  protected XmlElementDescriptor computeElementDescriptor() {
    for (XmlElementDescriptorProvider provider :
        Extensions.getExtensions(XmlElementDescriptorProvider.EP_NAME)) {
      XmlElementDescriptor elementDescriptor = provider.getDescriptor(this);
      if (elementDescriptor != null) {
        return elementDescriptor;
      }
    }

    final String namespace = getNamespace();
    if (XmlUtil.EMPTY_URI.equals(namespace)) { // nonqualified items
      final XmlTag parent = getParentTag();
      if (parent != null) {
        final XmlElementDescriptor descriptor = parent.getDescriptor();
        if (descriptor != null) {
          XmlElementDescriptor fromParent = descriptor.getElementDescriptor(this, parent);
          if (fromParent != null && !(fromParent instanceof AnyXmlElementDescriptor)) {
            return fromParent;
          }
        }
      }
    }

    XmlElementDescriptor elementDescriptor = null;
    final XmlNSDescriptor nsDescriptor = getNSDescriptor(namespace, false);

    LOG.debug(
        "Descriptor for namespace "
            + namespace
            + " is "
            + (nsDescriptor != null ? nsDescriptor.getClass().getCanonicalName() : "NULL"));

    if (nsDescriptor != null) {
      if (!DumbService.getInstance(getProject()).isDumb()
          || DumbService.isDumbAware(nsDescriptor)) {
        elementDescriptor = nsDescriptor.getElementDescriptor(this);
      }
    }
    if (elementDescriptor == null) {
      return XmlUtil.findXmlDescriptorByType(this);
    }

    return elementDescriptor;
  }
  protected void checkAttribute(
      @NotNull final XmlAttribute attribute,
      @NotNull final ProblemsHolder holder,
      final boolean isOnTheFly) {
    final XmlTag tag = attribute.getParent();

    if (tag instanceof HtmlTag) {
      XmlElementDescriptor elementDescriptor = tag.getDescriptor();
      if (elementDescriptor == null || elementDescriptor instanceof AnyXmlElementDescriptor) {
        return;
      }

      XmlAttributeDescriptor attributeDescriptor =
          elementDescriptor.getAttributeDescriptor(attribute);

      final String name = attribute.getName();

      if (attributeDescriptor == null && !attribute.isNamespaceDeclaration()) {
        if (!XmlUtil.attributeFromTemplateFramework(name, tag)
            && (!isCustomValuesEnabled() || !isCustomValue(name))) {
          final ASTNode node = attribute.getNode();
          assert node != null;
          final PsiElement nameElement =
              XmlChildRole.ATTRIBUTE_NAME_FINDER.findChild(node).getPsi();

          boolean maySwitchToHtml5 =
              HtmlUtil.isCustomHtml5Attribute(name) && !HtmlUtil.hasNonHtml5Doctype(tag);
          LocalQuickFix[] quickfixes = new LocalQuickFix[maySwitchToHtml5 ? 3 : 2];
          quickfixes[0] =
              new AddCustomTagOrAttributeIntentionAction(
                  getShortName(), name, XmlEntitiesInspection.UNKNOWN_ATTRIBUTE);
          quickfixes[1] = new RemoveAttributeIntentionAction(name);
          if (maySwitchToHtml5) {
            quickfixes[2] = new SwitchToHtml5WithHighPriorityAction();
          }

          holder.registerProblem(
              nameElement,
              XmlErrorMessages.message("attribute.is.not.allowed.here", name),
              ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
              quickfixes);
        }
      }
    }
  }
Example #29
0
  public List<DomStub> getChildrenByName(final CharSequence name, @Nullable final String nsKey) {
    final List<DomStub> stubs = getChildrenStubs();
    if (stubs.isEmpty()) {
      return Collections.emptyList();
    }

    final String s = nsKey == null ? "" : nsKey;
    final List<DomStub> result = new SmartList<DomStub>();
    //noinspection ForLoopReplaceableByForEach
    for (int i = 0, size = stubs.size(); i < size; i++) {
      final DomStub stub = stubs.get(i);
      if (XmlUtil.getLocalName(stub.getName()).equals(name)
          && Comparing.equal(s, stub.getNamespaceKey())) {
        result.add(stub);
      }
    }
    return result;
  }
  @Nullable
  protected TypeDescriptor findTypeDescriptorImpl(
      XmlTag rootTag, final String name, String namespace, Set<XmlTag> visited) {
    XmlNSDescriptorImpl responsibleDescriptor = this;
    if (namespace != null && namespace.length() != 0 && !namespace.equals(getDefaultNamespace())) {
      final XmlNSDescriptor nsDescriptor = rootTag.getNSDescriptor(namespace, true);

      if (nsDescriptor instanceof XmlNSDescriptorImpl) {
        responsibleDescriptor = (XmlNSDescriptorImpl) nsDescriptor;
      }
    }

    if (responsibleDescriptor != this) {
      return responsibleDescriptor.findTypeDescriptor(XmlUtil.findLocalNameByQualifiedName(name));
    }

    if (rootTag == null) return null;
    if (visited != null) {
      if (visited.contains(rootTag)) return null;
      visited.add(rootTag);
    }

    final Pair<QNameKey, XmlTag> pair =
        new Pair<QNameKey, XmlTag>(new QNameKey(name, namespace), rootTag);

    final CachedValue<TypeDescriptor> descriptor = myTypesMap.get(pair);
    if (descriptor != null) {
      TypeDescriptor value = descriptor.getValue();
      if (value == null
          || (value instanceof ComplexTypeDescriptor
              && ((ComplexTypeDescriptor) value).getDeclaration().isValid())) return value;
    }

    XmlTag[] tags = rootTag.getSubTags();

    if (visited == null) {
      visited = new HashSet<XmlTag>(1);
      visited.add(rootTag);
    }

    return doFindIn(tags, name, namespace, pair, rootTag, visited);
  }