+++ /dev/null
-/*
- * 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 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.
- */
-public class ParsedStructureTree implements StructureTree {
-
- private SAXTransformerFactory factory;
-
- private List pageSequenceStructures = new ArrayList();
-
- /**
- * Creates a new instance.
- *
- * @param factory a factory internally used to build the structures of page
- * sequences
- */
- public ParsedStructureTree(SAXTransformerFactory factory) {
- this.factory = factory;
- }
-
- /**
- * 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 <structure-tree> element and its
- * descendants
- * @throws SAXException if there is an error when creating the handler
- */
- public ContentHandler getHandlerForNextPageSequence() throws SAXException {
- TransformerHandler structureTreeBuilder;
- try {
- structureTreeBuilder = factory.newTransformerHandler();
- } catch (TransformerConfigurationException e) {
- throw new SAXException(e);
- }
- final DOMResult domResult = new DOMResult();
- structureTreeBuilder.setResult(domResult);
- return new DelegatingContentHandler(structureTreeBuilder) {
-
- public void characters(char[] ch, int start, int length) throws SAXException {
- /*
- * There's no text node in the structure tree. This is just
- * whitespace => ignore
- */
- }
-
- public void endDocument() throws SAXException {
- super.endDocument();
- pageSequenceStructures.add(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();
- }
- }
-
-}
+++ /dev/null
-/*
- * 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();
- }
- }
-
-}
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();
+ }
+ }
}
--- /dev/null
+/*
+ * 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 javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.dom.DOMResult;
+import javax.xml.transform.sax.SAXTransformerFactory;
+import javax.xml.transform.sax.TransformerHandler;
+
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+
+import org.apache.fop.util.DelegatingContentHandler;
+
+/**
+ * Helper class that re-builds a structure tree from what is stored in an
+ * intermediate XML file (IF XML or Area Tree XML).
+ */
+public final class StructureTreeBuilder {
+
+ private final SAXTransformerFactory factory;
+
+ private final StructureTree structureTree = new StructureTree();
+
+ /**
+ * Creates a new instance.
+ *
+ * @param factory a factory internally used to build the structures of page
+ * sequences
+ */
+ 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.
+ *
+ * @return a handler for parsing the <structure-tree> or
+ * <structureTree> element and its descendants
+ * @throws SAXException if there is an error when creating the handler
+ */
+ public ContentHandler getHandlerForNextPageSequence() throws SAXException {
+ TransformerHandler structureTreeBuilder;
+ try {
+ structureTreeBuilder = factory.newTransformerHandler();
+ } catch (TransformerConfigurationException e) {
+ throw new SAXException(e);
+ }
+ final DOMResult domResult = new DOMResult();
+ structureTreeBuilder.setResult(domResult);
+ return new DelegatingContentHandler(structureTreeBuilder) {
+
+ public void characters(char[] ch, int start, int length) throws SAXException {
+ /*
+ * There's no text node in the structure tree. This is just
+ * whitespace => ignore
+ */
+ }
+
+ public void endDocument() throws SAXException {
+ super.endDocument();
+ structureTree.addPageSequenceStructure(domResult.getNode().getFirstChild()
+ .getChildNodes());
+ }
+ };
+ }
+
+}
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;
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);
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;
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);
}
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());
}
}
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();
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;
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);
}
elementHandlers.put(EL_IMAGE, new ImageHandler());
if (userAgent.isAccessibilityEnabled()) {
- structureTree = new ParsedStructureTree(tFactory);
- userAgent.setStructureTree(structureTree);
+ structureTreeBuilder = new StructureTreeBuilder(tFactory);
+ userAgent.setStructureTree(structureTreeBuilder.getStructureTree());
}
}
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();
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;
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);
/** logging instance */
private static Log log = LogFactory.getLog(PDFDocumentHandler.class);
- private int pageSequenceNumber;
+ private int pageSequenceIndex;
private boolean accessEnabled;
}
if (accessEnabled) {
- NodeList nodes = getUserAgent().getStructureTree().getPageSequence(
- ++pageSequenceNumber);
+ NodeList nodes = getUserAgent().getStructureTree().getPageSequence(pageSequenceIndex++);
logicalStructureHandler.processStructureTree(nodes, getContext().getLanguage());
}
}
private PDFLogicalStructureHandler logicalStructureHandler;
- private int pageSequenceNumber;
+ private int pageSequenceIndex;
/** Reference in the structure tree to the image being rendered. */
private String imageReference;
}
pdfUtil.generateDefaultXMPMetadata();
if (accessEnabled) {
- NodeList nodes = getUserAgent().getStructureTree().getPageSequence(
- ++pageSequenceNumber);
+ NodeList nodes = getUserAgent().getStructureTree().getPageSequence(pageSequenceIndex++);
logicalStructureHandler.processStructureTree(nodes, language);
}
}
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;
/** 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.
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 {