summaryrefslogtreecommitdiffstats
path: root/sdo-java/tags/1.0-incubating-beta1/sdo/tools/src/main/java/org/apache/tuscany/sdo/generate/XSD2JavaGenerator.java
blob: 6d9b1ad76566d1ed8a2a9b5c78fb37a51733a13b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
 *
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */
package org.apache.tuscany.sdo.generate;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.tuscany.sdo.helper.XSDHelperImpl;
import org.apache.tuscany.sdo.util.DataObjectUtil;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.impl.EPackageRegistryImpl;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.BasicExtendedMetaData;
import org.eclipse.emf.ecore.util.ExtendedMetaData;
import org.eclipse.xsd.XSDSchema;

import commonj.sdo.helper.XSDHelper;

public class XSD2JavaGenerator extends JavaGenerator
{
  /**
   * Generate static SDOs from XML Schema
   * 
   *   Usage arguments: see JavaGenerator
   *   
   *     [ -targetDirectory <target-root-directory> ]
   *     [ -javaPackage <java-package-name> ]
   *     [ -schemaNamespace <namespace-uri> ]
   *     [ other options ... ]
   *     <xsd-file> | <wsdl-file>
   *
   *   Options:
   *   
   *     -schemaNamespace
   *         Generate classes for XSD types in the specified targetNamespace. By default, types in the
   *         targetNamespace of the first schema in the specified xsd or wsdl file are generated.
   *         
   *     NOTE: see the base class JavaGenerator for other options.
   *         
   *   Example:
   *   
   *     generate somedir/somefile.xsd
   *     
   *   See base class JavaGenerator for details and the other options.
   *
   */
  public static void main(String args[])
  {
    try
    {
      XSD2JavaGenerator generator = new XSD2JavaGenerator();
      generator.processArguments(args);
      generator.run(args);
    }
    catch (IllegalArgumentException e)
    {
      printUsage();
    }
  }
  
  protected String schemaNamespace = null;
  protected String generateBuiltIn = null;

  protected int handleArgument(String args[], int index)
  {
    if (args[index].equalsIgnoreCase("-schemaNamespace"))
    {
      schemaNamespace = args[++index];
    }
    else if (args[index].equalsIgnoreCase("-generateBuiltIn"))
    {
      // Internal option used when regenerating one of the built-in (predefined) models (e.g., commonj.sdo).
      generateBuiltIn = args[++index];
    }
    else
    {
      return super.handleArgument(args, index);
    }
    
    return index + 1;
  }

  protected void run(String args[])
  {
    String xsdFileName = args[inputIndex];
    generateFromXMLSchema(xsdFileName, schemaNamespace, targetDirectory, javaPackage, prefix, genOptions, generateBuiltIn);
  }

  public static void generateFromXMLSchema(String xsdFileName, String namespace, String targetDirectory, String javaPackage, String prefix, int genOptions)
  {
    generateFromXMLSchema(xsdFileName, namespace, targetDirectory, javaPackage, prefix, genOptions, null);
  }

  protected static void generateFromXMLSchema(String xsdFileName, String namespace, String targetDirectory, String javaPackage, String prefix, int genOptions, String regenerateBuiltIn)
  {
    DataObjectUtil.initRuntime();
    EPackage.Registry packageRegistry = new EPackageRegistryImpl(EPackage.Registry.INSTANCE);
    ExtendedMetaData extendedMetaData = new BasicExtendedMetaData(packageRegistry);
    XSDHelper xsdHelper = new XSDHelperImpl(extendedMetaData, regenerateBuiltIn);

    try
    {
      File inputFile = new File(xsdFileName).getAbsoluteFile();
      InputStream inputStream = new FileInputStream(inputFile);
      xsdHelper.define(inputStream, inputFile.toURI().toString());

      if (targetDirectory == null)
      {
        targetDirectory = new File(xsdFileName).getCanonicalFile().getParent();
      }
      else
      {
        targetDirectory = new File(targetDirectory).getCanonicalPath();
      }

      if (!packageRegistry.values().isEmpty())
      {
        String packageURI = namespace != null ? namespace : getSchemaNamespace(xsdFileName);
        generatePackages(packageRegistry.values(), packageURI, null, targetDirectory, javaPackage, prefix, genOptions);
      }

      /*
      for (Iterator iter = packageRegistry.values().iterator(); iter.hasNext();)
      {
        EPackage ePackage = (EPackage)iter.next();
        String basePackage = extractBasePackageName(ePackage, javaPackage);
        if (prefix == null)
        {
          prefix = CodeGenUtil.capName(ePackage.getName());
        }
        generateFromEPackage(ePackage, targetDirectory, basePackage, prefix, genOptions);
      }
      */
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
  }
  
  public static String getSchemaNamespace(String xsdFileName)
  {
    ResourceSet resourceSet = DataObjectUtil.createResourceSet();
    File inputFile = new File(xsdFileName).getAbsoluteFile();
    Resource model = resourceSet.getResource(URI.createURI(inputFile.toURI().toString()), true);
    XSDSchema schema = (XSDSchema)model.getContents().get(0);
    return schema.getTargetNamespace();
  }

  protected static void printUsage()
  {
    System.out.println("Usage arguments:");
    System.out.println("  [ -targetDirectory <target-root-directory> ]");
    System.out.println("  [ -javaPackage <java-package-name> ]");
    System.out.println("  [ -prefix <prefix-string> ]");
    System.out.println("  [ -schemaNamespace <namespace-uri> ]");
    System.out.println("  [ -noInterfaces ]");
    System.out.println("  [ -noContainment ]");
    System.out.println("  [ -noNotification ]");
    System.out.println("  [ -noUnsettable ]");
    /* Future Option: System.out.println("  [ -sparsePattern | -storePattern ]"); */
    /* Future Option: System.out.println("  [ -arrayAccessors ]"); */
    /* Future Option: System.out.println("  [ -generateLoader ]"); */
    /* Future Option: System.out.println("  [ -interfaceDataObject ]"); */
    System.out.println("  <xsd-file> | <wsdl-file>");
    System.out.println("");
    System.out.println("For example:");
    System.out.println("");
    System.out.println("  generate somedir/somefile.xsd");
  }

}