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