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