use of org.eclipse.persistence.internal.oxm.mappings.Descriptor in project eclipselink by eclipse-ee4j.
the class MappingsGenerator method generateMapEntryClassAndDescriptor.
private Class<?> generateMapEntryClassAndDescriptor(Property property, NamespaceResolver nr) {
JavaClass keyType = property.getKeyType();
JavaClass valueType = property.getValueType();
if (keyType == null) {
keyType = helper.getJavaClass("java.lang.Object");
}
if (valueType == null) {
valueType = helper.getJavaClass("java.lang.Object");
}
String mapEntryClassName = getJaxbClassLoader().nextAvailableGeneratedClassName();
MapEntryGeneratedKey mapKey = new MapEntryGeneratedKey(keyType.getQualifiedName(), valueType.getQualifiedName());
Class<?> generatedClass = getGeneratedMapEntryClasses().get(mapKey);
if (generatedClass == null) {
generatedClass = generateMapEntryClass(mapEntryClassName, keyType.getQualifiedName(), valueType.getQualifiedName());
getGeneratedMapEntryClasses().put(mapKey, generatedClass);
Descriptor desc = new XMLDescriptor();
desc.setJavaClass(generatedClass);
desc.addMapping((CoreMapping) generateMappingForType(keyType, Property.DEFAULT_KEY_NAME));
desc.addMapping((CoreMapping) generateMappingForType(valueType, Property.DEFAULT_VALUE_NAME));
desc.setNamespaceResolver(nr);
project.addDescriptor((CoreDescriptor) desc);
}
return generatedClass;
}
use of org.eclipse.persistence.internal.oxm.mappings.Descriptor in project eclipselink by eclipse-ee4j.
the class MappingsGenerator method processGlobalElements.
public void processGlobalElements(CoreProject project) {
// root elements.
if (this.globalElements == null && this.localElements == null) {
return;
}
List<ElementDeclaration> elements = new ArrayList<>();
elements.addAll(this.localElements);
elements.addAll(this.globalElements.values());
for (ElementDeclaration nextElement : elements) {
QName next = nextElement.getElementName();
String nextClassName = nextElement.getJavaTypeName();
TypeInfo type = this.typeInfo.get(nextClassName);
if (helper.isBuiltInJavaType(nextElement.getJavaType()) || (type != null && type.isEnumerationType())) {
// generate a class/descriptor for this element
String attributeTypeName = nextClassName;
if (nextElement.getJavaType().isPrimitive()) {
attributeTypeName = helper.getClassForJavaClass(nextElement.getJavaType()).getName();
}
if (nextElement.getAdaptedJavaTypeName() != null) {
attributeTypeName = nextElement.getAdaptedJavaTypeName();
}
if (next == null) {
if (isBinaryData(nextElement.getJavaType())) {
Class<?> generatedClass = addByteArrayWrapperAndDescriptor(type, nextElement.getJavaType().getRawName(), nextElement, nextClassName, attributeTypeName);
this.qNamesToGeneratedClasses.put(next, generatedClass);
if (nextElement.getTypeMappingInfo() != null) {
typeMappingInfoToGeneratedClasses.put(nextElement.getTypeMappingInfo(), generatedClass);
}
try {
Class<Object> declaredClass = PrivilegedAccessHelper.getClassForName(nextClassName, false, helper.getClassLoader());
this.qNamesToDeclaredClasses.put(next, declaredClass);
} catch (Exception e) {
}
}
if (nextElement.getJavaType().isEnum()) {
if (!(helper.getClassLoader() instanceof DynamicClassLoader)) {
// Only generate enum wrappers in non-dynamic case.
Class<?> generatedClass = addEnumerationWrapperAndDescriptor(type, nextElement.getJavaType().getRawName(), nextElement, nextClassName, attributeTypeName);
this.qNamesToGeneratedClasses.put(next, generatedClass);
if (nextElement.getTypeMappingInfo() != null) {
typeMappingInfoToGeneratedClasses.put(nextElement.getTypeMappingInfo(), generatedClass);
}
try {
Class<Object> declaredClass = PrivilegedAccessHelper.getClassForName(nextClassName, false, helper.getClassLoader());
this.qNamesToDeclaredClasses.put(next, declaredClass);
} catch (Exception ex) {
}
}
}
continue;
}
Class<?> generatedClass = generateWrapperClassAndDescriptor(type, next, nextElement, nextClassName, attributeTypeName);
this.qNamesToGeneratedClasses.put(next, generatedClass);
if (type != null && type.isEnumerationType() && nextElement.isXmlRootElement()) {
this.classToGeneratedClasses.put(type.getJavaClassName(), generatedClass);
}
try {
Class<Object> declaredClass = PrivilegedAccessHelper.getClassForName(nextClassName, false, helper.getClassLoader());
this.qNamesToDeclaredClasses.put(next, declaredClass);
} catch (Exception e) {
}
} else if (type != null && !type.isTransient()) {
if (next.getNamespaceURI() == null || next.getNamespaceURI().equals("")) {
type.getDescriptor().addRootElement(next.getLocalPart());
} else {
Descriptor descriptor = type.getDescriptor();
String uri = next.getNamespaceURI();
String prefix = getPrefixForNamespace(uri, descriptor.getNamespaceResolver());
descriptor.addRootElement(getQualifiedString(prefix, next.getLocalPart()));
}
}
}
}
use of org.eclipse.persistence.internal.oxm.mappings.Descriptor in project eclipselink by eclipse-ee4j.
the class JAXBUnmarshaller method buildJAXBElementFromObject.
/**
* Create a JAXBElement instance. If the object is an instance
* of XMLRoot, we will use its field values to create the
* JAXBElement. If the object is not an XMLRoot instance, we
* will have to determine the 'name' value. This will be done
* using the object's descriptor default root element - any
* prefix will be resolved, and a QName created.
*/
private JAXBElement buildJAXBElementFromObject(Object obj, Class<?> declaredClass) {
// JAXBElement from the returned XMLRoot object
if (obj instanceof Root) {
JAXBElement jaxbElement = jaxbContext.createJAXBElementFromXMLRoot(((Root) obj), declaredClass);
if (((Root) obj).isNil()) {
jaxbElement.setNil(((Root) obj).isNil());
jaxbElement.setValue(null);
}
return jaxbElement;
}
if (obj instanceof JAXBElement) {
return (JAXBElement) obj;
}
// at this point, the default root element of the object being marshalled
// to == the root element - here we need to create a JAXBElement
// instance using information from the returned object
org.eclipse.persistence.sessions.Session sess = xmlUnmarshaller.getXMLContext().getSession(obj);
Descriptor desc = (Descriptor) sess.getClassDescriptor(obj);
// here we are assuming that if we've gotten this far, there
// must be a default root element set on the descriptor. if
// this is incorrect, we need to check for null and throw an
// exception
String rootName = desc.getDefaultRootElement();
if (rootName == null) {
return jaxbContext.createJAXBElement(new QName(""), obj.getClass(), obj);
}
String rootNamespaceUri = null;
int idx = rootName.indexOf(':');
if (idx != -1) {
rootNamespaceUri = desc.getNamespaceResolver().resolveNamespacePrefix(rootName.substring(0, idx));
rootName = rootName.substring(idx + 1);
}
QName qname;
if (rootNamespaceUri == null) {
qname = new QName(rootName);
} else {
qname = new QName(rootNamespaceUri, rootName);
}
if (declaredClass != null) {
return jaxbContext.createJAXBElement(qname, declaredClass, obj);
} else {
return jaxbContext.createJAXBElement(qname, obj.getClass(), obj);
}
}
use of org.eclipse.persistence.internal.oxm.mappings.Descriptor in project eclipselink by eclipse-ee4j.
the class JAXBIntrospector method getElementName.
@Override
public QName getElementName(Object obj) {
if (!isElement(obj)) {
return null;
}
if (obj instanceof JAXBElement) {
return ((JAXBElement) obj).getName();
}
try {
Descriptor descriptor = (Descriptor) context.getSession(obj).getDescriptor(obj);
XPathFragment rootFragment = descriptor.getDefaultRootElementField().getXPathFragment();
return new QName(rootFragment.getNamespaceURI(), rootFragment.getLocalName());
} catch (XMLMarshalException e) {
return null;
}
}
use of org.eclipse.persistence.internal.oxm.mappings.Descriptor in project eclipselink by eclipse-ee4j.
the class BaseDBWSBuilderHelper method buildSchema.
/**
* Build the schema
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public void buildSchema(NamingConventionTransformer nct) {
Project oxProject = dbwsBuilder.getOxProject();
Schema schema = null;
List<Descriptor> descriptorsToProcess = new ArrayList<Descriptor>();
for (Descriptor desc : (List<Descriptor>) (List) oxProject.getOrderedDescriptors()) {
String alias = desc.getAlias();
if (!DEFAULT_SIMPLE_XML_FORMAT_TAG.equals(alias)) {
descriptorsToProcess.add(desc);
}
}
if (descriptorsToProcess.size() > 0) {
// need a deep-copy clone of oxProject; simplest way is to marshall/unmarshall to a stream
StringWriter sw = new StringWriter();
XMLProjectWriter.write(oxProject, sw);
XRDynamicClassLoader specialLoader = new XRDynamicClassLoader(this.getClass().getClassLoader());
Project oxProjectClone = XMLProjectReader.read(new StringReader(sw.toString()), specialLoader);
ProjectHelper.fixOROXAccessors(oxProjectClone, null);
XMLLogin xmlLogin = new XMLLogin();
DOMPlatform domPlatform = new DOMPlatform();
domPlatform.getConversionManager().setLoader(specialLoader);
xmlLogin.setPlatform(domPlatform);
oxProjectClone.setLogin(xmlLogin);
// initialize reference descriptors
oxProjectClone.createDatabaseSession();
SchemaModelGenerator schemaGenerator = new SchemaModelGenerator(XMLConversionManager.getDefaultXMLManager(), true);
SchemaModelGeneratorProperties sgProperties = new SchemaModelGeneratorProperties();
// set element form default to qualified for target namespace
sgProperties.addProperty(dbwsBuilder.getTargetNamespace(), ELEMENT_FORM_QUALIFIED_KEY, true);
Map<String, Schema> schemaMap = schemaGenerator.generateSchemas(descriptorsToProcess, sgProperties);
Schema s = schemaMap.get(dbwsBuilder.getTargetNamespace());
// check existing schema for any top-level ComplexTypes
if (dbwsBuilder.getSchema() != null && s != null) {
Map<String, ComplexType> topLevelComplexTypes = dbwsBuilder.getSchema().getTopLevelComplexTypes();
for (Map.Entry<String, ComplexType> me : topLevelComplexTypes.entrySet()) {
s.addTopLevelComplexTypes(me.getValue());
}
// add any additional namespaces
NamespaceResolver snr = s.getNamespaceResolver();
NamespaceResolver nr = dbwsBuilder.getSchema().getNamespaceResolver();
// we only want to add prefix/uri pairs for prefixes that don't already exist
for (String prefix : nr.getPrefixesToNamespaces().keySet()) {
if (snr.resolveNamespacePrefix(prefix) == null) {
snr.put(prefix, nr.resolveNamespacePrefix(prefix));
}
}
// switch
schema = s;
schema.setNamespaceResolver(snr);
}
} else {
schema = new Schema();
addSimpleXMLFormat(schema);
schema.setTargetNamespace(dbwsBuilder.getTargetNamespace());
}
dbwsBuilder.setSchema(schema);
}
Aggregations