rdonkin 2004/02/23 13:41:13 Modified: betwixt/src/java/org/apache/commons/betwixt/schema Tag: REFACTORING-BRANCH_2004-01-13 ComplexLocalElement.java ComplexType.java Element.java ElementReference.java GlobalComplexType.java LocalComplexType.java LocalElement.java Schema.java SchemaTranscriber.java SimpleLocalElement.java betwixt/src/test/org/apache/commons/betwixt/schema Tag: REFACTORING-BRANCH_2004-01-13 TestSchemaTranscriber.java Removed: betwixt/src/java/org/apache/commons/betwixt/schema Tag: REFACTORING-BRANCH_2004-01-13 Element.betwixt Log: Added support for customizable schema type mappings. Revision Changes Path No revision No revision 1.1.2.2 +6 -6 jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/Attic/ComplexLocalElement.java Index: ComplexLocalElement.java =================================================================== RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/Attic/ComplexLocalElement.java,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -r1.1.2.1 -r1.1.2.2 --- ComplexLocalElement.java 8 Feb 2004 12:12:18 -0000 1.1.2.1 +++ ComplexLocalElement.java 23 Feb 2004 21:41:13 -0000 1.1.2.2 @@ -72,9 +72,9 @@ public class ComplexLocalElement extends LocalElement { private LocalComplexType type; - public ComplexLocalElement(ElementDescriptor descriptor, Schema schema) throws IntrospectionException { + public ComplexLocalElement(TranscriptionConfiguration configuration, ElementDescriptor descriptor, Schema schema) throws IntrospectionException { super(descriptor, schema); - setType(new LocalComplexType(descriptor, schema)); + setType(new LocalComplexType(configuration, descriptor, schema)); } public LocalComplexType getType() { 1.1.2.8 +10 -10 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.7 retrieving revision 1.1.2.8 diff -u -r1.1.2.7 -r1.1.2.8 --- ComplexType.java 8 Feb 2004 12:11:17 -0000 1.1.2.7 +++ ComplexType.java 23 Feb 2004 21:41:13 -0000 1.1.2.8 @@ -81,7 +81,7 @@ public ComplexType() {} - public ComplexType(ElementDescriptor elementDescriptor, Schema schema) throws IntrospectionException { + public ComplexType(TranscriptionConfiguration configuration, ElementDescriptor elementDescriptor, Schema schema) throws IntrospectionException { if (elementDescriptor.isHollow()) { // need to introspector for filled descriptor Class type = elementDescriptor.getSingularPropertyType(); @@ -91,10 +91,10 @@ XMLBeanInfo filledBeanInfo = schema.introspect(type); elementDescriptor = filledBeanInfo.getElementDescriptor(); } - init(elementDescriptor, schema); + init(configuration, elementDescriptor, schema); } - protected void init(ElementDescriptor elementDescriptor, Schema schema) throws IntrospectionException { + protected void init(TranscriptionConfiguration configuration, ElementDescriptor elementDescriptor, Schema schema) throws IntrospectionException { AttributeDescriptor[] attributeDescriptors = elementDescriptor.getAttributeDescriptors(); for (int i=0,length=attributeDescriptors.length; i<length ; i++) { @@ -110,11 +110,11 @@ ElementDescriptor[] elementDescriptors = elementDescriptor.getElementDescriptors(); for (int i=0,length=elementDescriptors.length; i<length ; i++) { if (elementDescriptors[i].isHollow()) { - elements.add(new ElementReference(elementDescriptors[i], schema)); + elements.add(new ElementReference(configuration, elementDescriptors[i], schema)); } else if (elementDescriptors[i].isSimple()){ - elements.add(new SimpleLocalElement(elementDescriptors[i], schema)); + elements.add(new SimpleLocalElement(configuration, elementDescriptors[i], schema)); } else { - elements.add(new ComplexLocalElement(elementDescriptors[i], schema)); + elements.add(new ComplexLocalElement(configuration, elementDescriptors[i], schema)); } } } 1.1.2.6 +6 -131 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.5 retrieving revision 1.1.2.6 diff -u -r1.1.2.5 -r1.1.2.6 --- Element.java 8 Feb 2004 12:11:17 -0000 1.1.2.5 +++ Element.java 23 Feb 2004 21:41:13 -0000 1.1.2.6 @@ -61,135 +61,10 @@ package org.apache.commons.betwixt.schema; - - /** - * Models the Element tag in the XML schema. * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a> * @version $Revision$ */ -public class Element { - //TODO: going to ignore the issue of namespacing for the moment - public static final String STRING_SIMPLE_TYPE="xsd:string"; - - private String name; - private String type; - - private GlobalComplexType complexType; - - public Element() {} - - public Element(String name, String type) { - setName(name); - setType(type); - } - - public Element(String name, GlobalComplexType complexType) { - setName(name); - setComplexType(complexType); - } - - - - - /** - * Gets the element name - * @return element name, not null - */ - public String getName() { - return name; - } - - /** - * Sets the element name - * @param string not null - */ - public void setName(String string) { - name = string; - } - - /** - * Gets the element type - * @return - */ - public String getType() { - return type; - } - - /** - * Sets the element type - * @param string - */ - public void setType(String string) { - type = string; - } - - - /** - * Gets the anonymous type definition for this element, if one exists. - * @return ComplexType, null if there is no associated anonymous type definition - */ - public GlobalComplexType getComplexType() { - return complexType; - } - - /** - * Sets the anonymous type definition for this element - * @param type ComplexType to be set as the anonymous type definition, - * null if the type is to be referenced - */ - public void setComplexType(GlobalComplexType type) { - this.type = type.getName(); - complexType = type; - } - - public boolean equals(Object obj) { - boolean result = false; - if (obj instanceof Element) { - Element element = (Element) obj; - result = isEqual(type, element.type) && - isEqual(name, element.name); - } - return result; - } - - public int hashCode() { - return 0; - } - - /** - * Null safe equals method - * @param one - * @param two - * @return - */ - private boolean isEqual(String one, String two) { - boolean result = false; - if (one == null) { - result = (two == null); - } - else - { - result = one.equals(two); - } - - return result; - } - - public String toString() { - StringBuffer buffer = new StringBuffer(); - buffer.append("<xsd:element name='"); - buffer.append(name); - buffer.append("' type='"); - buffer.append(type); - buffer.append("'>"); - - if (complexType != null) { - buffer.append(complexType); - } - buffer.append("</xsd:element>"); - return buffer.toString(); - } - - +public interface Element { + public String getName(); } 1.1.2.2 +5 -5 jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/Attic/ElementReference.java Index: ElementReference.java =================================================================== RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/Attic/ElementReference.java,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -r1.1.2.1 -r1.1.2.2 --- ElementReference.java 8 Feb 2004 12:12:50 -0000 1.1.2.1 +++ ElementReference.java 23 Feb 2004 21:41:13 -0000 1.1.2.2 @@ -69,7 +69,7 @@ * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a> * @version $Revision$ */ -public class ElementReference extends Element { +public class ElementReference extends GlobalElement { protected String maxOccurs = "1"; @@ -84,10 +84,10 @@ super(name, type); } - public ElementReference(ElementDescriptor elementDescriptor, Schema schema) throws IntrospectionException { + public ElementReference(TranscriptionConfiguration configuration, ElementDescriptor elementDescriptor, Schema schema) throws IntrospectionException { setName(elementDescriptor.getLocalName()); if (elementDescriptor.isHollow()) { - setComplexType( new GlobalComplexType(elementDescriptor, schema)); + setComplexType( new GlobalComplexType(configuration, elementDescriptor, schema)); schema.addComplexType(getComplexType()); if (elementDescriptor.isCollective()) { maxOccurs = "unbounded"; 1.1.2.2 +8 -8 jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/Attic/GlobalComplexType.java Index: GlobalComplexType.java =================================================================== RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/Attic/GlobalComplexType.java,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -r1.1.2.1 -r1.1.2.2 --- GlobalComplexType.java 8 Feb 2004 12:13:41 -0000 1.1.2.1 +++ GlobalComplexType.java 23 Feb 2004 21:41:13 -0000 1.1.2.2 @@ -83,13 +83,13 @@ * Constructs a new ComplexType from the descriptor given. * @param elementDescriptor */ - public GlobalComplexType(ElementDescriptor elementDescriptor, Schema schema) throws IntrospectionException { - super(elementDescriptor, schema); + public GlobalComplexType(TranscriptionConfiguration configuration, ElementDescriptor elementDescriptor, Schema schema) throws IntrospectionException { + super(configuration, elementDescriptor, schema); } - protected void init(ElementDescriptor elementDescriptor, Schema schema) throws IntrospectionException { + protected void init(TranscriptionConfiguration configuration, ElementDescriptor elementDescriptor, Schema schema) throws IntrospectionException { setName(elementDescriptor.getPropertyType().getName()); - super.init(elementDescriptor, schema); + super.init(configuration, elementDescriptor, schema); } /** 1.1.2.2 +6 -6 jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/Attic/LocalComplexType.java Index: LocalComplexType.java =================================================================== RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/Attic/LocalComplexType.java,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -r1.1.2.1 -r1.1.2.2 --- LocalComplexType.java 8 Feb 2004 12:14:13 -0000 1.1.2.1 +++ LocalComplexType.java 23 Feb 2004 21:41:13 -0000 1.1.2.2 @@ -76,8 +76,8 @@ public LocalComplexType() {} - public LocalComplexType(ElementDescriptor elementDescriptor, Schema schema) throws IntrospectionException { - super(elementDescriptor, schema); + public LocalComplexType(TranscriptionConfiguration configuration, ElementDescriptor elementDescriptor, Schema schema) throws IntrospectionException { + super(configuration, elementDescriptor, schema); } public boolean equals(Object obj) { 1.1.2.3 +5 -5 jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/Attic/LocalElement.java Index: LocalElement.java =================================================================== RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/Attic/LocalElement.java,v retrieving revision 1.1.2.2 retrieving revision 1.1.2.3 diff -u -r1.1.2.2 -r1.1.2.3 --- LocalElement.java 8 Feb 2004 12:11:17 -0000 1.1.2.2 +++ LocalElement.java 23 Feb 2004 21:41:13 -0000 1.1.2.3 @@ -69,7 +69,7 @@ * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a> * @version $Revision$ */ -public class LocalElement { +public class LocalElement implements Element { protected String name; 1.1.2.7 +8 -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.6 retrieving revision 1.1.2.7 diff -u -r1.1.2.6 -r1.1.2.7 --- Schema.java 8 Feb 2004 12:11:17 -0000 1.1.2.6 +++ Schema.java 23 Feb 2004 21:41:13 -0000 1.1.2.7 @@ -133,7 +133,7 @@ * Adds a new element to those defined. * @param element not null */ - public void addElement(Element element) { + public void addElement(GlobalElement element) { elements.add(element); } @@ -158,15 +158,15 @@ * Adds global (top level) element and type declarations matching the given descriptor. * @param elementDescriptor ElementDescriptor not null */ - public void addGlobalElementType(ElementDescriptor elementDescriptor) throws IntrospectionException { + public void addGlobalElementType(TranscriptionConfiguration configuration, 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( + GlobalElement element = new GlobalElement( elementDescriptor.getLocalName(), elementDescriptor.getPropertyType().getName()); addElement(element); - GlobalComplexType type = new GlobalComplexType(elementDescriptor, this); + GlobalComplexType type = new GlobalComplexType(configuration, elementDescriptor, this); addComplexType(type); } 1.1.2.4 +22 -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.3 retrieving revision 1.1.2.4 diff -u -r1.1.2.3 -r1.1.2.4 --- SchemaTranscriber.java 4 Feb 2004 22:57:41 -0000 1.1.2.3 +++ SchemaTranscriber.java 23 Feb 2004 21:41:13 -0000 1.1.2.4 @@ -77,7 +77,6 @@ * and Betwixt can be used to output this to xml. * This should allow both SAX and text. * </p> - * TODO: it's very likely that strategies will be needed to allow flexibility in mapping later * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a> * @version $Revision$ */ @@ -88,9 +87,26 @@ /** Used to introspect beans in order to generate XML */ private XMLIntrospector introspector = new XMLIntrospector(); + private TranscriptionConfiguration configuration = new TranscriptionConfiguration(); public SchemaTranscriber() {} + /** + * Gets the configuration for the XMLBeanInfo to XML schema transcription. + * @return TranscriptionConfiguration, not null + */ + public TranscriptionConfiguration getConfiguration() { + return configuration; + } + + /** + * Sets the configuration for the XMLBeanInfo to XML schema transcription. + * @param configuration TranscriptionConfiguration, not null + */ + public void setConfiguration(TranscriptionConfiguration configuration) { + this.configuration = configuration; + } + /** * Gets the XMLIntrospector used to create XMLInfoBean's. * @return XMLIntrospector used to create XMLInfoBean's used to generate schema, not null @@ -129,7 +145,7 @@ public Schema generate(XMLBeanInfo xmlBeanInfo) throws IntrospectionException { ElementDescriptor elementDescriptor = xmlBeanInfo.getElementDescriptor(); Schema schema = new Schema(introspector); - schema.addGlobalElementType(elementDescriptor); + schema.addGlobalElementType(configuration, elementDescriptor); return schema; } 1.1.2.2 +21 -7 jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/Attic/SimpleLocalElement.java Index: SimpleLocalElement.java =================================================================== RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/Attic/SimpleLocalElement.java,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -r1.1.2.1 -r1.1.2.2 --- SimpleLocalElement.java 8 Feb 2004 12:11:40 -0000 1.1.2.1 +++ SimpleLocalElement.java 23 Feb 2004 21:41:13 -0000 1.1.2.2 @@ -78,9 +78,13 @@ setType(type); } - public SimpleLocalElement(ElementDescriptor descriptor, Schema schema) throws IntrospectionException { + public SimpleLocalElement( + TranscriptionConfiguration configuration, + ElementDescriptor descriptor, + Schema schema) + throws IntrospectionException { super(descriptor, schema); - setType("xsd:string"); + setType(configuration.getDataTypeMapper().toXMLSchemaDataType(descriptor.getPropertyType())); } public String getType() { @@ -112,5 +116,15 @@ } return one.equals(two); - } + } + + public String toString() { + StringBuffer buffer = new StringBuffer(); + buffer.append("<element name='"); + buffer.append(getName()); + buffer.append("' type='"); + buffer.append(getType()); + buffer.append("'/>"); + return buffer.toString(); + } } No revision No revision 1.1.2.7 +10 -78 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.6 retrieving revision 1.1.2.7 diff -u -r1.1.2.6 -r1.1.2.7 --- TestSchemaTranscriber.java 8 Feb 2004 12:11:17 -0000 1.1.2.6 +++ TestSchemaTranscriber.java 23 Feb 2004 21:41:13 -0000 1.1.2.7 @@ -61,8 +61,6 @@ package org.apache.commons.betwixt.schema; -import java.util.Iterator; - import org.apache.commons.betwixt.AbstractTestCase; import org.apache.commons.betwixt.strategy.HyphenatedNameMapper; @@ -86,7 +84,7 @@ simplestBeanType.setName("org.apache.commons.betwixt.schema.SimplestBean"); simplestBeanType.addAttribute(new Attribute("name", "xsd:string")); - Element root = new Element("SimplestBean", "org.apache.commons.betwixt.schema.SimplestBean"); + GlobalElement root = new GlobalElement("SimplestBean", "org.apache.commons.betwixt.schema.SimplestBean"); expected.addComplexType(simplestBeanType); expected.addElement(root); @@ -104,7 +102,7 @@ simplestBeanType.setName("org.apache.commons.betwixt.schema.SimplestElementBean"); simplestBeanType.addElement(new SimpleLocalElement("name", "xsd:string")); - Element root = new Element("SimplestBean", "org.apache.commons.betwixt.schema.SimplestElementBean"); + GlobalElement root = new GlobalElement("SimplestBean", "org.apache.commons.betwixt.schema.SimplestElementBean"); expected.addComplexType(simplestBeanType); expected.addElement(root); @@ -127,7 +125,7 @@ simpleBeanType.addElement(new SimpleLocalElement("three", "xsd:string")); simpleBeanType.addElement(new SimpleLocalElement("four", "xsd:string")); expected.addComplexType(simpleBeanType); - expected.addElement(new Element("simple", "org.apache.commons.betwixt.schema.SimpleBean")); + expected.addElement(new GlobalElement("simple", "org.apache.commons.betwixt.schema.SimpleBean")); assertEquals("Simple bean schema", expected, out); @@ -154,7 +152,7 @@ orderLineType.addAttribute(new Attribute("quantity", "xsd:string")); orderLineType.addElement(new ElementReference("product", productBeanType)); expected.addComplexType(orderLineType); - expected.addElement(new Element("OrderLineBean", OrderLineBean.class.getName())); + expected.addElement(new GlobalElement("OrderLineBean", OrderLineBean.class.getName())); assertEquals("Transcriber schema", expected, out); } @@ -201,75 +199,9 @@ orderType.addElement(new ElementReference("customer", customerBeanType)); orderType.addElement(new ElementReference("line", orderLineType)); expected.addComplexType(orderType); - expected.addElement(new Element("order-bean", OrderBean.class.getName())); + expected.addElement(new GlobalElement("order-bean", OrderBean.class.getName())); assertEquals("Transcriber schema", expected, out); } - - private void printDifferences(Schema one, Schema two) { - for( Iterator it=one.getComplexTypes().iterator();it.hasNext(); ) { - GlobalComplexType complexType = (GlobalComplexType)it.next(); - if (!two.getComplexTypes().contains(complexType)) { - boolean matched = false; - for (Iterator otherIter=two.getComplexTypes().iterator(); it.hasNext();) { - GlobalComplexType otherType = (GlobalComplexType) otherIter.next(); - if (otherType.getName().equals(complexType.getName())) { - printDifferences(complexType, otherType); - matched = true; - break; - } - } - if (!matched) { - System.err.println("Missing Complex type: " + complexType); - } - } - } - - } - - private void printDifferences(GlobalComplexType one, GlobalComplexType two) { - System.err.println("Type " + one + " is not equal to " + two); - for (Iterator it = one.getElements().iterator(); it.hasNext();) { - Element elementOne = (Element) it.next(); - if (!two.getElements().contains(elementOne)) { - boolean matched = false; - for (Iterator otherIter=two.getElements().iterator(); it.hasNext();) { - Element elementTwo = (Element) otherIter.next(); - if (elementTwo.getName().equals(elementTwo.getName())) { - printDifferences(elementOne, elementTwo); - matched = true; - break; - } - } - if (!matched) { - System.err.println("Missing Element: " + elementOne); - } - } - } - for (Iterator it = one.getAttributes().iterator(); it.hasNext();) { - Attribute attributeOne = (Attribute) it.next(); - if (!two.getAttributes().contains(attributeOne)) { - boolean matched = false; - for (Iterator otherIter=two.getAttributes().iterator(); it.hasNext();) { - Attribute attributeTwo = (Attribute) otherIter.next(); - if (attributeTwo.getName().equals(attributeTwo.getName())) { - printDifferences(attributeOne, attributeTwo); - matched = true; - break; - } - } - if (!matched) { - System.err.println("Missing Attribute: " + attributeOne); - } - } - } - } - - private void printDifferences(Attribute one , Attribute two) { - System.err.println("Attribute " + one + " is not equals to " + two); - } - - private void printDifferences(Element one , Element two) { - System.err.println("Element " + one + " is not equals to " + two); - } + }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]