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);
}
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);
}
}
}
}
}
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;
}
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;
}
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);
}
Aggregations