View Javadoc
1   package org.codehaus.modello.plugin.xsd;
2   
3   import org.codehaus.modello.ModelloException;
4   import org.codehaus.modello.model.Model;
5   import org.codehaus.modello.model.Version;
6   import org.codehaus.modello.plugin.xsd.metadata.XsdModelMetadata;
7   import org.codehaus.modello.plugins.xml.metadata.XmlModelMetadata;
8   import org.codehaus.plexus.util.StringUtils;
9   
10  /*
11   * Copyright (c) 2004, Codehaus.org
12   *
13   * Permission is hereby granted, free of charge, to any person obtaining a copy of
14   * this software and associated documentation files (the "Software"), to deal in
15   * the Software without restriction, including without limitation the rights to
16   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
17   * of the Software, and to permit persons to whom the Software is furnished to do
18   * so, subject to the following conditions:
19   *
20   * The above copyright notice and this permission notice shall be included in all
21   * copies or substantial portions of the Software.
22   *
23   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29   * SOFTWARE.
30   */
31  
32  /**
33   * Helper methods to deal with XML schema representation of the model.
34   *
35   * @author <a href="mailto:hboutemy@codehaus.org">Hervé Boutemy</a>
36   */
37  public class XsdModelHelper {
38      public static String getNamespace(Model model, Version version) throws ModelloException {
39          XmlModelMetadata xmlModelMetadata = (XmlModelMetadata) model.getMetadata(XmlModelMetadata.ID);
40  
41          XsdModelMetadata xsdModelMetadata = (XsdModelMetadata) model.getMetadata(XsdModelMetadata.ID);
42  
43          String namespace;
44          if (StringUtils.isNotEmpty(xsdModelMetadata.getNamespace())) {
45              namespace = xsdModelMetadata.getNamespace(version);
46          } else {
47              // xsd.namespace is not set, try using xml.namespace
48              if (StringUtils.isEmpty(xmlModelMetadata.getNamespace())) {
49                  throw new ModelloException("Cannot generate xsd without xmlns specification:"
50                          + " <model xml.namespace='...'> or <model xsd.namespace='...'>");
51              }
52  
53              namespace = xmlModelMetadata.getNamespace(version);
54          }
55  
56          return namespace;
57      }
58  
59      public static String getTargetNamespace(Model model, Version version, String namespace) {
60          XsdModelMetadata xsdModelMetadata = (XsdModelMetadata) model.getMetadata(XsdModelMetadata.ID);
61  
62          String targetNamespace;
63          if (xsdModelMetadata.getTargetNamespace() == null) {
64              // xsd.targetNamespace not set, using namespace
65              targetNamespace = namespace;
66          } else {
67              targetNamespace = xsdModelMetadata.getTargetNamespace(version);
68          }
69          return targetNamespace;
70      }
71  
72      public static String getTargetNamespace(Model model, Version version) throws ModelloException {
73          return getTargetNamespace(model, version, getNamespace(model, version));
74      }
75  }