
schema.xsd2pgschema.4.3.4.source-code.xmlsplitter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xsd2pgschema Show documentation
Show all versions of xsd2pgschema Show documentation
Database replication tool based on XML Schema
/*
xsd2pgschema - Database replication tool based on XML Schema
Copyright 2017-2022 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.implement.XmlSplitterImpl;
import net.sf.xsd2pgschema.option.*;
import net.sf.xsd2pgschema.serverutil.*;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.lang.invoke.MethodHandles;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.NoSuchAlgorithmException;
import java.util.HashSet;
import java.util.concurrent.LinkedBlockingQueue;
import javax.xml.parsers.ParserConfigurationException;
import org.nustaq.serialization.FSTConfiguration;
import org.xml.sax.SAXException;
import com.github.antlr.grammars_v4.xpath.xpathListenerException;
/**
* Split large XML file into small ones based on XPath query.
*
* @author yokochi
*/
public class xmlsplitter {
/** The XML directory name contains split XML files. */
private static String xml_dir_name = "xml_work";
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
/** The PostgreSQL data model option. */
PgSchemaOption option = new PgSchemaOption(false);
option.stdout_msg = false;
/** The FST configuration. */
FSTConfiguration fst_conf = FSTConfiguration.createDefaultConfiguration();
fst_conf.registerClass(PgSchemaServerQuery.class,PgSchemaServerReply.class,PgSchema.class); // FST optimization
/** The XML file filter. */
XmlFileFilter xml_file_filter = new XmlFileFilter();
/** The source XML file queue. */
LinkedBlockingQueue xml_file_queue;
/** The target XML file patterns. */
HashSet xml_file_names = new HashSet();
/** The XPath expression pointing document key. */
String xpath_doc_key = "";
/** The shard size. */
int shard_size = 1;
boolean touch_xml = false;
for (int i = 0; i < args.length; i++) {
if (args[i].startsWith("--"))
touch_xml = false;
if (args[i].equals("--xsd") && i + 1 < args.length)
option.root_schema_location = args[++i];
else if (args[i].equals("--xml") && i + 1 < args.length) {
String xml_file_name = args[++i];
if (xml_file_name.isEmpty()) {
System.err.println("XML file name is empty.");
showUsage();
}
xml_file_names.add(xml_file_name);
touch_xml = true;
}
else if (args[i].equals("--xml-file-ext") && i + 1 < args.length) {
if (!xml_file_filter.setExt(args[++i]))
showUsage();
}
else if (args[i].equals("--xml-dir") && i + 1 < args.length)
xml_dir_name = args[++i];
else if (args[i].equals("--xpath-doc-key") && i + 1 < args.length)
xpath_doc_key = args[++i];
else if (args[i].equals("--no-wild-card"))
option.wild_card = false;
else if (args[i].equals("--pg-public-schema"))
option.pg_named_schema = false;
else if (args[i].equals("--pg-named-schema"))
option.pg_named_schema = true;
else if (args[i].equals("--no-pgschema-serv"))
option.pg_schema_server = false;
else if (args[i].equals("--pgschema-serv-host") && i + 1 < args.length)
option.pg_schema_server_host = args[++i];
else if (args[i].equals("--pgschema-serv-port") && i + 1 < args.length)
option.pg_schema_server_port = Integer.valueOf(args[++i]);
else if (args[i].equals("--no-cache-xsd"))
option.cache_xsd = false;
else if (args[i].equals("--verbose"))
option.verbose = true;
else if (args[i].equals("--shard-size") && i + 1 < args.length) {
shard_size = Integer.valueOf(args[++i]);
if (shard_size <= 0) {
System.err.println("Out of range (shard-size).");
showUsage();
}
}
else if (touch_xml) {
String xml_file_name = args[i];
if (xml_file_name.isEmpty()) {
System.err.println("XML file name is empty.");
showUsage();
}
xml_file_names.add(xml_file_name);
}
else {
System.err.println("Illegal option: " + args[i] + ".");
showUsage();
}
}
if (option.root_schema_location.isEmpty()) {
System.err.println("XSD schema location is empty.");
showUsage();
}
PgSchemaClientType client_type = PgSchemaClientType.xpath_evaluation;
InputStream is = null;
boolean server_alive = option.pingPgSchemaServer(fst_conf);
boolean no_data_model = server_alive ? !option.matchPgSchemaServer(fst_conf, client_type) : true;
if (no_data_model) {
is = PgSchemaUtil.getSchemaInputStream(option.root_schema_location, null, false);
if (is == null)
showUsage();
}
if (xml_file_names.size() == 0) {
System.err.println("XML file name is empty.");
showUsage();
}
FilenameFilter filename_filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(xml_file_filter.getAbsoluteExt());
}
};
xml_file_queue = PgSchemaUtil.getQueueOfTargetFiles(xml_file_names, filename_filter, false);
if (xml_file_queue.size() == 0) {
System.err.println("Not found target XML files (--xml=\"" + String.join(",", xml_file_names) + "\", --xml-file-ext=\"" + xml_file_filter.ext + "\").");
showUsage();
}
xml_file_names.clear();
Path xml_dir_path = Paths.get(xml_dir_name);
if (!Files.isDirectory(xml_dir_path)) {
try {
Files.createDirectory(xml_dir_path);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
try {
String original_caller = MethodHandles.lookup().lookupClass().getName();
XmlSplitterImpl splitter = new XmlSplitterImpl(shard_size, is, xml_dir_path, xml_file_queue, option, fst_conf, xpath_doc_key);
splitter.exec();
if (splitter.updated)
option.updatePgSchemaServer(fst_conf, splitter.client.schema, client_type, original_caller);
} catch (IOException | NoSuchAlgorithmException | ParserConfigurationException | SAXException | PgSchemaException | xpathListenerException e) {
e.printStackTrace();
System.exit(1);
}
}
/**
* Show usage.
*/
private static void showUsage() {
String[] usage = {
"xmlsplitter: Split large XML file into small ones based on XPath query",
"Usage: --xsd SCHEMA_LOCATION --xml SRC_XML_FILE_OR_DIRECTORY --xml-dir DST_DIRECTORY (default=\"" + xml_dir_name + "\")",
" --xml-file-ext SRC_FILE_EXTENSION [xml (default) | gz (indicates xml.gz suffix) | zip (indicates xml.zip suffix)]",
" --xpath-doc-key XPATH_EXPR_FOR_DOC_KEY",
" --no-wild-card (turn off wild card extension)",
" --shard-size SHARD_SIZE (default=1)",
"Option: --pg-public-schema (utilize \"public\" schema, default)",
" --pg-named-schema (enable explicit named schema)",
" --no-cache-xsd (retrieve XML Schemata without caching)",
" --no-pgschema-serv (not utilize PgSchema server)",
" --pgschema-serv-host PG_SCHEMA_SERV_HOST_NAME (default=\"" + PgSchemaUtil.pg_schema_server_host + "\")",
" --pgschema-serv-port PG_SCHEMA_SERV_PORT_NUMBER (default=" + PgSchemaUtil.pg_schema_server_port + ")",
" --verbose (verbose mode)"
};
for (String s : usage)
System.err.println(s);
System.exit(1);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy