Search in sources :

Example 1 with CElementInfo

use of com.sun.tools.xjc.model.CElementInfo in project midpoint by Evolveum.

the class StepSchemaConstants method run.

@Override
public boolean run(Outline outline, Options opt, ErrorHandler errorHandler) throws Exception {
    Model model = outline.getModel();
    JDefinedClass schemaConstants = model.codeModel._class(CLASS_NAME);
    //creating namespaces
    List<FieldBox<String>> namespaces = new ArrayList<FieldBox<String>>();
    for (PrefixMapper prefix : PrefixMapper.values()) {
        namespaces.add(new FieldBox("NS_" + prefix.getNamespaceName(), prefix.getNamespace()));
    }
    Collections.sort(namespaces);
    for (FieldBox<String> field : namespaces) {
        JFieldVar var = createNSFieldDefinition(outline, schemaConstants, field.getFieldName(), field.getValue());
        getNamespaceFields().put(field.getValue(), var);
    }
    //creating qnames
    List<FieldBox<QName>> fields = new ArrayList<FieldBox<QName>>();
    Map<QName, CElementInfo> map = model.getElementMappings(null);
    Set<Map.Entry<QName, CElementInfo>> set = map.entrySet();
    for (Map.Entry<QName, CElementInfo> entry : set) {
        QName qname = entry.getKey();
        CElementInfo info = entry.getValue();
        String fieldName = ProcessorUtils.fieldPrefixedUnderscoredUpperCase(info.getSqueezedName(), qname);
        fields.add(new FieldBox(fieldName, qname));
    }
    //sort field by name and create qname definitions in class
    Collections.sort(fields);
    for (FieldBox<QName> field : fields) {
        JFieldVar var = namespaceFields.get(field.getValue().getNamespaceURI());
        if (var != null) {
            createQNameDefinition(outline, schemaConstants, field.getFieldName(), var, field.getValue());
        } else {
            ProcessorUtils.createPSFField(outline, schemaConstants, field.getFieldName(), field.getValue());
        }
    }
    return true;
}
Also used : PrefixMapper(com.evolveum.midpoint.schema.xjc.PrefixMapper) QName(javax.xml.namespace.QName) CElementInfo(com.sun.tools.xjc.model.CElementInfo) Model(com.sun.tools.xjc.model.Model)

Example 2 with CElementInfo

use of com.sun.tools.xjc.model.CElementInfo 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)2 QName (javax.xml.namespace.QName)2 PrefixMapper (com.evolveum.midpoint.schema.xjc.PrefixMapper)1 Model (com.sun.tools.xjc.model.Model)1 XSComponent (com.sun.xml.xsom.XSComponent)1 XSSchemaSet (com.sun.xml.xsom.XSSchemaSet)1 StringWriter (java.io.StringWriter)1