001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2005 Danet GmbH (www.danet.de), BU-TEL.
004: * All rights reserved.
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * $Id: IncludeXML.java,v 1.4 2007/03/27 21:59:44 mlipp Exp $
021: *
022: * $Log: IncludeXML.java,v $
023: * Revision 1.4 2007/03/27 21:59:44 mlipp
024: * Fixed lots of checkstyle warnings.
025: *
026: * Revision 1.3 2006/09/29 12:32:10 drmlipp
027: * Consistently using WfMOpen as projct name now.
028: *
029: * Revision 1.2 2005/04/28 11:14:51 drmlipp
030: * Added filter option.
031: *
032: * Revision 1.1 2005/04/27 09:25:17 drmlipp
033: * Added utility for including XML in process description.
034: *
035: */
036: package de.danet.an.workflow.util;
037:
038: import java.io.FileInputStream;
039: import java.io.FileOutputStream;
040: import java.io.IOException;
041: import java.io.StringWriter;
042:
043: import javax.xml.parsers.ParserConfigurationException;
044: import javax.xml.parsers.SAXParser;
045: import javax.xml.parsers.SAXParserFactory;
046: import javax.xml.transform.OutputKeys;
047: import javax.xml.transform.TransformerConfigurationException;
048: import javax.xml.transform.TransformerFactory;
049: import javax.xml.transform.sax.SAXTransformerFactory;
050: import javax.xml.transform.sax.TransformerHandler;
051: import javax.xml.transform.stream.StreamResult;
052:
053: import org.xml.sax.Attributes;
054: import org.xml.sax.InputSource;
055: import org.xml.sax.SAXException;
056: import org.xml.sax.XMLReader;
057: import org.xml.sax.helpers.AttributesImpl;
058: import org.xml.sax.helpers.XMLFilterImpl;
059:
060: /**
061: * This class provides ...
062: *
063: * @author <a href="mailto:lipp@danet.de">unknown</a>
064: * @version $Revision: 1.4 $
065: */
066:
067: public class IncludeXML {
068:
069: private class IncludeHandler extends XMLFilterImpl {
070:
071: /**
072: * @see org.xml.sax.ContentHandler#characters
073: */
074: public void characters(char[] chars, int start, int end)
075: throws SAXException {
076: String text = new String(chars, start, end);
077: if (text.startsWith("%include(")
078: && text.indexOf(')') == text.length() - 1) {
079: String fn = text.substring(9, text.length() - 1);
080: System.out.println("Including " + fn + " ...");
081: String tbi = null;
082: try {
083: tbi = xmlFileAsString(fn);
084: } catch (Exception e) {
085: System.out.println("Cannot include " + fn + ": "
086: + e.getMessage());
087: throw new SAXException(e);
088: }
089: super .characters(tbi.toCharArray(), 0, tbi
090: .toCharArray().length);
091: return;
092: }
093: super .characters(chars, start, end);
094: }
095: }
096:
097: private class IncludeFilter extends XMLFilterImpl {
098:
099: /**
100: * @see org.xml.sax.ContentHandler#startElement
101: */
102: public void startElement(String namespaceURI, String localName,
103: String qName, Attributes atts) throws SAXException {
104: if (vxmlXsiFilter
105: && namespaceURI
106: .equals("http://www.w3.org/2001/vxml")
107: && localName.equals("vxml")) {
108: AttributesImpl a = new AttributesImpl(atts);
109: for (int i = 0; i < a.getLength(); i++) {
110: if (a.getQName(i).startsWith("xmlns:")
111: && (a.getValue(i)
112: .equals("http://www.w3.org/2001/XMLSchema-instance"))) {
113: a.removeAttribute(i);
114: i -= 1;
115: continue;
116: }
117: if (a
118: .getURI(i)
119: .equals(
120: "http://www.w3.org/2001/XMLSchema-instance")) {
121: a.removeAttribute(i);
122: i -= 1;
123: continue;
124: }
125: }
126: atts = a;
127: }
128: super .startElement(namespaceURI, localName, qName, atts);
129: }
130: }
131:
132: private String inFileName = null;
133: private String outFileName = null;
134: private boolean vxmlXsiFilter = false;
135:
136: private SAXParserFactory spf = SAXParserFactory.newInstance();
137: private SAXTransformerFactory stf = (SAXTransformerFactory) TransformerFactory
138: .newInstance();
139:
140: private String xmlFileAsString(String fileName) throws IOException,
141: TransformerConfigurationException,
142: ParserConfigurationException, SAXException {
143: TransformerHandler serializer = stf.newTransformerHandler();
144: serializer.getTransformer().setOutputProperty(
145: OutputKeys.INDENT, "no");
146: serializer.getTransformer().setOutputProperty(
147: OutputKeys.OMIT_XML_DECLARATION, "yes");
148: StringWriter res = new StringWriter();
149: serializer.setResult(new StreamResult(res));
150:
151: SAXParser sp = spf.newSAXParser();
152: XMLReader reader = sp.getXMLReader();
153:
154: IncludeFilter incf = new IncludeFilter();
155: incf.setContentHandler(serializer);
156: incf.setDTDHandler(serializer);
157:
158: reader.setContentHandler(incf);
159: reader.setDTDHandler(incf);
160: reader.setFeature("http://xml.org/sax/features/namespaces",
161: true);
162: reader.parse(new InputSource(new FileInputStream(fileName)));
163: return res.toString();
164: }
165:
166: private void parseArgs(String[] args) {
167: for (int i = 0; i < args.length; i++) {
168: if (args[i].startsWith("-")) {
169: if (args[i].equals("-vxmlXsiFilter")) {
170: vxmlXsiFilter = true;
171: }
172: } else {
173: if (inFileName == null) {
174: inFileName = args[i];
175: } else if (outFileName == null) {
176: outFileName = args[i];
177: }
178: }
179: }
180: }
181:
182: public void run(String[] args) throws Exception {
183: parseArgs(args);
184:
185: // prepare output transformer
186: TransformerHandler serializer = stf.newTransformerHandler();
187: serializer.getTransformer().setOutputProperty(
188: OutputKeys.ENCODING, "ISO-8859-1");
189: serializer.getTransformer().setOutputProperty(
190: OutputKeys.INDENT, "yes");
191: serializer.setResult(new StreamResult(new FileOutputStream(
192: outFileName)));
193:
194: IncludeHandler ih = new IncludeHandler();
195: ih.setContentHandler(serializer);
196: ih.setDTDHandler(serializer);
197:
198: SAXParser sp = spf.newSAXParser();
199: XMLReader reader = sp.getXMLReader();
200: reader.setContentHandler(ih);
201: reader.setDTDHandler(ih);
202: reader.parse(new InputSource(new FileInputStream(inFileName)));
203:
204: // var tf = Packages.javax.xml.transform.TransformerFactory.newInstance();
205: // var t = tf.newTransformer();
206: // var xmlString = new java.io.StringWriter();
207: // t.transform(new Packages.javax.xml.transform.stream.StreamSource(inFile),
208: // new Packages.javax.xml.transform.stream.StreamResult(xmlString));
209:
210: }
211:
212: public static void main(String[] args) throws Exception {
213: try {
214: (new IncludeXML()).run(args);
215: } catch (Exception e) {
216: e.printStackTrace();
217: }
218: }
219: }
|