TestInc.java
@author
package test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.ParsingException;
import nu.xom.Serializer;
import nu.xom.ValidityException;
import nu.xom.xinclude.BadParseAttributeException;
import nu.xom.xinclude.InclusionLoopException;
import nu.xom.xinclude.NoIncludeLocationException;
import nu.xom.xinclude.XIncludeException;
import nu.xom.xinclude.XIncluder;
public class TestInc {
Logger log = Logger.getLogger("TestInc");
void processInclude(String filein, String fileout){
Builder builder = new Builder(false);
log.log(Level.INFO,"process "+filein+" -> "+fileout);
try {
URL url = new File(filein).toURL();
log.log(Level.INFO, "Load Input Document ["+url+"]");
Document doc = builder.build(url.toString());
log.log(Level.INFO, "Resolve XInclude statement");
Document result = XIncluder.resolve(doc);
OutputStream os = System.out;
if (fileout!=null) {
log.log(Level.INFO, "Write output file ["+fileout+"]");
os = new FileOutputStream(fileout);
}
Serializer ser = new Serializer(os);
ser.write(result);
} catch (BadParseAttributeException e) {
log.log(Level.SEVERE, "BadParseAttributeException : "+e.getMessage());
} catch (InclusionLoopException e) {
log.log(Level.SEVERE, "InclusionLoopException : "+e.getMessage());
} catch (NoIncludeLocationException e) {
log.log(Level.SEVERE, "NoIncludeLocationException : "+e.getMessage());
} catch (XIncludeException e) {
log.log(Level.SEVERE, "XIncludeException : "+e.getMessage());
} catch (ValidityException e) {
log.log(Level.SEVERE, "ValidityException : "+e.getMessage());
} catch (ParsingException e) {
log.log(Level.SEVERE, "ParsingException : "+e.getMessage());
} catch (IOException e) {
log.log(Level.SEVERE, "IOException : "+e.getMessage());
}
}
public static void main(String[] args) {
CommandLineParser parser = new GnuParser();
Options options = new Options();
Option infile = OptionBuilder.withArgName("input").hasArg()
.withDescription("XML input file" ).create("input");
Option outfile = OptionBuilder.withArgName("output").hasArg()
.withDescription("Result document after XML inclusion" ).create("output");
options.addOption(infile);
options.addOption(outfile);
try {
CommandLine line = parser.parse( options, args );
if( line.hasOption("input") ) {
TestInc ti=new TestInc();
ti.processInclude(line.getOptionValue("input"), line.getOptionValue("output"));
}
}
catch( ParseException exp ) {
Logger.getLogger("TestInc").log(Level.SEVERE, "Command Line ParseException : "+exp.getMessage());
}
}
}