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 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);
  }
 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));
 }
  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 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;
  }
  @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);
  }
Пример #7
0
  @Nullable
  private BidirectionalMap<String, String> computeNamespaceMap(PsiElement parent) {
    BidirectionalMap<String, String> map = null;
    if (hasNamespaceDeclarations()) {
      map = new BidirectionalMap<String, String>();
      final XmlAttribute[] attributes = getAttributes();

      for (final XmlAttribute attribute : attributes) {
        if (attribute.isNamespaceDeclaration()) {
          final String name = attribute.getName();
          int splitIndex = name.indexOf(':');
          final String value = getRealNs(attribute.getValue());

          if (value != null) {
            if (splitIndex < 0) {
              map.put("", value);
            } else {
              map.put(XmlUtil.findLocalNameByQualifiedName(name), value);
            }
          }
        }
      }
    }

    if (parent instanceof XmlDocument) {
      final XmlExtension extension = XmlExtension.getExtensionByElement(parent);
      if (extension != null) {
        final String[][] defaultNamespace =
            extension.getNamespacesFromDocument((XmlDocument) parent, map != null);
        if (defaultNamespace != null) {
          if (map == null) {
            map = new BidirectionalMap<String, String>();
          }
          for (final String[] prefix2ns : defaultNamespace) {
            map.put(prefix2ns[0], getRealNs(prefix2ns[1]));
          }
        }
      }
    }
    return map;
  }
 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;
 }
  private void initSubstitutes() {
    if (mySubstitutions == null) {
      mySubstitutions = new HashMap<String, List<XmlTag>>();

      XmlTag[] tags = myTag.getSubTags();

      for (XmlTag tag : tags) {
        if (equalsToSchemaName(tag, ELEMENT_TAG_NAME)) {
          final String substAttr = tag.getAttributeValue("substitutionGroup");
          if (substAttr != null) {
            String substLocalName = XmlUtil.findLocalNameByQualifiedName(substAttr);
            List<XmlTag> list = mySubstitutions.get(substLocalName);
            if (list == null) {
              list = new LinkedList<XmlTag>();
              mySubstitutions.put(substLocalName, list);
            }
            list.add(tag);
          }
        }
      }
    }
  }
 @Nullable
 protected TypeDescriptor findTypeDescriptor(final String qname, XmlTag context) {
   String namespace = context.getNamespaceByPrefix(XmlUtil.findPrefixByQualifiedName(qname));
   return findTypeDescriptor(XmlUtil.findLocalNameByQualifiedName(qname), namespace);
 }