//Copyright (c) 2007 University of Auckland
//Contributed to the FBench extension of the OOONEIDA Workbench project under the Common Public License
/*
* Created on 19/06/2006
*/
package fbench;
import org.jdom.DocType;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import java.io.File;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.StringReader;
import java.util.List;
import org.jdom.transform.JDOMResult;
import org.jdom.transform.JDOMSource;
import org.jdom.transform.XSLTransformException;
import org.jdom.transform.XSLTransformer;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
public class ParserLib {
public static XMLOutputter xmlOutputter = new XMLOutputter(Format
.getPrettyFormat());
// Default constructor, currently does nothing
public ParserLib() {
}
public ParserLib(String inputXMLFile, String outputXMLFile,
String xsltFile, boolean validation) {
}
private final static SAXBuilder getSaxBuilder(boolean validation) {
SAXBuilder saxBuilder = new SAXBuilder(
"org.apache.xerces.parsers.SAXParser", validation);
saxBuilder.setFeature("http://xml.org/sax/features/validation",
validation);
// set shcema support
saxBuilder
.setFeature("http://apache.org/xml/features/validation/schema",
validation);
return saxBuilder;
}
// Parse the input XML file with/without validation and output the Document
// object
public final static Document parser(String inputXMLFile, boolean validation)
throws IOException, JDOMException {
File inputXML = new File(inputXMLFile);
if(!inputXML.exists()) {
System.err.println("Specified not exists");
return null;
}
else {
return getSaxBuilder(validation).build(inputXMLFile);
}
}
public final static Document parser(StringReader inputXML,
boolean validation) throws IOException, JDOMException {
return getSaxBuilder(validation).build(inputXML);
}
public final static Document parser(File inputXML, boolean validation)
throws IOException, JDOMException {
return getSaxBuilder(validation).build(inputXML);
}
public final static Document validator(Document doc) throws JDOMException,
IOException {
return getSaxBuilder(true).build(
new StringReader(xmlOutputter
.outputString(doc)));
// return saxBuilder.build(new
// StringReader(xmlOutputter.outputString(doc)));
}
public final static Document validator(String input) throws JDOMException,
IOException {
return getSaxBuilder(true).build(new StringReader(input));
}
public final static Document validator(StringReader strReader)
throws JDOMException, IOException {
return getSaxBuilder(true).build(strReader);
}
// Write the in-memory parsed XML file to an external file
public final static void writeToFile(String outputXMLFile, Document doc)
throws IOException {
FileOutputStream outStream = new FileOutputStream(outputXMLFile);
xmlOutputter.output(doc, outStream);
outStream.flush();
outStream.close();
}
// Write the in-memory parsed XML file to an external file
public final static void writeToFile(String outputXMLFile, String xmlString, boolean export)
throws IOException, JDOMException {
// Do not validate on export
Document doc = ParserLib .parser(new StringReader(xmlString), !export);
if(export){
doc.setDocType(new DocType("NetConditionEventSystem"));
writeToFile(outputXMLFile, doc);
}
else{
writeToFile(outputXMLFile, doc);
}
}
// Print out the parsed XML file to the standard output
public final static void writeToScreen(Document doc) {
try {
xmlOutputter.output(doc, System.out);
System.out.println();
}
catch(IOException e) {
System.out.println(e.getMessage());
System.exit(0);
}
}
public final static void writeToScreen(Element elem) {
try {
xmlOutputter.output(elem, System.out);
System.out.println();
}
catch(IOException e) {
System.out.println(e.getMessage());
System.exit(0);
}
}
// Transform the given Document object according to the specified XSLT file
// This function utilises the JDOMResult and JDOMSource objects
public final static Document transformer(String xsltFile, Document inputXML) {
Document outputXML = null;
StreamSource streamSrc = new StreamSource(xsltFile);
TransformerFactory tFactory = TransformerFactory.newInstance();
try {
Transformer transformer = tFactory.newTransformer(streamSrc);
JDOMResult jdomRes = new JDOMResult();
JDOMSource jdomSrc = new JDOMSource(inputXML);
transformer.transform(jdomSrc, jdomRes);
outputXML = jdomRes.getDocument();
}
catch(TransformerConfigurationException e) {
e.printStackTrace();
System.exit(0);
}
catch(TransformerException e) {
e.printStackTrace();
System.exit(0);
}
return outputXML;
}
// Alternative approach to last funciton
public final static Document simpleTransformer(String xsltFile,
Document inputXML) {
Document outputXML = null;
XSLTransformer transformer;
try {
transformer = new XSLTransformer(xsltFile);
outputXML = transformer.transform(inputXML);
}
catch(XSLTransformException e) {
e.printStackTrace();
}
return outputXML;
}
// This function searches for the elements in the "inputDoc" with the name
// "nodeName" and type "nodeType"
public final static List search(String nodeName, Object nodeType,
Document inputDoc) {
List searchResult = null;
return searchResult;
}
}