pbwest      2002/11/16 17:40:20

  Modified:    src/org/apache/fop/fo Tag: fop-0_20_2-maintain
                        FOTreeBuilder.java
  Log:
  Line ending fixes.
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.30.2.6  +285 -285  xml-fop/src/org/apache/fop/fo/FOTreeBuilder.java
  
  Index: FOTreeBuilder.java
  ===================================================================
  RCS file: /home/cvs/xml-fop/src/org/apache/fop/fo/FOTreeBuilder.java,v
  retrieving revision 1.30.2.5
  retrieving revision 1.30.2.6
  diff -u -r1.30.2.5 -r1.30.2.6
  --- FOTreeBuilder.java        2 Aug 2002 20:28:47 -0000       1.30.2.5
  +++ FOTreeBuilder.java        17 Nov 2002 01:40:19 -0000      1.30.2.6
  @@ -1,285 +1,285 @@
  -/*
  - * $Id$
  - * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  - * For details on use and redistribution please refer to the
  - * LICENSE file included with these sources.
  - */
  -
  -package org.apache.fop.fo;
  -
  -// FOP
  -import org.apache.fop.layout.AreaTree;
  -import org.apache.fop.apps.FOPException;
  -import org.apache.fop.apps.StreamRenderer;
  -import org.apache.fop.fo.pagination.Root;
  -import org.apache.fop.fo.pagination.PageSequence;
  -import org.apache.fop.extensions.ExtensionObj;
  -
  -// Avalon
  -import org.apache.avalon.framework.logger.Logger;
  -
  -// SAX
  -import org.xml.sax.helpers.DefaultHandler;
  -import org.xml.sax.SAXException;
  -import org.xml.sax.InputSource;
  -import org.xml.sax.Attributes;
  -
  -// Java
  -import java.util.HashMap;
  -import java.util.ArrayList;
  -import java.io.IOException;
  -
  -/**
  - * SAX Handler that builds the formatting object tree.
  - * 
  - * Modified by Mark Lillywhite [EMAIL PROTECTED] Now uses
  - * StreamRenderer to automagically render the document as
  - * soon as it receives a page-sequence end-tag. Also,
  - * calls methods to set up and shut down the renderer at
  - * the beginning and end of the FO document. Finally,
  - * supresses adding the PageSequence object to the Root,
  - * since it is parsed immediately.
  - */
  -public class FOTreeBuilder extends DefaultHandler implements TreeBuilder {
  -
  -    /**
  -     * table mapping element names to the makers of objects
  -     * representing formatting objects
  -     */
  -    protected HashMap fobjTable = new HashMap();
  -
  -    protected ArrayList namespaces = new ArrayList();
  -
  -    /**
  -     * class that builds a property list for each formatting object
  -     */
  -    protected HashMap propertylistTable = new HashMap();
  -
  -    /**
  -     * current formatting object being handled
  -     */
  -    protected FObj currentFObj = null;
  -
  -    /**
  -     * the root of the formatting object tree
  -     */
  -    protected FObj rootFObj = null;
  -
  -    /**
  -     * set of names of formatting objects encountered but unknown
  -     */
  -    protected HashMap unknownFOs = new HashMap();
  -
  -    /**
  -     *
  -     * The class that handles formatting and rendering to a stream
  -     * ([EMAIL PROTECTED])
  -     */
  -    private StreamRenderer streamRenderer;
  -
  -    private Logger log;
  -
  -    public FOTreeBuilder() {}
  -
  -    public void setLogger(Logger logger) {
  -        log = logger;
  -    }
  -
  -    public void setStreamRenderer(StreamRenderer streamRenderer) {
  -        this.streamRenderer = streamRenderer;
  -    }
  -
  -    public StreamRenderer getStreamRenderer() {
  -        return this.streamRenderer;
  -    }
  -
  -    /**
  -     * add a mapping from element name to maker.
  -     *
  -     * @param namespaceURI namespace URI of formatting object element
  -     * @param maker Maker for class representing formatting object
  -     */
  -    public void addMapping(String namespaceURI, HashMap table) {
  -        this.fobjTable.put(namespaceURI, table);
  -        this.namespaces.add(namespaceURI.intern());
  -    }
  -
  -    /**
  -     * add a mapping from element name to maker.
  -     *
  -     * @param namespaceURI namespace URI of formatting object element
  -     * @param maker Maker for class representing formatting object
  -     */
  -    public void addPropertyList(String namespaceURI, HashMap list) {
  -        PropertyListBuilder plb;
  -        plb = (PropertyListBuilder)this.propertylistTable.get(namespaceURI);
  -        if (plb == null) {
  -            plb = new PropertyListBuilder();
  -            plb.addList(list);
  -            this.propertylistTable.put(namespaceURI, plb);
  -        } else {
  -            plb.addList(list);
  -        }
  -    }
  -
  -    /**
  -     * add a mapping from element name to maker.
  -     *
  -     * @param namespaceURI namespace URI of formatting object element
  -     * @param localName local name of formatting object element
  -     * @param maker Maker for class representing formatting object
  -     */
  -    public void addElementPropertyList(String namespaceURI, String localName,
  -                                       HashMap list) {
  -        PropertyListBuilder plb;
  -        plb = (PropertyListBuilder)this.propertylistTable.get(namespaceURI);
  -        if (plb == null) {
  -            plb = new PropertyListBuilder();
  -            plb.addElementList(localName, list);
  -            this.propertylistTable.put(namespaceURI, plb);
  -        } else {
  -            plb.addElementList(localName, list);
  -        }
  -    }
  -
  -    public void addPropertyListBuilder(String namespaceURI,
  -                                       PropertyListBuilder propbuilder) {
  -        PropertyListBuilder plb;
  -        plb = (PropertyListBuilder)this.propertylistTable.get(namespaceURI);
  -        if (plb == null) {
  -            this.propertylistTable.put(namespaceURI, propbuilder);
  -        } else {
  -            // Error already added
  -        }
  -    }
  -
  -    /**
  -     * SAX Handler for characters
  -     */
  -    public void characters(char data[], int start, int length) {
  -        if(currentFObj != null) {
  -            currentFObj.addCharacters(data, start, length);
  -        }
  -    }
  -
  -    /**
  -     * SAX Handler for the end of an element
  -     */
  -    public void endElement(String uri, String localName, String rawName)
  -    throws SAXException {
  -        currentFObj.end();
  -
  -        //
  -        // [EMAIL PROTECTED] - tell the stream renderer to render
  -        // this page-sequence
  -        //
  -        if(currentFObj instanceof PageSequence) {
  -            streamRenderer.render((PageSequence) currentFObj);
  -        } else if(currentFObj instanceof ExtensionObj) {
  -            if(!(currentFObj.getParent() instanceof ExtensionObj)) {
  -                streamRenderer.addExtension((ExtensionObj)currentFObj);
  -            }
  -        }
  -
  -        currentFObj = (FObj)currentFObj.getParent();
  -    }
  -
  -    /**
  -     * SAX Handler for the start of the document
  -     */
  -    public void startDocument()
  -    throws SAXException {
  -        rootFObj = null;    // allows FOTreeBuilder to be reused
  -        log.info("building formatting object tree");
  -        streamRenderer.startRenderer();
  -    }
  -
  -    public void endDocument()
  -    throws SAXException {
  -        log.info("Parsing of document complete, stopping renderer");
  -        streamRenderer.stopRenderer();
  -    }
  -
  -    /**
  -     * SAX Handler for the start of an element
  -     */
  -    public void startElement(String uri, String localName, String rawName,
  -                             Attributes attlist) throws SAXException {
  -        /* the formatting object started */
  -        FObj fobj;
  -
  -        /* the maker for the formatting object started */
  -        FObj.Maker fobjMaker = null;
  -
  -        /* look up Maker for the element */
  -        HashMap table = (HashMap)fobjTable.get(uri);
  -        if(table != null) {
  -            fobjMaker = (FObj.Maker)table.get(localName);
  -        }
  -
  -        PropertyListBuilder currentListBuilder =
  -            (PropertyListBuilder)this.propertylistTable.get(uri);
  -
  -        boolean foreignXML = false;
  -        if (fobjMaker == null) {
  -            String fullName = uri + "^" + localName;
  -            if (!this.unknownFOs.containsKey(fullName)) {
  -                this.unknownFOs.put(fullName, "");
  -                log.error("Unknown formatting object "
  -                                       + fullName);
  -            }
  -            if(namespaces.contains(uri.intern())) {
  -                // fall back
  -                fobjMaker = new Unknown.Maker();
  -            } else {
  -                fobjMaker = new UnknownXMLObj.Maker(uri, localName);
  -                foreignXML = true;
  -            }
  -        }
  -
  -        try {
  -            PropertyList list = null;
  -            if (currentListBuilder != null) {
  -                list =
  -                    currentListBuilder.makeList(uri, localName, attlist,
  -                                                (currentFObj == null) ? null
  -                                                : currentFObj.properties, 
currentFObj);
  -            } else if(foreignXML) {
  -                list = new DirectPropertyListBuilder.AttrPropertyList(attlist);
  -            } else {
  -                if(currentFObj == null) {
  -                    throw new FOPException("Invalid XML or missing namespace");
  -                }
  -                list = currentFObj.properties;
  -            }
  -            fobj = fobjMaker.make(currentFObj, list);
  -            fobj.setLogger(log);
  -        } catch (FOPException e) {
  -            throw new SAXException(e);
  -        }
  -
  -        if (rootFObj == null) {
  -            rootFObj = fobj;
  -            if (!fobj.getName().equals("fo:root")) {
  -                throw new SAXException(new FOPException("Root element must"
  -                                                        + " be root, not "
  -                                                        + fobj.getName()));
  -            }
  -        } else if(!(fobj instanceof org.apache.fop.fo.pagination.PageSequence)) {
  -            currentFObj.addChild(fobj);
  -        }
  -
  -        currentFObj = fobj;
  -    }
  -
  -    public void reset() {
  -        currentFObj = null;
  -        rootFObj = null;
  -        streamRenderer = null;
  -    }
  -
  -    public boolean hasData() {
  -        return (rootFObj != null);
  -    }
  -
  -}
  +/*
  + * $Id$
  + * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  + * For details on use and redistribution please refer to the
  + * LICENSE file included with these sources.
  + */
  +
  +package org.apache.fop.fo;
  +
  +// FOP
  +import org.apache.fop.layout.AreaTree;
  +import org.apache.fop.apps.FOPException;
  +import org.apache.fop.apps.StreamRenderer;
  +import org.apache.fop.fo.pagination.Root;
  +import org.apache.fop.fo.pagination.PageSequence;
  +import org.apache.fop.extensions.ExtensionObj;
  +
  +// Avalon
  +import org.apache.avalon.framework.logger.Logger;
  +
  +// SAX
  +import org.xml.sax.helpers.DefaultHandler;
  +import org.xml.sax.SAXException;
  +import org.xml.sax.InputSource;
  +import org.xml.sax.Attributes;
  +
  +// Java
  +import java.util.HashMap;
  +import java.util.ArrayList;
  +import java.io.IOException;
  +
  +/**
  + * SAX Handler that builds the formatting object tree.
  + * 
  + * Modified by Mark Lillywhite [EMAIL PROTECTED] Now uses
  + * StreamRenderer to automagically render the document as
  + * soon as it receives a page-sequence end-tag. Also,
  + * calls methods to set up and shut down the renderer at
  + * the beginning and end of the FO document. Finally,
  + * supresses adding the PageSequence object to the Root,
  + * since it is parsed immediately.
  + */
  +public class FOTreeBuilder extends DefaultHandler implements TreeBuilder {
  +
  +    /**
  +     * table mapping element names to the makers of objects
  +     * representing formatting objects
  +     */
  +    protected HashMap fobjTable = new HashMap();
  +
  +    protected ArrayList namespaces = new ArrayList();
  +
  +    /**
  +     * class that builds a property list for each formatting object
  +     */
  +    protected HashMap propertylistTable = new HashMap();
  +
  +    /**
  +     * current formatting object being handled
  +     */
  +    protected FObj currentFObj = null;
  +
  +    /**
  +     * the root of the formatting object tree
  +     */
  +    protected FObj rootFObj = null;
  +
  +    /**
  +     * set of names of formatting objects encountered but unknown
  +     */
  +    protected HashMap unknownFOs = new HashMap();
  +
  +    /**
  +     *
  +     * The class that handles formatting and rendering to a stream
  +     * ([EMAIL PROTECTED])
  +     */
  +    private StreamRenderer streamRenderer;
  +
  +    private Logger log;
  +
  +    public FOTreeBuilder() {}
  +
  +    public void setLogger(Logger logger) {
  +        log = logger;
  +    }
  +
  +    public void setStreamRenderer(StreamRenderer streamRenderer) {
  +        this.streamRenderer = streamRenderer;
  +    }
  +
  +    public StreamRenderer getStreamRenderer() {
  +        return this.streamRenderer;
  +    }
  +
  +    /**
  +     * add a mapping from element name to maker.
  +     *
  +     * @param namespaceURI namespace URI of formatting object element
  +     * @param maker Maker for class representing formatting object
  +     */
  +    public void addMapping(String namespaceURI, HashMap table) {
  +        this.fobjTable.put(namespaceURI, table);
  +        this.namespaces.add(namespaceURI.intern());
  +    }
  +
  +    /**
  +     * add a mapping from element name to maker.
  +     *
  +     * @param namespaceURI namespace URI of formatting object element
  +     * @param maker Maker for class representing formatting object
  +     */
  +    public void addPropertyList(String namespaceURI, HashMap list) {
  +        PropertyListBuilder plb;
  +        plb = (PropertyListBuilder)this.propertylistTable.get(namespaceURI);
  +        if (plb == null) {
  +            plb = new PropertyListBuilder();
  +            plb.addList(list);
  +            this.propertylistTable.put(namespaceURI, plb);
  +        } else {
  +            plb.addList(list);
  +        }
  +    }
  +
  +    /**
  +     * add a mapping from element name to maker.
  +     *
  +     * @param namespaceURI namespace URI of formatting object element
  +     * @param localName local name of formatting object element
  +     * @param maker Maker for class representing formatting object
  +     */
  +    public void addElementPropertyList(String namespaceURI, String localName,
  +                                       HashMap list) {
  +        PropertyListBuilder plb;
  +        plb = (PropertyListBuilder)this.propertylistTable.get(namespaceURI);
  +        if (plb == null) {
  +            plb = new PropertyListBuilder();
  +            plb.addElementList(localName, list);
  +            this.propertylistTable.put(namespaceURI, plb);
  +        } else {
  +            plb.addElementList(localName, list);
  +        }
  +    }
  +
  +    public void addPropertyListBuilder(String namespaceURI,
  +                                       PropertyListBuilder propbuilder) {
  +        PropertyListBuilder plb;
  +        plb = (PropertyListBuilder)this.propertylistTable.get(namespaceURI);
  +        if (plb == null) {
  +            this.propertylistTable.put(namespaceURI, propbuilder);
  +        } else {
  +            // Error already added
  +        }
  +    }
  +
  +    /**
  +     * SAX Handler for characters
  +     */
  +    public void characters(char data[], int start, int length) {
  +        if(currentFObj != null) {
  +            currentFObj.addCharacters(data, start, length);
  +        }
  +    }
  +
  +    /**
  +     * SAX Handler for the end of an element
  +     */
  +    public void endElement(String uri, String localName, String rawName)
  +    throws SAXException {
  +        currentFObj.end();
  +
  +        //
  +        // [EMAIL PROTECTED] - tell the stream renderer to render
  +        // this page-sequence
  +        //
  +        if(currentFObj instanceof PageSequence) {
  +            streamRenderer.render((PageSequence) currentFObj);
  +        } else if(currentFObj instanceof ExtensionObj) {
  +            if(!(currentFObj.getParent() instanceof ExtensionObj)) {
  +                streamRenderer.addExtension((ExtensionObj)currentFObj);
  +            }
  +        }
  +
  +        currentFObj = (FObj)currentFObj.getParent();
  +    }
  +
  +    /**
  +     * SAX Handler for the start of the document
  +     */
  +    public void startDocument()
  +    throws SAXException {
  +        rootFObj = null;    // allows FOTreeBuilder to be reused
  +        log.info("building formatting object tree");
  +        streamRenderer.startRenderer();
  +    }
  +
  +    public void endDocument()
  +    throws SAXException {
  +        log.info("Parsing of document complete, stopping renderer");
  +        streamRenderer.stopRenderer();
  +    }
  +
  +    /**
  +     * SAX Handler for the start of an element
  +     */
  +    public void startElement(String uri, String localName, String rawName,
  +                             Attributes attlist) throws SAXException {
  +        /* the formatting object started */
  +        FObj fobj;
  +
  +        /* the maker for the formatting object started */
  +        FObj.Maker fobjMaker = null;
  +
  +        /* look up Maker for the element */
  +        HashMap table = (HashMap)fobjTable.get(uri);
  +        if(table != null) {
  +            fobjMaker = (FObj.Maker)table.get(localName);
  +        }
  +
  +        PropertyListBuilder currentListBuilder =
  +            (PropertyListBuilder)this.propertylistTable.get(uri);
  +
  +        boolean foreignXML = false;
  +        if (fobjMaker == null) {
  +            String fullName = uri + "^" + localName;
  +            if (!this.unknownFOs.containsKey(fullName)) {
  +                this.unknownFOs.put(fullName, "");
  +                log.error("Unknown formatting object "
  +                                       + fullName);
  +            }
  +            if(namespaces.contains(uri.intern())) {
  +                // fall back
  +                fobjMaker = new Unknown.Maker();
  +            } else {
  +                fobjMaker = new UnknownXMLObj.Maker(uri, localName);
  +                foreignXML = true;
  +            }
  +        }
  +
  +        try {
  +            PropertyList list = null;
  +            if (currentListBuilder != null) {
  +                list =
  +                    currentListBuilder.makeList(uri, localName, attlist,
  +                                                (currentFObj == null) ? null
  +                                                : currentFObj.properties, 
currentFObj);
  +            } else if(foreignXML) {
  +                list = new DirectPropertyListBuilder.AttrPropertyList(attlist);
  +            } else {
  +                if(currentFObj == null) {
  +                    throw new FOPException("Invalid XML or missing namespace");
  +                }
  +                list = currentFObj.properties;
  +            }
  +            fobj = fobjMaker.make(currentFObj, list);
  +            fobj.setLogger(log);
  +        } catch (FOPException e) {
  +            throw new SAXException(e);
  +        }
  +
  +        if (rootFObj == null) {
  +            rootFObj = fobj;
  +            if (!fobj.getName().equals("fo:root")) {
  +                throw new SAXException(new FOPException("Root element must"
  +                                                        + " be root, not "
  +                                                        + fobj.getName()));
  +            }
  +        } else if(!(fobj instanceof org.apache.fop.fo.pagination.PageSequence)) {
  +            currentFObj.addChild(fobj);
  +        }
  +
  +        currentFObj = fobj;
  +    }
  +
  +    public void reset() {
  +        currentFObj = null;
  +        rootFObj = null;
  +        streamRenderer = null;
  +    }
  +
  +    public boolean hasData() {
  +        return (rootFObj != null);
  +    }
  +
  +}
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to