1 package org.codehaus.mojo.jaxb2.schemageneration.postprocessing.schemaenhancement; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 import org.codehaus.mojo.jaxb2.shared.Validate; 23 24 /** 25 * <p>Data holder for schema transformation operations, to permit customization of the 26 * schema namespace prefix and file name of generated schema. As the <code>schemagen</code> 27 * tool has no mechanics to control namespace prefix and file name of generated schema, 28 * the Jaxb2-Maven-plugin must supply a post-processing step to work around this situation.</p> 29 * <p>Each <code>TransformSchema</code> object holds data pertaining to changes for one namespace 30 * URI - either namespace prefix used within the generated XSD or resulting filename for 31 * the schema definition.</p> 32 * <ol> 33 * <li><strong>Namespace prefix</strong>. Each XML element within a namespace uses the supplied 34 * prefix. (As a reference, in the XML element <code><foo:bar/></code>, the namespace prefix 35 * is "foo" and the element name is "bar"). The Schemagen tool by default only generates namespace 36 * prefixes on the form "ns1", "ns2" etc., which means that the generated schema will contain elements 37 * on the form <code><xs:extension base="ns1:nazgulEntity"></code>. 38 * Use a <code>toPrefix</code> element to change the namespace prefix of a particular XML URI to 39 * simplify understanding the schema.</li> 40 * <li><strong>Filename</strong>. By default, the Schemagen tool creates files called "schema1.xsd", 41 * "schema2.xsd" etc. Since the XSD imports one another, simply changing the filename will frequently 42 * break the schema structure - you will need to change all import statements in all generated XSD files 43 * to match the new file names. The Jaxb2 Maven plugin can do all this housekeeping automatically, if you 44 * create a transformSchema element containing a <code>toFile</code> element to change the filename for a 45 * particular XML URI. Changing the file names frequently improves overview and usability of the generated schema 46 * files.</li> 47 * </ol> 48 * <h2>Example TransformSchemas</h2> 49 * <p>The URI element is mandatory for each TransformSchema element. The first example illustrates how 50 * to use the TransformSchema element to change the prefix and file name of 3 XML URIs. This is the recommended 51 * use of a TransformSchema - change both prefix and filename to something meaningful for each URI:</p> 52 * <pre> 53 * <transformSchemas> 54 * <transformSchema> 55 * <uri>http://some/namespace</uri> 56 * <toPrefix>some</toPrefix> 57 * <toFile>some_schema.xsd</toFile> 58 * </transformSchema> 59 * <transformSchema> 60 * <uri>http://another/namespace</uri> 61 * <toPrefix>another</toPrefix> 62 * <toFile>another_schema.xsd</toFile> 63 * </transformSchema> 64 * <transformSchema> 65 * <uri>http://yet/another/namespace</uri> 66 * <toPrefix>yetAnother</toPrefix> 67 * <toFile>yet_another_schema.xsd</toFile> 68 * </transformSchema> 69 * </transformSchemas> 70 * </pre> 71 * <p>The URI element is mandatory for each TransformSchema element, along with at least one of the other two 72 * elements in the TransformSchema. This implies that partial configuration for TransformSchema can be used, 73 * although <em>this is not recommended</em> since the readability and usability of the automatically generated 74 * namespace prefixes and file names are poor. The second example illustrates how to use the TransformSchema element 75 * to change either prefix or file name for 2 XML URIs:</p> 76 * <pre> 77 * <transformSchemas> 78 * <transformSchema> 79 * <uri>http://another/namespace</uri> 80 * <toPrefix>another</toPrefix> 81 * </transformSchema> 82 * <transformSchema> 83 * <uri>http://yet/another/namespace</uri> 84 * <toFile>yet_another_schema.xsd</toFile> 85 * </transformSchema> 86 * </transformSchemas> 87 * </pre> 88 * 89 * @author <a href="mailto:lj@jguru.se">Lennart Jörelid</a> 90 * @since 1.4 91 */ 92 public class TransformSchema { 93 94 /** 95 * The empty XML Namespace. 96 */ 97 public static final String EMPTY_NAMESPACE = ""; 98 99 // Internal state 100 private String uri = EMPTY_NAMESPACE; 101 private String toPrefix; 102 private String toFile; 103 104 /** 105 * Default constructor. 106 */ 107 public TransformSchema() { 108 } 109 110 /** 111 * Compound constructor, creating a TransformSchema instruction wrapping the supplied data. 112 * 113 * @param uri The URI of this Schema, such as 114 * <code>http://www.jguru.se/some/namespace</code>. Cannot be null or empty. 115 * @param toPrefix The new namespace prefix for this Schema. Optional. 116 * @param toFile The new name of the generated schema file. 117 */ 118 public TransformSchema(final String uri, final String toPrefix, final String toFile) { 119 this.uri = uri; 120 this.toPrefix = toPrefix; 121 this.toFile = toFile; 122 } 123 124 /** 125 * @return The URI of this Schema, such as <code>http://www.jguru.se/some/namespace</code>. 126 * The namespace URI is mapped to its prefix in the schema element, i.e: 127 * <code>xmlns:xs="http://www.w3.org/2001/XMLSchema"</code> or 128 * <code>xmlns:foo="http://www.acme.com/xml/schema/foo"</code>. 129 */ 130 public String getUri() { 131 return uri; 132 } 133 134 /** 135 * @return The namespace prefix of this Schema. Each schema element is related to its namespace using the prefix. 136 * For an XML element <code><foo:bar/></code>, the prefix is "foo" (and the element name is "bar"). 137 */ 138 public String getToPrefix() { 139 return toPrefix; 140 } 141 142 /** 143 * @return the name of the target file if/when renamed. 144 */ 145 public String getToFile() { 146 return toFile; 147 } 148 149 /** 150 * Assigns the URI of this Schema, such as <code>http://www.jguru.se/some/namespace</code>. 151 * The namespace URI is mapped to its prefix in the schema element, i.e: 152 * <code>xmlns:xs="http://www.w3.org/2001/XMLSchema"</code> or 153 * <code>xmlns:foo="http://www.acme.com/xml/schema/foo"</code>. 154 * 155 * @param uri The non-empty uri of this Schema. 156 */ 157 public void setUri(final String uri) { 158 159 // Check sanity 160 Validate.notEmpty(uri, "uri"); 161 162 // Assign internal state 163 this.uri = uri; 164 } 165 166 /** 167 * Assigns the namespace prefix of this Schema. Each schema element is related to its namespace 168 * using the prefix. For an XML element <code><foo:bar/></code>, the prefix is "foo" 169 * (and the element name is "bar"). 170 * 171 * @param toPrefix The non-empty prefix to assign. 172 */ 173 public void setToPrefix(final String toPrefix) { 174 175 // Check sanity 176 Validate.notEmpty(toPrefix, "toPrefix"); 177 178 // Assign internal state 179 this.toPrefix = toPrefix; 180 } 181 182 /** 183 * Assigns the the name of the target file if/when renamed. 184 * 185 * @param toFile The non-empty filename to assign. 186 */ 187 public void setToFile(final String toFile) { 188 189 // Check sanity 190 Validate.notEmpty(toFile, "toFile"); 191 192 // Assign internal state 193 this.toFile = toFile; 194 } 195 196 /** 197 * {@inheritDoc} 198 */ 199 public String toString() { 200 return "[ uri: " + uri + " --> prefix: " + toPrefix + ", file: " + toFile + " ]"; 201 } 202 }