You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StructureTree.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.accessibility;
  19. import java.io.StringWriter;
  20. import java.io.Writer;
  21. import java.util.ArrayList;
  22. import java.util.Iterator;
  23. import java.util.List;
  24. import javax.xml.transform.Transformer;
  25. import javax.xml.transform.TransformerFactory;
  26. import javax.xml.transform.dom.DOMSource;
  27. import javax.xml.transform.stream.StreamResult;
  28. import org.w3c.dom.Node;
  29. import org.w3c.dom.NodeList;
  30. /**
  31. * A reduced version of the document's FO tree, containing only its logical
  32. * structure. Used by accessible output formats.
  33. */
  34. public final class StructureTree {
  35. private final List pageSequenceStructures = new ArrayList();
  36. /**
  37. * Package-private default constructor.
  38. */
  39. public StructureTree() { }
  40. private static boolean flowOrStaticContentNodes(NodeList nodes) {
  41. for (int i = 0; i < nodes.getLength(); i++) {
  42. Node node = nodes.item(i);
  43. if (node.getNodeType() != Node.ELEMENT_NODE) {
  44. return false;
  45. }
  46. String name = node.getLocalName();
  47. if (!(name.equals("flow") || name.equals("static-content"))) {
  48. return false;
  49. }
  50. }
  51. return true;
  52. }
  53. void addPageSequenceStructure(NodeList structureTree) {
  54. assert flowOrStaticContentNodes(structureTree);
  55. pageSequenceStructures.add(structureTree);
  56. }
  57. /**
  58. * Returns the list of nodes that are the children of the given page sequence.
  59. *
  60. * @param index index of the page sequence, 0-based
  61. * @return its children nodes
  62. */
  63. public NodeList getPageSequence(int index) {
  64. return (NodeList) pageSequenceStructures.get(index);
  65. }
  66. /**
  67. * Returns an XML-like representation of the structure trees.
  68. * <p>
  69. * <strong>Note:</strong> use only for debugging purpose, as this method
  70. * performs non-trivial operations.
  71. * </p>
  72. * @return a string representation of this object
  73. */
  74. public String toString() {
  75. try {
  76. Transformer t = TransformerFactory.newInstance().newTransformer();
  77. Writer str = new StringWriter();
  78. for (Iterator iter = pageSequenceStructures.iterator(); iter.hasNext();) {
  79. NodeList nodes = (NodeList) iter.next();
  80. for (int i = 0, c = nodes.getLength(); i < c; i++) {
  81. t.transform(new DOMSource(nodes.item(i)), new StreamResult(str));
  82. }
  83. }
  84. return str.toString();
  85. } catch (Exception e) {
  86. return e.toString();
  87. }
  88. }
  89. }