Search in sources :

Example 6 with NamespaceResolver

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

the class DOMReader method reportElementEvents.

protected void reportElementEvents(Element elem, String newUri, String newName) throws SAXException {
    this.currentNode = elem;
    IndexedAttributeList attributes = buildAttributeList(elem);
    String namespaceUri = null;
    String qname = null;
    String lname = null;
    if (newName == null) {
        // Handle null local name
        lname = elem.getLocalName();
        if (lname == null) {
            // If local name is null, use the node name
            lname = elem.getNodeName();
            qname = lname;
            handlePrefixedAttribute(elem);
        } else {
            qname = getQName(elem);
        }
        namespaceUri = elem.getNamespaceURI();
        if (namespaceUri == null) {
            namespaceUri = "";
        }
    } else {
        namespaceUri = newUri;
        lname = newName;
        qname = newName;
        if (namespaceUri != null && isNamespaceAware()) {
            NamespaceResolver tmpNR = new NamespaceResolver();
            tmpNR.setDOM(elem);
            String prefix = tmpNR.resolveNamespaceURI(namespaceUri);
            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 = tmpNR.generatePrefix();
                    contentHandler.startPrefixMapping(prefix, namespaceUri);
                } else if (defaultNamespace != namespaceUri) {
                    prefix = tmpNR.generatePrefix();
                    contentHandler.startPrefixMapping(prefix, namespaceUri);
                } else {
                    prefix = Constants.EMPTY_STRING;
                }
            }
            if (prefix != null && prefix.length() > 0) {
                qname = prefix + Constants.COLON + qname;
            }
        }
    }
    contentHandler.startElement(namespaceUri, lname, qname, attributes);
    handleChildNodes(elem.getChildNodes());
    contentHandler.endElement(namespaceUri, lname, qname);
    endPrefixMappings(elem);
}
Also used : StackUnmarshalNamespaceResolver(org.eclipse.persistence.internal.oxm.record.namespaces.StackUnmarshalNamespaceResolver) NamespaceResolver(org.eclipse.persistence.internal.oxm.NamespaceResolver)

Example 7 with NamespaceResolver

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

the class XMLFragmentReader method handlePrefixedAttribute.

/**
 * Handle prefixed attribute - may need to declare the namespace
 * URI locally.
 */
@Override
protected void handlePrefixedAttribute(Element elem) throws SAXException {
    // Need to determine if the URI for the prefix needs to be
    // declared locally:
    // If the prefix or URI are not in the resolver, or the URI
    // associated with the prefix (in the resolver) is different
    // than node's URI, write out the node's URI locally
    String prefix = elem.getPrefix();
    if (prefix == null) {
        prefix = Constants.EMPTY_STRING;
    }
    String uri = resolveNamespacePrefix(prefix);
    if ((uri == null && elem.getNamespaceURI() != null) || (uri != null && !uri.equals(elem.getNamespaceURI()))) {
        NamespaceResolver tmpresolver = getTempResolver(elem);
        tmpresolver.put(prefix, elem.getNamespaceURI());
        if (!nsresolverList.contains(tmpresolver)) {
            nsresolverList.add(tmpresolver);
        }
        String namespaceURI = elem.getNamespaceURI();
        if (null == namespaceURI) {
            namespaceURI = Constants.EMPTY_STRING;
        }
        getContentHandler().startPrefixMapping(prefix, namespaceURI);
    }
    NamedNodeMap attributes = elem.getAttributes();
    if (attributes != null) {
        for (int x = 0; x < attributes.getLength(); x++) {
            Node attribute = attributes.item(x);
            if (XMLConstants.XMLNS_ATTRIBUTE.equals(attribute.getPrefix())) {
                NamespaceResolver tmpresolver = getTempResolver(elem);
                tmpresolver.put(attribute.getLocalName(), attribute.getNodeValue());
                if (!nsresolverList.contains(tmpresolver)) {
                    nsresolverList.add(tmpresolver);
                }
            } else if (XMLConstants.XMLNS_ATTRIBUTE.equals(attribute.getNodeName())) {
                NamespaceResolver tmpresolver = getTempResolver(elem);
                String namespace = attribute.getNodeValue();
                if (null == namespace) {
                    namespace = Constants.EMPTY_STRING;
                }
                tmpresolver.put(Constants.EMPTY_STRING, namespace);
                if (!nsresolverList.contains(tmpresolver)) {
                    nsresolverList.add(tmpresolver);
                }
            }
        }
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Node(org.w3c.dom.Node) NamespaceResolver(org.eclipse.persistence.internal.oxm.NamespaceResolver)

Example 8 with NamespaceResolver

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

the class XMLFragmentReader method resolveNamespacePrefix.

/**
 * Convenience method that iterates over each namespace resolver
 * in the resolver list until it locates a uri for 'prefix' or
 * the final resolver is reached w/o success.
 * @return true if a URI exists in one of the resolvers in the
 * list, false otherwise
 */
protected String resolveNamespacePrefix(String prefix) {
    String uri = null;
    if (null == prefix || prefix.length() == 0) {
        for (int i = nsresolverList.size() - 1; i >= 0; i--) {
            NamespaceResolver next = nsresolverList.get(i);
            uri = next.getDefaultNamespaceURI();
            if ((uri != null) && uri.length() > 0) {
                break;
            }
        }
    } else {
        for (int i = nsresolverList.size() - 1; i >= 0; i--) {
            NamespaceResolver next = nsresolverList.get(i);
            uri = next.resolveNamespacePrefix(prefix);
            if ((uri != null) && uri.length() > 0) {
                break;
            }
        }
    }
    return uri;
}
Also used : NamespaceResolver(org.eclipse.persistence.internal.oxm.NamespaceResolver)

Example 9 with NamespaceResolver

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

the class MarshalRecord method processNamespaceResolverForXSIPrefix.

/**
 * INTERNAL:
 * Private function to process or create an entry in the NamespaceResolver for the xsi prefix.
 * @return xsi prefix
 * @since EclipseLink 2.4
 */
protected String processNamespaceResolverForXSIPrefix(NamespaceResolver namespaceResolver) {
    String xsiPrefix;
    if (null == namespaceResolver) {
        // add new xsi entry into the properties map
        xsiPrefix = Constants.SCHEMA_INSTANCE_PREFIX;
        namespaceResolver = new NamespaceResolver();
        namespaceResolver.put(xsiPrefix, javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
        namespaceDeclaration(xsiPrefix, javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
    } else {
        // find an existing xsi entry in the map
        xsiPrefix = namespaceResolver.resolveNamespaceURI(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
        if (null == xsiPrefix) {
            xsiPrefix = namespaceResolver.generatePrefix(Constants.SCHEMA_INSTANCE_PREFIX);
            namespaceDeclaration(xsiPrefix, javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
        }
    }
    return xsiPrefix;
}
Also used : NamespaceResolver(org.eclipse.persistence.internal.oxm.NamespaceResolver)

Example 10 with NamespaceResolver

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

the class ValidatingMarshalRecord method addXsiTypeAndClassIndicatorIfRequired.

/**
 * @since EclipseLink 2.5.0
 */
@Override
public boolean addXsiTypeAndClassIndicatorIfRequired(Descriptor xmlDescriptor, Descriptor referenceDescriptor, Field xmlField, Object originalObject, Object obj, boolean wasXMLRoot, boolean isRootElement) {
    validatingRecord.setNamespaceResolver(new NamespaceResolver(marshalRecord.getNamespaceResolver()));
    validatingRecord.addXsiTypeAndClassIndicatorIfRequired(xmlDescriptor, referenceDescriptor, xmlField, originalObject, obj, wasXMLRoot, isRootElement);
    return marshalRecord.addXsiTypeAndClassIndicatorIfRequired(xmlDescriptor, referenceDescriptor, xmlField, originalObject, obj, wasXMLRoot, isRootElement);
}
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