You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xmlbeans.apache.org by Lars C <la...@gmail.com> on 2007/06/13 13:51:02 UTC
patch: schemaInstaceGenerator.java
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)
- {
- for (int i = 0; i < badOpts.length; i++)
- System.out.println("Unrecognized option: " + badOpts[i]);
- printUsage();
- return;
- }
-
- if (cl.getOpt("license") != null)
- {
- CommandLine.printLicense();
- System.exit(0);
- return;
- }
-
- if (cl.getOpt("version") != null)
- {
- CommandLine.printVersion();
- System.exit(0);
- return;
- }
-
- boolean dl = (cl.getOpt("dl") != null);
- boolean nopvr = (cl.getOpt("nopvr") != null);
- boolean noupa = (cl.getOpt("noupa") != null);
-
- File[] schemaFiles = cl.filesEndingWith(".xsd");
- String rootName = cl.getOpt("name");
-
- if (rootName == null)
- {
- System.out.println("Required option \"-name\" must be
present");
- return;
- }
-
- // Process Schema files
- List sdocs = new ArrayList();
- for (int i = 0; i < schemaFiles.length; i++)
- {
- try
- {
- sdocs.add(XmlObject.Factory.parse(schemaFiles[i],
- (new
XmlOptions()).setLoadLineNumbers().setLoadMessageDigest()));
- }
- catch (Exception e)
- {
- System.err.println("Can not load schema file: " +
schemaFiles[i] + ": ");
- e.printStackTrace();
- }
- }
-
- XmlObject[] schemas = (XmlObject[]) sdocs.toArray(new XmlObject[
sdocs.size()]);
-
- SchemaTypeSystem sts = null;
- if (schemas.length > 0)
- {
- Collection errors = new ArrayList();
- XmlOptions compileOptions = new XmlOptions();
- if (dl)
- compileOptions.setCompileDownloadUrls();
- if (nopvr)
- compileOptions.setCompileNoPvrRule();
- if (noupa)
- compileOptions.setCompileNoUpaRule ();
-
- try
- {
- sts = XmlBeans.compileXsd(schemas,
XmlBeans.getBuiltinTypeSystem(), compileOptions);
- }
- catch (Exception e)
- {
- if (errors.isEmpty() || !(e instanceof XmlException))
- e.printStackTrace();
-
- System.out.println("Schema compilation errors: ");
- for (Iterator i = errors.iterator(); i.hasNext(); )
- System.out.println(i.next());
- }
- }
-
- if (sts == null)
- {
- System.out.println("No Schemas to process.");
- return;
- }
- SchemaType[] globalElems = sts.documentTypes();
- SchemaType elem = null;
- for (int i = 0; i < globalElems.length; i++)
- {
- if ( rootName.equals
(globalElems[i].getDocumentElementName().getLocalPart()))
- {
- elem = globalElems[i];
- break;
- }
- }
-
- if (elem == null)
- {
- System.out.println("Could not find a global element with name
\"" + rootName + "\"");
- return;
- }
-
- // Now generate it
- String result = SampleXmlUtil.createSampleForType(elem);
+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 (" -outdir output path to XML file");
+ 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("outdir");
+ 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) {
+ for (int i = 0; i < badOpts.length; i++)
+ System.out.println("Unrecognized option: " + badOpts[i]);
+ printUsage();
+ return;
+ }
+
+ if (cl.getOpt("license") != null) {
+ CommandLine.printLicense();
+ System.exit(0);
+ return;
+ }
+
+ if (cl.getOpt("version") != null) {
+ CommandLine.printVersion();
+ System.exit(0);
+ return;
+ }
+
+ boolean dl = (cl.getOpt("dl") != null);
+ boolean nopvr = (cl.getOpt("nopvr") != null);
+ boolean noupa = ( cl.getOpt("noupa") != null);
+
+ File[] schemaFiles = cl.filesEndingWith(".xsd");
+ String rootName = cl.getOpt("name");
+
+ if (rootName == null) {
+ System.out.println("Required option \"-name\" must be
present");
+ return;
+ }
+
+ // Process Schema files
+ List sdocs = new ArrayList();
+ for (int i = 0; i < schemaFiles.length; i++) {
+ try {
+ sdocs.add(XmlObject.Factory.parse(schemaFiles[i], (new
XmlOptions()).setLoadLineNumbers()
+ .setLoadMessageDigest()));
+ } catch (Exception e) {
+ System.err.println("Can not load schema file: " +
schemaFiles[i] + ": ");
+ e.printStackTrace();
+ }
+ }
+
+ XmlObject[] schemas = (XmlObject[]) sdocs.toArray(new XmlObject[
sdocs.size()]);
+
+ SchemaTypeSystem sts = null;
+ if (schemas.length > 0) {
+ Collection errors = new ArrayList();
+ XmlOptions compileOptions = new XmlOptions();
+ if (dl)
+ compileOptions.setCompileDownloadUrls();
+ if (nopvr)
+ compileOptions.setCompileNoPvrRule();
+ if (noupa)
+ compileOptions.setCompileNoUpaRule ();
+
+ try {
+ sts = XmlBeans.compileXsd(schemas,
XmlBeans.getBuiltinTypeSystem(), compileOptions);
+ } catch (Exception e) {
+ if (errors.isEmpty() || !(e instanceof XmlException))
+ e.printStackTrace();
+
+ System.out.println("Schema compilation errors: ");
+ for (Iterator i = errors.iterator(); i.hasNext();)
+ System.out.println(i.next());
+ }
+ }
+
+ if (sts == null) {
+ System.out.println("No Schemas to process.");
+ return;
+ }
+ SchemaType[] globalElems = sts.documentTypes();
+ SchemaType elem = null;
+ for (int i = 0; i < globalElems.length; i++) {
+ if
(rootName.equals(globalElems[i].getDocumentElementName().getLocalPart()))
{
+ elem = globalElems[i];
+ break;
+ }
+ }
+
+ if (elem == null) {
+ System.out.println("Could not find a global element with name
\"" + rootName + "\"");
+ return;
+ }
+
+ // Now generate it
+ String result = SampleXmlUtil.createSampleForType(elem);
+
+ String outPathName = cl.getOpt("outdir");
+ if (outPathName != null) {
+ File outFile = new File(outPathName);
+ FileWriter out;
+ try {
+ out = new FileWriter(outFile);
+ out.write(result);
+ out.close ();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ System.out.println("xml instance " +outPathName +" created");
+ System.exit(0);
+ return;
+ }
- System.out.println(result);
+ System.out.println(result);
- return;
- }
+ }
}