From 9000109e9e5d6440bff4aaea613bc4f965720695 Mon Sep 17 00:00:00 2001 From: Peter Bernard West Date: Thu, 15 Jan 2004 02:00:20 +0000 Subject: [PATCH] FoXMLSerialHandler renamed to FoXmlSerialHandler git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/FOP_0-20-0_Alt-Design@197190 13f79535-47bb-0310-9956-ffa450edef68 --- src/java/org/apache/fop/apps/Driver.java | 10 +- .../apache/fop/xml/FoXmlSerialHandler.java | 290 ++++++++++++++++++ 2 files changed, 295 insertions(+), 5 deletions(-) create mode 100644 src/java/org/apache/fop/xml/FoXmlSerialHandler.java diff --git a/src/java/org/apache/fop/apps/Driver.java b/src/java/org/apache/fop/apps/Driver.java index 1c56b161d..02c0794bc 100644 --- a/src/java/org/apache/fop/apps/Driver.java +++ b/src/java/org/apache/fop/apps/Driver.java @@ -62,7 +62,7 @@ import org.apache.fop.fo.FOTree; import org.apache.fop.layout.AreaTree; import org.apache.fop.messaging.MessageHandler; import org.apache.fop.version.Version; -import org.apache.fop.xml.FoXMLSerialHandler; +import org.apache.fop.xml.FoXmlSerialHandler; import org.apache.fop.xml.SyncedXmlEventsBuffer; /** @@ -80,7 +80,7 @@ public class Driver { private XMLReader parser; private InputSource source; - private FoXMLSerialHandler xmlhandler; + private FoXmlSerialHandler xmlhandler; private SyncedXmlEventsBuffer xmlevents; private FOTree foTree; private AreaTree areaTree = new AreaTree(); @@ -108,13 +108,13 @@ public class Driver { * the input source, the parser, the * {@link org.apache.fop.xml.SyncedXmlEventsBuffer SyncedXmlEventsBuffer} * (xmlevents), the - * {@link org.apache.fop.xml.FoXMLSerialHandler FoXMLSerialHandler} + * {@link org.apache.fop.xml.FoXmlSerialHandler FoXmlSerialHandler} * (xmlhandler) and the * {@link org.apache.fop.fo.FOTree FOTree} (foTree). * *

The xmlhandler uses the source and the parser to * generate XML events which it stores in xmlevents. - * FoXMLSerialHandler implements Runnable. + * FoXmlSerialHandler implements Runnable. * *

The foTree reads events from the xmlevents * buffer, which it interprets to build the FO tree. FOTree @@ -139,7 +139,7 @@ public class Driver { //setParserFeatures(parser); xmlevents = new SyncedXmlEventsBuffer(); - xmlhandler = new FoXMLSerialHandler(xmlevents, parser, source); + xmlhandler = new FoXmlSerialHandler(xmlevents, parser, source); foTree = new FOTree(xmlevents); driverThread = Thread.currentThread(); diff --git a/src/java/org/apache/fop/xml/FoXmlSerialHandler.java b/src/java/org/apache/fop/xml/FoXmlSerialHandler.java new file mode 100644 index 000000000..7c6ff2750 --- /dev/null +++ b/src/java/org/apache/fop/xml/FoXmlSerialHandler.java @@ -0,0 +1,290 @@ +/* + * $Id$ + * + * ============================================================================ + * The Apache Software License, Version 1.1 + * ============================================================================ + * + * Copyright (C) 1999-2004 The Apache Software Foundation. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modifica- + * tion, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. The end-user documentation included with the redistribution, if any, must + * include the following acknowledgment: "This product includes software + * developed by the Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, if + * and wherever such third-party acknowledgments normally appear. + * + * 4. The names "FOP" and "Apache Software Foundation" must not be used to + * endorse or promote products derived from this software without prior + * written permission. For written permission, please contact + * apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", nor may + * "Apache" appear in their name, without prior written permission of the + * Apache Software Foundation. + * + * 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 THE + * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- + * DING, 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. + * + * This software consists of voluntary contributions made by many individuals + * on behalf of the Apache Software Foundation and was originally created by + * James Tauber . For more information on the Apache + * Software Foundation, please see . + * + * + * @author Peter B. West + * @version $Revision$ $Name$ + */ +package org.apache.fop.xml; + +import java.util.NoSuchElementException; + +import org.xml.sax.Attributes; +import org.xml.sax.InputSource; +import org.xml.sax.XMLReader; +import org.xml.sax.helpers.AttributesImpl; +import org.xml.sax.helpers.DefaultHandler; + +import org.apache.fop.apps.Driver; +import org.apache.fop.apps.FOPException; +import org.apache.fop.configuration.Configuration; +import org.apache.fop.fo.FObjectNames; +import org.apache.fop.xml.XMLNamespaces; + +/** + * FoXmlSerialHandler is the ContentHandler for the + * background XMLReader thread. + */ + +public class FoXmlSerialHandler extends DefaultHandler implements Runnable { + + private static final String tag = "$Name$"; + private static final String revision = "$Revision$"; + + private SyncedXmlEventsBuffer events; + private XMLReader parser; + private XMLNamespaces namespaces; + private InputSource source; + private Thread foThread; + private boolean errorDump; + + /** + * @param events the events buffer. + * @param parser the xml parser. + * @param source the parser input source. + */ + public FoXmlSerialHandler + (SyncedXmlEventsBuffer events, XMLReader parser, InputSource source) + { + this.events = events; + this.parser = parser; + this.source = source; + namespaces = events.getNamespaces(); + parser.setContentHandler(this); + errorDump = Configuration.getBooleanValue("debugMode").booleanValue(); + } + + public void setFoThread(Thread foThread) { + this.foThread = foThread; + } + + /** + * This is the run method for the callback parser thread. + */ + public void run() { + // I''m in the thread - run the parser + try { + parser.parse(source); + //System.out.println("Parser terminating."); + } catch (Exception e) { + if (errorDump) Driver.dumpError(e); + if (foThread != null) { + try { + foThread.interrupt(); + } catch (Exception ex) {} //ignore + } + throw new RuntimeException(e.getMessage()); + } + } + + /** + * Utility routine for the callback methods. It captures the + * InterruptedException that is possible from the put + * method of a SyncedXmlEventsBuffer. + */ + public void putEvent(XMLEvent event) throws NoSuchElementException { + synchronized (events) { + try { + events.put(event); + } catch (InterruptedException e) { + throw new RuntimeException + ("Producer interrupted: " +e.getMessage()); + } + } + } + + /** + * Callback routine for the parser. + */ + public void startDocument() throws NoSuchElementException { + synchronized (events) { + XMLEvent event = acquireXMLEvent(XMLNamespaces.DefAttrNSIndex); + //System.out.println("StartDocument thread " + // + Thread.currentThread().getName()); + event.type = XMLEvent.STARTDOCUMENT; + //System.out.println("SerialHandler: " + event); + putEvent(event); + } + } + + /** + * Callback routine for the parser. + */ + public void endDocument() throws NoSuchElementException { + synchronized (events) { + XMLEvent event = acquireXMLEvent(XMLNamespaces.DefAttrNSIndex); + //System.out.println("EndDocument thread " + //+ Thread.currentThread().getName()); + event.type = XMLEvent.ENDDOCUMENT; + //System.out.println("SerialHandler: " + event); + putEvent(event); + events.producerExhausted(); + } + } + + /** + * An internal method to acquire an event for a given namespace + * @param nsIndex the namespace index + * @return the acquired event + */ + private XMLEvent acquireXMLEvent(int nsIndex) { + try { + return + namespaces.acquireXMLEvent(nsIndex); + } catch (FOPException ex) { + throw new RuntimeException( + "Namespace index " + nsIndex + " not recognized"); + } + } + + /** + * Callback routine for the parser. + * @param uri the URI of the element name + * @param localName the localname of the element + * @param qName the (prefix) qualified name of the element + * @param attributes the attribute set of the element, as an object + * implementing the Attributes interface. + */ + public void startElement(String uri, String localName, + String qName, Attributes attributes) + throws NoSuchElementException + { + synchronized (events) { + try { + int uriIndex = namespaces.getURIIndex(uri); + XMLEvent event = acquireXMLEvent(uriIndex); + if (uriIndex == XMLNamespaces.XSLNSpaceIndex) { + event.setFoType(FObjectNames.getFOIndex(localName)); + } + //System.out.println("startElement: acquired " + event.id); + //System.out.println("StartElement thread " + // + Thread.currentThread().getName()); + event.type = XMLEvent.STARTELEMENT; + // Is this from the fo: namespace? + event.uriIndex = uriIndex; + event.localName = localName; + //event.qName = qName; + event.attributes = new AttributesImpl(attributes); + //System.out.println("SerialHandler: " + event); + putEvent(event); + } catch (FOPException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + + /** + * Callback routine for parser. + * @param uri the URI of this element name + * @param localName the unqualified localname of this element + * @param qName the (prefix) qualified name of this element + */ + public void endElement(String uri, String localName, + String qName) + throws NoSuchElementException + { + synchronized (events) { + try { + int uriIndex = namespaces.getURIIndex(uri); + XMLEvent event = namespaces.acquireXMLEvent(uriIndex); + //System.out.println("endElement: acquired " + event.id); + //System.out.println("EndElement thread " + //+ Thread.currentThread().getName()); + event.type = XMLEvent.ENDELEMENT; + event.uriIndex = uriIndex; + if (uriIndex == XMLNamespaces.XSLNSpaceIndex) { + event.setFoType(FObjectNames.getFOIndex(localName)); + } + event.localName = localName; + //event.qName = qName; + putEvent(event); + } catch (FOPException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + + /** + * Callback routine for parser to handle characters. + * @param ch the array of characters + * @param start starting position of this set of characters in the + * ch array. + * @param length number of characters in this set + */ + public void characters(char[] ch, int start, int length) + throws NoSuchElementException + { + synchronized (events) { + try { + // TODO chars events have no namespace, but a namespace is + // essential for subsequent processing. Use the default + // attribute namespace (the empty string), and rely on + // downstream processing to determine the environment in + // which the characters belong. + XMLEvent event + = namespaces.acquireXMLEvent(XMLNamespaces.DefAttrNSIndex); + //System.out.println("characters thread " + // + Thread.currentThread().getName()); + event.type = XMLEvent.CHARACTERS; + event.chars = new String(ch, start, length); + // Can't setFoType, because this event is now an XMLEvent, + // not an FoXMLEvent + //event.setFoType(FObjectNames.PCDATA); + //System.out.println("SerialHandler: " + event); + putEvent(event); + } catch (FOPException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } +} -- 2.39.5