Search in sources :

Example 16 with NamespaceResolver

use of org.eclipse.persistence.internal.oxm.NamespaceResolver in project eclipselink by eclipse-ee4j.

the class XMLFragmentReader method cleanupNamespaceResolvers.

/**
 * Remove any temporary namespace resolvers created while processing
 * a given element.
 */
protected void cleanupNamespaceResolvers(Element elem) {
    NamespaceResolver tmpresolver = tmpresolverMap.get(elem);
    if (tmpresolver != null) {
        tmpresolverMap.remove(elem);
        nsresolverList.remove(tmpresolver);
    }
}
Also used : NamespaceResolver(org.eclipse.persistence.internal.oxm.NamespaceResolver)

Example 17 with NamespaceResolver

use of org.eclipse.persistence.internal.oxm.NamespaceResolver in project eclipselink by eclipse-ee4j.

the class SchemaModelGenerator method getDefaultRootElementAsQName.

/**
 * Return a QName representation of a qualified table name (aka default root element).  The
 * given descriptor's namespace resolver will be used to determine the correct prefix - if
 * any - to be used.
 */
protected QName getDefaultRootElementAsQName(Descriptor desc, String qualifiedTableName) {
    QName qName = null;
    int idx = qualifiedTableName.indexOf(Constants.COLON);
    String localName = qualifiedTableName.substring(idx + 1);
    NamespaceResolver nsResolver = desc.getNamespaceResolver();
    if (nsResolver == null) {
        qName = new QName(localName);
    } else if (idx > -1) {
        String prefix = qualifiedTableName.substring(0, idx);
        String uri = nsResolver.resolveNamespacePrefix(prefix);
        qName = new QName(uri, localName);
    } else {
        if (nsResolver.getDefaultNamespaceURI() != null) {
            qName = new QName(nsResolver.getDefaultNamespaceURI(), localName);
        } else {
            qName = new QName(localName);
        }
    }
    return qName;
}
Also used : QName(javax.xml.namespace.QName) NamespaceResolver(org.eclipse.persistence.internal.oxm.NamespaceResolver)

Example 18 with NamespaceResolver

use of org.eclipse.persistence.internal.oxm.NamespaceResolver in project eclipselink by eclipse-ee4j.

the class SchemaModelGenerator method generateSchemas.

/**
 * Generates a Map of EclipseLink schema model Schema objects for a given list of XMLDescriptors.
 * The descriptors are assumed to have been initialized.  One Schema  object will be generated
 * per namespace.
 *
 * @param descriptorsToProcess list of XMLDescriptors which will be used to generate Schema objects
 * @param properties holds a namespace to Properties map containing schema settings, such as elementFormDefault
 * @param additionalGlobalElements a map of QName-Type entries identifying additional global elements to be added
 * @return a map of namespaces to EclipseLink schema model Schema objects
 * @throws DescriptorException if the reference descriptor for a composite mapping is not in the list of descriptors
 * @see Schema
 */
public Map<String, Schema> generateSchemas(List<Descriptor> descriptorsToProcess, SchemaModelGeneratorProperties properties, SchemaModelOutputResolver outputResolver, Map<QName, Type> additionalGlobalElements) throws DescriptorException {
    Map<String, Schema> schemas = generateSchemas(descriptorsToProcess, properties, additionalGlobalElements);
    // write out the generates schema(s) via the given output resolver
    Project proj = new SchemaModelProject();
    XMLContext context = new XMLContext(proj);
    XMLMarshaller marshaller = context.createMarshaller();
    Descriptor schemaDescriptor = (Descriptor) proj.getDescriptor(Schema.class);
    int schemaCount = 0;
    for (Entry<String, Schema> entry : schemas.entrySet()) {
        Schema schema = entry.getValue();
        try {
            NamespaceResolver schemaNamespaces = schema.getNamespaceResolver();
            schemaNamespaces.put(Constants.SCHEMA_PREFIX, "http://www.w3.org/2001/XMLSchema");
            schemaDescriptor.setNamespaceResolver(schemaNamespaces);
            javax.xml.transform.Result target = outputResolver.createOutput(schema.getTargetNamespace(), schema.getName());
            marshaller.marshal(schema, target);
            schemaCount++;
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    return schemas;
}
Also used : XMLContext(org.eclipse.persistence.oxm.XMLContext) XMLMarshaller(org.eclipse.persistence.oxm.XMLMarshaller) Schema(org.eclipse.persistence.internal.oxm.schema.model.Schema) IOException(java.io.IOException) Project(org.eclipse.persistence.sessions.Project) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) NamespaceResolver(org.eclipse.persistence.internal.oxm.NamespaceResolver)

Example 19 with NamespaceResolver

use of org.eclipse.persistence.internal.oxm.NamespaceResolver in project eclipselink by eclipse-ee4j.

the class DomToXMLEventWriter method writeElement.

private void writeElement(Element elem, String newNamespace, String newName, XMLEventWriter xew) throws XMLStreamException {
    String prefix = null;
    String namespace = null;
    String localName = null;
    String nodeName = null;
    if (newName != null) {
        namespace = newNamespace;
        localName = newName;
        nodeName = newName;
        if (newNamespace != null && newNamespace.length() > 0) {
            NamespaceResolver tempNR = new NamespaceResolver();
            tempNR.setDOM(elem);
            prefix = tempNR.resolveNamespaceURI(namespace);
            if (prefix == null || prefix.length() == 0) {
                String defaultNamespace = elem.getAttributeNS(javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI, javax.xml.XMLConstants.XMLNS_ATTRIBUTE);
                if (defaultNamespace == null) {
                    prefix = tempNR.generatePrefix();
                } else if (defaultNamespace != namespace) {
                    prefix = tempNR.generatePrefix();
                } else {
                    prefix = Constants.EMPTY_STRING;
                }
            }
        }
    } else {
        prefix = elem.getPrefix();
        namespace = elem.getNamespaceURI();
        localName = elem.getLocalName();
        nodeName = elem.getNodeName();
    }
    String defaultNamespace = xew.getNamespaceContext().getNamespaceURI(Constants.EMPTY_STRING);
    boolean needToAddDefaultNS = false;
    if (prefix != null && prefix.length() > 0) {
        String namespaceURI = xew.getNamespaceContext().getNamespaceURI(prefix);
        xew.add(xmlEventFactory.createStartElement(prefix, namespace, localName));
        if (!(namespace.equals(namespaceURI))) {
            xew.add(xmlEventFactory.createNamespace(prefix, namespace));
        }
    } else {
        if (namespace == null || namespace.length() == 0) {
            xew.add(xmlEventFactory.createStartElement("", "", nodeName));
            if (defaultNamespace != null && defaultNamespace.length() > 0) {
                // write default namespace declaration
                xew.add(xmlEventFactory.createNamespace(Constants.EMPTY_STRING));
            }
        } else {
            needToAddDefaultNS = true;
            xew.add(xmlEventFactory.createStartElement(Constants.EMPTY_STRING, namespace, localName));
        }
    }
    NodeList childNodes = elem.getChildNodes();
    NamedNodeMap attrs = elem.getAttributes();
    ArrayList<Attr> nonNamespaceDeclAttrs = new ArrayList<>();
    // write out any namespace decls first
    for (int i = 0; i < attrs.getLength(); i++) {
        Attr next = (Attr) attrs.item(i);
        if (next.getNodeType() == Node.ATTRIBUTE_NODE) {
            if (next.getPrefix() != null && next.getPrefix().equals(javax.xml.XMLConstants.XMLNS_ATTRIBUTE)) {
                String currentUri = xew.getNamespaceContext().getNamespaceURI(next.getLocalName());
                if (currentUri == null || !currentUri.equals(next.getValue())) {
                    xew.add(xmlEventFactory.createNamespace(next.getLocalName(), next.getValue()));
                }
            } else {
                if (next.getName().equals(javax.xml.XMLConstants.XMLNS_ATTRIBUTE)) {
                    // Part of bug fix 398446 modified fix for Bug 387464.
                    xew.add(xmlEventFactory.createNamespace(next.getValue()));
                    needToAddDefaultNS = false;
                } else {
                    nonNamespaceDeclAttrs.add(next);
                }
            }
        }
    }
    if (needToAddDefaultNS) {
        if (defaultNamespace == null || !defaultNamespace.equals(namespace)) {
            xew.add(xmlEventFactory.createNamespace(namespace));
        }
    }
    for (Attr next : nonNamespaceDeclAttrs) {
        if (next.getPrefix() == null) {
            xew.add(xmlEventFactory.createAttribute(next.getName(), next.getValue()));
        } else {
            xew.add(xmlEventFactory.createAttribute(next.getPrefix(), next.getNamespaceURI(), next.getLocalName(), next.getValue()));
        }
    }
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node next = childNodes.item(i);
        if (next.getNodeType() == Node.TEXT_NODE) {
            xew.add(xmlEventFactory.createCharacters(next.getNodeValue()));
        } else if (next.getNodeType() == Node.CDATA_SECTION_NODE) {
            xew.add(xmlEventFactory.createCData(next.getNodeValue()));
        } else if (next.getNodeType() == Node.COMMENT_NODE) {
            xew.add(xmlEventFactory.createComment(next.getNodeValue()));
        } else if (next.getNodeType() == Node.ELEMENT_NODE) {
            writeElement((Element) next, null, null, xew);
        }
    }
    if (elem.getPrefix() != null && elem.getPrefix().length() > 0) {
        xew.add(xmlEventFactory.createEndElement(elem.getPrefix(), elem.getNamespaceURI(), elem.getLocalName()));
    } else if (elem.getNamespaceURI() != null && elem.getNamespaceURI().length() > 0) {
        xew.add(xmlEventFactory.createEndElement("", elem.getNamespaceURI(), elem.getLocalName()));
    } else {
        xew.add(xmlEventFactory.createEndElement("", "", elem.getNodeName()));
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) NamespaceResolver(org.eclipse.persistence.internal.oxm.NamespaceResolver) Attr(org.w3c.dom.Attr)

Example 20 with NamespaceResolver

use of org.eclipse.persistence.internal.oxm.NamespaceResolver in project eclipselink by eclipse-ee4j.

the class DomToXMLStreamWriter method getPrefix.

protected String getPrefix(NamespaceContext nc, Element elem, String uri) {
    NamespaceResolver tempResovler = new NamespaceResolver();
    tempResovler.setDOM(elem);
    String prefix = tempResovler.resolveNamespaceURI(uri);
    if (prefix == null || prefix.length() == 0) {
        prefix = tempResovler.generatePrefix();
    }
    return prefix;
}
Also used : NamespaceResolver(org.eclipse.persistence.internal.oxm.NamespaceResolver)

Aggregations

NamespaceResolver (org.eclipse.persistence.internal.oxm.NamespaceResolver)20 ArrayList (java.util.ArrayList)3 Descriptor (org.eclipse.persistence.internal.oxm.mappings.Descriptor)3 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)3 NamedNodeMap (org.w3c.dom.NamedNodeMap)3 Node (org.w3c.dom.Node)3 CoreDescriptor (org.eclipse.persistence.core.descriptors.CoreDescriptor)2 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)2 DescriptorException (org.eclipse.persistence.exceptions.DescriptorException)2 JAXBException (org.eclipse.persistence.exceptions.JAXBException)2 InstanceVariableAttributeAccessor (org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor)2 MethodAttributeAccessor (org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor)2 JAXBSetMethodAttributeAccessor (org.eclipse.persistence.internal.jaxb.JAXBSetMethodAttributeAccessor)2 Field (org.eclipse.persistence.internal.oxm.mappings.Field)2 StackUnmarshalNamespaceResolver (org.eclipse.persistence.internal.oxm.record.namespaces.StackUnmarshalNamespaceResolver)2 JavaClass (org.eclipse.persistence.jaxb.javamodel.JavaClass)2 IOException (java.io.IOException)1 List (java.util.List)1 QName (javax.xml.namespace.QName)1 CoreMapping (org.eclipse.persistence.core.mappings.CoreMapping)1