001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.betwixt.schema;
019:
020: import java.beans.IntrospectionException;
021:
022: import org.apache.commons.betwixt.BindingConfiguration;
023: import org.apache.commons.betwixt.ElementDescriptor;
024: import org.apache.commons.betwixt.IntrospectionConfiguration;
025: import org.apache.commons.betwixt.XMLBeanInfo;
026: import org.apache.commons.betwixt.XMLIntrospector;
027:
028: /**
029: * <p>Generates XML Schemas for Betwixt mappings.
030: *
031: * </p><p>
032: * The basic idea is that an object model for the schema will be created
033: * and Betwixt can be used to output this to xml.
034: * This should allow both SAX and text.
035: * </p>
036: * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
037: * @version $Revision: 438373 $
038: */
039: public class SchemaTranscriber {
040:
041: public static final String W3C_SCHEMA_URI = "http://www.w3.org/2001/XMLSchema";
042: public static final String W3C_SCHEMA_INSTANCE_URI = "http://www.w3.org/2001/XMLSchema-instance";
043:
044: /** Used to introspect beans in order to generate XML */
045: private XMLIntrospector introspector = new XMLIntrospector();
046: private TranscriptionConfiguration configuration = new TranscriptionConfiguration();
047:
048: public SchemaTranscriber() {
049: }
050:
051: /**
052: * Gets the configuration for the XMLBeanInfo to XML schema transcription.
053: * @return TranscriptionConfiguration, not null
054: */
055: public TranscriptionConfiguration getConfiguration() {
056: return configuration;
057: }
058:
059: /**
060: * Sets the configuration for the XMLBeanInfo to XML schema transcription.
061: * @param configuration TranscriptionConfiguration, not null
062: */
063: public void setConfiguration(
064: TranscriptionConfiguration configuration) {
065: this .configuration = configuration;
066: }
067:
068: /**
069: * Gets the XMLIntrospector used to create XMLInfoBean's.
070: * @return XMLIntrospector used to create XMLInfoBean's used to generate schema, not null
071: */
072: public XMLIntrospector getXMLIntrospector() {
073: return introspector;
074: }
075:
076: /**
077: * <p>Sets the XMLIntrospector used to create XMLInfoBeans.
078: * </p></p>
079: * <strong>Note:</strong> certain properties will be reconfigured so that
080: * the introspection will produce correct results.
081: * </p>
082: * @param introspector XMLIntrospector used to create XMLInfoBean's used to generate schema, not null
083: */
084: public void setXMLIntrospector(XMLIntrospector introspector) {
085: this .introspector = introspector;
086: }
087:
088: /**
089: * Generates an XML Schema model for the given class.
090: * @param clazz not null
091: * @return Schema model, not null
092: */
093: public Schema generate(Class clazz) throws IntrospectionException {
094: XMLBeanInfo beanInfo = introspector.introspect(clazz);
095: return generate(beanInfo);
096: }
097:
098: /**
099: * Generates an XML Schema model from the given XMLBeanInfo
100: * @param xmlBeanInfo not null
101: * @return Schema model, not null
102: */
103: public Schema generate(XMLBeanInfo xmlBeanInfo)
104: throws IntrospectionException {
105: ElementDescriptor elementDescriptor = xmlBeanInfo
106: .getElementDescriptor();
107: Schema schema = new Schema(introspector);
108: schema.addGlobalElementType(configuration, elementDescriptor);
109: return schema;
110: }
111:
112: /**
113: * <p>Gets an <code>IntrospectionConfiguration</code> that is suitable
114: * for introspecting {@link Schema}.
115: * </p><p>
116: * <strong>Note:</strong> A new instance is created each time this method is called.
117: * It can therefore be safely be modified.
118: * </p>
119: *
120: * @return IntrospectionConfiguration, not null
121: */
122: public IntrospectionConfiguration createSchemaIntrospectionConfiguration() {
123: IntrospectionConfiguration configuration = new IntrospectionConfiguration();
124: configuration.getPrefixMapper()
125: .setPrefix(W3C_SCHEMA_URI, "xsd");
126: configuration.getPrefixMapper().setPrefix(
127: W3C_SCHEMA_INSTANCE_URI, "xsi");
128: return configuration;
129: }
130:
131: /**
132: * <p>Gets a <code>BindingConfiguration</code> that is suitable for mapping {@link Schema}.
133: * </p><p>
134: * <strong>Note:</strong> A new instance is created each time this method is called.
135: * It can therefore be safely be modified.
136: * </p>
137: * @return BindingConfiguration, not null
138: */
139: public BindingConfiguration createSchemaBindingConfiguration() {
140: BindingConfiguration configuration = new BindingConfiguration();
141: configuration.setMapIDs(false);
142: return configuration;
143: }
144: }
|