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);
}
}
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());
}
Aggregations