Search in sources :

Example 1 with XSComponent

use of com.sun.xml.xsom.XSComponent in project midpoint by Evolveum.

the class SchemaProcessor method updateObjectFactoryElements.

/**
     * Marks ObjectFactory.createXYZ methods for elements with a:rawType annotation as @Raw.
     */
private void updateObjectFactoryElements(Outline outline) {
    XSSchemaSet schemaSet = outline.getModel().schemaComponent;
    for (CElementInfo elementInfo : outline.getModel().getAllElements()) {
        QName name = elementInfo.getElementName();
        XSComponent elementDecl;
        if (elementInfo.getSchemaComponent() != null) {
            // it's strange but elements seem not to have this filled-in...
            elementDecl = elementInfo.getSchemaComponent();
        } else {
            elementDecl = schemaSet.getElementDecl(name.getNamespaceURI(), name.getLocalPart());
        }
        boolean isRaw = hasAnnotation(elementDecl, A_RAW_TYPE);
        if (isRaw) {
            print("*** Raw element found: " + elementInfo.getElementName());
            JDefinedClass objectFactory = outline.getPackageContext(elementInfo._package()).objectFactory();
            // finding method corresponding to the given element
            boolean methodFound = false;
            for (JMethod method : objectFactory.methods()) {
                for (JAnnotationUse annotationUse : method.annotations()) {
                    if (XmlElementDecl.class.getName().equals(annotationUse.getAnnotationClass().fullName())) {
                        // ugly method of finding the string value of the annotation members (couldn't find any better)
                        JAnnotationValue namespaceValue = annotationUse.getAnnotationMembers().get("namespace");
                        StringWriter namespaceWriter = new StringWriter();
                        JFormatter namespaceFormatter = new JFormatter(namespaceWriter);
                        namespaceValue.generate(namespaceFormatter);
                        JAnnotationValue nameValue = annotationUse.getAnnotationMembers().get("name");
                        StringWriter nameWriter = new StringWriter();
                        JFormatter nameFormatter = new JFormatter(nameWriter);
                        nameValue.generate(nameFormatter);
                        if (("\"" + name.getNamespaceURI() + "\"").equals(namespaceWriter.toString()) && ("\"" + name.getLocalPart() + "\"").equals(nameWriter.toString())) {
                            print("*** Annotating method as @Raw: " + method.name());
                            method.annotate(Raw.class);
                            methodFound = true;
                            break;
                        }
                    }
                }
            }
            if (!methodFound) {
                throw new IllegalStateException("No factory method found for element " + name);
            }
        }
    }
}
Also used : XSSchemaSet(com.sun.xml.xsom.XSSchemaSet) QName(javax.xml.namespace.QName) CElementInfo(com.sun.tools.xjc.model.CElementInfo) XSComponent(com.sun.xml.xsom.XSComponent) StringWriter(java.io.StringWriter)

Aggregations

CElementInfo (com.sun.tools.xjc.model.CElementInfo)1 XSComponent (com.sun.xml.xsom.XSComponent)1 XSSchemaSet (com.sun.xml.xsom.XSSchemaSet)1 StringWriter (java.io.StringWriter)1 QName (javax.xml.namespace.QName)1