Home » xmlbeans-2.5.0-src » org.apache.xmlbeans.impl » xsd2inst » [javadoc | source]

    1   /*   Copyright 2004 The Apache Software Foundation
    2    *
    3    *   Licensed under the Apache License, Version 2.0 (the "License");
    4    *   you may not use this file except in compliance with the License.
    5    *   You may obtain a copy of the License at
    6    *
    7    *       http://www.apache.org/licenses/LICENSE-2.0
    8    *
    9    *   Unless required by applicable law or agreed to in writing, software
   10    *   distributed under the License is distributed on an "AS IS" BASIS,
   11    *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   12    *   See the License for the specific language governing permissions and
   13    *  limitations under the License.
   14    */
   15   
   16   package org.apache.xmlbeans.impl.xsd2inst;
   17   
   18   import org.apache.xmlbeans.XmlObject;
   19   import java.util.ArrayList;
   20   import java.io.File;
   21   import java.util.HashSet;
   22   import org.apache.xmlbeans.XmlOptions;
   23   import org.apache.xmlbeans.XmlBeans;
   24   import org.apache.xmlbeans.SchemaTypeSystem;
   25   import org.apache.xmlbeans.SchemaType;
   26   import org.apache.xmlbeans.impl.xsd2inst.SampleXmlUtil;
   27   import java.util.Set;
   28   import org.apache.xmlbeans.XmlException;
   29   import java.util.Iterator;
   30   import java.util.List;
   31   import java.util.Collection;
   32   import org.apache.xmlbeans.impl.tool.CommandLine;
   33   
   34   public class SchemaInstanceGenerator
   35   {
   36       public static void printUsage()
   37       {
   38           System.out.println("Generates a document based on the given Schema file");
   39           System.out.println("having the given element as root.");
   40           System.out.println("The tool makes reasonable attempts to create a valid document,");
   41           System.out.println("but this is not always possible since, for example, ");
   42           System.out.println("there are schemas for which no valid instance document ");
   43           System.out.println("can be produced.");
   44           System.out.println("Usage: xsd2inst [flags] schema.xsd -name element_name");
   45           System.out.println("Flags:");
   46           System.out.println("    -name    the name of the root element");
   47           System.out.println("    -dl      enable network downloads for imports and includes");
   48           System.out.println("    -nopvr   disable particle valid (restriction) rule");
   49           System.out.println("    -noupa   diable unique particle attributeion rule");
   50           System.out.println("    -license prints license information");
   51       }
   52   
   53       public static void main(String[] args)
   54       {
   55           Set flags = new HashSet();
   56           Set opts = new HashSet();
   57           flags.add("h");
   58           flags.add("help");
   59           flags.add("usage");
   60           flags.add("license");
   61           flags.add("version");
   62           flags.add("dl");
   63           flags.add("noupa");
   64           flags.add("nopvr");
   65           flags.add("partial");
   66           opts.add("name");
   67   
   68           CommandLine cl = new CommandLine(args, flags, opts);
   69    
   70           if (cl.getOpt("h") != null || cl.getOpt("help") != null || cl.getOpt("usage") != null)
   71           {
   72               printUsage();
   73               return;
   74           }
   75   
   76           String[] badOpts = cl.getBadOpts();
   77           if (badOpts.length > 0)
   78           {
   79               for (int i = 0; i < badOpts.length; i++)
   80                   System.out.println("Unrecognized option: " + badOpts[i]);
   81               printUsage();
   82               return;
   83           }
   84   
   85           if (cl.getOpt("license") != null)
   86           {
   87               CommandLine.printLicense();
   88               System.exit(0);
   89               return;
   90           }
   91   
   92           if (cl.getOpt("version") != null)
   93           {
   94               CommandLine.printVersion();
   95               System.exit(0);
   96               return;
   97           }
   98   
   99           boolean dl = (cl.getOpt("dl") != null);
  100           boolean nopvr = (cl.getOpt("nopvr") != null);
  101           boolean noupa = (cl.getOpt("noupa") != null);
  102   
  103           File[] schemaFiles = cl.filesEndingWith(".xsd");
  104           String rootName = cl.getOpt("name");
  105   
  106           if (rootName == null)
  107           {
  108               System.out.println("Required option \"-name\" must be present");
  109               return;
  110           }
  111           
  112           // Process Schema files
  113           List sdocs = new ArrayList();
  114           for (int i = 0; i < schemaFiles.length; i++)
  115           {
  116               try
  117               {
  118                   sdocs.add(XmlObject.Factory.parse(schemaFiles[i],
  119                           (new XmlOptions()).setLoadLineNumbers().setLoadMessageDigest()));
  120               }
  121               catch (Exception e)
  122               {
  123                   System.err.println("Can not load schema file: " + schemaFiles[i] + ": ");
  124                   e.printStackTrace();
  125               }
  126           }
  127   
  128           XmlObject[] schemas = (XmlObject[]) sdocs.toArray(new XmlObject[sdocs.size()]);
  129   
  130           SchemaTypeSystem sts = null;
  131           if (schemas.length > 0)
  132           {
  133               Collection errors = new ArrayList();
  134               XmlOptions compileOptions = new XmlOptions();
  135               if (dl)
  136                   compileOptions.setCompileDownloadUrls();
  137               if (nopvr)
  138                   compileOptions.setCompileNoPvrRule();
  139               if (noupa)
  140                   compileOptions.setCompileNoUpaRule();
  141   
  142               try
  143               {
  144                   sts = XmlBeans.compileXsd(schemas, XmlBeans.getBuiltinTypeSystem(), compileOptions);
  145               }
  146               catch (Exception e)
  147               {
  148                   if (errors.isEmpty() || !(e instanceof XmlException))
  149                       e.printStackTrace();
  150   
  151                   System.out.println("Schema compilation errors: ");
  152                   for (Iterator i = errors.iterator(); i.hasNext(); )
  153                       System.out.println(i.next());
  154               }
  155           }
  156   
  157           if (sts == null)
  158           {
  159               System.out.println("No Schemas to process.");
  160               return;
  161           }
  162           SchemaType[] globalElems = sts.documentTypes();
  163           SchemaType elem = null;
  164           for (int i = 0; i < globalElems.length; i++)
  165           {
  166               if (rootName.equals(globalElems[i].getDocumentElementName().getLocalPart()))
  167               {
  168                   elem = globalElems[i];
  169                   break;
  170               }
  171           }
  172   
  173           if (elem == null)
  174           {
  175               System.out.println("Could not find a global element with name \"" + rootName + "\"");
  176               return;
  177           }
  178           
  179           // Now generate it
  180           String result = SampleXmlUtil.createSampleForType(elem);
  181   
  182           System.out.println(result);
  183   
  184           return;
  185       }
  186   }

Home » xmlbeans-2.5.0-src » org.apache.xmlbeans.impl » xsd2inst » [javadoc | source]