
schema.xsd2pgschema.5.2.5.source-code.xsdvalidator Maven / Gradle / Ivy
/*
xsd2pgschema - Database replication tool based on XML Schema
Copyright 2019-2021 Masashi Yokochi
https://sourceforge.net/projects/xsd2pgschema/
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import net.sf.xsd2pgschema.*;
import net.sf.xsd2pgschema.option.*;
import net.sf.xsd2pgschema.xmlutil.XmlParser;
import net.sf.xsd2pgschema.xmlutil.XmlValidator;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* Validate XML Schema.
*
* @author yokochi
*/
public class xsdvalidator {
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
/** The PostgreSQL data model option. */
PgSchemaOption option = new PgSchemaOption(true);
option.validate = option.full_check = true; // enable XML Schema validation
option.verbose = true;
/** The XML file filter. */
XmlFileFilter xml_file_filter = new XmlFileFilter();
String xsd_for_xsd = PgSchemaUtil.xsd_for_xsd_11_mutable;
for (int i = 0; i < args.length; i++) {
if (args[i].equals("--xsd") && i + 1 < args.length)
option.root_schema_location = args[++i];
else if (args[i].equals("--schema-ver") && i + 1 < args.length) {
switch (args[++i]) {
case "1.1",
"11",
"1.1-mutable",
"11-mutable",
"1.1_mutable",
"11_mutable",
"1.1_2009",
"11_2009",
"2009" -> xsd_for_xsd = PgSchemaUtil.xsd_for_xsd_11_mutable;
case "1.1-immutable",
"11-immutable",
"1.1_immutable",
"11_immutable",
"1.1_2012_04",
"11_2012_04",
"2012_04",
"1.1_2012-04",
"11_2012-04",
"2012-04",
"1.1_2012",
"11_2012",
"2012" -> xsd_for_xsd = PgSchemaUtil.xsd_for_xsd_11_immutable;
case "1.0",
"10",
"1",
"1.0_2001",
"10_2001",
"1_2001",
"2001" -> xsd_for_xsd = PgSchemaUtil.xsd_for_xsd_10;
default -> showUsage();
}
}
else {
System.err.println("Illegal option: " + args[i] + ".");
showUsage();
}
}
if (option.root_schema_location.isEmpty()) {
System.err.println("XSD schema location is empty.");
showUsage();
}
if (PgSchemaUtil.getSchemaFileName(option.root_schema_location).equals(PgSchemaUtil.xsd_for_xsd_name)) {
System.err.println("Avoid XSD file name: " + PgSchemaUtil.xsd_for_xsd_name);
System.exit(1);
}
Path xsd_for_xsd_path = Paths.get(PgSchemaUtil.xsd_for_xsd_name);
if (Files.exists(xsd_for_xsd_path)) {
try {
Files.delete(xsd_for_xsd_path);
} catch (IOException e) {
e.printStackTrace();
}
}
try {
XmlValidator validator = new XmlValidator(PgSchemaUtil.getSchemaFilePath(xsd_for_xsd, null, false), option.full_check);
new XmlParser(validator, Paths.get(option.root_schema_location), xml_file_filter, option);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Show usage.
*/
private static void showUsage() {
String[] usage = {
"xsdvalidator: Validate XML Schema",
"Usage: --xsd SCHEMA_LOCATION --xml XML_FILE_OR_DIRECTORY",
"Option: --schema-ver XML_SCHEMA_VER (choose from \"1.1-mutable\" (default), \"1.1-immutable\", or \"1.0\")"
};
for (String s : usage)
System.err.println(s);
System.exit(1);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy