Classes in this File | Line Coverage | Branch Coverage | Complexity | |||||||
SchemaTranscriber |
|
| 1.0;1 |
1 | /* |
|
2 | * Copyright 2004 The Apache Software Foundation. |
|
3 | * |
|
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 | * you may not use this file except in compliance with the License. |
|
6 | * You may obtain a copy of the License at |
|
7 | * |
|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 | * |
|
10 | * Unless required by applicable law or agreed to in writing, software |
|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 | * See the License for the specific language governing permissions and |
|
14 | * limitations under the License. |
|
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/'>Apache 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 | 247 | private XMLIntrospector introspector = new XMLIntrospector(); |
45 | 247 | private TranscriptionConfiguration configuration = new TranscriptionConfiguration(); |
46 | ||
47 | 494 | 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 | 0 | 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 | 0 | this.configuration = configuration; |
63 | 0 | } |
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 | 364 | 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 | 0 | this.introspector = introspector; |
83 | 0 | } |
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 | 247 | XMLBeanInfo beanInfo = introspector.introspect(clazz); |
92 | 247 | 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 | 247 | ElementDescriptor elementDescriptor = xmlBeanInfo.getElementDescriptor(); |
102 | 247 | Schema schema = new Schema(introspector); |
103 | 247 | schema.addGlobalElementType(configuration, elementDescriptor); |
104 | 247 | 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 | 169 | IntrospectionConfiguration configuration = new IntrospectionConfiguration(); |
119 | 169 | configuration.getPrefixMapper().setPrefix(W3C_SCHEMA_URI, "xsd"); |
120 | 169 | configuration.getPrefixMapper().setPrefix(W3C_SCHEMA_INSTANCE_URI, "xsi"); |
121 | 169 | 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 | 169 | BindingConfiguration configuration = new BindingConfiguration(); |
134 | 169 | configuration.setMapIDs(false); |
135 | 169 | return configuration; |
136 | } |
|
137 | } |