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.

IFStructureTreeBuilder.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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.render.intermediate;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import java.util.Locale;
  22. import org.xml.sax.Attributes;
  23. import org.xml.sax.ContentHandler;
  24. import org.xml.sax.SAXException;
  25. import org.xml.sax.helpers.AttributesImpl;
  26. import org.xml.sax.helpers.DefaultHandler;
  27. import org.apache.fop.accessibility.StructureTree2SAXEventAdapter;
  28. import org.apache.fop.accessibility.StructureTreeElement;
  29. import org.apache.fop.accessibility.StructureTreeEventHandler;
  30. import org.apache.fop.fo.extensions.InternalElementMapping;
  31. import org.apache.fop.util.XMLUtil;
  32. /**
  33. * Saves structure tree events as SAX events in order to replay them when it's
  34. * time to stream the structure tree to the output.
  35. */
  36. final class IFStructureTreeBuilder implements StructureTreeEventHandler {
  37. static final class IFStructureTreeElement implements StructureTreeElement {
  38. final String id;
  39. IFStructureTreeElement() {
  40. this.id = null;
  41. }
  42. IFStructureTreeElement(String id) {
  43. this.id = id;
  44. }
  45. }
  46. /** A SAX handler that records events to replay them later. */
  47. static class SAXEventRecorder extends DefaultHandler {
  48. private final List<SAXEventRecorder.Event> events = new ArrayList<SAXEventRecorder.Event>();
  49. private abstract static class Event {
  50. abstract void replay(ContentHandler handler) throws SAXException;
  51. }
  52. private abstract static class Element extends SAXEventRecorder.Event {
  53. protected final String uri;
  54. protected final String localName;
  55. protected final String qName;
  56. private Element(String uri, String localName, String qName) {
  57. this.uri = uri;
  58. this.localName = localName;
  59. this.qName = qName;
  60. }
  61. }
  62. private static final class StartElement extends SAXEventRecorder.Element {
  63. private final Attributes attributes;
  64. private StartElement(String uri, String localName, String qName,
  65. Attributes attributes) {
  66. super(uri, localName, qName);
  67. this.attributes = attributes;
  68. }
  69. @Override
  70. void replay(ContentHandler handler) throws SAXException {
  71. handler.startElement(uri, localName, qName, attributes);
  72. }
  73. }
  74. private static final class EndElement extends SAXEventRecorder.Element {
  75. private EndElement(String uri, String localName, String qName) {
  76. super(uri, localName, qName);
  77. }
  78. @Override
  79. void replay(ContentHandler handler) throws SAXException {
  80. handler.endElement(uri, localName, qName);
  81. }
  82. }
  83. private static final class StartPrefixMapping extends SAXEventRecorder.Event {
  84. protected final String prefix;
  85. protected final String uri;
  86. private StartPrefixMapping(String prefix, String uri) {
  87. this.prefix = prefix;
  88. this.uri = uri;
  89. }
  90. @Override
  91. void replay(ContentHandler handler) throws SAXException {
  92. handler.startPrefixMapping(prefix, uri);
  93. }
  94. }
  95. private static final class EndPrefixMapping extends SAXEventRecorder.Event {
  96. protected final String prefix;
  97. private EndPrefixMapping(String prefix) {
  98. this.prefix = prefix;
  99. }
  100. @Override
  101. void replay(ContentHandler handler) throws SAXException {
  102. handler.endPrefixMapping(prefix);
  103. }
  104. }
  105. @Override
  106. public void startElement(String uri, String localName, String qName,
  107. Attributes attributes) throws SAXException {
  108. events.add(new StartElement(uri, localName, qName, attributes));
  109. }
  110. @Override
  111. public void endElement(String uri, String localName, String qName) throws SAXException {
  112. events.add(new EndElement(uri, localName, qName));
  113. }
  114. @Override
  115. public void startPrefixMapping(String prefix, String uri) throws SAXException {
  116. events.add(new StartPrefixMapping(prefix, uri));
  117. }
  118. @Override
  119. public void endPrefixMapping(String prefix) throws SAXException {
  120. events.add(new EndPrefixMapping(prefix));
  121. }
  122. /**
  123. * Replays the recorded events.
  124. *
  125. * @param handler {@code ContentHandler} to replay events on
  126. */
  127. public void replay(ContentHandler handler) throws SAXException {
  128. for (SAXEventRecorder.Event e : events) {
  129. e.replay(handler);
  130. }
  131. }
  132. }
  133. private StructureTreeEventHandler delegate;
  134. private final List<SAXEventRecorder> pageSequenceEventRecorders = new ArrayList<SAXEventRecorder>();
  135. private int idCounter;
  136. /**
  137. * Replay SAX events for a page sequence.
  138. * @param handler The handler that receives SAX events
  139. * @param pageSequenceIndex The index of the page sequence
  140. * @throws SAXException
  141. */
  142. public void replayEventsForPageSequence(ContentHandler handler,
  143. int pageSequenceIndex) throws SAXException {
  144. pageSequenceEventRecorders.get(pageSequenceIndex).replay(handler);
  145. }
  146. public void startPageSequence(Locale locale) {
  147. SAXEventRecorder eventRecorder = new SAXEventRecorder();
  148. pageSequenceEventRecorders.add(eventRecorder);
  149. delegate = StructureTree2SAXEventAdapter.newInstance(eventRecorder);
  150. delegate.startPageSequence(locale);
  151. }
  152. public void endPageSequence() {
  153. delegate.endPageSequence();
  154. }
  155. public StructureTreeElement startNode(String name, Attributes attributes) {
  156. delegate.startNode(name, attributes);
  157. return new IFStructureTreeElement();
  158. }
  159. public void endNode(String name) {
  160. delegate.endNode(name);
  161. }
  162. public StructureTreeElement startImageNode(String name, Attributes attributes) {
  163. String id = getNextID();
  164. AttributesImpl atts = addIDAttribute(attributes, id);
  165. delegate.startImageNode(name, atts);
  166. return new IFStructureTreeElement(id);
  167. }
  168. public StructureTreeElement startReferencedNode(String name, Attributes attributes) {
  169. String id = getNextID();
  170. AttributesImpl atts = addIDAttribute(attributes, id);
  171. delegate.startReferencedNode(name, atts);
  172. return new IFStructureTreeElement(id);
  173. }
  174. private String getNextID() {
  175. return Integer.toHexString(idCounter++);
  176. }
  177. private AttributesImpl addIDAttribute(Attributes attributes, String id) {
  178. AttributesImpl atts = new AttributesImpl(attributes);
  179. atts.addAttribute(InternalElementMapping.URI,
  180. InternalElementMapping.STRUCT_ID,
  181. InternalElementMapping.STANDARD_PREFIX + ":" + InternalElementMapping.STRUCT_ID,
  182. XMLUtil.CDATA,
  183. id);
  184. return atts;
  185. }
  186. }