use of org.eclipse.persistence.oxm.XMLDescriptor in project eclipselink by eclipse-ee4j.
the class MappingsGenerator method setupAttributeGroups.
private void setupAttributeGroups(JavaClass javaClass) {
TypeInfo info = this.typeInfo.get(javaClass.getQualifiedName());
XMLDescriptor descriptor = (XMLDescriptor) info.getDescriptor();
if (!info.getObjectGraphs().isEmpty()) {
for (XmlNamedObjectGraph next : info.getObjectGraphs()) {
AttributeGroup group = descriptor.getAttributeGroup(next.getName());
Map<String, List<CoreAttributeGroup>> subgraphs = processSubgraphs(next.getXmlNamedSubgraph());
for (XmlNamedAttributeNode nextAttributeNode : next.getXmlNamedAttributeNode()) {
if (nextAttributeNode.getSubgraph() == null || nextAttributeNode.getSubgraph().length() == 0) {
group.addAttribute(nextAttributeNode.getName());
} else {
List<CoreAttributeGroup> nestedGroups = subgraphs.get(nextAttributeNode.getSubgraph());
if (nestedGroups == null || nestedGroups.isEmpty()) {
Property property = info.getProperties().get(nextAttributeNode.getName());
if (property == null) {
// if there's no property associated with the attributeNode, just ignore it
continue;
}
JavaClass cls = property.getActualType();
TypeInfo referenceType = typeInfo.get(cls.getQualifiedName());
if (referenceType != null) {
AttributeGroup targetGroup = (AttributeGroup) referenceType.getDescriptor().getAttributeGroup(nextAttributeNode.getSubgraph());
group.addAttribute(nextAttributeNode.getName(), targetGroup);
} else {
// TODO: Exception
}
} else {
if (nestedGroups.size() == 1) {
group.addAttribute(nextAttributeNode.getName(), nestedGroups.get(0));
} else {
group.addAttribute(nextAttributeNode.getName(), nestedGroups);
}
}
}
}
for (XmlNamedSubgraph nextSubclass : next.getXmlNamedSubclassGraph()) {
AttributeGroup subclassGroup = new AttributeGroup(next.getName(), nextSubclass.getType(), true);
group.getSubClassGroups().put(nextSubclass.getType(), subclassGroup);
for (XmlNamedAttributeNode nextAttributeNode : nextSubclass.getXmlNamedAttributeNode()) {
if (nextAttributeNode.getSubgraph() == null || nextAttributeNode.getSubgraph().length() == 0) {
subclassGroup.addAttribute(nextAttributeNode.getName());
} else {
List<CoreAttributeGroup> nestedGroups = subgraphs.get(nextAttributeNode.getSubgraph());
if (nestedGroups == null || nestedGroups.isEmpty()) {
Property property = info.getProperties().get(nextAttributeNode.getName());
JavaClass cls = property.getActualType();
TypeInfo referenceType = typeInfo.get(cls.getQualifiedName());
if (referenceType != null) {
AttributeGroup targetGroup = (AttributeGroup) referenceType.getDescriptor().getAttributeGroup(nextAttributeNode.getSubgraph());
subclassGroup.addAttribute(nextAttributeNode.getName(), targetGroup);
} else {
// TODO: Exception
}
} else {
if (nestedGroups.size() == 1) {
subclassGroup.addAttribute(nextAttributeNode.getName(), nestedGroups.get(0));
} else {
subclassGroup.addAttribute(nextAttributeNode.getName(), nestedGroups);
}
}
}
}
}
}
}
}
use of org.eclipse.persistence.oxm.XMLDescriptor 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.oxm.XMLDescriptor in project eclipselink by eclipse-ee4j.
the class MultipleMappingPerFieldTestCases method testMappings.
/**
* We expect two mappings for 'currencyPairCode'. We will verify mapping count,
* user-set properties, etc.
*
* Positive test.
*/
public void testMappings() {
XMLDescriptor xDesc = ((org.eclipse.persistence.jaxb.JAXBContext) jaxbContext).getXMLContext().getDescriptor(new QName("QuoteRequest"));
assertNotNull("No descriptor was generated for CustomQuoteRequest.", xDesc);
int currencyPairCodeCount = 0;
int dateCount = 0;
Vector<DatabaseMapping> mappings = xDesc.getMappings();
for (int i = 0; i < mappings.size(); i++) {
DatabaseMapping mapping = mappings.get(i);
if (mapping.getAttributeName().equals("currencyPairCode")) {
currencyPairCodeCount++;
// check user-set properties
Map props = mapping.getProperties();
assertNotNull("No user-defined properties exist on the mapping for [currencyPairCode]", props);
assertTrue("Expected [2] user-defined properties, but there were [" + props.size() + "]", props.size() == 2);
if (mapping.getField().getName().equals("QuoteReq/Instrmt/@Sym")) {
// verify value-types
assertTrue("Expected value-type [String] for key [1] but was [" + props.get("1").getClass().getName() + "]", props.get("1") instanceof String);
assertTrue("Expected value-type [Integer] for key [2] but was [" + props.get("2").getClass().getName() + "]", props.get("2") instanceof Integer);
// verify values
assertTrue("Expected property value [A] for key [" + 1 + "] but was [" + props.get("1") + "]", "A".equals(props.get("1")));
assertTrue("Expected property value [66] for key [" + 2 + "] but was [" + props.get("2") + "]", 66 == (Integer) props.get("2"));
} else {
// assume "QuoteReq/Leg/@Sym"
// verify value-types
assertTrue("Expected value-type [String] for key [2] but was [" + props.get("1").getClass().getName() + "]", props.get("1") instanceof String);
assertTrue("Expected value-type [Double] for key [1] but was [" + props.get("2").getClass().getName() + "]", props.get("2") instanceof Double);
// verify values
assertTrue("Expected property value [B] for key [" + 1 + "] but was [" + props.get("1") + "]", "B".equals(props.get("1")));
assertTrue("Expected property value [9.9] for key [" + 2 + "] but was [" + props.get("2") + "]", 9.9 == (Double) props.get("2"));
}
} else if (mapping.getAttributeName().equals("date")) {
dateCount++;
}
}
assertTrue("Expected [2] mappings for attribute [currencyPairCode], but was [" + currencyPairCodeCount + "]", currencyPairCodeCount == 2);
assertTrue("Expected [3] mappings for attribute [date], but was [" + dateCount + "]", dateCount == 3);
}
use of org.eclipse.persistence.oxm.XMLDescriptor in project eclipselink by eclipse-ee4j.
the class XmlElementTestCases method testContainerType.
/*
public void testTeamWithListOfPlayersSchemaGen() {
String metadataFile = PATH + "team-oxm.xml";
InputStream iStream = loader.getResourceAsStream(metadataFile);
if (iStream == null) {
fail("Couldn't load metadata file [" + metadataFile + "]");
}
HashMap<String, Source> metadataSourceMap = new HashMap<String, Source>();
metadataSourceMap.put(CONTEXT_PATH, new StreamSource(iStream));
Map<String, Map<String, Source>> properties = new HashMap<String, Map<String, Source>>();
properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, metadataSourceMap);
try {
JAXBContext jContext = (JAXBContext) JAXBContextFactory.createContext(new Class[] { Team.class }, properties, loader);
MySchemaOutputResolver oResolver = new MySchemaOutputResolver();
jContext.generateSchema(oResolver);
// validate schema
String controlSchema = PATH + "team-schema.xsd";
compareSchemas(oResolver.schemaFiles.get(EMPTY_NAMESPACE), new File(controlSchema));
} catch (JAXBException e) {
fail("An exception occurred creating the JAXBContext: " + e.getMessage());
}
}
*/
/**
* Test setting the container class via container-type attribute.
*
* Positive test.
*/
public void testContainerType() {
XMLDescriptor xDesc = ((JAXBContext) jaxbContext).getXMLContext().getDescriptor(new QName("employee"));
assertNotNull("No descriptor was generated for Employee.", xDesc);
DatabaseMapping mapping = xDesc.getMappingForAttributeName("myEmployees");
assertNotNull("No mapping exists on Employee for attribute [myEmployees].", mapping);
assertTrue("Expected an XMLCompositeCollectionMapping for attribute [myEmployees], but was [" + mapping.toString() + "].", mapping instanceof XMLCompositeCollectionMapping);
assertTrue("Expected container class [java.util.LinkedList] but was [" + mapping.getContainerPolicy().getContainerClassName() + "]", mapping.getContainerPolicy().getContainerClassName().equals("java.util.LinkedList"));
}
use of org.eclipse.persistence.oxm.XMLDescriptor in project eclipselink by eclipse-ee4j.
the class XmlJoinNodeTestCases method testContainerType.
public void testContainerType() {
JAXBContext jCtx = null;
try {
InputStream inputStream = ClassLoader.getSystemResourceAsStream(OXM_DOC_V2);
HashMap<String, Source> metadataSourceMap = new HashMap<>();
metadataSourceMap.put("org.eclipse.persistence.testing.jaxb.externalizedmetadata.xmljoinnode", new StreamSource(inputStream));
Map<String, Object> invalidProperties = new HashMap<>();
invalidProperties.put(JAXBContextProperties.OXM_METADATA_SOURCE, metadataSourceMap);
jCtx = (JAXBContext) JAXBContextFactory.createContext(new Class<?>[] { Company.class }, invalidProperties);
} catch (JAXBException e) {
e.printStackTrace();
fail("An exception occurred while creating the JAXBContext.");
}
XMLDescriptor xDesc = jCtx.getXMLContext().getDescriptor(new QName("company"));
assertNotNull("No descriptor was generated for Company.", xDesc);
DatabaseMapping mapping = xDesc.getMappingForAttributeName("employees");
assertNotNull("No mapping exists on Customer for attribute [employees].", mapping);
assertTrue("Expected an XMLCollectionReferenceMapping for attribute [employees], but was [" + mapping.toString() + "].", mapping instanceof XMLCollectionReferenceMapping);
assertTrue("Expected container class [java.util.LinkedList] but was [" + mapping.getContainerPolicy().getContainerClassName() + "]", mapping.getContainerPolicy().getContainerClassName().equals("java.util.LinkedList"));
}
Aggregations