You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by rd...@apache.org on 2004/02/04 23:57:41 UTC
cvs commit: jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/schema TestSchemaGeneration.java TestSchemaTranscriber.java TestSchemaValidity.java
rdonkin 2004/02/04 14:57:41
Modified: betwixt/src/java/org/apache/commons/betwixt/schema Tag:
REFACTORING-BRANCH_2004-01-13 ComplexType.java
Element.java Schema.betwixt Schema.java
SchemaTranscriber.java
betwixt/src/test/org/apache/commons/betwixt/schema Tag:
REFACTORING-BRANCH_2004-01-13
TestSchemaGeneration.java
TestSchemaTranscriber.java TestSchemaValidity.java
Log:
Added support for generating schemas from complex property types
Revision Changes Path
No revision
No revision
1.1.2.5 +19 -8 jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/Attic/ComplexType.java
Index: ComplexType.java
===================================================================
RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/Attic/ComplexType.java,v
retrieving revision 1.1.2.4
retrieving revision 1.1.2.5
diff -u -r1.1.2.4 -r1.1.2.5
--- ComplexType.java 2 Feb 2004 22:21:44 -0000 1.1.2.4
+++ ComplexType.java 4 Feb 2004 22:57:41 -0000 1.1.2.5
@@ -61,12 +61,14 @@
package org.apache.commons.betwixt.schema;
+import java.beans.IntrospectionException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.betwixt.AttributeDescriptor;
import org.apache.commons.betwixt.ElementDescriptor;
+import org.apache.commons.betwixt.XMLBeanInfo;
import org.apache.commons.collections.CollectionUtils;
/**
@@ -90,7 +92,16 @@
* Constructs a new ComplexType from the descriptor given.
* @param elementDescriptor
*/
- public ComplexType(ElementDescriptor elementDescriptor) {
+ public ComplexType(ElementDescriptor elementDescriptor, Schema schema) throws IntrospectionException {
+ if (elementDescriptor.isHollow()) {
+ // need to introspector for filled descriptor
+ XMLBeanInfo filledBeanInfo = schema.introspect(elementDescriptor.getPropertyType());
+ elementDescriptor = filledBeanInfo.getElementDescriptor();
+ }
+ init(elementDescriptor, schema);
+ }
+
+ private void init(ElementDescriptor elementDescriptor, Schema schema) throws IntrospectionException {
setName(elementDescriptor.getPropertyType().getName());
AttributeDescriptor[] attributeDescriptors = elementDescriptor.getAttributeDescriptors();
for (int i=0,length=attributeDescriptors.length; i<length ; i++) {
@@ -102,11 +113,11 @@
}
}
+ //TODO: add support for spacing elements
ElementDescriptor[] elementDescriptors = elementDescriptor.getElementDescriptors();
- //TODO: add support for referenced complex classes
for (int i=0,length=elementDescriptors.length; i<length ; i++) {
- elements.add(new Element(elementDescriptors[i]));
- }
+ elements.add(new Element(elementDescriptors[i], schema));
+ }
}
/**
1.1.2.3 +17 -7 jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/Attic/Element.java
Index: Element.java
===================================================================
RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/Attic/Element.java,v
retrieving revision 1.1.2.2
retrieving revision 1.1.2.3
diff -u -r1.1.2.2 -r1.1.2.3
--- Element.java 31 Jan 2004 15:38:09 -0000 1.1.2.2
+++ Element.java 4 Feb 2004 22:57:41 -0000 1.1.2.3
@@ -61,6 +61,8 @@
package org.apache.commons.betwixt.schema;
+import java.beans.IntrospectionException;
+
import org.apache.commons.betwixt.ElementDescriptor;
/**
@@ -84,11 +86,18 @@
public Element(String name, ComplexType complexType) {
setName(name);
- setType(type);
+ setComplexType(complexType);
}
- public Element(ElementDescriptor elementDescriptor) {
- this(elementDescriptor.getLocalName(), "xsd:string");
+ public Element(ElementDescriptor elementDescriptor, Schema schema) throws IntrospectionException {
+ setName(elementDescriptor.getLocalName());
+ if (elementDescriptor.isHollow()) {
+ setComplexType( new ComplexType(elementDescriptor, schema));
+ schema.addComplexType(getComplexType());
+ } else {
+
+ setType("xsd:string");
+ }
}
@@ -139,6 +148,7 @@
* null if the type is to be referenced
*/
public void setComplexType(ComplexType type) {
+ this.type = type.getName();
complexType = type;
}
1.1.2.2 +0 -1 jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/Attic/Schema.betwixt
Index: Schema.betwixt
===================================================================
RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/Attic/Schema.betwixt,v
retrieving revision 1.1.2.1
retrieving revision 1.1.2.2
diff -u -r1.1.2.1 -r1.1.2.2
--- Schema.betwixt 2 Feb 2004 22:25:45 -0000 1.1.2.1
+++ Schema.betwixt 4 Feb 2004 22:57:41 -0000 1.1.2.2
@@ -13,6 +13,5 @@
name='simpleType'
property='simpleTypes'
uri='http://www.w3.org/2001/XMLSchema'/>
- <addDefaults/>
</element>
</info>
1.1.2.4 +30 -8 jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/Attic/Schema.java
Index: Schema.java
===================================================================
RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/Attic/Schema.java,v
retrieving revision 1.1.2.3
retrieving revision 1.1.2.4
diff -u -r1.1.2.3 -r1.1.2.4
--- Schema.java 1 Feb 2004 22:55:48 -0000 1.1.2.3
+++ Schema.java 4 Feb 2004 22:57:41 -0000 1.1.2.4
@@ -61,11 +61,14 @@
package org.apache.commons.betwixt.schema;
+import java.beans.IntrospectionException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.betwixt.ElementDescriptor;
+import org.apache.commons.betwixt.XMLBeanInfo;
+import org.apache.commons.betwixt.XMLIntrospector;
import org.apache.commons.collections.CollectionUtils;
/**
@@ -78,9 +81,28 @@
private List elements = new ArrayList();
private List complexTypes = new ArrayList();
- private List simpleTypes = new ArrayList();
-
+ private List simpleTypes = new ArrayList();
+
+ private XMLIntrospector introspector;
+ public Schema() {
+ this(new XMLIntrospector());
+ }
+
+ public Schema(XMLIntrospector introspector) {
+ this.introspector = introspector;
+ }
+
+ /**
+ * Introspects the given type giving an <code>XMLBeanInfo</code>.
+ * @param type Class to introspect, not null
+ * @return <code>XMLBeanInfo</code>, not null
+ * @throws IntrospectionException
+ */
+ public XMLBeanInfo introspect(Class type) throws IntrospectionException {
+ return introspector.introspect(type);
+ }
+
/**
* Gets the complex types defined
* @return list of <code>ComplexType</code>'s not null
@@ -136,7 +158,7 @@
* Adds global (top level) element and type declarations matching the given descriptor.
* @param elementDescriptor ElementDescriptor not null
*/
- public void addGlobalElementType(ElementDescriptor elementDescriptor) {
+ public void addGlobalElementType(ElementDescriptor elementDescriptor) throws IntrospectionException {
// need to create a global element declaration and a complex type
// use the fully qualified class name as the type name
Element element = new Element(
@@ -144,7 +166,7 @@
elementDescriptor.getPropertyType().getName());
addElement(element);
- ComplexType type = new ComplexType(elementDescriptor);
+ ComplexType type = new ComplexType(elementDescriptor, this);
addComplexType(type);
}
1.1.2.3 +6 -6 jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/Attic/SchemaTranscriber.java
Index: SchemaTranscriber.java
===================================================================
RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/Attic/SchemaTranscriber.java,v
retrieving revision 1.1.2.2
retrieving revision 1.1.2.3
diff -u -r1.1.2.2 -r1.1.2.3
--- SchemaTranscriber.java 2 Feb 2004 22:21:44 -0000 1.1.2.2
+++ SchemaTranscriber.java 4 Feb 2004 22:57:41 -0000 1.1.2.3
@@ -126,9 +126,9 @@
* @param xmlBeanInfo not null
* @return Schema model, not null
*/
- public Schema generate(XMLBeanInfo xmlBeanInfo) {
+ public Schema generate(XMLBeanInfo xmlBeanInfo) throws IntrospectionException {
ElementDescriptor elementDescriptor = xmlBeanInfo.getElementDescriptor();
- Schema schema = new Schema();
+ Schema schema = new Schema(introspector);
schema.addGlobalElementType(elementDescriptor);
return schema;
}
No revision
No revision
1.1.2.5 +41 -4 jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/schema/Attic/TestSchemaGeneration.java
Index: TestSchemaGeneration.java
===================================================================
RCS file: /home/cvs/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/schema/Attic/TestSchemaGeneration.java,v
retrieving revision 1.1.2.4
retrieving revision 1.1.2.5
diff -u -r1.1.2.4 -r1.1.2.5
--- TestSchemaGeneration.java 3 Feb 2004 20:44:04 -0000 1.1.2.4
+++ TestSchemaGeneration.java 4 Feb 2004 22:57:41 -0000 1.1.2.5
@@ -65,6 +65,7 @@
import org.apache.commons.betwixt.AbstractTestCase;
import org.apache.commons.betwixt.io.BeanWriter;
+import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
/**
* Tests for the generation of schema from the object models.
@@ -155,5 +156,41 @@
"</xsd:schema>";
xmlAssertIsomorphicContent(parseString(expected), parseString(xsd));
+ }
+
+
+ public void testOrderLineBean() throws Exception {
+ SchemaTranscriber transcriber = new SchemaTranscriber();
+ transcriber.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
+ transcriber.getXMLIntrospector().getConfiguration().setAttributeNameMapper(new HyphenatedNameMapper());
+ Schema schema = transcriber.generate(OrderLineBean.class);
+
+ StringWriter out = new StringWriter();
+ out.write("<?xml version='1.0'?>");
+ BeanWriter writer = new BeanWriter(out);
+ writer.setBindingConfiguration(transcriber.createSchemaBindingConfiguration());
+ writer.getXMLIntrospector().setConfiguration(transcriber.createSchemaIntrospectionConfiguration());
+ writer.write(schema);
+
+ String xsd = out.getBuffer().toString();
+
+ String expected ="<?xml version='1.0'?><xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
+ "<xsd:element name='OrderLineBean' type='org.apache.commons.betwixt.schema.OrderLineBean'/>" +
+ "<xsd:complexType name='org.apache.commons.betwixt.schema.ProductBean'>" +
+ "<xsd:sequence/>" +
+ "<xsd:attribute name='barcode' type='xsd:string'/>" +
+ "<xsd:attribute name='code' type='xsd:string'/>" +
+ "<xsd:attribute name='display-name' type='xsd:string'/>" +
+ "<xsd:attribute name='name' type='xsd:string'/>" +
+ "</xsd:complexType>" +
+ "<xsd:complexType name='org.apache.commons.betwixt.schema.OrderLineBean'>" +
+ "<xsd:sequence>" +
+ "<xsd:element name='product' type='org.apache.commons.betwixt.schema.ProductBean'/>" +
+ "</xsd:sequence>" +
+ "<xsd:attribute name='quantity' type='xsd:string'/>" +
+ "</xsd:complexType>" +
+ "</xsd:schema>";
+
+ xmlAssertIsomorphicContent(parseString(expected), parseString(xsd), true);
}
}
1.1.2.4 +32 -4 jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/schema/Attic/TestSchemaTranscriber.java
Index: TestSchemaTranscriber.java
===================================================================
RCS file: /home/cvs/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/schema/Attic/TestSchemaTranscriber.java,v
retrieving revision 1.1.2.3
retrieving revision 1.1.2.4
diff -u -r1.1.2.3 -r1.1.2.4
--- TestSchemaTranscriber.java 2 Feb 2004 22:21:44 -0000 1.1.2.3
+++ TestSchemaTranscriber.java 4 Feb 2004 22:57:41 -0000 1.1.2.4
@@ -62,6 +62,7 @@
package org.apache.commons.betwixt.schema;
import org.apache.commons.betwixt.AbstractTestCase;
+import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
/**
* Tests for the SchemaTranscriber.
@@ -129,4 +130,31 @@
assertEquals("Simple bean schema", expected, out);
}
+
+ public void testOrderLine() throws Exception {
+ SchemaTranscriber transcriber = new SchemaTranscriber();
+ transcriber.getXMLIntrospector().getConfiguration().setAttributeNameMapper(new HyphenatedNameMapper());
+ transcriber.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
+ Schema out = transcriber.generate(OrderLineBean.class);
+
+ Schema expected = new Schema();
+
+ ComplexType productBeanType = new ComplexType();
+ productBeanType.setName(ProductBean.class.getName());
+ productBeanType.addAttribute(new Attribute("barcode", "xsd:string"));
+ productBeanType.addAttribute(new Attribute("code", "xsd:string"));
+ productBeanType.addAttribute(new Attribute("name", "xsd:string"));
+ productBeanType.addAttribute(new Attribute("display-name", "xsd:string"));
+ expected.addComplexType(productBeanType);
+
+ ComplexType orderLineType = new ComplexType();
+ orderLineType.setName(OrderLineBean.class.getName());
+ orderLineType.addAttribute(new Attribute("quantity", "xsd:string"));
+ orderLineType.addElement(new Element("product", productBeanType));
+ expected.addComplexType(orderLineType);
+ expected.addElement(new Element("OrderLineBean", OrderLineBean.class.getName()));
+
+ assertEquals("Transcriber schema", expected, out);
+
+ }
}
1.1.2.5 +42 -5 jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/schema/Attic/TestSchemaValidity.java
Index: TestSchemaValidity.java
===================================================================
RCS file: /home/cvs/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/schema/Attic/TestSchemaValidity.java,v
retrieving revision 1.1.2.4
retrieving revision 1.1.2.5
diff -u -r1.1.2.4 -r1.1.2.5
--- TestSchemaValidity.java 3 Feb 2004 20:44:04 -0000 1.1.2.4
+++ TestSchemaValidity.java 4 Feb 2004 22:57:41 -0000 1.1.2.5
@@ -66,6 +66,7 @@
import org.apache.commons.betwixt.AbstractTestCase;
import org.apache.commons.betwixt.io.BeanWriter;
+import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
import org.xml.sax.InputSource;
/**
@@ -79,6 +80,7 @@
super(name);
}
+
private String generateSchema(Class clazz) throws Exception {
SchemaTranscriber transcriber = new SchemaTranscriber();
transcriber.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
@@ -146,5 +148,40 @@
String xml = out.getBuffer().toString();
xmlAssertIsValid(new InputSource(new StringReader(xml)), new InputSource(new StringReader(xsd)));
- }
+ }
+
+ private String generateOrderLineSchema() throws Exception {
+ SchemaTranscriber transcriber = new SchemaTranscriber();
+ transcriber.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
+ transcriber.getXMLIntrospector().getConfiguration().setAttributeNameMapper(new HyphenatedNameMapper());
+ Schema schema = transcriber.generate(OrderLineBean.class);
+
+ StringWriter out = new StringWriter();
+ out.write("<?xml version='1.0'?>");
+ BeanWriter writer = new BeanWriter(out);
+ writer.setBindingConfiguration(transcriber.createSchemaBindingConfiguration());
+ writer.getXMLIntrospector().setConfiguration(transcriber.createSchemaIntrospectionConfiguration());
+ writer.write(schema);
+
+ String xsd = out.getBuffer().toString();
+ return xsd;
+ }
+
+ public void testOrderLine() throws Exception {
+
+ String xsd = generateOrderLineSchema();
+ StringWriter out = new StringWriter();
+ out.write("<?xml version='1.0'?>");
+ BeanWriter writer = new BeanWriter(out);
+ writer.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
+ writer.getXMLIntrospector().getConfiguration().setAttributeNameMapper(new HyphenatedNameMapper());
+ writer.getXMLIntrospector().getConfiguration().getPrefixMapper().setPrefix(SchemaTranscriber.W3C_SCHEMA_INSTANCE_URI, "xsi");
+ writer.getBindingConfiguration().setMapIDs(false);
+ OrderLineBean bean = new OrderLineBean(3, new ProductBean("00112234", "A11", "Fat Fish", "A Fat Fish"));
+ writer.write(bean);
+
+ String xml = out.getBuffer().toString();
+
+ xmlAssertIsValid(new InputSource(new StringReader(xml)), new InputSource(new StringReader(xsd)));
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org