/*-- Copyright 2001-2003 Elliotte Rusty Harold. All rights reserved. This file is part of XIncluder, a Java class library for integrating XInclude processing with SAX, DOM, and JDOM. XIncluder is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. XIncluder 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 Lesser General Public License for more details.t You should have received a copy of the GNU Lesser General Public License along with XIncluder; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ELLIOTTE RUSTY HAROLD OR ANY OTHER CONTRIBUTORS TO THIS PACKAGE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package com.elharo.xml.xinclude; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.ContentHandler; import org.xml.sax.EntityResolver; import org.xml.sax.helpers.XMLReaderFactory; import org.xml.sax.XMLReader; import org.xml.sax.Locator; import org.xml.sax.Attributes; import org.xml.sax.ext.LexicalHandler; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.io.OutputStream; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.File; import java.net.URL; import java.net.MalformedURLException; import java.util.Stack; /** *
SAXXIncluder
is a simple ContentHandler
* that writes its XML document onto an output stream after resolving
* all xinclude:include
elements.
*
* The only current known bug is that the notation and * unparsed entity information items are not included * in the result infoset. Furthermore, processing * instructions in the DTD are not included. Note that this is * only relevant to the source infoset. The DOCTYPE declaration * is specifically excluded from included infosets. *
* ** I also need to check how section 4.4.3.1 applies for inscope * namespaces in included documents. Currently this is not an issue * because I only include full documents, but it may become an * an issue when XPointer support is added. *
* ** There's no XPointer support yet. Only full documents are * included. *
** The xinclude:fallback element is not yet supported. *
*
* The parser used to drive this must support the LexicalHandler
* interface. It must also provide a Locator
object.
* These are optional in SAX, but Xerces-J does support these features.
*
* The driver method for the SAXXIncluder program. * Output is written to System.out. *
* * @param args contains the URLs and/or filenames * of the documents to be procesed. */ public static void main(String[] args) { // make this more robust XMLReader parser; try { parser = XMLReaderFactory.createXMLReader(); } catch (SAXException e) { try { parser = XMLReaderFactory.createXMLReader( "org.apache.xerces.parsers.SAXParser"); } catch (SAXException e2) { System.err.println("Could not find an XML parser"); return; } } // Need better namespace handling try { parser.setFeature("http://xml.org/sax/features/namespace-prefixes", true); } catch (SAXException e) { System.err.println(e); return; } if (args.length == 0) return; EntityResolver resolver = null; int arg = 0; if (args[0].equals("-r")) { try { resolver = (EntityResolver) Class.forName(args[1]).newInstance(); parser.setEntityResolver(resolver); } catch (Exception ex) { System.err.println("Could not load requested EntityResolver"); return; } arg = 2; } for (; arg < args.length; arg++) { try { /* URL base; try { base = new URL(args[i]); } catch (MalformedURLException e) { File f = new File(args[i]); base = f.toURL(); } */ XIncludeFilter includer = new XIncludeFilter(); includer.setParent(parser); SAXXIncluder s = new SAXXIncluder(System.out); includer.setContentHandler(s); if (resolver != null) includer.setEntityResolver(resolver); try { includer.setProperty( "http://xml.org/sax/properties/lexical-handler", s); s.setFilter(includer); } catch (SAXException e) { // Will not support comments } includer.parse(args[arg]); } catch (SAXParseException e) { System.err.println(e); System.err.println("Problem in " + e.getSystemId() + " at line " + e.getLineNumber()); } catch (Exception e) { // be specific about exceptions???? System.err.println(e); e.printStackTrace(); } } } }