1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.betwixt.schema;
19
20 import java.beans.IntrospectionException;
21
22 import org.apache.commons.betwixt.BindingConfiguration;
23 import org.apache.commons.betwixt.ElementDescriptor;
24 import org.apache.commons.betwixt.IntrospectionConfiguration;
25 import org.apache.commons.betwixt.XMLBeanInfo;
26 import org.apache.commons.betwixt.XMLIntrospector;
27
28 /***
29 * <p>Generates XML Schemas for Betwixt mappings.
30 *
31 * </p><p>
32 * The basic idea is that an object model for the schema will be created
33 * and Betwixt can be used to output this to xml.
34 * This should allow both SAX and text.
35 * </p>
36 * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
37 * @version $Revision: 438373 $
38 */
39 public class SchemaTranscriber {
40
41 public static final String W3C_SCHEMA_URI = "http://www.w3.org/2001/XMLSchema";
42 public static final String W3C_SCHEMA_INSTANCE_URI= "http://www.w3.org/2001/XMLSchema-instance";
43
44 /*** Used to introspect beans in order to generate XML */
45 private XMLIntrospector introspector = new XMLIntrospector();
46 private TranscriptionConfiguration configuration = new TranscriptionConfiguration();
47
48 public SchemaTranscriber() {}
49
50 /***
51 * Gets the configuration for the XMLBeanInfo to XML schema transcription.
52 * @return TranscriptionConfiguration, not null
53 */
54 public TranscriptionConfiguration getConfiguration() {
55 return configuration;
56 }
57
58 /***
59 * Sets the configuration for the XMLBeanInfo to XML schema transcription.
60 * @param configuration TranscriptionConfiguration, not null
61 */
62 public void setConfiguration(TranscriptionConfiguration configuration) {
63 this.configuration = configuration;
64 }
65
66 /***
67 * Gets the XMLIntrospector used to create XMLInfoBean's.
68 * @return XMLIntrospector used to create XMLInfoBean's used to generate schema, not null
69 */
70 public XMLIntrospector getXMLIntrospector() {
71 return introspector;
72 }
73
74 /***
75 * <p>Sets the XMLIntrospector used to create XMLInfoBeans.
76 * </p></p>
77 * <strong>Note:</strong> certain properties will be reconfigured so that
78 * the introspection will produce correct results.
79 * </p>
80 * @param introspector XMLIntrospector used to create XMLInfoBean's used to generate schema, not null
81 */
82 public void setXMLIntrospector(XMLIntrospector introspector) {
83 this.introspector = introspector;
84 }
85
86 /***
87 * Generates an XML Schema model for the given class.
88 * @param clazz not null
89 * @return Schema model, not null
90 */
91 public Schema generate(Class clazz) throws IntrospectionException {
92 XMLBeanInfo beanInfo = introspector.introspect(clazz);
93 return generate(beanInfo);
94 }
95
96 /***
97 * Generates an XML Schema model from the given XMLBeanInfo
98 * @param xmlBeanInfo not null
99 * @return Schema model, not null
100 */
101 public Schema generate(XMLBeanInfo xmlBeanInfo) throws IntrospectionException {
102 ElementDescriptor elementDescriptor = xmlBeanInfo.getElementDescriptor();
103 Schema schema = new Schema(introspector);
104 schema.addGlobalElementType(configuration, elementDescriptor);
105 return schema;
106 }
107
108 /***
109 * <p>Gets an <code>IntrospectionConfiguration</code> that is suitable
110 * for introspecting {@link Schema}.
111 * </p><p>
112 * <strong>Note:</strong> A new instance is created each time this method is called.
113 * It can therefore be safely be modified.
114 * </p>
115 *
116 * @return IntrospectionConfiguration, not null
117 */
118 public IntrospectionConfiguration createSchemaIntrospectionConfiguration() {
119 IntrospectionConfiguration configuration = new IntrospectionConfiguration();
120 configuration.getPrefixMapper().setPrefix(W3C_SCHEMA_URI, "xsd");
121 configuration.getPrefixMapper().setPrefix(W3C_SCHEMA_INSTANCE_URI, "xsi");
122 return configuration;
123 }
124
125 /***
126 * <p>Gets a <code>BindingConfiguration</code> that is suitable for mapping {@link Schema}.
127 * </p><p>
128 * <strong>Note:</strong> A new instance is created each time this method is called.
129 * It can therefore be safely be modified.
130 * </p>
131 * @return BindingConfiguration, not null
132 */
133 public BindingConfiguration createSchemaBindingConfiguration() {
134 BindingConfiguration configuration = new BindingConfiguration();
135 configuration.setMapIDs(false);
136 return configuration;
137 }
138 }