1
0
mirror of https://github.com/fr13Dev/generatingXSD synced 2024-12-22 11:03:13 +00:00

moved to mvn

This commit is contained in:
nikolaibeliaev 2019-03-20 15:27:00 +07:00
parent 6ae76859f9
commit 00c4bda5f5
2 changed files with 36 additions and 123 deletions

36
pom.xml Normal file
View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>dev.fr13</groupId>
<artifactId>xml2xsd</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>3.0.2</version>
</dependency>
</dependencies>
</project>

View File

@ -1,123 +0,0 @@
package dev.fr13;
import org.apache.commons.cli.*;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.impl.inst2xsd.Inst2Xsd;
import org.apache.xmlbeans.impl.inst2xsd.Inst2XsdOptions;
import org.apache.xmlbeans.impl.xb.xsdschema.SchemaDocument;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class XsdSchema {
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 {
XsdSchema xsdSchema = new XsdSchema(args);
xsdSchema.checkSourceFile();
XmlObject xmlObject = XmlObject.Factory.parse(xsdSchema.SOURCE_FILE);
SchemaDocument schema = xsdSchema.getSchema(xmlObject);
if (schema != null)
schema.save(xsdSchema.OUTPUT_FILE);
}
private SchemaDocument getSchema(XmlObject xmlObject) {
Inst2XsdOptions inst2XsdOptions = new Inst2XsdOptions();
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;
}
private XsdSchema(String[] args) {
Options options = new Options();
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();
CommandLine cmd = null;
try {
cmd = commandLineParser.parse(options, args);
} catch (ParseException e) {
System.out.println(e.getMessage());
helpFormatter.printHelp("This tool creates xsd file from xml.", options);
System.exit(1);
}
SOURCE_FILE = new File(cmd.getOptionValue("input"));
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);
}
String ns = cmd.getOptionValue("name-space");
NAME_SPACE = ns != null ? ns : "http://v8.default.com";
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 "2":
this.SCHEMA_DESIGN = Inst2XsdOptions.DESIGN_SALAMI_SLICE;
break;
case "3":
this.SCHEMA_DESIGN = Inst2XsdOptions.DESIGN_VENETIAN_BLIND;
break;
default:
throw new IllegalArgumentException("Unknown value for design type.");
}
}
}
private void checkSourceFile() throws FileNotFoundException {
Path path = Paths.get(SOURCE_FILE.toURI());
if (Files.notExists(path)) {
throw new FileNotFoundException("no such file " + SOURCE_FILE.getPath());
}
}
}