Skip to content

Instantly share code, notes, and snippets.

@danizen
Created January 19, 2018 17:29
Show Gist options
  • Save danizen/17860a5fabd11739d7e4ccb32364e8cc to your computer and use it in GitHub Desktop.
Save danizen/17860a5fabd11739d7e4ccb32364e8cc to your computer and use it in GitHub Desktop.
ConfigurationDumper example
package gov.nih.nlm.occs.norconex.util;
import static com.norconex.collector.core.AbstractCollectorLauncher.ARG_CONFIG;
import static com.norconex.collector.core.AbstractCollectorLauncher.ARG_VARIABLES;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringReader;
import java.util.regex.Pattern;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import com.norconex.commons.lang.config.ConfigurationException;
import com.norconex.commons.lang.config.ConfigurationLoader;
import com.norconex.commons.lang.config.XMLConfigurationUtil;
/**
* Dumps a Velocity template loaded by `com.norconex.commons.long.config.ConfigurationLoader`
*
* @author davisda4
*/
public class ConfigurationDumper {
public static final String ARG_OUTPUT = "output";
public ConfigurationDumper() {
}
public static void main(String[] args) {
ConfigurationDumper dumper = new ConfigurationDumper();
dumper.dump(args);
}
protected Options createCommandLineOptions() {
Options options = new Options();
options.addOption("c", ARG_CONFIG, true, "Required: Collector configuration file.");
options.addOption("v", ARG_VARIABLES, true, "Optional: variable file.");
options.addOption("o", ARG_OUTPUT, true, "Optional: output file.");
return options;
}
protected CommandLine parseCommandLineArguments(String[] args) {
Options options = createCommandLineOptions();
CommandLineParser parser = new DefaultParser();
CommandLine cmd = null;
try {
cmd = parser.parse(options, args);
if (!cmd.hasOption(ARG_CONFIG)) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("<dumper>", options );
System.exit(-1);
}
} catch (ParseException e) {
System.err.println("Could not parse arguments.");
e.printStackTrace(System.err);
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("<dumper>", options );
System.exit(-1);
}
return cmd;
}
public void dump(String[] args) {
CommandLine cmd = parseCommandLineArguments(args);
File configFile = new File(cmd.getOptionValue(ARG_CONFIG));
File varsFile = null;
if (cmd.hasOption(ARG_VARIABLES)) {
varsFile = new File(cmd.getOptionValue(ARG_VARIABLES));
}
File outputFile = null;
if (cmd.hasOption(ARG_OUTPUT)) {
outputFile = new File(cmd.getOptionValue(ARG_OUTPUT));
}
OutputStream outputStream = System.out;
// Validate arguments
if (!configFile.isFile()) {
System.err.println("Invalid configuration file path: "
+ configFile.getAbsolutePath());
System.exit(-1);
}
if (varsFile != null && !varsFile.isFile()) {
System.err.println("Invalid variable file path: "
+ varsFile.getAbsolutePath());
System.exit(-1);
}
String xmlString = null;
// Load with velocity template engine
try {
ConfigurationLoader configLoader = new ConfigurationLoader();
xmlString = configLoader.loadString(configFile, varsFile);
xmlString = Pattern.compile("((?!^)<\\?xml.*?\\?>|<\\!DOCTYPE.*?>)",
Pattern.MULTILINE).matcher(xmlString).replaceAll("");
} catch (ConfigurationException e) {
String msg = String.format("config %s, vars %s: unable to render configuration to string", configFile, varsFile);
System.err.println(msg);
e.printStackTrace();
}
// Write to system output or output file
try {
if (outputFile != null) {
outputStream = new FileOutputStream(outputFile);
if (!outputFile.exists()) {
outputFile.createNewFile();
}
}
outputStream.write(xmlString.getBytes());
}
catch (IOException ioe) {
String msg = String.format("%s: unable to write to file", outputFile);
System.err.println(msg);
ioe.printStackTrace();
}
if (outputStream != System.out) {
try { outputStream.flush(); } catch (IOException e) { }
try { outputStream.close(); } catch (IOException e) { }
}
// Parse as XML
try {
XMLConfigurationUtil.newXMLConfiguration(new StringReader(xmlString));
}
catch (ConfigurationException e) {
String msg = String.format("config %s, vars %s: unable to parse as XML", configFile, varsFile);
System.err.println(msg);
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment