use of org.eclipse.persistence.internal.oxm.Namespace in project eclipselink by eclipse-ee4j.
the class AbstractMarshalRecordImpl method addExtraNamespacesToNamespaceResolver.
@Override
public List addExtraNamespacesToNamespaceResolver(Descriptor descriptor, CoreAbstractSession session, boolean allowOverride, boolean ignoreEqualResolvers) {
if (equalNamespaceResolvers && !ignoreEqualResolvers) {
return null;
}
org.eclipse.persistence.internal.oxm.NamespaceResolver descriptorNamespaceResolver = descriptor.getNamespaceResolver();
if (null == descriptorNamespaceResolver || !descriptorNamespaceResolver.hasPrefixesToNamespaces()) {
return null;
}
Map<String, String> prefixesToNamespaces = descriptorNamespaceResolver.getPrefixesToNamespaces();
if (prefixesToNamespaces.size() == 0) {
return null;
}
List returnList = new ArrayList(prefixesToNamespaces.size());
org.eclipse.persistence.internal.oxm.NamespaceResolver marshalRecordNamespaceResolver = namespaceResolver;
for (Entry<String, String> entry : prefixesToNamespaces.entrySet()) {
// if isn't already on a parentadd namespace to this element
String prefix = marshalRecordNamespaceResolver.resolveNamespaceURI(entry.getValue());
if (prefix == null || prefix.length() == 0) {
// if there is no prefix already declared for this uri in the nr add this one
// unless that prefix is already bound to another namespace uri
prefix = entry.getKey();
if (hasCustomNamespaceMapper) {
String newPrefix = getMarshaller().getNamespacePrefixMapper().getPreferredPrefix(entry.getValue(), prefix, true);
if (newPrefix != null && !(newPrefix.length() == 0)) {
prefix = newPrefix;
}
}
String uri = marshalRecordNamespaceResolver.resolveNamespacePrefix(prefix);
if (hasCustomNamespaceMapper || allowOverride || uri == null || uri.length() == 0) {
// if this uri is unknown, the cutom mapper will return the preferred prefix for this uri
marshalRecordNamespaceResolver.put(entry.getKey(), entry.getValue());
returnList.add(new Namespace(prefix, entry.getValue()));
}
} else if (allowOverride) {
// if overrides are allowed, add the prefix if the URI is different
if (!prefix.equals(entry.getKey()) && !hasCustomNamespaceMapper) {
// if prefix exists for uri but is different then add this
// unless using a custom namespace prefix mapper. Then prefix is expected to be different
marshalRecordNamespaceResolver.put(entry.getKey(), entry.getValue());
returnList.add(new Namespace(entry.getKey(), entry.getValue()));
}
}
}
return returnList;
}
use of org.eclipse.persistence.internal.oxm.Namespace in project eclipselink by eclipse-ee4j.
the class MarshalRecord method getPrefixForFragment.
protected String getPrefixForFragment(XPathFragment xPathFragment) {
if (!hasCustomNamespaceMapper) {
return xPathFragment.getPrefix();
}
String uri = xPathFragment.getNamespaceURI();
if (uri == null || uri.length() == 0) {
return Constants.EMPTY_STRING;
}
String defaultNamespace = getNamespaceResolver().getDefaultNamespaceURI();
if (defaultNamespace != null && defaultNamespace.equals(uri)) {
return Constants.EMPTY_STRING;
}
String prefix = this.getNamespaceResolver().resolveNamespaceURI(uri);
if (prefix != null) {
return prefix;
}
for (Object next : getNamespaceResolver().getNamespaces()) {
Namespace ns = (Namespace) next;
uri = ns.getNamespaceURI();
prefix = ns.getPrefix();
}
return xPathFragment.getPrefix();
}
use of org.eclipse.persistence.internal.oxm.Namespace in project eclipselink by eclipse-ee4j.
the class GenerateSchemaTestCases method compareSchemaStrings.
/**
* Compares a schema model Schema (as a string) against a string representation
* of an XML schema.
*/
protected boolean compareSchemaStrings(Schema generatedSchema, String controlSchema) {
Project p = new SchemaModelProject();
Vector namespaces = generatedSchema.getNamespaceResolver().getNamespaces();
for (int i = 0; i < namespaces.size(); i++) {
Namespace next = (Namespace) namespaces.get(i);
((XMLDescriptor) p.getDescriptor(Schema.class)).getNamespaceResolver().put(next.getPrefix(), next.getNamespaceURI());
}
XMLContext context = new XMLContext(p);
XMLMarshaller marshaller = context.createMarshaller();
StringWriter generatedSchemaWriter = new StringWriter();
marshaller.marshal(generatedSchema, generatedSchemaWriter);
return generatedSchemaWriter.toString().equals(controlSchema);
}
use of org.eclipse.persistence.internal.oxm.Namespace in project eclipselink by eclipse-ee4j.
the class GenerateSchemaTestCases method writeSchema.
/**
* Write the given schema to the T_WORK folder.
*/
protected void writeSchema(Schema generatedSchema) {
try {
Project p = new SchemaModelProject();
Vector namespaces = generatedSchema.getNamespaceResolver().getNamespaces();
for (int i = 0; i < namespaces.size(); i++) {
Namespace next = (Namespace) namespaces.get(i);
((XMLDescriptor) p.getDescriptor(Schema.class)).getNamespaceResolver().put(next.getPrefix(), next.getNamespaceURI());
}
XMLContext context = new XMLContext(p);
XMLMarshaller marshaller = context.createMarshaller();
FileWriter generatedSchemaWriter = new FileWriter(new File(TMP_DIR, "generatedSchema.xsd"));
marshaller.marshal(generatedSchema, generatedSchemaWriter);
} catch (Exception ex) {
ex.printStackTrace();
}
}
use of org.eclipse.persistence.internal.oxm.Namespace in project eclipselink by eclipse-ee4j.
the class XMLAnyAttributeMapping method writeSingleValue.
@Override
public void writeSingleValue(Object attributeValue, Object parent, XMLRecord row, AbstractSession session) {
ContainerPolicy cp = this.getContainerPolicy();
if ((attributeValue == null) || (cp.sizeFor(attributeValue) == 0)) {
return;
}
DOMRecord record = (DOMRecord) row;
if (record.getDOM().getNodeType() != Node.ELEMENT_NODE) {
return;
}
DOMRecord recordToModify = record;
Element root = (Element) record.getDOM();
if (field != null) {
root = (Element) XPathEngine.getInstance().create((XMLField) getField(), root, session);
recordToModify = new DOMRecord(root);
}
List extraNamespaces = new ArrayList();
NamespaceResolver nr = recordToModify.getNamespaceResolver();
for (Object iter = cp.iteratorFor(attributeValue); cp.hasNext(iter); ) {
Map.Entry entry = (Map.Entry) cp.nextEntry(iter, session);
Object key = entry.getKey();
if ((key != null) && key instanceof QName) {
Object value = entry.getValue();
QName attributeName = (QName) key;
String namespaceURI = attributeName.getNamespaceURI();
String qualifiedName = attributeName.getLocalPart();
if (nr != null) {
String prefix = nr.resolveNamespaceURI(attributeName.getNamespaceURI());
if ((prefix != null) && prefix.length() > 0) {
qualifiedName = prefix + XMLConstants.COLON + qualifiedName;
} else if (attributeName.getNamespaceURI() != null && attributeName.getNamespaceURI().length() > 0) {
String generatedPrefix = nr.generatePrefix();
qualifiedName = generatedPrefix + XMLConstants.COLON + qualifiedName;
nr.put(generatedPrefix, attributeName.getNamespaceURI());
extraNamespaces.add(new Namespace(generatedPrefix, attributeName.getNamespaceURI()));
recordToModify.getNamespaceResolver().put(generatedPrefix, attributeName.getNamespaceURI());
}
}
if (namespaceURI != null) {
root.setAttributeNS(namespaceURI, qualifiedName, value.toString());
} else {
root.setAttribute(attributeName.getLocalPart(), value.toString());
}
}
}
((XMLObjectBuilder) descriptor.getObjectBuilder()).writeExtraNamespaces(extraNamespaces, recordToModify);
recordToModify.removeExtraNamespacesFromNamespaceResolver(extraNamespaces, session);
}
Aggregations