Search in sources :

Example 1 with XmlNs

use of org.eclipse.persistence.jaxb.xmlmodel.XmlSchema.XmlNs in project eclipselink by eclipse-ee4j.

the class XMLProcessor method processXmlSchema.

/**
 * Process an XmlSchema. This involves creating a NamespaceInfo instance and
 * populating it based on the given XmlSchema.
 *
 * @see NamespaceInfo
 * @see AnnotationsProcessor
 * @return newly created namespace info, or null if schema is null
 */
private NamespaceInfo processXmlSchema(XmlBindings xmlBindings, String packageName) {
    XmlSchema schema = xmlBindings.getXmlSchema();
    if (schema == null) {
        return null;
    }
    // create NamespaceInfo
    NamespaceInfo nsInfo = this.aProcessor.findInfoForNamespace(schema.getNamespace());
    if (nsInfo == null) {
        nsInfo = new NamespaceInfo();
    }
    // process XmlSchema
    XmlNsForm form = schema.getAttributeFormDefault();
    nsInfo.setAttributeFormQualified(form.equals(XmlNsForm.QUALIFIED));
    form = schema.getElementFormDefault();
    nsInfo.setElementFormQualified(form.equals(XmlNsForm.QUALIFIED));
    if (!nsInfo.isElementFormQualified() || nsInfo.isAttributeFormQualified()) {
        aProcessor.setDefaultNamespaceAllowed(false);
    }
    // make sure defaults are set, not null
    nsInfo.setLocation(schema.getLocation() == null ? GENERATE : schema.getLocation());
    String namespace = schema.getNamespace();
    if (namespace == null) {
        namespace = this.aProcessor.getDefaultTargetNamespace();
    }
    nsInfo.setNamespace(namespace == null ? "" : schema.getNamespace());
    NamespaceResolver nsr = new NamespaceResolver();
    // process XmlNs
    for (XmlNs xmlns : schema.getXmlNs()) {
        nsr.put(xmlns.getPrefix(), xmlns.getNamespaceUri());
    }
    nsInfo.setNamespaceResolver(nsr);
    return nsInfo;
}
Also used : XmlSchema(org.eclipse.persistence.jaxb.xmlmodel.XmlSchema) XmlNsForm(org.eclipse.persistence.jaxb.xmlmodel.XmlNsForm) XmlNs(org.eclipse.persistence.jaxb.xmlmodel.XmlSchema.XmlNs) NamespaceResolver(org.eclipse.persistence.oxm.NamespaceResolver)

Example 2 with XmlNs

use of org.eclipse.persistence.jaxb.xmlmodel.XmlSchema.XmlNs in project eclipselink by eclipse-ee4j.

the class XmlBindingsGenerator method generateXmlBindings.

/**
 * Generate an XmlBindings instance based on a list of XML descriptors.
 *
 * OXM metadata files are processed on a per package basis, hence it is
 * assumed that the given list of descriptors are from the same package.
 */
public static XmlBindings generateXmlBindings(String packageName, List<XMLDescriptor> descriptors) {
    String defaultNamespace = null;
    Map<String, String> prefixMap = new HashMap<String, String>();
    JavaTypes jTypes = new JavaTypes();
    for (XMLDescriptor xdesc : descriptors) {
        // mappings to the map
        if (xdesc.getNamespaceResolver() != null) {
            if (defaultNamespace == null) {
                defaultNamespace = xdesc.getNamespaceResolver().getDefaultNamespaceURI();
            }
            Map<String, String> preMap = xdesc.getNamespaceResolver().getPrefixesToNamespaces();
            for (String pfx : preMap.keySet()) {
                // ignore mime prefix/url for now
                if (!pfx.equals(XML_MIME_PREFIX)) {
                    prefixMap.put(pfx, preMap.get(pfx));
                }
            }
        }
        // generate a JavaType instance for the XML descriptor
        jTypes.getJavaType().add(generateJavaType(xdesc));
    }
    XmlBindings xmlBindings = null;
    // if there are no JavaTypes, there's nothing to do
    if (jTypes.getJavaType().size() > 0) {
        xmlBindings = new XmlBindings();
        xmlBindings.setJavaTypes(jTypes);
        xmlBindings.setPackageName(packageName);
        // handle XmlSchema
        if (defaultNamespace != null || !prefixMap.isEmpty()) {
            XmlSchema xSchema = new XmlSchema();
            xSchema.setNamespace(defaultNamespace == null ? EMPTY_STRING : defaultNamespace);
            xSchema.setElementFormDefault(XmlNsForm.QUALIFIED);
            // handle XmlNs
            if (!prefixMap.isEmpty()) {
                XmlNs xmlNs;
                for (String pfx : prefixMap.keySet()) {
                    xmlNs = new XmlNs();
                    xmlNs.setNamespaceUri(prefixMap.get(pfx));
                    xmlNs.setPrefix(pfx);
                    xSchema.getXmlNs().add(xmlNs);
                }
            }
            xmlBindings.setXmlSchema(xSchema);
        }
    }
    return xmlBindings;
}
Also used : JavaTypes(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings.JavaTypes) XmlBindings(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) HashMap(java.util.HashMap) XmlSchema(org.eclipse.persistence.jaxb.xmlmodel.XmlSchema) XmlNs(org.eclipse.persistence.jaxb.xmlmodel.XmlSchema.XmlNs)

Aggregations

XmlSchema (org.eclipse.persistence.jaxb.xmlmodel.XmlSchema)2 XmlNs (org.eclipse.persistence.jaxb.xmlmodel.XmlSchema.XmlNs)2 HashMap (java.util.HashMap)1 XmlBindings (org.eclipse.persistence.jaxb.xmlmodel.XmlBindings)1 JavaTypes (org.eclipse.persistence.jaxb.xmlmodel.XmlBindings.JavaTypes)1 XmlNsForm (org.eclipse.persistence.jaxb.xmlmodel.XmlNsForm)1 NamespaceResolver (org.eclipse.persistence.oxm.NamespaceResolver)1 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)1