/**
   * 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;
  }
Ejemplo n.º 2
0
 @Override
 public ValidationResult validate(EnunciateFreemarkerModel model) {
   ValidationResult result = super.validate(model);
   for (SchemaInfo schemaInfo : model.getNamespacesToSchemas().values()) {
     for (Registry registry : schemaInfo.getRegistries()) {
       Collection<LocalElementDeclaration> localElements = registry.getLocalElementDeclarations();
       for (LocalElementDeclaration localElement : localElements) {
         JsonElementWrapper elementWrapper = localElement.getAnnotation(JsonElementWrapper.class);
         if (elementWrapper != null) {
           String jsonName = elementWrapper.namespace() + elementWrapper.name();
           Declaration previous = this.jsonNameDeclarations.put(jsonName, localElement);
           if (previous != null) {
             result.addError(
                 localElement,
                 "JSON name conflict with " + String.valueOf(previous.getPosition()));
           }
         }
       }
     }
   }
   return result;
 }