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  {
39      public static String getNamespace( Model model, Version version )
40          throws ModelloException
41      {
42          XmlModelMetadata xmlModelMetadata = (XmlModelMetadata) model.getMetadata( XmlModelMetadata.ID );
43  
44          XsdModelMetadata xsdModelMetadata = (XsdModelMetadata) model.getMetadata( XsdModelMetadata.ID );
45  
46          String namespace;
47          if ( StringUtils.isNotEmpty( xsdModelMetadata.getNamespace() ) )
48          {
49              namespace = xsdModelMetadata.getNamespace( version );
50          }
51          else
52          {
53              // xsd.namespace is not set, try using xml.namespace
54              if ( StringUtils.isEmpty( xmlModelMetadata.getNamespace() ) )
55              {
56                  throw new ModelloException( "Cannot generate xsd without xmlns specification:"
57                                              + " <model xml.namespace='...'> or <model xsd.namespace='...'>" );
58              }
59  
60              namespace = xmlModelMetadata.getNamespace( version );
61          }
62  
63          return namespace;
64      }
65  
66      public static String getTargetNamespace( Model model, Version version, String namespace )
67      {
68          XsdModelMetadata xsdModelMetadata = (XsdModelMetadata) model.getMetadata( XsdModelMetadata.ID );
69  
70          String targetNamespace;
71          if ( xsdModelMetadata.getTargetNamespace() == null )
72          {
73              // xsd.target-namespace not set, using namespace
74              targetNamespace = namespace;
75          }
76          else
77          {
78              targetNamespace = xsdModelMetadata.getTargetNamespace( version );
79          }
80          return targetNamespace;
81      }
82  
83      public static String getTargetNamespace( Model model, Version version )
84          throws ModelloException
85      {
86          return getTargetNamespace( model, version, getNamespace( model, version ) );
87      }
88  }