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 9.5KB

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