/**
   * Returns the qname of the element that has the first parameter as the namespace, the second as
   * the element.
   *
   * @param list The arguments.
   * @return The qname.
   */
  public Object exec(List list) throws TemplateModelException {
    if (list.size() < 1) {
      throw new TemplateModelException(
          "The isDefinedGlobally method must have a local element declaration as a parameter.");
    }

    TemplateModel from = (TemplateModel) list.get(0);
    Object unwrapped = BeansWrapper.getDefaultInstance().unwrap(from);
    if (!LocalElementDeclaration.class.isInstance(unwrapped)) {
      throw new TemplateModelException(
          "The isDefinedGlobally method must have a local element declaration as a parameter.");
    }

    LocalElementDeclaration decl = (LocalElementDeclaration) unwrapped;
    String namespace = decl.getNamespace();
    SchemaInfo schemaInfo = getModel().getNamespacesToSchemas().get(namespace);
    if (schemaInfo != null) {
      for (RootElementDeclaration rootElementDeclaration : schemaInfo.getGlobalElements()) {
        if (rootElementDeclaration.getName().equals(decl.getName())) {
          return true;
        }
      }

      for (ImplicitSchemaElement implicitSchemaElement : schemaInfo.getImplicitSchemaElements()) {
        if (implicitSchemaElement.getElementName().equals(decl.getName())) {
          return true;
        }
      }
    }

    return false;
  }
Пример #2
0
  @Override
  public ValidationResult validateRootElement(RootElementDeclaration rootElementDeclaration) {
    ValidationResult result = super.validateRootElement(rootElementDeclaration);
    String namespace = rootElementDeclaration.getNamespace();
    if (namespace == null) {
      namespace = "";
    }

    if (namespace.isEmpty()) {
      result.addError(rootElementDeclaration, "Root element should not be in the empty namespace.");
    }

    if (rootElementDeclaration.getName().toLowerCase().startsWith("web")) {
      result.addWarning(
          rootElementDeclaration,
          "You probably don't want a root element that starts with the name 'web'. Consider renaming using the @XmlRootElement annotation.");
    }

    JsonElementWrapper elementWrapper =
        rootElementDeclaration.getAnnotation(JsonElementWrapper.class);
    if (namespace.startsWith(CommonModels.GEDCOMX_DOMAIN) && elementWrapper == null) {
      result.addWarning(
          rootElementDeclaration,
          "Root elements in the '"
              + CommonModels.GEDCOMX_DOMAIN
              + "' namespace should probably be annotated with @"
              + JsonElementWrapper.class.getSimpleName()
              + ".");
    }

    if (elementWrapper != null) {
      String jsonName = elementWrapper.namespace() + elementWrapper.name();
      Declaration previous = this.jsonNameDeclarations.put(jsonName, rootElementDeclaration);
      if (previous != null) {
        result.addError(
            rootElementDeclaration,
            "JSON name conflict with " + String.valueOf(previous.getPosition()));
      }
    }

    return result;
  }