String (String[] args) { var flags = new HashSet<String>(); var opts = new HashSet<String>(); 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); String[] badOpts = cl.getBadOpts(); if (badOpts.length > 0) { throw new IllegalArgumentException("Unrecognized option: " + badOpts[0]); } 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) { throw new IllegalArgumentException("Required option \"-name\" must be present"); } // Process Schema files var sdocs = new ArrayList<XmlObject>(); for (File schemaFile : schemaFiles) { try { sdocs.add(XmlObject.Factory.parse(schemaFile, (new XmlOptions()).setLoadLineNumbers().setLoadMessageDigest())); } catch (Exception e) { throw new IllegalArgumentException("Can not load schema file: " + schemaFile + ": " + e.getLocalizedMessage()); } } XmlObject[] schemas = sdocs.toArray(new XmlObject[0]); SchemaTypeSystem sts = null; if (schemas.length > 0) { 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 (XmlException e) { StringBuilder out = new StringBuilder("Schema compilation errors: "); var errors = e.getErrors(); for (Object error : errors) { out.append("\n").append(error); } throw new IllegalArgumentException(out.toString()); } } if (sts == null) { throw new IllegalArgumentException("No Schemas to process."); } SchemaType[] globalElems = sts.documentTypes(); SchemaType elem = null; for (SchemaType globalElem : globalElems) { if (rootName.equals(globalElem.getDocumentElementName().getLocalPart())) { elem = globalElem; break; } } if (elem == null) { throw new IllegalArgumentException("Could not find a global element with name \"" + rootName + "\""); } // Now generate it return SampleXmlUtil.createSampleForType(elem); }