List Info

Thread: patch: schemaInstaceGenerator.java




patch: schemaInstaceGenerator.java
user name
2007-06-13 06:51:02
Hi
I have made a small change to the schemaInstaceGenerator so that it can write to a outputfile file when making a Xsd2inst.



Patch:
 

--- schemaOld.java    2007-06-08 15:01:16.590041000 +0200
+++ org/apache/xmlbeans/impl
/xsd2inst/SchemaInstanceGenerator.java ;   2007-06-08 14:54:52.015217000 +0200
-18,6 +18,8
 import org.apache.xmlbeans.XmlObject;
 import java.util.ArrayList;
 import java.io.File ;
+import java.io.FileWriter;
+import java.io.IOException;
 import java.util.HashSet;
 import org.apache.xmlbeans.XmlOptions;
 import org.apache.xmlbeans.XmlBeans;
-31,156 +33,154
 import java.util.Collection ;
 import org.apache.xmlbeans.impl.tool.CommandLine;
 
-public class SchemaInstanceGenerator
-{
-    public static void printUsage()
-    {
-     ;   System.out.println("Generates a document based on the given Schema file");
;       System.out.println("having the given element as root.";);
  ;     System.out.println("The tool makes reasonable attempts to create a valid document,");
-      ;  System.out.println("but this is not always possible since, for example, ");
-        System.out.println("there are schemas for which no valid instance document ");
-  ;      System.out.println("can be produced.");
-      ;  System.out.println("Usage: xsd2inst [flags] schema.xsd -name element_name");
-        System.out.println("Flags:");
-      ;  System.out.println("    -name ;   the name of the root element");
-        System.out.println("    -dl     ; enable network downloads for imports and includes");
-    ;    System.out.println("    -nopvr   disable particle valid (restriction) rule");
-     ;   System.out.println("    -noupa   diable unique particle attributeion rule");
-     ;   System.out.println ("    -license prints license information");
-  ;  }
-
;   public static void main(String[] args)
-    {
-     ;   Set flags = new HashSet();
-        Set opts = new HashSet();
-        flags.add ("h");
-    ;    flags.add("help");
-      ;  flags.add("usage");
-    ;    flags.add("license");
-        flags.add("version");
-        flags.add("dl");
-      ;  flags.add("noupa");
-    ;    flags.add("nopvr");
-    ;    flags.add("partial");
-        opts.add("name");
-
-    ;    CommandLine cl = new CommandLine(args, flags, opts);
-
-       ; if (cl.getOpt("h") != null || cl.getOpt("help") != null || cl.getOpt("usage") != null)
-        {
-     ;       printUsage();
  ;         return;
-    ;    }
-
-    ;    String[] badOpts = cl.getBadOpts();
-        if (badOpts.length > 0)
  ;     {
- &nbsp;   ; &nbsp; &nbsp; &nbsp; for (int i = 0; i < badOpts.length; i++)
-&nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp;  System.out.println(&quot;Unrecognized option: " + badOpts[i]);
; &nbsp; &nbsp; &nbsp; &nbsp;   printUsage();
  ; &nbsp; &nbsp; &nbsp; &nbsp; return;
-&nbsp;   ; &nbsp;  }
-
; &nbsp; &nbsp; &nbsp; if (cl.getOpt("license") != null)
-&nbsp; &nbsp; &nbsp;   {
- &nbsp;   ; &nbsp; &nbsp; &nbsp; CommandLine.printLicense();
-&nbsp; &nbsp; &nbsp;   ; &nbsp;  System.exit(0);
-  ; &nbsp; &nbsp; &nbsp; &nbsp;  return;
-&nbsp;   ; &nbsp;  }
-
; &nbsp; &nbsp; &nbsp; if (cl.getOpt("version") != null)
-&nbsp; &nbsp; &nbsp;   {
- &nbsp;   ; &nbsp; &nbsp; &nbsp; CommandLine.printVersion();
-&nbsp; &nbsp; &nbsp;   ; &nbsp;  System.exit(0);
-&nbsp; &nbsp; &nbsp; &nbsp;   ;  return;
-&nbsp;   ; &nbsp;  }
-
-&nbsp; &nbsp; &nbsp;   boolean dl = (cl.getOpt("dl&quot;) != null);
-&nbsp; &nbsp;   ;  boolean nopvr = (cl.getOpt("nopvr") != null);
-&nbsp; &nbsp;   ;  boolean noupa = (cl.getOpt("noupa") != null);
-
-  ; &nbsp; &nbsp;  File[] schemaFiles = cl.filesEndingWith(".xsd&quot;);
-&nbsp; &nbsp;   ;  String rootName = cl.getOpt("name&quot;);
-
-  ; &nbsp; &nbsp;  if (rootName == null)
-&nbsp; &nbsp; &nbsp;   {
- &nbsp;   ; &nbsp; &nbsp; &nbsp; System.out.println(&quot;Required option "-name" must be present&quot;);
-&nbsp; &nbsp;   ; &nbsp; &nbsp;  return;
-&nbsp;   ; &nbsp;  }
- &nbsp;   ; &nbsp;
- &nbsp; &nbsp;   ; // Process Schema files
-&nbsp; &nbsp; &nbsp;   List sdocs = new ArrayList();
- &nbsp;   ; &nbsp; for (int i = 0; i < schemaFiles.length; i++)
-&nbsp; &nbsp; &nbsp; &nbsp; {
- &nbsp;   ; &nbsp; &nbsp; &nbsp; try
-&nbsp; &nbsp; &nbsp;   ; &nbsp;  {
- &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   sdocs.add(XmlObject.Factory.parse(schemaFiles[i],
-&nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; (new XmlOptions()).setLoadLineNumbers().setLoadMessageDigest()));
-&nbsp; &nbsp; &nbsp;   ; &nbsp;  }
- &nbsp;   ; &nbsp; &nbsp; &nbsp; catch (Exception e)
-&nbsp; &nbsp; &nbsp; &nbsp;   ;  {
- &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   System.err.println(&quot;Can not load schema file: " + schemaFiles[i] + ": ");
-  ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; e.printStackTrace();
- &nbsp; &nbsp; &nbsp; &nbsp;   ; }
- &nbsp;   ; &nbsp; }
-
; &nbsp; &nbsp; &nbsp; XmlObject[] schemas = (XmlObject[]) sdocs.toArray(new XmlObject[sdocs.size()]);
-
-&nbsp; &nbsp; &nbsp;   SchemaTypeSystem sts = null;
-&nbsp; &nbsp; &nbsp;   if (schemas.length > 0)
  ; &nbsp; &nbsp; {
- &nbsp;   ; &nbsp; &nbsp; &nbsp; Collection errors = new ArrayList();
- &nbsp;   ; &nbsp; &nbsp; &nbsp; XmlOptions compileOptions = new XmlOptions();
-&nbsp; &nbsp; &nbsp; &nbsp;   ;  if (dl)
-&nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp;  compileOptions.setCompileDownloadUrls();
-&nbsp;   ; &nbsp; &nbsp; &nbsp;  if (nopvr)
-&nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; compileOptions.setCompileNoPvrRule();
-&nbsp; &nbsp; &nbsp; &nbsp;   ;  if (noupa)
-&nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; compileOptions.setCompileNoUpaRule ();
-
- &nbsp; &nbsp; &nbsp;   ; &nbsp; try
; &nbsp; &nbsp; &nbsp; &nbsp;   {
- &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   sts = XmlBeans.compileXsd(schemas, XmlBeans.getBuiltinTypeSystem(), compileOptions);
-&nbsp; &nbsp; &nbsp;   ; &nbsp;  }
- &nbsp;   ; &nbsp; &nbsp; &nbsp; catch (Exception e)
  ; &nbsp; &nbsp; &nbsp; &nbsp; {
; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp;  if (errors.isEmpty() || !(e instanceof XmlException))
; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp;  e.printStackTrace();
-
- &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(&quot;Schema compilation errors: ");
-  ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; for (Iterator i = errors.iterator(); i.hasNext(); )
- &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp;  System.out.println(i.next());
-&nbsp;   ; &nbsp; &nbsp; &nbsp;  }
- &nbsp;   ; &nbsp; }
-
; &nbsp; &nbsp; &nbsp; if (sts == null)
-&nbsp; &nbsp; &nbsp;   {
- &nbsp;   ; &nbsp; &nbsp; &nbsp; System.out.println(&quot;No Schemas to process.&quot;);
-&nbsp;   ; &nbsp; &nbsp; &nbsp;  return;
-&nbsp;   ; &nbsp;  }
- &nbsp;   ; &nbsp; SchemaType[] globalElems = sts.documentTypes();
- &nbsp; &nbsp; &nbsp;  SchemaType elem = null;
-&nbsp; &nbsp; &nbsp;   for (int i = 0; i < globalElems.length; i++)
-&nbsp; &nbsp; &nbsp; &nbsp; {
- &nbsp;   ; &nbsp; &nbsp; &nbsp; if ( rootName.equals(globalElems[i].getDocumentElementName().getLocalPart()))
- &nbsp; &nbsp; &nbsp; &nbsp;   ; {
- &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   elem = globalElems[i];
-&nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp;  break;
-&nbsp; &nbsp;   ; &nbsp; &nbsp;  }
- &nbsp;   ; &nbsp; }
-
; &nbsp; &nbsp; &nbsp; if (elem == null)
-&nbsp;   ; &nbsp;  {
- &nbsp;   ; &nbsp; &nbsp; &nbsp; System.out.println(&quot;Could not find a global element with name "&quot; + rootName + "&quot;");
- &nbsp; &nbsp; &nbsp; &nbsp;   ; return;
-&nbsp;   ; &nbsp;  }
- &nbsp;   ; &nbsp;
- &nbsp; &nbsp;   ; // Now generate it
-&nbsp; &nbsp; &nbsp; &nbsp; String result = SampleXmlUtil.createSampleForType(elem);
+public class SchemaInstanceGenerator {
+ &nbsp;  public static void printUsage() {
+ &nbsp;   &nbsp;  System.out.println(&quot;Generates a document based on the given Schema file");
; &nbsp;  &nbsp;  System.out.println(&quot;having the given element as root.";);
  ;  &nbsp;  System.out.println(&quot;The tool makes reasonable attempts to create a valid document,&quot;);
+&nbsp; &nbsp;  &nbsp;  System.out.println(&quot;but this is not always possible since, for example, ");
+ &nbsp;   &nbsp;  System.out.println(&quot;there are schemas for which no valid instance document ");
+  ;   &nbsp;  System.out.println(&quot;can be produced.&quot;);
+&nbsp; &nbsp;  &nbsp;  System.out.println(&quot;Usage: xsd2inst [flags] schema.xsd -name element_name");
+ &nbsp;   &nbsp;  System.out.println(&quot;Flags:&quot;);
+&nbsp; &nbsp;  &nbsp;  System.out.println(&quot; &nbsp;  -name ; &nbsp; the name of the root element&quot;);
+&nbsp; &nbsp;  &nbsp;  System.out.println(&quot; &nbsp;  -dl &nbsp;   ; enable network downloads for imports and includes&quot;);
+&nbsp;    &nbsp;  System.out.println(&quot; &nbsp;  -nopvr&nbsp;  disable particle valid (restriction) rule");
+ &nbsp;   &nbsp;  System.out.println(&quot; &nbsp;  -noupa&nbsp;  diable unique particle attributeion rule");
+ &nbsp;   &nbsp;  System.out.println ("&nbsp; &nbsp; -outdir output path to XML file");
+ &nbsp;   &nbsp;  System.out.println(&quot; &nbsp;  -license prints license information");
+  ;  }
+
; &nbsp; public static void main(String[] args) {
+ &nbsp;   &nbsp;  Set flags = new HashSet();
+ &nbsp;   &nbsp;  Set opts = new HashSet();
+ &nbsp;   &nbsp;  flags.add("h&quot;);
; &nbsp;  &nbsp;  flags.add("help&quot;);
+&nbsp; &nbsp;  &nbsp;  flags.add("usage");
+&nbsp;    &nbsp;  flags.add("license");
+ &nbsp;   &nbsp;  flags.add ("version";);
  ;  &nbsp;  flags.add("dl&quot;);
+&nbsp; &nbsp;  &nbsp;  flags.add("noupa");
+&nbsp;    &nbsp;  flags.add("nopvr");
+&nbsp;    &nbsp;  flags.add("partial");
+ &nbsp;   &nbsp;  opts.add(&quot;outdir");
+ &nbsp;   &nbsp;  opts.add(&quot;name&quot;);
+
+&nbsp;    &nbsp;  CommandLine cl = new CommandLine(args, flags, opts);
+
+  ;   &nbsp;  if (cl.getOpt("h&quot;) != null || cl.getOpt("help&quot;) != null || cl.getOpt("usage") != null) {
; &nbsp;  &nbsp;   &nbsp;  printUsage();
  ;  &nbsp;   &nbsp;  return;
+&nbsp;    &nbsp;  }
+
; &nbsp;  &nbsp;  String[] badOpts = cl.getBadOpts();
+&nbsp; &nbsp;  &nbsp;  if (badOpts.length > 0) {
+ &nbsp;   &nbsp;   &nbsp;  for (int i = 0; i < badOpts.length; i++)
+&nbsp; &nbsp;  &nbsp;   &nbsp;   &nbsp;  System.out.println(&quot;Unrecognized option: " + badOpts[i]);
+ &nbsp;   &nbsp;   &nbsp;  printUsage();
  ;  &nbsp;   &nbsp;  return;
+&nbsp;    &nbsp;  }
+
; &nbsp;  &nbsp;  if (cl.getOpt("license") != null) {
+&nbsp; &nbsp;  &nbsp;   &nbsp;  CommandLine.printLicense();
+&nbsp; &nbsp;  &nbsp;   &nbsp;  System.exit(0);
+&nbsp; &nbsp;  &nbsp;   &nbsp;  return;
+&nbsp;    &nbsp;  }
+
; &nbsp;  &nbsp;  if (cl.getOpt("version") != null) {
+ &nbsp;   &nbsp;   &nbsp;  CommandLine.printVersion();
+ &nbsp;   &nbsp;   &nbsp;  System.exit(0);
+&nbsp; &nbsp;  &nbsp;   &nbsp;  return;
+&nbsp;    &nbsp;  }
+
; &nbsp;  &nbsp;  boolean dl = (cl.getOpt("dl&quot;) != null);
+&nbsp; &nbsp;  &nbsp;  boolean nopvr = (cl.getOpt("nopvr") != null);
+&nbsp; &nbsp;  &nbsp;  boolean noupa = ( cl.getOpt("noupa") != null);
+
+  ;   &nbsp;  File[] schemaFiles = cl.filesEndingWith(&quot;.xsd&quot;);
+&nbsp; &nbsp;  &nbsp;  String rootName = cl.getOpt("name&quot;);
+
+  ;   &nbsp;  if (rootName == null) {
+ &nbsp;   &nbsp;   &nbsp;  System.out.println("Required option "-name" must be present&quot;);
+&nbsp; &nbsp;  &nbsp;   &nbsp;  return;
+&nbsp;    &nbsp;  }
+
; &nbsp;  &nbsp;  // Process Schema files
+&nbsp; &nbsp;  &nbsp;  List sdocs = new ArrayList();
+ &nbsp;   &nbsp;  for (int i = 0; i < schemaFiles.length; i++) {
+ &nbsp;   &nbsp;   &nbsp;  try {
+ &nbsp;   &nbsp;   &nbsp;   &nbsp;  sdocs.add(XmlObject.Factory.parse(schemaFiles[i], (new XmlOptions()).setLoadLineNumbers()
+ &nbsp;   &nbsp;   &nbsp;   &nbsp;   &nbsp;   &nbsp;  .setLoadMessageDigest()));
+&nbsp; &nbsp;  &nbsp;   &nbsp;  } catch (Exception e) {
; &nbsp;  &nbsp;   &nbsp;   &nbsp;  System.err.println(&quot;Can not load schema file: " + schemaFiles[i] + ": ");
+  ;   &nbsp;   &nbsp;   &nbsp;  e.printStackTrace();
+ &nbsp;   &nbsp;   &nbsp;  }
+ &nbsp;   &nbsp;  }
+
; &nbsp;  &nbsp;  XmlObject[] schemas = (XmlObject[]) sdocs.toArray(new XmlObject[sdocs.size()]);
+
+&nbsp; &nbsp;  &nbsp;  SchemaTypeSystem sts = null;
+&nbsp; &nbsp;  &nbsp;  if (schemas.length > 0) {
+ &nbsp;   &nbsp;   &nbsp;  Collection errors = new ArrayList();
+ &nbsp;   &nbsp;   &nbsp;  XmlOptions compileOptions = new XmlOptions();
+&nbsp; &nbsp;  &nbsp;   &nbsp;  if (dl)
+&nbsp; &nbsp;  &nbsp;   &nbsp;   &nbsp;  compileOptions.setCompileDownloadUrls();
+&nbsp;    &nbsp;   &nbsp;  if (nopvr)
+&nbsp;    &nbsp;   &nbsp;   &nbsp;  compileOptions.setCompileNoPvrRule();
+&nbsp; &nbsp;  &nbsp;   &nbsp;  if (noupa)
+&nbsp;    &nbsp;   &nbsp;   &nbsp;  compileOptions.setCompileNoUpaRule ();
+
+ &nbsp;   &nbsp;   &nbsp;  try {
+ &nbsp;   &nbsp;   &nbsp;   &nbsp;  sts = XmlBeans.compileXsd(schemas, XmlBeans.getBuiltinTypeSystem(), compileOptions);
+&nbsp; &nbsp;  &nbsp;   &nbsp;  } catch (Exception e) {
+ &nbsp;   &nbsp;   &nbsp;   &nbsp;  if (errors.isEmpty() || !(e instanceof XmlException))
+&nbsp; &nbsp;  &nbsp;   &nbsp;   &nbsp;   &nbsp;  e.printStackTrace();
+
+ &nbsp;   &nbsp;   &nbsp;   &nbsp;  System.out.println(&quot;Schema compilation errors: ");
+  ;   &nbsp;   &nbsp;   &nbsp;  for (Iterator i = errors.iterator(); i.hasNext();)
  ;  &nbsp;   &nbsp;   &nbsp;   &nbsp;  System.out.println(i.next());
+ &nbsp;   &nbsp;   &nbsp;  }
+ &nbsp;   &nbsp;  }
+
; &nbsp;  &nbsp;  if (sts == null) {
+ &nbsp;   &nbsp;   &nbsp;  System.out.println(&quot;No Schemas to process.&quot;);
+&nbsp; &nbsp;  &nbsp;   &nbsp;  return;
+&nbsp;    &nbsp;  }
+ &nbsp;   &nbsp;  SchemaType[] globalElems = sts.documentTypes();
+ &nbsp;   &nbsp;  SchemaType elem = null;
+&nbsp; &nbsp;  &nbsp;  for (int i = 0; i < globalElems.length; i++) {
+ &nbsp;   &nbsp;   &nbsp;  if (rootName.equals(globalElems[i].getDocumentElementName().getLocalPart())) {
+ &nbsp;   &nbsp;   &nbsp;   &nbsp;  elem = globalElems[i];
+&nbsp; &nbsp;  &nbsp;   &nbsp;   &nbsp;  break;
+&nbsp; &nbsp;  &nbsp;   &nbsp;  }
+ &nbsp;   &nbsp;  }
+
; &nbsp;  &nbsp;  if (elem == null) {
+ &nbsp;   &nbsp;   &nbsp;  System.out.println(&quot;Could not find a global element with name "&quot; + rootName + "&quot;");
+ &nbsp;   &nbsp;   &nbsp;  return;
+&nbsp;    &nbsp;  }
+
; &nbsp;  &nbsp;  // Now generate it
  ;  &nbsp;  String result = SampleXmlUtil.createSampleForType(elem);
+
+ &nbsp;   &nbsp;  String outPathName = cl.getOpt("outdir");
+  ;   &nbsp;  if (outPathName != null) {
; &nbsp;  &nbsp;   &nbsp;  File outFile = new File(outPathName);
+&nbsp;    &nbsp;   &nbsp;  FileWriter out;
+&nbsp; &nbsp;  &nbsp;   &nbsp;  try {
+ &nbsp;   &nbsp;   &nbsp;   &nbsp;  out = new FileWriter(outFile);
+ &nbsp;   &nbsp;   &nbsp;   &nbsp;  out.write(result);
+&nbsp;    &nbsp;   &nbsp;   &nbsp;  out.close ();
+&nbsp; &nbsp;  &nbsp;   &nbsp;  } catch (IOException e) {
+ &nbsp;   &nbsp;   &nbsp;   &nbsp;  // TODO Auto-generated catch block
+&nbsp; &nbsp;  &nbsp;   &nbsp;   &nbsp;  e.printStackTrace();
+ &nbsp;   &nbsp;   &nbsp;  }
+ &nbsp;   &nbsp;   &nbsp;  System.out.println(&quot;xml instance " +outPathName +" created&quot;);
+&nbsp; &nbsp;  &nbsp;   &nbsp;  System.exit(0);
+&nbsp; &nbsp;  &nbsp;   &nbsp;  return;
+&nbsp;    &nbsp;  }
 
-  ; &nbsp; &nbsp;  System.out.println(result);
+&nbsp; &nbsp;  &nbsp;  System.out.println(result);
&nbsp;
- &nbsp;   ; &nbsp; return;
-&nbsp;   }
+ &nbsp;  }
 }
[1]

about | contact  Other archives ( Real Estate discussion Medical topics )