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.

StructureTreeBuilder.java 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 javax.xml.transform.TransformerConfigurationException;
  20. import javax.xml.transform.dom.DOMResult;
  21. import javax.xml.transform.sax.SAXTransformerFactory;
  22. import javax.xml.transform.sax.TransformerHandler;
  23. import org.xml.sax.ContentHandler;
  24. import org.xml.sax.SAXException;
  25. import org.apache.fop.util.DelegatingContentHandler;
  26. /**
  27. * Helper class that re-builds a structure tree from what is stored in an
  28. * intermediate XML file (IF XML or Area Tree XML).
  29. */
  30. public final class StructureTreeBuilder {
  31. private final SAXTransformerFactory factory;
  32. private final StructureTree structureTree = new StructureTree();
  33. /**
  34. * Creates a new instance.
  35. *
  36. * @param factory a factory internally used to build the structures of page
  37. * sequences
  38. */
  39. public StructureTreeBuilder(SAXTransformerFactory factory) {
  40. this.factory = factory;
  41. }
  42. /**
  43. * Returns the structure tree that will result from the parsing.
  44. *
  45. * @return the structure tree built by this object
  46. */
  47. public StructureTree getStructureTree() {
  48. return structureTree;
  49. }
  50. /**
  51. * Returns a ContenHandler for parsing the structure of a new page sequence.
  52. * It is assumed that page sequences are being parsed in the document order.
  53. *
  54. * @return a handler for parsing the <structure-tree> or
  55. * <structureTree> element and its descendants
  56. * @throws SAXException if there is an error when creating the handler
  57. */
  58. public ContentHandler getHandlerForNextPageSequence() throws SAXException {
  59. TransformerHandler structureTreeBuilder;
  60. try {
  61. structureTreeBuilder = factory.newTransformerHandler();
  62. } catch (TransformerConfigurationException e) {
  63. throw new SAXException(e);
  64. }
  65. final DOMResult domResult = new DOMResult();
  66. structureTreeBuilder.setResult(domResult);
  67. return new DelegatingContentHandler(structureTreeBuilder) {
  68. public void characters(char[] ch, int start, int length) throws SAXException {
  69. /*
  70. * There's no text node in the structure tree. This is just
  71. * whitespace => ignore
  72. */
  73. }
  74. public void endDocument() throws SAXException {
  75. super.endDocument();
  76. structureTree.addPageSequenceStructure(domResult.getNode().getFirstChild()
  77. .getChildNodes());
  78. }
  79. };
  80. }
  81. }