Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

DocumentNavigationHandler.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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.extensions;
  19. import java.awt.Point;
  20. import java.awt.Rectangle;
  21. import java.util.Map;
  22. import java.util.Stack;
  23. import org.xml.sax.Attributes;
  24. import org.xml.sax.SAXException;
  25. import org.xml.sax.helpers.DefaultHandler;
  26. import org.apache.commons.logging.Log;
  27. import org.apache.commons.logging.LogFactory;
  28. import org.apache.fop.accessibility.StructureTreeElement;
  29. import org.apache.fop.fo.extensions.InternalElementMapping;
  30. import org.apache.fop.render.intermediate.IFDocumentNavigationHandler;
  31. import org.apache.fop.render.intermediate.IFException;
  32. import org.apache.fop.util.XMLUtil;
  33. /**
  34. * ContentHandler that handles the IF document navigation namespace.
  35. */
  36. public class DocumentNavigationHandler extends DefaultHandler
  37. implements DocumentNavigationExtensionConstants {
  38. /** Logger instance */
  39. protected static final Log log = LogFactory.getLog(DocumentNavigationHandler.class);
  40. private StringBuffer content = new StringBuffer();
  41. private Stack objectStack = new Stack();
  42. private IFDocumentNavigationHandler navHandler;
  43. private StructureTreeElement structureTreeElement;
  44. private Map<String, StructureTreeElement> structureTreeElements;
  45. /**
  46. * Main constructor.
  47. * @param navHandler the navigation handler that will receive the events
  48. * @param structureTreeElements the elements representing the structure of the document
  49. */
  50. public DocumentNavigationHandler(IFDocumentNavigationHandler navHandler,
  51. Map<String, StructureTreeElement> structureTreeElements) {
  52. this.navHandler = navHandler;
  53. assert structureTreeElements != null;
  54. this.structureTreeElements = structureTreeElements;
  55. }
  56. /** {@inheritDoc} */
  57. public void startElement(String uri, String localName, String qName, Attributes attributes)
  58. throws SAXException {
  59. boolean handled = false;
  60. if (NAMESPACE.equals(uri)) {
  61. if (BOOKMARK_TREE.getLocalName().equals(localName)) {
  62. if (!objectStack.isEmpty()) {
  63. throw new SAXException(localName + " must be the root element!");
  64. }
  65. BookmarkTree bookmarkTree = new BookmarkTree();
  66. objectStack.push(bookmarkTree);
  67. } else if (BOOKMARK.getLocalName().equals(localName)) {
  68. String title = attributes.getValue("title");
  69. String s = attributes.getValue("starting-state");
  70. boolean show = !"hide".equals(s);
  71. Bookmark b = new Bookmark(title, show, null);
  72. Object o = objectStack.peek();
  73. if (o instanceof AbstractAction) {
  74. AbstractAction action = (AbstractAction)objectStack.pop();
  75. o = objectStack.peek();
  76. ((Bookmark)o).setAction(action);
  77. }
  78. if (o instanceof BookmarkTree) {
  79. ((BookmarkTree)o).addBookmark(b);
  80. } else {
  81. ((Bookmark)o).addChildBookmark(b);
  82. }
  83. objectStack.push(b);
  84. } else if (NAMED_DESTINATION.getLocalName().equals(localName)) {
  85. if (!objectStack.isEmpty()) {
  86. throw new SAXException(localName + " must be the root element!");
  87. }
  88. String name = attributes.getValue("name");
  89. NamedDestination dest = new NamedDestination(name, null);
  90. objectStack.push(dest);
  91. } else if (LINK.getLocalName().equals(localName)) {
  92. if (!objectStack.isEmpty()) {
  93. throw new SAXException(localName + " must be the root element!");
  94. }
  95. Rectangle targetRect = XMLUtil.getAttributeAsRectangle(attributes, "rect");
  96. structureTreeElement = structureTreeElements.get(attributes.getValue(
  97. InternalElementMapping.URI, InternalElementMapping.STRUCT_REF));
  98. Link link = new Link(null, targetRect);
  99. objectStack.push(link);
  100. } else if (GOTO_XY.getLocalName().equals(localName)) {
  101. String idref = attributes.getValue("idref");
  102. GoToXYAction action;
  103. if (idref != null) {
  104. action = new GoToXYAction(idref);
  105. } else {
  106. String id = attributes.getValue("id");
  107. int pageIndex = XMLUtil.getAttributeAsInt(attributes, "page-index");
  108. final Point location;
  109. if (pageIndex < 0) {
  110. location = null;
  111. } else {
  112. if (hasNavigation() && !inBookmark()) {
  113. int currentPageIndex = navHandler.getPageIndex();
  114. if (currentPageIndex >= 0) {
  115. pageIndex = currentPageIndex;
  116. }
  117. }
  118. final int x = XMLUtil
  119. .getAttributeAsInt(attributes, "x");
  120. final int y = XMLUtil
  121. .getAttributeAsInt(attributes, "y");
  122. location = new Point(x, y);
  123. }
  124. action = new GoToXYAction(id, pageIndex, location);
  125. }
  126. if (structureTreeElement != null) {
  127. action.setStructureTreeElement(structureTreeElement);
  128. }
  129. objectStack.push(action);
  130. } else if (GOTO_URI.getLocalName().equals(localName)) {
  131. String id = attributes.getValue("id");
  132. String gotoURI = attributes.getValue("uri");
  133. String showDestination = attributes.getValue("show-destination");
  134. boolean newWindow = "new".equals(showDestination);
  135. URIAction action = new URIAction(gotoURI, newWindow);
  136. if (id != null) {
  137. action.setID(id);
  138. }
  139. if (structureTreeElement != null) {
  140. action.setStructureTreeElement(structureTreeElement);
  141. }
  142. objectStack.push(action);
  143. } else {
  144. throw new SAXException(
  145. "Invalid element '" + localName + "' in namespace: " + uri);
  146. }
  147. handled = true;
  148. }
  149. if (!handled) {
  150. if (NAMESPACE.equals(uri)) {
  151. throw new SAXException("Unhandled element '" + localName + "' in namespace: "
  152. + uri);
  153. } else {
  154. log.warn("Unhandled element '" + localName + "' in namespace: " + uri);
  155. }
  156. }
  157. }
  158. private boolean inBookmark() {
  159. return !objectStack.empty() && objectStack.peek() instanceof Bookmark;
  160. }
  161. /** {@inheritDoc} */
  162. public void endElement(String uri, String localName, String qName) throws SAXException {
  163. if (NAMESPACE.equals(uri)) {
  164. try {
  165. if (BOOKMARK_TREE.getLocalName().equals(localName)) {
  166. BookmarkTree tree = (BookmarkTree)objectStack.pop();
  167. if (hasNavigation()) {
  168. this.navHandler.renderBookmarkTree(tree);
  169. }
  170. } else if (BOOKMARK.getLocalName().equals(localName)) {
  171. if (objectStack.peek() instanceof AbstractAction) {
  172. AbstractAction action = (AbstractAction)objectStack.pop();
  173. Bookmark b = (Bookmark)objectStack.pop();
  174. b.setAction(action);
  175. } else {
  176. objectStack.pop();
  177. }
  178. } else if (NAMED_DESTINATION.getLocalName().equals(localName)) {
  179. AbstractAction action = (AbstractAction)objectStack.pop();
  180. NamedDestination dest = (NamedDestination)objectStack.pop();
  181. dest.setAction(action);
  182. if (hasNavigation()) {
  183. this.navHandler.renderNamedDestination(dest);
  184. }
  185. } else if (LINK.getLocalName().equals(localName)) {
  186. AbstractAction action = (AbstractAction)objectStack.pop();
  187. Link link = (Link)objectStack.pop();
  188. link.setAction(action);
  189. if (hasNavigation()) {
  190. this.navHandler.renderLink(link);
  191. }
  192. } else if (localName.startsWith("goto-")) {
  193. if (objectStack.size() == 1) {
  194. //Stand-alone action
  195. AbstractAction action = (AbstractAction)objectStack.pop();
  196. if (hasNavigation()) {
  197. this.navHandler.addResolvedAction(action);
  198. }
  199. }
  200. }
  201. } catch (IFException ife) {
  202. throw new SAXException(ife);
  203. }
  204. }
  205. content.setLength(0); // Reset text buffer (see characters())
  206. }
  207. private boolean hasNavigation() {
  208. return this.navHandler != null;
  209. }
  210. /** {@inheritDoc} */
  211. public void characters(char[] ch, int start, int length) throws SAXException {
  212. content.append(ch, start, length);
  213. }
  214. /** {@inheritDoc} */
  215. public void endDocument() throws SAXException {
  216. assert objectStack.isEmpty();
  217. }
  218. }