use of org.eclipse.persistence.internal.oxm.schema.model.Schema in project eclipselink by eclipse-ee4j.
the class PathbasedMappingTestCases method testSchemaGen.
public void testSchemaGen() throws Exception {
Schema generatedSchema = null;
Document tDoc = null;
Document cDoc = null;
boolean exception = false;
String msg = null;
try {
SchemaModelGeneratorProperties props = new SchemaModelGeneratorProperties();
props.addProperty(MYNS, SchemaModelGeneratorProperties.ELEMENT_FORM_QUALIFIED_KEY, false);
XMLContext xCtx = new XMLContext("org.eclipse.persistence.testing.oxm.schemamodelgenerator.mappings.pathbased", Thread.currentThread().getContextClassLoader());
Project prj = xCtx.getSession(0).getProject();
loginProject(prj);
List<Descriptor> descriptorsToProcess = setupDescriptorList(prj);
Map<String, Schema> generatedSchemas = sg.generateSchemas(descriptorsToProcess, props);
generatedSchema = generatedSchemas.get(MYNS);
assertNotNull("No schema was generated for namespace [" + MYNS + "]", generatedSchema);
writeSchema(generatedSchema);
tDoc = getDocument(generatedSchema);
cDoc = getDocument(XSD_RESOURCE);
assertNotNull("Schema to Document conversion failed", tDoc);
assertNotNull("A problem occurred loading the control schema", cDoc);
assertTrue("Schema comparsion failed", comparer.isSchemaEqual(cDoc, tDoc));
SchemaFactory sFact = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
File schemaFile = new File(TMP_DIR, "generatedSchema.xsd");
javax.xml.validation.Schema theSchema = sFact.newSchema(schemaFile);
Validator validator = theSchema.newValidator();
StreamSource ss = new StreamSource(new File(Thread.currentThread().getContextClassLoader().getResource(XML_RESOURCE).toURI()));
validator.validate(ss);
} catch (Exception ex) {
exception = true;
msg = ex.toString();
ex.printStackTrace();
}
assertTrue("Schema validation failed unexpectedly: " + msg, exception == false);
}
use of org.eclipse.persistence.internal.oxm.schema.model.Schema in project eclipselink by eclipse-ee4j.
the class NillableSchemaTestCases method runTest.
public void runTest(boolean nillable) throws Exception {
Schema generatedSchema = null;
Document tDoc = null;
Document cDoc = null;
try {
boolean setSchemaContext = true;
boolean setDefaultRootElement = true;
SchemaModelGeneratorProperties props = new SchemaModelGeneratorProperties();
props.addProperty(MYNS, SchemaModelGeneratorProperties.ELEMENT_FORM_QUALIFIED_KEY, true);
Project prj = new NillableTestProject(nillable);
loginProject(prj);
List<Descriptor> descriptorsToProcess = setupDescriptorList(prj);
Map<String, Schema> generatedSchemas = sg.generateSchemas(descriptorsToProcess, props);
generatedSchema = generatedSchemas.get(MYNS);
// debugging
// writeSchema(generatedSchema);
assertNotNull("No schema was generated for namespace [" + MYNS + "]", generatedSchema);
tDoc = getDocument(generatedSchema);
if (nillable) {
cDoc = getDocument(NILLABLE_RESOURCE);
} else {
cDoc = getDocument(NON_NILLABLE_RESOURCE);
}
} catch (Exception ex) {
ex.printStackTrace();
fail();
}
assertNotNull("Schema to Document conversion failed", tDoc);
assertNotNull("A problem occurred loading the control schema", cDoc);
assertTrue("Schema comparsion failed", comparer.isSchemaEqual(cDoc, tDoc));
}
use of org.eclipse.persistence.internal.oxm.schema.model.Schema in project eclipselink by eclipse-ee4j.
the class RequiredSchemaTestCases method runTest.
public void runTest(boolean required) throws Exception {
Schema generatedSchema = null;
Document tDoc = null;
Document cDoc = null;
try {
boolean setSchemaContext = true;
boolean setDefaultRootElement = true;
SchemaModelGeneratorProperties props = new SchemaModelGeneratorProperties();
props.addProperty(MYNS, SchemaModelGeneratorProperties.ELEMENT_FORM_QUALIFIED_KEY, true);
Project prj = new RequiredTestProject(required);
loginProject(prj);
List<Descriptor> descriptorsToProcess = setupDescriptorList(prj);
Map<String, Schema> generatedSchemas = sg.generateSchemas(descriptorsToProcess, props);
generatedSchema = generatedSchemas.get(MYNS);
// debugging
// writeSchema(generatedSchema);
assertNotNull("No schema was generated for namespace [" + MYNS + "]", generatedSchema);
tDoc = getDocument(generatedSchema);
if (required) {
cDoc = getDocument(REQUIRED_RESOURCE);
} else {
cDoc = getDocument(NON_REQUIRED_RESOURCE);
}
} catch (Exception ex) {
ex.printStackTrace();
fail();
}
assertNotNull("Schema to Document conversion failed", tDoc);
assertNotNull("A problem occurred loading the control schema", cDoc);
assertTrue("Schema comparsion failed", comparer.isSchemaEqual(cDoc, tDoc));
}
use of org.eclipse.persistence.internal.oxm.schema.model.Schema in project eclipselink by eclipse-ee4j.
the class SchemaModelGenerator method generateSchemas.
/**
* Generates a Map of EclipseLink schema model Schema objects for a given list of XMLDescriptors.
* The descriptors are assumed to have been initialized. One Schema object will be generated
* per namespace.
*
* @param descriptorsToProcess list of XMLDescriptors which will be used to generate Schema objects
* @param properties holds a namespace to Properties map containing schema settings, such as elementFormDefault
* @param additionalGlobalElements a map of QName-Type entries identifying additional global elements to be added
* @return a map of namespaces to EclipseLink schema model Schema objects
* @throws DescriptorException if the reference descriptor for a composite mapping is not in the list of descriptors
* @see Schema
*/
public Map<String, Schema> generateSchemas(List<Descriptor> descriptorsToProcess, SchemaModelGeneratorProperties properties, SchemaModelOutputResolver outputResolver, Map<QName, Type> additionalGlobalElements) throws DescriptorException {
Map<String, Schema> schemas = generateSchemas(descriptorsToProcess, properties, additionalGlobalElements);
// write out the generates schema(s) via the given output resolver
Project proj = new SchemaModelProject();
XMLContext context = new XMLContext(proj);
XMLMarshaller marshaller = context.createMarshaller();
Descriptor schemaDescriptor = (Descriptor) proj.getDescriptor(Schema.class);
int schemaCount = 0;
for (Entry<String, Schema> entry : schemas.entrySet()) {
Schema schema = entry.getValue();
try {
NamespaceResolver schemaNamespaces = schema.getNamespaceResolver();
schemaNamespaces.put(Constants.SCHEMA_PREFIX, "http://www.w3.org/2001/XMLSchema");
schemaDescriptor.setNamespaceResolver(schemaNamespaces);
javax.xml.transform.Result target = outputResolver.createOutput(schema.getTargetNamespace(), schema.getName());
marshaller.marshal(schema, target);
schemaCount++;
} catch (IOException ex) {
ex.printStackTrace();
}
}
return schemas;
}
use of org.eclipse.persistence.internal.oxm.schema.model.Schema in project eclipselink by eclipse-ee4j.
the class SchemaModelGenerator method handleFragNamespace.
/**
* This method will generate a global element if required (based in URI and elementFormDefault) and
* set a reference to it on a given element accordingly. This method will typically be used for
* direct mappings.
*/
protected Element handleFragNamespace(XPathFragment frag, HashMap<String, Schema> schemaForNamespace, Schema workingSchema, SchemaModelGeneratorProperties properties, Element element, String schemaTypeString) {
String fragUri = frag.getNamespaceURI();
// may need to add a global element
Schema s = getSchema(fragUri, null, schemaForNamespace, properties);
String targetNS = workingSchema.getTargetNamespace();
if ((s.isElementFormDefault() && !fragUri.equals(targetNS)) || (!s.isElementFormDefault() && fragUri.length() > 0)) {
if (s.getTopLevelElements().get(frag.getLocalName()) == null) {
Element globalElement = new Element();
globalElement.setName(frag.getLocalName());
globalElement.setType(schemaTypeString);
s.addTopLevelElement(globalElement);
}
element = new Element();
element.setRef(frag.getShortName());
} else {
element = buildElement(frag, schemaTypeString, Occurs.ZERO, null);
}
return element;
}
Aggregations