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

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