aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org
diff options
context:
space:
mode:
authorVincent Hennebert <vhennebert@apache.org>2009-10-15 14:25:28 +0000
committerVincent Hennebert <vhennebert@apache.org>2009-10-15 14:25:28 +0000
commit113a028123216ba7f6d6c376d0b7b0a9f76986bd (patch)
treee4f0d849b8813563c3b2021b8a1d98384e7159b1 /src/java/org
parenta3960ce47f877ae7c9d767dc3804be1bc4ffae24 (diff)
downloadxmlgraphics-fop-113a028123216ba7f6d6c376d0b7b0a9f76986bd.tar.gz
xmlgraphics-fop-113a028123216ba7f6d6c376d0b7b0a9f76986bd.zip
Merged SimpleStructureTree and ParsedStructureTree classes into a single one, used by both TransformerNodeEndProcessing and intermediate XML parsers
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_Accessibility@825504 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org')
-rw-r--r--src/java/org/apache/fop/accessibility/SimpleStructureTree.java66
-rw-r--r--src/java/org/apache/fop/accessibility/StructureTree.java70
-rw-r--r--src/java/org/apache/fop/accessibility/StructureTreeBuilder.java (renamed from src/java/org/apache/fop/accessibility/ParsedStructureTree.java)69
-rw-r--r--src/java/org/apache/fop/accessibility/TransformerNodeEndProcessing.java11
-rw-r--r--src/java/org/apache/fop/area/AreaTreeParser.java23
-rw-r--r--src/java/org/apache/fop/render/intermediate/IFParser.java22
-rw-r--r--src/java/org/apache/fop/render/intermediate/IFSerializer.java4
-rw-r--r--src/java/org/apache/fop/render/pdf/PDFDocumentHandler.java5
-rw-r--r--src/java/org/apache/fop/render/pdf/PDFRenderer.java5
-rw-r--r--src/java/org/apache/fop/render/xml/XMLRenderer.java6
10 files changed, 126 insertions, 155 deletions
diff --git a/src/java/org/apache/fop/accessibility/SimpleStructureTree.java b/src/java/org/apache/fop/accessibility/SimpleStructureTree.java
deleted file mode 100644
index c009b7b30..000000000
--- a/src/java/org/apache/fop/accessibility/SimpleStructureTree.java
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/* $Id$ */
-
-package org.apache.fop.accessibility;
-
-import java.io.StringWriter;
-import java.io.Writer;
-
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerFactory;
-import javax.xml.transform.dom.DOMSource;
-import javax.xml.transform.stream.StreamResult;
-
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
-
-/**
- * A StructureTree implementation created from the reduced FO tree, in the form
- * of a single DOM document obtained by XSL Transformation of the original FO
- * tree.
- */
-final class SimpleStructureTree implements StructureTree {
-
- private final Node reducedFOTree;
-
- SimpleStructureTree(Node reducedFOTree) {
- this.reducedFOTree = reducedFOTree;
- }
-
- /** {@inheritDoc} */
- public NodeList getPageSequence(int number) {
- Node pageSequence = reducedFOTree.getFirstChild().getChildNodes().item(number - 1);
- assert pageSequence.getNodeType() == Node.ELEMENT_NODE
- && pageSequence.getLocalName().equals("page-sequence");
- return pageSequence.getChildNodes();
- }
-
- /** {@inheritDoc} */
- public String toString() {
- try {
- Transformer t = TransformerFactory.newInstance().newTransformer();
- Writer str = new StringWriter();
- t.transform(new DOMSource(reducedFOTree), new StreamResult(str));
- return str.toString();
- } catch (Exception e) {
- return e.toString();
- }
- }
-
-}
diff --git a/src/java/org/apache/fop/accessibility/StructureTree.java b/src/java/org/apache/fop/accessibility/StructureTree.java
index 09a33f151..a0fdaac21 100644
--- a/src/java/org/apache/fop/accessibility/StructureTree.java
+++ b/src/java/org/apache/fop/accessibility/StructureTree.java
@@ -19,20 +19,84 @@
package org.apache.fop.accessibility;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* A reduced version of the document's FO tree, containing only its logical
* structure. Used by accessible output formats.
*/
-public interface StructureTree {
+public final class StructureTree {
+
+ private final List pageSequenceStructures = new ArrayList();
+
+ /**
+ * Package-private default constructor.
+ */
+ StructureTree() { }
+
+ private static boolean flowOrStaticContentNodes(NodeList nodes) {
+ for (int i = 0; i < nodes.getLength(); i++) {
+ Node node = nodes.item(i);
+ if (node.getNodeType() != Node.ELEMENT_NODE) {
+ return false;
+ }
+ String name = node.getLocalName();
+ if (!(name.equals("flow") || name.equals("static-content"))) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ void addPageSequenceStructure(NodeList structureTree) {
+ assert flowOrStaticContentNodes(structureTree);
+ pageSequenceStructures.add(structureTree);
+ }
/**
* Returns the list of nodes that are the children of the given page sequence.
*
- * @param number number of the page sequence, 1-based
+ * @param index index of the page sequence, 0-based
* @return its children nodes
*/
- NodeList getPageSequence(int number);
+ public NodeList getPageSequence(int index) {
+ return (NodeList) pageSequenceStructures.get(index);
+ }
+
+ /**
+ * Returns an XML-like representation of the structure trees.
+ * <p>
+ * <strong>Note:</strong> use only for debugging purpose, as this method
+ * performs non-trivial operations.
+ * </p>
+ * @return a string representation of this object
+ */
+ public String toString() {
+ try {
+ Transformer t = TransformerFactory.newInstance().newTransformer();
+ Writer str = new StringWriter();
+ for (Iterator iter = pageSequenceStructures.iterator(); iter.hasNext();) {
+ NodeList nodes = (NodeList) iter.next();
+ for (int i = 0, c = nodes.getLength(); i < c; i++) {
+ t.transform(new DOMSource(nodes.item(i)), new StreamResult(str));
+ }
+ }
+ return str.toString();
+ } catch (Exception e) {
+ return e.toString();
+ }
+ }
}
diff --git a/src/java/org/apache/fop/accessibility/ParsedStructureTree.java b/src/java/org/apache/fop/accessibility/StructureTreeBuilder.java
index 646496252..cd9aa011a 100644
--- a/src/java/org/apache/fop/accessibility/ParsedStructureTree.java
+++ b/src/java/org/apache/fop/accessibility/StructureTreeBuilder.java
@@ -19,36 +19,25 @@
package org.apache.fop.accessibility;
-import java.io.StringWriter;
-import java.io.Writer;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-
-import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
-import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
-import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
-import javax.xml.transform.stream.StreamResult;
-import org.w3c.dom.NodeList;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.apache.fop.util.DelegatingContentHandler;
/**
- * A StructureTree implementation re-created from the structure stored in an IF
- * XML document.
+ * Helper class that re-builds a structure tree from what is stored in an
+ * intermediate XML file (IF XML or Area Tree XML).
*/
-public class ParsedStructureTree implements StructureTree {
+public final class StructureTreeBuilder {
- private SAXTransformerFactory factory;
+ private final SAXTransformerFactory factory;
- private List pageSequenceStructures = new ArrayList();
+ private final StructureTree structureTree = new StructureTree();
/**
* Creates a new instance.
@@ -56,17 +45,25 @@ public class ParsedStructureTree implements StructureTree {
* @param factory a factory internally used to build the structures of page
* sequences
*/
- public ParsedStructureTree(SAXTransformerFactory factory) {
+ public StructureTreeBuilder(SAXTransformerFactory factory) {
this.factory = factory;
}
/**
+ * Returns the structure tree that will result from the parsing.
+ *
+ * @return the structure tree built by this object
+ */
+ public StructureTree getStructureTree() {
+ return structureTree;
+ }
+
+ /**
* Returns a ContenHandler for parsing the structure of a new page sequence.
* It is assumed that page sequences are being parsed in the document order.
- * This class will automatically number the structure trees.
*
- * @return a handler for parsing the &lt;structure-tree&gt; element and its
- * descendants
+ * @return a handler for parsing the &lt;structure-tree&gt; or
+ * &lt;structureTree&gt; element and its descendants
* @throws SAXException if there is an error when creating the handler
*/
public ContentHandler getHandlerForNextPageSequence() throws SAXException {
@@ -89,38 +86,10 @@ public class ParsedStructureTree implements StructureTree {
public void endDocument() throws SAXException {
super.endDocument();
- pageSequenceStructures.add(domResult.getNode().getFirstChild().getChildNodes());
+ structureTree.addPageSequenceStructure(domResult.getNode().getFirstChild()
+ .getChildNodes());
}
};
}
- /** {@inheritDoc} */
- public NodeList getPageSequence(int number) {
- return (NodeList) pageSequenceStructures.get(number - 1);
- }
-
- /**
- * Returns an XML-like representation of the structure trees.
- * <p>
- * <strong>Note:</strong> use only for debugging purpose, as this method
- * performs non-trivial operations.
- * </p>
- * @return a string representation of this object
- */
- public String toString() {
- try {
- Transformer t = TransformerFactory.newInstance().newTransformer();
- Writer str = new StringWriter();
- for (Iterator iter = pageSequenceStructures.iterator(); iter.hasNext();) {
- NodeList nodes = (NodeList) iter.next();
- for (int i = 0, c = nodes.getLength(); i < c; i++) {
- t.transform(new DOMSource(nodes.item(i)), new StreamResult(str));
- }
- }
- return str.toString();
- } catch (Exception e) {
- return e.toString();
- }
- }
-
}
diff --git a/src/java/org/apache/fop/accessibility/TransformerNodeEndProcessing.java b/src/java/org/apache/fop/accessibility/TransformerNodeEndProcessing.java
index 00d4a5b56..36f36fdef 100644
--- a/src/java/org/apache/fop/accessibility/TransformerNodeEndProcessing.java
+++ b/src/java/org/apache/fop/accessibility/TransformerNodeEndProcessing.java
@@ -32,10 +32,12 @@ import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
-import org.apache.commons.io.output.ByteArrayOutputStream;
+import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
+import org.apache.commons.io.output.ByteArrayOutputStream;
+
import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.FOUserAgent;
@@ -78,7 +80,12 @@ class TransformerNodeEndProcessing extends TransformerNode {
Source src = new StreamSource(new ByteArrayInputStream(enrichedFO));
DOMResult res = new DOMResult();
transformer.transform(src, res);
- userAgent.setStructureTree(new SimpleStructureTree(res.getNode()));
+ StructureTree structureTree = new StructureTree();
+ NodeList pageSequences = res.getNode().getFirstChild().getChildNodes();
+ for (int i = 0; i < pageSequences.getLength(); i++) {
+ structureTree.addPageSequenceStructure(pageSequences.item(i).getChildNodes());
+ }
+ userAgent.setStructureTree(structureTree);
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
saxParserFactory.setNamespaceAware(true);
diff --git a/src/java/org/apache/fop/area/AreaTreeParser.java b/src/java/org/apache/fop/area/AreaTreeParser.java
index 12e31530d..784ca0684 100644
--- a/src/java/org/apache/fop/area/AreaTreeParser.java
+++ b/src/java/org/apache/fop/area/AreaTreeParser.java
@@ -57,7 +57,7 @@ import org.apache.xmlgraphics.image.loader.ImageManager;
import org.apache.xmlgraphics.image.loader.ImageSessionContext;
import org.apache.xmlgraphics.util.QName;
-import org.apache.fop.accessibility.ParsedStructureTree;
+import org.apache.fop.accessibility.StructureTreeBuilder;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.area.Trait.Background;
import org.apache.fop.area.Trait.InternalLink;
@@ -160,17 +160,17 @@ public class AreaTreeParser {
private Locator locator;
- private ParsedStructureTree structureTree;
+ private StructureTreeBuilder structureTreeBuilder;
- private ContentHandler structureTreeBuilder;
+ private ContentHandler structureTreeBuilderWrapper;
- private final class StructureTreeBuilder extends DelegatingContentHandler {
+ private final class StructureTreeBuilderWrapper extends DelegatingContentHandler {
private Attributes pageSequenceAttributes;
- private StructureTreeBuilder(Attributes pageSequenceAttributes,
- ParsedStructureTree structureTree) throws SAXException {
- super(structureTree.getHandlerForNextPageSequence());
+ private StructureTreeBuilderWrapper(Attributes pageSequenceAttributes)
+ throws SAXException {
+ super(structureTreeBuilder.getHandlerForNextPageSequence());
this.pageSequenceAttributes = new AttributesImpl(pageSequenceAttributes);
}
@@ -216,9 +216,10 @@ public class AreaTreeParser {
makers.put("bookmarkTree", new BookmarkTreeMaker());
makers.put("bookmark", new BookmarkMaker());
makers.put("destination", new DestinationMaker());
+
if (userAgent.isAccessibilityEnabled()) {
- structureTree = new ParsedStructureTree(tFactory);
- userAgent.setStructureTree(structureTree);
+ structureTreeBuilder = new StructureTreeBuilder(tFactory);
+ userAgent.setStructureTree(structureTreeBuilder.getStructureTree());
}
}
@@ -296,10 +297,10 @@ public class AreaTreeParser {
boolean handled = true;
if ("".equals(uri)) {
if (localName.equals("pageSequence") && userAgent.isAccessibilityEnabled()) {
- structureTreeBuilder = new StructureTreeBuilder(attributes, structureTree);
+ structureTreeBuilderWrapper = new StructureTreeBuilderWrapper(attributes);
} else if (localName.equals("structureTree")) {
if (userAgent.isAccessibilityEnabled()) {
- delegate = structureTreeBuilder;
+ delegate = structureTreeBuilderWrapper;
} else {
/* Delegate to a handler that does nothing */
delegate = new DefaultHandler();
diff --git a/src/java/org/apache/fop/render/intermediate/IFParser.java b/src/java/org/apache/fop/render/intermediate/IFParser.java
index 9ef65ce6f..e7bdd1b23 100644
--- a/src/java/org/apache/fop/render/intermediate/IFParser.java
+++ b/src/java/org/apache/fop/render/intermediate/IFParser.java
@@ -46,7 +46,7 @@ import org.apache.commons.logging.LogFactory;
import org.apache.xmlgraphics.util.QName;
-import org.apache.fop.accessibility.ParsedStructureTree;
+import org.apache.fop.accessibility.StructureTreeBuilder;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.fo.ElementMapping;
import org.apache.fop.fo.ElementMappingRegistry;
@@ -151,17 +151,17 @@ public class IFParser implements IFConstants {
private ContentHandler navParser;
- private ParsedStructureTree structureTree;
+ private StructureTreeBuilder structureTreeBuilder;
- private ContentHandler structureTreeBuilder;
+ private ContentHandler structureTreeBuilderWrapper;
- private final class StructureTreeBuilder extends DelegatingContentHandler {
+ private final class StructureTreeBuilderWrapper extends DelegatingContentHandler {
private Attributes pageSequenceAttributes;
- private StructureTreeBuilder(Attributes pageSequenceAttributes,
- ParsedStructureTree structureTree) throws SAXException {
- super(structureTree.getHandlerForNextPageSequence());
+ private StructureTreeBuilderWrapper(Attributes pageSequenceAttributes)
+ throws SAXException {
+ super(structureTreeBuilder.getHandlerForNextPageSequence());
this.pageSequenceAttributes = new AttributesImpl(pageSequenceAttributes);
}
@@ -196,8 +196,8 @@ public class IFParser implements IFConstants {
elementHandlers.put(EL_IMAGE, new ImageHandler());
if (userAgent.isAccessibilityEnabled()) {
- structureTree = new ParsedStructureTree(tFactory);
- userAgent.setStructureTree(structureTree);
+ structureTreeBuilder = new StructureTreeBuilder(tFactory);
+ userAgent.setStructureTree(structureTreeBuilder.getStructureTree());
}
}
@@ -227,10 +227,10 @@ public class IFParser implements IFConstants {
boolean handled = true;
if (NAMESPACE.equals(uri)) {
if (localName.equals(EL_PAGE_SEQUENCE) && userAgent.isAccessibilityEnabled()) {
- structureTreeBuilder = new StructureTreeBuilder(attributes, structureTree);
+ structureTreeBuilderWrapper = new StructureTreeBuilderWrapper(attributes);
} else if (localName.equals(EL_STRUCTURE_TREE)) {
if (userAgent.isAccessibilityEnabled()) {
- delegate = structureTreeBuilder;
+ delegate = structureTreeBuilderWrapper;
} else {
/* Delegate to a handler that does nothing */
delegate = new DefaultHandler();
diff --git a/src/java/org/apache/fop/render/intermediate/IFSerializer.java b/src/java/org/apache/fop/render/intermediate/IFSerializer.java
index 9d7c15a61..b6136f036 100644
--- a/src/java/org/apache/fop/render/intermediate/IFSerializer.java
+++ b/src/java/org/apache/fop/render/intermediate/IFSerializer.java
@@ -63,7 +63,7 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler
implements IFConstants, IFPainter, IFDocumentNavigationHandler {
private IFDocumentHandler mimicHandler;
- private int pageSequenceCounter; // used for accessibility
+ private int pageSequenceIndex; // used for accessibility
/** Holds the intermediate format state */
private IFState state;
@@ -224,7 +224,7 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler
if (this.getUserAgent().isAccessibilityEnabled()) {
StructureTree structureTree = getUserAgent().getStructureTree();
handler.startElement(EL_STRUCTURE_TREE); // add structure tree
- NodeList nodes = structureTree.getPageSequence(++pageSequenceCounter);
+ NodeList nodes = structureTree.getPageSequence(pageSequenceIndex++);
for (int i = 0, n = nodes.getLength(); i < n; i++) {
Node node = nodes.item(i);
new DOM2SAX(handler).writeFragment(node);
diff --git a/src/java/org/apache/fop/render/pdf/PDFDocumentHandler.java b/src/java/org/apache/fop/render/pdf/PDFDocumentHandler.java
index b9d726bf2..3cd601bfa 100644
--- a/src/java/org/apache/fop/render/pdf/PDFDocumentHandler.java
+++ b/src/java/org/apache/fop/render/pdf/PDFDocumentHandler.java
@@ -62,7 +62,7 @@ public class PDFDocumentHandler extends AbstractBinaryWritingIFDocumentHandler {
/** logging instance */
private static Log log = LogFactory.getLog(PDFDocumentHandler.class);
- private int pageSequenceNumber;
+ private int pageSequenceIndex;
private boolean accessEnabled;
@@ -186,8 +186,7 @@ public class PDFDocumentHandler extends AbstractBinaryWritingIFDocumentHandler {
}
if (accessEnabled) {
- NodeList nodes = getUserAgent().getStructureTree().getPageSequence(
- ++pageSequenceNumber);
+ NodeList nodes = getUserAgent().getStructureTree().getPageSequence(pageSequenceIndex++);
logicalStructureHandler.processStructureTree(nodes, getContext().getLanguage());
}
}
diff --git a/src/java/org/apache/fop/render/pdf/PDFRenderer.java b/src/java/org/apache/fop/render/pdf/PDFRenderer.java
index 17ab1755c..380241ba0 100644
--- a/src/java/org/apache/fop/render/pdf/PDFRenderer.java
+++ b/src/java/org/apache/fop/render/pdf/PDFRenderer.java
@@ -204,7 +204,7 @@ public class PDFRenderer extends AbstractPathOrientedRenderer implements PDFConf
private PDFLogicalStructureHandler logicalStructureHandler;
- private int pageSequenceNumber;
+ private int pageSequenceIndex;
/** Reference in the structure tree to the image being rendered. */
private String imageReference;
@@ -428,8 +428,7 @@ public class PDFRenderer extends AbstractPathOrientedRenderer implements PDFConf
}
pdfUtil.generateDefaultXMPMetadata();
if (accessEnabled) {
- NodeList nodes = getUserAgent().getStructureTree().getPageSequence(
- ++pageSequenceNumber);
+ NodeList nodes = getUserAgent().getStructureTree().getPageSequence(pageSequenceIndex++);
logicalStructureHandler.processStructureTree(nodes, language);
}
}
diff --git a/src/java/org/apache/fop/render/xml/XMLRenderer.java b/src/java/org/apache/fop/render/xml/XMLRenderer.java
index d86814d25..a8a1a1911 100644
--- a/src/java/org/apache/fop/render/xml/XMLRenderer.java
+++ b/src/java/org/apache/fop/render/xml/XMLRenderer.java
@@ -41,7 +41,6 @@ import org.xml.sax.SAXException;
import org.apache.xmlgraphics.util.QName;
import org.apache.xmlgraphics.util.XMLizable;
-import org.apache.fop.accessibility.StructureTree;
import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.MimeConstants;
@@ -108,7 +107,7 @@ public class XMLRenderer extends AbstractXMLRenderer {
/** If not null, the XMLRenderer will mimic another renderer by using its font setup. */
protected Renderer mimic;
- private int pageSequenceNumber;
+ private int pageSequenceIndex;
/**
* Creates a new XML renderer.
@@ -446,10 +445,9 @@ public class XMLRenderer extends AbstractXMLRenderer {
transferForeignObjects(pageSequence);
startElement("pageSequence", atts);
if (this.getUserAgent().isAccessibilityEnabled()) {
- StructureTree structureTree = getUserAgent().getStructureTree();
String structureTreeElement = "structureTree";
startElement(structureTreeElement);
- NodeList nodes = structureTree.getPageSequence(++pageSequenceNumber);
+ NodeList nodes = getUserAgent().getStructureTree().getPageSequence(pageSequenceIndex++);
for (int i = 0, n = nodes.getLength(); i < n; i++) {
Node node = nodes.item(i);
try {