Search in sources :

Example 16 with TypeDefinition

use of com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition in project enunciate by stoicflame.

the class CXMLClientModule method readResource.

/**
 * Reads a resource into string form.
 *
 * @param resource The resource to read.
 * @return The string form of the resource.
 */
protected String readResource(String resource, Map<String, Object> model, NameForTypeDefinitionMethod nameForTypeDefinition) {
    Method exampleResource = findExampleResourceMethod();
    if (exampleResource != null) {
        TypeDefinition typeDefinition = findRequestElement(exampleResource);
        if (typeDefinition != null) {
            model.put("input_element_name", nameForTypeDefinition.calculateName(typeDefinition));
        }
        typeDefinition = findResponseElement(exampleResource);
        if (typeDefinition != null) {
            model.put("output_element_name", nameForTypeDefinition.calculateName(typeDefinition));
        }
    }
    URL res = CXMLClientModule.class.getResource(resource);
    try {
        return processTemplate(res, model);
    } catch (TemplateException e) {
        throw new EnunciateException(e);
    } catch (IOException e) {
        throw new EnunciateException(e);
    }
}
Also used : TemplateException(freemarker.template.TemplateException) EnunciateException(com.webcohesion.enunciate.EnunciateException) FindRootElementMethod(com.webcohesion.enunciate.modules.jaxb.util.FindRootElementMethod) IsFacetExcludedMethod(com.webcohesion.enunciate.util.freemarker.IsFacetExcludedMethod) Method(com.webcohesion.enunciate.api.resources.Method) ReferencedNamespacesMethod(com.webcohesion.enunciate.modules.jaxb.util.ReferencedNamespacesMethod) AccessorOverridesAnotherMethod(com.webcohesion.enunciate.modules.jaxb.util.AccessorOverridesAnotherMethod) IOException(java.io.IOException) URL(java.net.URL) TypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition)

Example 17 with TypeDefinition

use of com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition in project enunciate by stoicflame.

the class NameForTypeDefinitionMethod method exec.

public Object exec(List list) throws TemplateModelException {
    if (list.size() < 1) {
        throw new TemplateModelException("The nameForTypeDefinition method must have a type definition as a parameter.");
    }
    TemplateModel from = (TemplateModel) list.get(0);
    Object unwrapped = FreemarkerUtil.unwrap(from);
    if (!(unwrapped instanceof TypeDefinition)) {
        throw new TemplateModelException("The nameForTypeDefinition method must have a type definition as a parameter.");
    }
    return calculateName((TypeDefinition) unwrapped);
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) TemplateModel(freemarker.template.TemplateModel) TypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition)

Example 18 with TypeDefinition

use of com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition in project enunciate by stoicflame.

the class XmlFunctionIdentifierMethod method exec.

/**
 * 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 xmlFunctionIdentifier method must have a qname, type definition, or xml type as a parameter.");
    }
    TemplateModel from = (TemplateModel) list.get(0);
    Object unwrapped = FreemarkerUtil.unwrap(from);
    if (unwrapped instanceof Accessor) {
        DecoratedTypeMirror accessorType = ((Accessor) unwrapped).getBareAccessorType();
        if (accessorType.isInstanceOf(JAXBElement.class.getName())) {
            unwrapped = KnownXmlType.ANY_TYPE.getQname();
        } else if (unwrapped instanceof Element && ((Element) unwrapped).getRef() != null) {
            unwrapped = ((Element) unwrapped).getRef();
        } else {
            XmlType xmlType = ((Accessor) unwrapped).getBaseType();
            if (xmlType instanceof SpecifiedXmlType) {
                // bypass specified types for client code generation.
                unwrapped = XmlTypeFactory.getXmlType(((Accessor) unwrapped).getAccessorType(), ((Accessor) unwrapped).getContext());
            } else {
                unwrapped = xmlType;
            }
        }
    }
    if (unwrapped instanceof XmlType) {
        if (unwrapped instanceof XmlClassType && ((XmlType) unwrapped).isAnonymous()) {
            unwrapped = ((XmlClassType) unwrapped).getTypeDefinition();
        } else {
            unwrapped = ((XmlType) unwrapped).getQname();
        }
    }
    if (unwrapped instanceof TypeDefinition) {
        if (((TypeDefinition) unwrapped).isAnonymous()) {
            // if anonymous, we have to come up with a unique (albeit nonstandard) name for the xml type.
            unwrapped = new QName(((TypeDefinition) unwrapped).getNamespace(), "anonymous" + ((TypeDefinition) unwrapped).getSimpleName());
        } else {
            unwrapped = ((TypeDefinition) unwrapped).getQname();
        }
    }
    if (unwrapped instanceof ElementDeclaration) {
        unwrapped = ((ElementDeclaration) unwrapped).getQname();
    }
    if (!(unwrapped instanceof QName)) {
        throw new TemplateModelException("The xmlFunctionIdentifier method must have a qname, type definition, or xml type as a parameter.");
    }
    QName qname = (QName) unwrapped;
    String namespace = qname.getNamespaceURI();
    if ("".equals(namespace)) {
        namespace = null;
    }
    String prefix = this.ns2prefix.get(namespace);
    if (prefix == null || prefix.isEmpty()) {
        prefix = "_";
    }
    prefix = prefix.replace('-', '_');
    String localName = qname.getLocalPart();
    if ("".equals(localName)) {
        return null;
    }
    StringBuilder identifier = new StringBuilder();
    identifier.append(Character.toLowerCase(prefix.charAt(0)));
    identifier.append(prefix.substring(1));
    identifier.append(Character.toUpperCase(localName.charAt(0)));
    identifier.append(localName.substring(1));
    return CXMLClientModule.scrubIdentifier(identifier.toString());
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) QName(javax.xml.namespace.QName) JAXBElement(javax.xml.bind.JAXBElement) Element(com.webcohesion.enunciate.modules.jaxb.model.Element) TemplateModel(freemarker.template.TemplateModel) JAXBElement(javax.xml.bind.JAXBElement) Accessor(com.webcohesion.enunciate.modules.jaxb.model.Accessor) TypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition) ElementDeclaration(com.webcohesion.enunciate.modules.jaxb.model.ElementDeclaration)

Example 19 with TypeDefinition

use of com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition in project enunciate by stoicflame.

the class NamespaceImpl method getTypes.

@Override
public List<? extends DataType> getTypes() {
    FacetFilter facetFilter = this.registrationContext.getFacetFilter();
    ArrayList<DataType> dataTypes = new ArrayList<DataType>();
    for (TypeDefinition typeDefinition : this.schema.getTypeDefinitions()) {
        if (!facetFilter.accept(typeDefinition)) {
            continue;
        }
        if (typeDefinition instanceof ComplexTypeDefinition) {
            dataTypes.add(new ComplexDataTypeImpl((ComplexTypeDefinition) typeDefinition, registrationContext));
        } else if (typeDefinition instanceof EnumTypeDefinition) {
            dataTypes.add(new EnumDataTypeImpl((EnumTypeDefinition) typeDefinition, registrationContext));
        }
    }
    return dataTypes;
}
Also used : FacetFilter(com.webcohesion.enunciate.facets.FacetFilter) EnumTypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.EnumTypeDefinition) ArrayList(java.util.ArrayList) DataType(com.webcohesion.enunciate.api.datatype.DataType) ComplexTypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.ComplexTypeDefinition) TypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition) EnumTypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.EnumTypeDefinition) ComplexTypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.ComplexTypeDefinition)

Aggregations

TypeDefinition (com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition)19 SchemaInfo (com.webcohesion.enunciate.modules.jaxb.model.SchemaInfo)8 EnunciateException (com.webcohesion.enunciate.EnunciateException)6 TemplateModel (freemarker.template.TemplateModel)6 TemplateModelException (freemarker.template.TemplateModelException)6 URL (java.net.URL)6 FacetFilter (com.webcohesion.enunciate.facets.FacetFilter)5 FindRootElementMethod (com.webcohesion.enunciate.modules.jaxb.util.FindRootElementMethod)5 EnunciateJaxbContext (com.webcohesion.enunciate.modules.jaxb.EnunciateJaxbContext)4 Accessor (com.webcohesion.enunciate.modules.jaxb.model.Accessor)4 Element (com.webcohesion.enunciate.modules.jaxb.model.Element)4 AccessorOverridesAnotherMethod (com.webcohesion.enunciate.modules.jaxb.util.AccessorOverridesAnotherMethod)4 IsFacetExcludedMethod (com.webcohesion.enunciate.util.freemarker.IsFacetExcludedMethod)4 TemplateException (freemarker.template.TemplateException)4 TypeElement (javax.lang.model.element.TypeElement)4 ClientLibraryArtifact (com.webcohesion.enunciate.artifacts.ClientLibraryArtifact)3 FileArtifact (com.webcohesion.enunciate.artifacts.FileArtifact)3 MapType (com.webcohesion.enunciate.modules.jaxb.model.util.MapType)3 ReferencedNamespacesMethod (com.webcohesion.enunciate.modules.jaxb.util.ReferencedNamespacesMethod)3 IOException (java.io.IOException)3