Пример #1
0
  /**
   * Get the ISchemaElement corresponding to this IDocumentElementNode
   *
   * @param node
   * @param extensionPoint the extension point of the schema, if <code>null</code> it will be
   *     deduced
   * @return the ISchemaElement for <code>node</code>
   */
  public static ISchemaElement getSchemaElement(IDocumentElementNode node, String extensionPoint) {
    if (extensionPoint == null) {
      IMonitorObject obj = getTopLevelParent(node);
      if (!(obj instanceof IMonitorExtension)) return null;
      extensionPoint = ((IMonitorExtension) obj).getPoint();
    }
    ISchema schema = MDECore.getDefault().getSchemaRegistry().getSchema(extensionPoint);
    if (schema == null) return null;

    // Bug 213457 - look up elements based on the schema in which the parent is found
    if (schema.getIncludes().length == 0 || "extension".equals(node.getXMLTagName())) // $NON-NLS-1$
    return schema.findElement(node.getXMLTagName());

    // if element is not "extension" & has multiple sub-schemas,
    // Then search for the element in the same schema in which the parent element if found.
    Stack stack = new Stack();
    while (node != null) {
      String tagName = node.getXMLTagName();
      if ("extension".equals(tagName)) // $NON-NLS-1$
      break;
      stack.push(node.getXMLTagName());
      node = node.getParentNode();
    }
    ISchemaElement element = null;
    while (!stack.isEmpty()) {
      element = schema.findElement((String) stack.pop());
      if (element == null) return null;
      schema = element.getSchema();
    }
    return element;
  }
  protected void validateElement(Element element, ISchema schema, boolean isTopLevel) {
    String elementName = element.getNodeName();
    ISchemaElement schemaElement = schema.findElement(elementName);

    // Validate element occurrence violations
    if ((schemaElement != null) && (schemaElement.getType() instanceof ISchemaComplexType)) {
      validateMaxElementMult(element, schemaElement);
      validateMinElementMult(element, schemaElement);
    }

    ISchemaElement parentSchema = null;
    if (!"extension".equals(elementName)) { // $NON-NLS-1$
      Node parent = element.getParentNode();
      parentSchema = schema.findElement(parent.getNodeName());
    } else if (isTopLevel == false) {
      // This is an "extension" element; but, not a top level one.
      // It is nested within another "extension" element somewhere
      // e.g. "extension" element is a child element of another element
      // that is not a "plugin" elment
      // element
      // Report illegal element
      int severity = CompilerFlags.getFlag(fProject, CompilerFlags.P_UNKNOWN_ELEMENT);
      reportIllegalElement(element, severity);
      return;
    }

    if (parentSchema != null) {
      int severity = CompilerFlags.getFlag(fProject, CompilerFlags.P_UNKNOWN_ELEMENT);
      if (severity != CompilerFlags.IGNORE) {
        HashSet allowedElements = new HashSet();
        computeAllowedElements(parentSchema.getType(), allowedElements);
        if (!allowedElements.contains(elementName)) {
          reportIllegalElement(element, severity);
          return;
        }
      }
    }
    if (schemaElement == null && parentSchema != null) {
      ISchemaAttribute attr = parentSchema.getAttribute(elementName);
      if (attr != null && attr.getKind() == IMetaAttribute.JAVA) {
        if (attr.isDeprecated())
          reportDeprecatedAttribute(element, element.getAttributeNode("class")); // $NON-NLS-1$
        validateJavaAttribute(element, element.getAttributeNode("class")); // $NON-NLS-1$			
      }
    } else {
      if (schemaElement != null) {
        validateRequiredExtensionAttributes(element, schemaElement);
        validateExistingExtensionAttributes(element, element.getAttributes(), schemaElement);
        validateInternalExtensionAttribute(element, schemaElement);
        if (schemaElement.isDeprecated()) {
          if (schemaElement instanceof ISchemaRootElement)
            reportDeprecatedRootElement(
                element, ((ISchemaRootElement) schemaElement).getDeprecatedSuggestion());
          else reportDeprecatedElement(element);
        }
        if (schemaElement.hasTranslatableContent()) validateTranslatableElementContent(element);
        // Bug 213457 - look up elements based on the schema in which the parent is found
        schema = schemaElement.getSchema();
      }
      NodeList children = element.getChildNodes();
      for (int i = 0; i < children.getLength(); i++) {
        validateElement((Element) children.item(i), schema, false);
      }
    }
  }