/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2005 Danet GmbH (www.danet.de), BU-TEL.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: IncludeXML.java 2326 2007-03-27 21:59:44Z mlipp $
*
* $Log$
* Revision 1.3 2006/09/29 12:32:10 drmlipp
* Consistently using WfMOpen as projct name now.
*
* Revision 1.2 2005/04/28 11:14:51 drmlipp
* Added filter option.
*
* Revision 1.1 2005/04/27 09:25:17 drmlipp
* Added utility for including XML in process description.
*
*/
package de.danet.an.workflow.util;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.AttributesImpl;
import org.xml.sax.helpers.XMLFilterImpl;
/**
* This class provides ...
*
* @author <a href="mailto:lipp@danet.de">unknown</a>
* @version $Revision: 2326 $
*/
public class IncludeXML {
private class IncludeHandler extends XMLFilterImpl {
/**
* @see org.xml.sax.ContentHandler#characters
*/
public void characters(char[] chars, int start, int end)
throws SAXException {
String text = new String (chars, start, end);
if (text.startsWith("%include(")
&& text.indexOf(')') == text.length() - 1) {
String fn = text.substring(9, text.length() - 1);
System.out.println("Including " + fn + " ...");
String tbi = null;
try {
tbi = xmlFileAsString (fn);
} catch (Exception e) {
System.out.println("Cannot include " + fn + ": "
+ e.getMessage ());
throw new SAXException (e);
}
super.characters(tbi.toCharArray(), 0,
tbi.toCharArray().length);
return;
}
super.characters(chars, start, end);
}
}
private class IncludeFilter extends XMLFilterImpl {
/**
* @see org.xml.sax.ContentHandler#startElement
*/
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts)
throws SAXException {
if (vxmlXsiFilter
&& namespaceURI.equals("http://www.w3.org/2001/vxml")
&& localName.equals("vxml")) {
AttributesImpl a = new AttributesImpl (atts);
for (int i = 0; i < a.getLength(); i++) {
if (a.getQName(i).startsWith("xmlns:")
&& (a.getValue(i).equals
("http://www.w3.org/2001/XMLSchema-instance"))) {
a.removeAttribute(i);
i -= 1;
continue;
}
if (a.getURI(i).equals
("http://www.w3.org/2001/XMLSchema-instance")) {
a.removeAttribute(i);
i -= 1;
continue;
}
}
atts = a;
}
super.startElement(namespaceURI, localName, qName, atts);
}
}
private String inFileName = null;
private String outFileName = null;
private boolean vxmlXsiFilter = false;
private SAXParserFactory spf = SAXParserFactory.newInstance ();
private SAXTransformerFactory stf = (SAXTransformerFactory)
TransformerFactory.newInstance ();
private String xmlFileAsString (String fileName)
throws IOException, TransformerConfigurationException,
ParserConfigurationException, SAXException {
TransformerHandler serializer = stf.newTransformerHandler();
serializer.getTransformer().setOutputProperty(OutputKeys.INDENT, "no");
serializer.getTransformer().setOutputProperty(
OutputKeys.OMIT_XML_DECLARATION, "yes");
StringWriter res = new StringWriter();
serializer.setResult(new StreamResult(res));
SAXParser sp = spf.newSAXParser();
XMLReader reader = sp.getXMLReader();
IncludeFilter incf = new IncludeFilter ();
incf.setContentHandler(serializer);
incf.setDTDHandler(serializer);
reader.setContentHandler(incf);
reader.setDTDHandler(incf);
reader.setFeature("http://xml.org/sax/features/namespaces", true);
reader.parse(new InputSource(new FileInputStream(fileName)));
return res.toString();
}
private void parseArgs (String[] args) {
for (int i = 0; i < args.length; i++) {
if (args[i].startsWith("-")) {
if (args[i].equals("-vxmlXsiFilter")) {
vxmlXsiFilter = true;
}
} else {
if (inFileName == null) {
inFileName = args[i];
} else if (outFileName == null) {
outFileName = args[i];
}
}
}
}
public void run (String[] args) throws Exception {
parseArgs (args);
// prepare output transformer
TransformerHandler serializer = stf.newTransformerHandler();
serializer.getTransformer()
.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1");
serializer.getTransformer()
.setOutputProperty(OutputKeys.INDENT,"yes");
serializer.setResult
(new StreamResult(new FileOutputStream(outFileName)));
IncludeHandler ih = new IncludeHandler ();
ih.setContentHandler(serializer);
ih.setDTDHandler(serializer);
SAXParser sp = spf.newSAXParser ();
XMLReader reader = sp.getXMLReader();
reader.setContentHandler(ih);
reader.setDTDHandler(ih);
reader.parse (new InputSource(new FileInputStream(inFileName)));
// var tf = Packages.javax.xml.transform.TransformerFactory.newInstance();
// var t = tf.newTransformer();
// var xmlString = new java.io.StringWriter();
// t.transform(new Packages.javax.xml.transform.stream.StreamSource(inFile),
// new Packages.javax.xml.transform.stream.StreamResult(xmlString));
}
public static void main (String[] args) throws Exception {
try {
(new IncludeXML()).run (args);
} catch (Exception e) {
e.printStackTrace();
}
}
}