use of org.eclipse.persistence.internal.oxm.NamespaceResolver in project eclipselink by eclipse-ee4j.
the class MappingsGenerator method generateWrapperClassAndDescriptor.
private Class<?> generateWrapperClassAndDescriptor(TypeInfo type, QName next, ElementDeclaration nextElement, String nextClassName, String attributeTypeName) {
String namespaceUri = null;
if (next != null) {
// generate a class/descriptor for this element
namespaceUri = next.getNamespaceURI();
if (namespaceUri == null || namespaceUri.equals(XMLProcessor.DEFAULT)) {
namespaceUri = "";
}
}
TypeMappingInfo tmi = nextElement.getTypeMappingInfo();
Class<?> generatedClass = null;
JaxbClassLoader loader = getJaxbClassLoader();
if (tmi != null) {
generatedClass = CompilerHelper.getExisitingGeneratedClass(tmi, typeMappingInfoToGeneratedClasses, typeMappingInfoToAdapterClasses, helper.getClassLoader());
if (generatedClass == null) {
generatedClass = this.generateWrapperClass(loader.nextAvailableGeneratedClassName(), attributeTypeName, nextElement.isList(), next);
}
typeMappingInfoToGeneratedClasses.put(tmi, generatedClass);
} else {
generatedClass = this.generateWrapperClass(loader.nextAvailableGeneratedClassName(), attributeTypeName, nextElement.isList(), next);
}
this.qNamesToGeneratedClasses.put(next, generatedClass);
try {
Class<Object> declaredClass = PrivilegedAccessHelper.getClassForName(nextClassName, false, helper.getClassLoader());
this.qNamesToDeclaredClasses.put(next, declaredClass);
} catch (Exception e) {
}
Descriptor desc = (Descriptor) project.getDescriptor(generatedClass);
if (desc == null) {
desc = new XMLDescriptor();
desc.setJavaClass(generatedClass);
if (nextElement.isList()) {
DirectCollectionMapping<AbstractSession, AttributeAccessor, ContainerPolicy, Converter, ClassDescriptor, DatabaseField, XMLMarshaller, Session, XMLUnmarshaller, XMLRecord> mapping = new XMLCompositeDirectCollectionMapping();
mapping.setAttributeName("value");
mapping.setXPath("text()");
mapping.setUsesSingleNode(true);
mapping.setReuseContainer(true);
if (type != null && type.isEnumerationType()) {
mapping.setValueConverter(buildJAXBEnumTypeConverter(mapping, (EnumTypeInfo) type));
} else {
try {
Class<Object> fieldElementClass = PrivilegedAccessHelper.getClassForName(nextClassName, false, helper.getClassLoader());
mapping.setFieldElementClass(fieldElementClass);
} catch (ClassNotFoundException e) {
}
}
if (nextClassName.equals("[B") || nextClassName.equals("[Ljava.lang.Byte;")) {
((Field) mapping.getField()).setSchemaType(Constants.BASE_64_BINARY_QNAME);
} else if (nextClassName.equals("javax.xml.namespace.QName")) {
((Field) mapping.getField()).setSchemaType(Constants.QNAME_QNAME);
}
desc.addMapping((CoreMapping) mapping);
} else {
if (nextElement.getJavaTypeName().equals(OBJECT_CLASS_NAME)) {
CompositeObjectMapping<AbstractSession, AttributeAccessor, ContainerPolicy, Converter, ClassDescriptor, DatabaseField, XMLMarshaller, Session, UnmarshalKeepAsElementPolicy, XMLUnmarshaller, XMLRecord> mapping = new XMLCompositeObjectMapping();
mapping.setAttributeName("value");
mapping.setSetMethodName("setValue");
mapping.setGetMethodName("getValue");
mapping.setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT);
mapping.setXPath(".");
setTypedTextField((Field) mapping.getField());
desc.addMapping((CoreMapping) mapping);
} else if (isBinaryData(nextElement.getJavaType())) {
BinaryDataMapping<AbstractSession, AttributeAccessor, ContainerPolicy, Converter, ClassDescriptor, DatabaseField, XMLMarshaller, MimeTypePolicy, Session, XMLUnmarshaller, XMLRecord> mapping = new XMLBinaryDataMapping();
mapping.setAttributeName("value");
mapping.setXPath(".");
((Field) mapping.getField()).setSchemaType(Constants.BASE_64_BINARY_QNAME);
mapping.setSetMethodName("setValue");
mapping.setGetMethodName("getValue");
mapping.getNullPolicy().setNullRepresentedByXsiNil(true);
mapping.getNullPolicy().setNullRepresentedByEmptyNode(false);
Class<?> attributeClassification = org.eclipse.persistence.internal.helper.Helper.getClassFromClasseName(attributeTypeName, helper.getClassLoader());
mapping.setAttributeClassification(attributeClassification);
mapping.setShouldInlineBinaryData(false);
// if(nextElement.getTypeMappingInfo() != null) {
mapping.setSwaRef(nextElement.isXmlAttachmentRef());
mapping.setMimeType(nextElement.getXmlMimeType());
// }
desc.addMapping((CoreMapping) mapping);
} else {
DirectMapping<AbstractSession, AttributeAccessor, ContainerPolicy, Converter, ClassDescriptor, DatabaseField, XMLMarshaller, Session, XMLUnmarshaller, XMLRecord> mapping = new XMLDirectMapping();
mapping.setNullValueMarshalled(true);
mapping.setAttributeName("value");
mapping.setXPath("text()");
mapping.setSetMethodName("setValue");
mapping.setGetMethodName("getValue");
if (nextElement.getDefaultValue() != null) {
mapping.setNullValue(nextElement.getDefaultValue());
mapping.getNullPolicy().setNullRepresentedByXsiNil(true);
}
if (helper.isBuiltInJavaType(nextElement.getJavaType())) {
Class<?> attributeClassification = null;
if (nextElement.getJavaType().isPrimitive()) {
attributeClassification = XMLConversionManager.getDefaultManager().convertClassNameToClass(attributeTypeName);
} else {
attributeClassification = org.eclipse.persistence.internal.helper.Helper.getClassFromClasseName(attributeTypeName, helper.getClassLoader());
}
mapping.setAttributeClassification(attributeClassification);
}
IsSetNullPolicy nullPolicy = new IsSetNullPolicy("isSetValue", false, true, XMLNullRepresentationType.ABSENT_NODE);
// nullPolicy.setNullRepresentedByEmptyNode(true);
mapping.setNullPolicy(nullPolicy);
if (type != null && type.isEnumerationType()) {
mapping.setConverter(buildJAXBEnumTypeConverter(mapping, (EnumTypeInfo) type));
}
if (nextClassName.equals("[B") || nextClassName.equals("[Ljava.lang.Byte;")) {
((Field) mapping.getField()).setSchemaType(Constants.BASE_64_BINARY_QNAME);
} else if (nextClassName.equals("javax.xml.namespace.QName")) {
((Field) mapping.getField()).setSchemaType(Constants.QNAME_QNAME);
}
if (nextElement.getJavaTypeAdapterClass() != null) {
mapping.setConverter(new XMLJavaTypeConverter(nextElement.getJavaTypeAdapterClass()));
}
desc.addMapping((CoreMapping) mapping);
}
}
if (next != null) {
NamespaceInfo info = getNamespaceInfoForURI(namespaceUri);
if (info != null) {
NamespaceResolver resolver = getNamespaceResolverForDescriptor(info);
String prefix = null;
if (namespaceUri != Constants.EMPTY_STRING) {
prefix = resolver.resolveNamespaceURI(namespaceUri);
if (prefix == null) {
prefix = getPrefixForNamespace(namespaceUri, resolver);
}
}
desc.setNamespaceResolver(resolver);
if (nextElement.isXmlRootElement()) {
desc.setDefaultRootElement(getQualifiedString(prefix, next.getLocalPart()));
} else {
desc.setDefaultRootElement("");
desc.addRootElement(getQualifiedString(prefix, next.getLocalPart()));
desc.setResultAlwaysXMLRoot(true);
}
} else {
if (namespaceUri.equals("")) {
desc.setDefaultRootElement(next.getLocalPart());
} else {
NamespaceResolver resolver = new org.eclipse.persistence.oxm.NamespaceResolver();
String prefix = getPrefixForNamespace(namespaceUri, resolver);
desc.setNamespaceResolver(resolver);
if (nextElement.isXmlRootElement()) {
desc.setDefaultRootElement(getQualifiedString(prefix, next.getLocalPart()));
} else {
desc.setDefaultRootElement("");
desc.addRootElement(getQualifiedString(prefix, next.getLocalPart()));
desc.setResultAlwaysXMLRoot(true);
}
}
}
}
project.addDescriptor((CoreDescriptor) desc);
}
return generatedClass;
}
use of org.eclipse.persistence.internal.oxm.NamespaceResolver in project eclipselink by eclipse-ee4j.
the class MappingsGenerator method processDefaultNamespacePreferences.
private void processDefaultNamespacePreferences(Collection<PackageInfo> packageInfos) {
if (isDefaultNamespaceAllowed && globalNamespaceResolver.getDefaultNamespaceURI() == null) {
for (PackageInfo packageInfo : packageInfos) {
NamespaceResolver resolver = packageInfo.getNamespaceInfo().getNamespaceResolver();
String defaultNS = resolver.getDefaultNamespaceURI();
if (defaultNS != null) {
globalNamespaceResolver.setDefaultNamespaceURI(defaultNS);
break;
}
}
}
}
use of org.eclipse.persistence.internal.oxm.NamespaceResolver in project eclipselink by eclipse-ee4j.
the class MappingsGenerator method generateDescriptor.
public void generateDescriptor(JavaClass javaClass, CoreProject project) {
String jClassName = javaClass.getQualifiedName();
TypeInfo info = typeInfo.get(jClassName);
if (info.isTransient()) {
return;
}
NamespaceInfo namespaceInfo = this.packageToPackageInfoMappings.get(javaClass.getPackageName()).getNamespaceInfo();
String packageNamespace = namespaceInfo.getNamespace();
String elementName;
String namespace;
if (javaClass.getSuperclass() != null && javaClass.getSuperclass().getName().equals("jakarta.xml.bind.JAXBElement")) {
generateDescriptorForJAXBElementSubclass(javaClass, project, getNamespaceResolverForDescriptor(namespaceInfo));
return;
}
Descriptor descriptor = new XMLDescriptor();
org.eclipse.persistence.jaxb.xmlmodel.XmlRootElement rootElem = info.getXmlRootElement();
if (rootElem == null) {
try {
elementName = info.getXmlNameTransformer().transformRootElementName(javaClass.getName());
} catch (Exception ex) {
throw org.eclipse.persistence.exceptions.JAXBException.exceptionDuringNameTransformation(javaClass.getName(), info.getXmlNameTransformer().getClass().getName(), ex);
}
namespace = packageNamespace;
} else {
elementName = rootElem.getName();
if (elementName.equals(XMLProcessor.DEFAULT)) {
try {
elementName = info.getXmlNameTransformer().transformRootElementName(javaClass.getName());
} catch (Exception ex) {
throw org.eclipse.persistence.exceptions.JAXBException.exceptionDuringNameTransformation(javaClass.getName(), info.getXmlNameTransformer().getClass().getName(), ex);
}
}
namespace = rootElem.getNamespace();
}
descriptor.setJavaClassName(jClassName);
if (info.getFactoryMethodName() != null) {
descriptor.getInstantiationPolicy().useFactoryInstantiationPolicy(info.getObjectFactoryClassName(), info.getFactoryMethodName());
}
if (namespace.equals(XMLProcessor.DEFAULT)) {
namespace = namespaceInfo.getNamespace();
}
NamespaceResolver resolverForDescriptor = getNamespaceResolverForDescriptor(namespaceInfo);
JavaClass manyValueJavaClass = helper.getJavaClass(ManyValue.class);
if (!manyValueJavaClass.isAssignableFrom(javaClass)) {
if (isDefaultNamespaceAllowed && namespace.length() != 0 && globalNamespaceResolver.getDefaultNamespaceURI() == null && !resolverForDescriptor.getPrefixesToNamespaces().containsValue(namespace)) {
globalNamespaceResolver.setDefaultNamespaceURI(namespace);
resolverForDescriptor.setDefaultNamespaceURI(namespace);
}
if (rootElem == null) {
descriptor.setDefaultRootElement("");
} else {
if (namespace.length() == 0) {
descriptor.setDefaultRootElement(elementName);
} else {
descriptor.setDefaultRootElement(getQualifiedString(getPrefixForNamespace(namespace, resolverForDescriptor), elementName));
}
}
}
descriptor.setNamespaceResolver(resolverForDescriptor);
setSchemaContext(descriptor, info);
// set the ClassExtractor class name if necessary
if (info.isSetClassExtractorName()) {
descriptor.getInheritancePolicy().setClassExtractorName(info.getClassExtractorName());
}
// set any user-defined properties
if (info.getUserProperties() != null) {
descriptor.setProperties(info.getUserProperties());
}
if (info.isLocationAware()) {
Property locProp = null;
Iterator<Property> i = info.getPropertyList().iterator();
while (i.hasNext()) {
Property p = i.next();
if (p.getType().getName().equals(Constants.LOCATOR_CLASS_NAME)) {
locProp = p;
}
}
if (locProp != null && locProp.isTransient()) {
// don't make a mapping
if (locProp.isMethodProperty()) {
MethodAttributeAccessor aa = new MethodAttributeAccessor();
aa.setAttributeName(locProp.getPropertyName());
aa.setSetMethodName(locProp.getSetMethodName());
aa.setGetMethodName(locProp.getGetMethodName());
descriptor.setLocationAccessor(aa);
} else {
// instance variable property
InstanceVariableAttributeAccessor aa = new InstanceVariableAttributeAccessor();
aa.setAttributeName(locProp.getPropertyName());
descriptor.setLocationAccessor(aa);
}
}
}
if (!info.getObjectGraphs().isEmpty()) {
// these will be populated later to allow for linking
for (XmlNamedObjectGraph next : info.getObjectGraphs()) {
AttributeGroup attributeGroup = new AttributeGroup(next.getName(), info.getJavaClassName(), false);
((XMLDescriptor) descriptor).addAttributeGroup(attributeGroup);
// process subclass graphs for inheritance
// for(NamedSubgraph nextSubclass:next.getNamedSubclassGraph()) {
// attributeGroup.insertSubClass(new AttributeGroup(next.getName(), nextSubclass.getType()));
// }
}
}
project.addDescriptor((CoreDescriptor) descriptor);
info.setDescriptor(descriptor);
}
use of org.eclipse.persistence.internal.oxm.NamespaceResolver in project eclipselink by eclipse-ee4j.
the class DomToXMLStreamWriter method writeElement.
private void writeElement(Element elem, String newNamespace, String newName, XMLStreamWriter xsw) 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 = xsw.getNamespaceContext().getNamespaceURI(Constants.EMPTY_STRING);
boolean needToAddDefaultNS = false;
if (prefix != null && prefix.length() > 0) {
String namespaceURI = xsw.getNamespaceContext().getNamespaceURI(prefix);
xsw.writeStartElement(prefix, localName, namespace);
if (!(namespace.equals(namespaceURI))) {
xsw.writeNamespace(prefix, namespace);
}
} else {
if (namespace == null || namespace.length() == 0) {
xsw.writeStartElement(nodeName);
if (defaultNamespace != null && defaultNamespace.length() > 0) {
// write default namespace declaration
xsw.writeDefaultNamespace(Constants.EMPTY_STRING);
}
} else {
needToAddDefaultNS = true;
xsw.writeStartElement(Constants.EMPTY_STRING, localName, namespace);
}
}
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 = xsw.getNamespaceContext().getNamespaceURI(next.getLocalName());
if (currentUri == null || !currentUri.equals(next.getValue())) {
xsw.writeNamespace(next.getLocalName(), next.getValue());
}
} else {
if (next.getName().equals(javax.xml.XMLConstants.XMLNS_ATTRIBUTE)) {
// Part of bug fix 398446 modified fix for Bug 387464.
xsw.writeDefaultNamespace(next.getValue());
needToAddDefaultNS = false;
} else {
nonNamespaceDeclAttrs.add(next);
}
}
}
}
if (needToAddDefaultNS) {
if (defaultNamespace == null || !defaultNamespace.equals(namespace)) {
xsw.writeDefaultNamespace(namespace);
}
}
for (Attr next : nonNamespaceDeclAttrs) {
if (next.getPrefix() == null) {
xsw.writeAttribute(next.getName(), next.getValue());
} else {
xsw.writeAttribute(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) {
xsw.writeCharacters(next.getNodeValue());
} else if (next.getNodeType() == Node.CDATA_SECTION_NODE) {
xsw.writeCData(next.getNodeValue());
} else if (next.getNodeType() == Node.COMMENT_NODE) {
xsw.writeComment(next.getNodeValue());
} else if (next.getNodeType() == Node.ELEMENT_NODE) {
writeElement((Element) next, null, null, xsw);
}
}
xsw.writeEndElement();
}
use of org.eclipse.persistence.internal.oxm.NamespaceResolver 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;
}
Aggregations