Search in sources :

Example 1 with ElementDeclaration

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

the class ComplexTypeExampleImpl method getBody.

@Override
public String getBody() {
    try {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        builderFactory.setNamespaceAware(true);
        DocumentBuilder domBuilder = builderFactory.newDocumentBuilder();
        Document document = domBuilder.newDocument();
        String rootName = Character.toLowerCase(this.typeDefinition.getSimpleName().charAt(0)) + "-----";
        String rootNamespace = this.typeDefinition.getNamespace();
        ElementDeclaration element = typeDefinition.getContext().findElementDeclaration(typeDefinition);
        if (element != null) {
            rootName = element.getName();
            rootNamespace = element.getNamespace();
        }
        Element rootElement = document.createElementNS(rootNamespace, rootName);
        Element outer = rootElement;
        for (DataTypeReference.ContainerType container : this.containers) {
            Element containerEl = document.createElementNS("", container.name());
            containerEl.appendChild(outer);
            outer = containerEl;
        }
        document.appendChild(outer);
        Context context = new Context();
        context.stack = new LinkedList<String>();
        build(rootElement, this.typeDefinition, document, context);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        DOMSource source = new DOMSource(document);
        StringWriter value = new StringWriter();
        transformer.transform(source, new StreamResult(value));
        return value.toString();
    } catch (ParserConfigurationException e) {
        throw new EnunciateException(e);
    } catch (TransformerException e) {
        throw new EnunciateException(e);
    }
}
Also used : ApiRegistrationContext(com.webcohesion.enunciate.api.ApiRegistrationContext) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DataTypeReference(com.webcohesion.enunciate.api.datatype.DataTypeReference) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) TypeElement(javax.lang.model.element.TypeElement) DecoratedElement(com.webcohesion.enunciate.javac.decorations.element.DecoratedElement) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) StringWriter(java.io.StringWriter) DocumentBuilder(javax.xml.parsers.DocumentBuilder) EnunciateException(com.webcohesion.enunciate.EnunciateException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException) ElementDeclaration(com.webcohesion.enunciate.modules.jaxb.model.ElementDeclaration)

Example 2 with ElementDeclaration

use of com.webcohesion.enunciate.modules.jaxb.model.ElementDeclaration 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)

Aggregations

ElementDeclaration (com.webcohesion.enunciate.modules.jaxb.model.ElementDeclaration)2 EnunciateException (com.webcohesion.enunciate.EnunciateException)1 ApiRegistrationContext (com.webcohesion.enunciate.api.ApiRegistrationContext)1 DataTypeReference (com.webcohesion.enunciate.api.datatype.DataTypeReference)1 DecoratedElement (com.webcohesion.enunciate.javac.decorations.element.DecoratedElement)1 DecoratedTypeMirror (com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)1 Accessor (com.webcohesion.enunciate.modules.jaxb.model.Accessor)1 Element (com.webcohesion.enunciate.modules.jaxb.model.Element)1 TypeDefinition (com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition)1 TemplateModel (freemarker.template.TemplateModel)1 TemplateModelException (freemarker.template.TemplateModelException)1 StringWriter (java.io.StringWriter)1 TypeElement (javax.lang.model.element.TypeElement)1 JAXBElement (javax.xml.bind.JAXBElement)1 QName (javax.xml.namespace.QName)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Transformer (javax.xml.transform.Transformer)1 TransformerException (javax.xml.transform.TransformerException)1