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://commons.apache.org/'>Apache Commons Team</a>
037     * @version $Revision: 561314 $
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         * Gets the configuration for the XMLBeanInfo to XML schema transcription.
052         * @return TranscriptionConfiguration, not null
053         */
054        public TranscriptionConfiguration getConfiguration() {
055            return configuration;
056        }
057        
058        /**
059         * Sets the configuration for the XMLBeanInfo to XML schema transcription.
060         * @param configuration TranscriptionConfiguration, not null
061         */
062        public void setConfiguration(TranscriptionConfiguration configuration) {
063            this.configuration = configuration;
064        }
065        
066            /**
067             * Gets the XMLIntrospector used to create XMLInfoBean's.
068             * @return XMLIntrospector used to create XMLInfoBean's used to generate schema, not null
069             */
070            public XMLIntrospector getXMLIntrospector() {
071                    return introspector;
072            }
073            
074            /**
075             * <p>Sets the XMLIntrospector used to create XMLInfoBeans.
076         * </p></p>
077         * <strong>Note:</strong> certain properties will be reconfigured so that 
078         * the introspection will produce correct results.
079         * </p>
080             * @param introspector XMLIntrospector used to create XMLInfoBean's used to generate schema, not null
081             */
082            public void setXMLIntrospector(XMLIntrospector introspector) {
083                    this.introspector = introspector;
084            }
085    
086            /**
087             * Generates an XML Schema model for the given class.
088             * @param clazz not null
089             * @return Schema model, not null
090             */
091            public Schema generate(Class clazz) throws IntrospectionException {
092            XMLBeanInfo beanInfo = introspector.introspect(clazz);
093                    return generate(beanInfo);
094            }
095            
096            /**
097             * Generates an XML Schema model from the given XMLBeanInfo
098             * @param xmlBeanInfo not null
099             * @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    }