Search in sources :

Example 6 with Namespace

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;
}
Also used : ArrayList(java.util.ArrayList) NamespaceResolver(org.eclipse.persistence.internal.oxm.NamespaceResolver) ArrayList(java.util.ArrayList) List(java.util.List) Namespace(org.eclipse.persistence.internal.oxm.Namespace)

Example 7 with Namespace

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();
}
Also used : Namespace(org.eclipse.persistence.internal.oxm.Namespace)

Example 8 with Namespace

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);
}
Also used : Project(org.eclipse.persistence.sessions.Project) SchemaModelProject(org.eclipse.persistence.internal.oxm.schema.SchemaModelProject) StringWriter(java.io.StringWriter) SchemaModelProject(org.eclipse.persistence.internal.oxm.schema.SchemaModelProject) XMLContext(org.eclipse.persistence.oxm.XMLContext) XMLMarshaller(org.eclipse.persistence.oxm.XMLMarshaller) Vector(java.util.Vector) Namespace(org.eclipse.persistence.internal.oxm.Namespace)

Example 9 with Namespace

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();
    }
}
Also used : Project(org.eclipse.persistence.sessions.Project) SchemaModelProject(org.eclipse.persistence.internal.oxm.schema.SchemaModelProject) SchemaModelProject(org.eclipse.persistence.internal.oxm.schema.SchemaModelProject) XMLContext(org.eclipse.persistence.oxm.XMLContext) XMLMarshaller(org.eclipse.persistence.oxm.XMLMarshaller) FileWriter(java.io.FileWriter) Vector(java.util.Vector) File(java.io.File) Namespace(org.eclipse.persistence.internal.oxm.Namespace)

Example 10 with Namespace

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);
}
Also used : DOMRecord(org.eclipse.persistence.oxm.record.DOMRecord) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Namespace(org.eclipse.persistence.internal.oxm.Namespace) MappedKeyMapContainerPolicy(org.eclipse.persistence.internal.queries.MappedKeyMapContainerPolicy) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) XMLObjectBuilder(org.eclipse.persistence.internal.oxm.XMLObjectBuilder) NamespaceResolver(org.eclipse.persistence.oxm.NamespaceResolver) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) NamedNodeMap(org.w3c.dom.NamedNodeMap)

Aggregations

Namespace (org.eclipse.persistence.internal.oxm.Namespace)15 SchemaModelProject (org.eclipse.persistence.internal.oxm.schema.SchemaModelProject)6 XMLContext (org.eclipse.persistence.oxm.XMLContext)6 XMLMarshaller (org.eclipse.persistence.oxm.XMLMarshaller)6 Project (org.eclipse.persistence.sessions.Project)6 StringWriter (java.io.StringWriter)4 Vector (java.util.Vector)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 NamespaceResolver (org.eclipse.persistence.oxm.NamespaceResolver)3 Type (commonj.sdo.Type)2 DataObject (commonj.sdo.DataObject)1 XmlAccessorType (jakarta.xml.bind.annotation.XmlAccessorType)1 XmlElement (jakarta.xml.bind.annotation.XmlElement)1 XmlMimeType (jakarta.xml.bind.annotation.XmlMimeType)1 XmlSchemaType (jakarta.xml.bind.annotation.XmlSchemaType)1 XmlType (jakarta.xml.bind.annotation.XmlType)1 XmlJavaTypeAdapter (jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1