From 6ae76859f991af99a5efaf35dabfbfb10c0c9c17 Mon Sep 17 00:00:00 2001 From: nikolaibeliaev Date: Wed, 20 Mar 2019 10:47:35 +0700 Subject: [PATCH] prepared to mvn --- example.xml | 92 +++++++++-------------- src/dev/fr13/XsdSchema.java | 143 ++++++++++++++---------------------- примерсхемыкакматрешка.xsd | 24 ------ 3 files changed, 90 insertions(+), 169 deletions(-) delete mode 100644 примерсхемыкакматрешка.xsd diff --git a/example.xml b/example.xml index b5387d0..eff7d09 100644 --- a/example.xml +++ b/example.xml @@ -1,65 +1,39 @@ - - - 01GTKT - 01GTKT0/001 - VND - 1 - true - TM - TM - true - 123456789 - 1 - - - Lương Thị Huyền - - 3901212654 - HN VN - 09880830406 - - 123456789 - 1 - - - Supplier perfom test 1 - 0100109106-501 - test - 0123456789 - PerformanceTest1@viettel.com.vn - vtbank - 23423424 - - - TM - - + + + USD + true + + + Trade LLC + 558731212 + CA + 784875863 + World bank + 111177 + + + Tom Smith + 87312131 + CA + 95453121312 + 1 - ENGLISH_COURSE - Khóa học tiếng anh - khóa học - 3500000 + 0001 + ENGLISH_COURSE + ea + 1000 10 - 35000000 - 10 - 3500000 + 9000 + 10 + 1000 0 - 0 - - 35000000 - 35000000 - 3500000 - 38500000 - Ba mươi tám triệu năm trăm nghìn đồng chẵn - 0 - 10 - - - 10 - 35000000 - 3500000 - - + + 9000 + 1000 + 10000 + Ten thousands + + diff --git a/src/dev/fr13/XsdSchema.java b/src/dev/fr13/XsdSchema.java index f0ea58f..7a06e7d 100644 --- a/src/dev/fr13/XsdSchema.java +++ b/src/dev/fr13/XsdSchema.java @@ -16,48 +16,56 @@ import java.nio.file.Paths; public class XsdSchema { - private File sourceFile; - private boolean printHelp = false; - private final String nameSpace = ""; - private final Inst2XsdOptions schema = new Inst2XsdOptions(); + private final File SOURCE_FILE; + private final File OUTPUT_FILE; + private final String NAME_SPACE; + private final int SCHEMA_DESIGN; public static void main(String[] args) throws IOException, XmlException { - // write your code here - // delete setters and getters + XsdSchema xsdSchema = new XsdSchema(args); - /*if (xsdSchema.getPrintHelp()) { - System.out.println("HELP"); - return; - } else { - xsdSchema.checkSourceFile(); - } + xsdSchema.checkSourceFile(); - //System.out.println(xsdSchema.getSourceFile().getPath()); + XmlObject xmlObject = XmlObject.Factory.parse(xsdSchema.SOURCE_FILE); + SchemaDocument schema = xsdSchema.getSchema(xmlObject); + if (schema != null) + schema.save(xsdSchema.OUTPUT_FILE); - XmlObject [] xmlObjects = new XmlObject[1]; - xmlObjects[0] = XmlObject.Factory.parse(xsdSchema.getSourceFile()); - SchemaDocument schema = xsdSchema.getSchema(xmlObjects); - System.out.println(schema);*/ } - private SchemaDocument getSchema(XmlObject [] xmlObjects) { + private SchemaDocument getSchema(XmlObject xmlObject) { + Inst2XsdOptions inst2XsdOptions = new Inst2XsdOptions(); - inst2XsdOptions.setDesign(Inst2XsdOptions.DESIGN_VENETIAN_BLIND); + inst2XsdOptions.setDesign(SCHEMA_DESIGN); + + XmlObject [] xmlObjects = new XmlObject[1]; + xmlObjects[0] = xmlObject; + SchemaDocument [] schemaDocuments = Inst2Xsd.inst2xsd(xmlObjects, inst2XsdOptions); if (schemaDocuments != null && schemaDocuments.length > 0) { + schemaDocuments[0].getSchema().setTargetNamespace(NAME_SPACE); return schemaDocuments[0]; } - return null; } - XsdSchema(String[] args) { + private XsdSchema(String[] args) { Options options = new Options(); - Option option = new Option("i", "input", true, "input file path"); - option.setRequired(true); - options.addOption(option); + Option inputOption = new Option("i", "input", true, "input xml file path"); + inputOption.setRequired(true); + options.addOption(inputOption); + + Option outputOption = new Option("o", "output", true, "output xsd file path"); + options.addOption(outputOption); + + Option nameSpaceOption = new Option("n", "name-space", true, "target name space"); + options.addOption(nameSpaceOption); + + Option schemaOption = new Option("s", "schema-design", true, + "schema design: 1-Russian Doll, 2-Salami slice, 3-Venetian blind"); + options.addOption(schemaOption); CommandLineParser commandLineParser = new DefaultParser(); HelpFormatter helpFormatter = new HelpFormatter(); @@ -67,86 +75,49 @@ public class XsdSchema { cmd = commandLineParser.parse(options, args); } catch (ParseException e) { System.out.println(e.getMessage()); - helpFormatter.printHelp("bla bla", options); + helpFormatter.printHelp("This tool creates xsd file from xml.", options); System.exit(1); } - String input = cmd.getOptionValue("input"); - System.out.println(input); + SOURCE_FILE = new File(cmd.getOptionValue("input")); - /*int argsCount = args.length; - String pathToFile = ""; - - if (argsCount == 0) { - return; + String outputFile = cmd.getOptionValue("output"); + if (outputFile == null) { + this.OUTPUT_FILE = new File(SOURCE_FILE.getParent() + File.separatorChar + "schema.xsd"); + } else { + this.OUTPUT_FILE = new File(outputFile); } - for (int i=0; i < args.length; i++) { + String ns = cmd.getOptionValue("name-space"); + NAME_SPACE = ns != null ? ns : "http://v8.default.com"; - switch (args[i]) { - case "-i": - pathToFile = args.length > 1 ? args[i + 1]:""; + String schema = cmd.getOptionValue("schema-design"); + if (schema == null) { + this.SCHEMA_DESIGN = 3; + } + else { + switch (schema) { + case "1": + this.SCHEMA_DESIGN = Inst2XsdOptions.DESIGN_RUSSIAN_DOLL; break; - case "--input": - pathToFile = args.length > 1 ? args[i + 1]:""; + case "2": + this.SCHEMA_DESIGN = Inst2XsdOptions.DESIGN_SALAMI_SLICE; break; - case "-h": - setPrintHelp(true); + case "3": + this.SCHEMA_DESIGN = Inst2XsdOptions.DESIGN_VENETIAN_BLIND; break; - case "--help": - setPrintHelp(true); - break; - case "-nm": - //name space - break; - case "--namespace": - // name space - break; - case "-s": - //schema - break; - case "--schema": - // schema - break; - } - - if (printHelp) { - break; + default: + throw new IllegalArgumentException("Unknown value for design type."); } } - - if (pathToFile.isEmpty()) { - return; - } - - sourceFile = new File(pathToFile);*/ - - } - - private boolean getPrintHelp() { - return printHelp; - } - - public File getSourceFile() { - return sourceFile; - } - - private void setPrintHelp(boolean printHelp) { - this.printHelp = printHelp; } private void checkSourceFile() throws FileNotFoundException { - Path path = Paths.get(sourceFile.toURI()); + Path path = Paths.get(SOURCE_FILE.toURI()); if (Files.notExists(path)) { - throw new FileNotFoundException("no such file " + sourceFile.getPath()); + throw new FileNotFoundException("no such file " + SOURCE_FILE.getPath()); } } - - enum SchemaDesign { - RUSSIAN_DOLL, - SALAMI_SLICE, - VENETIAN_BLIND - } } diff --git a/примерсхемыкакматрешка.xsd b/примерсхемыкакматрешка.xsd deleted file mode 100644 index 02e1e33..0000000 --- a/примерсхемыкакматрешка.xsd +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file