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.

DocumentNavigationHandler.java 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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(
  97. attributes.getValue(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. final int x = XMLUtil
  113. .getAttributeAsInt(attributes, "x");
  114. final int y = XMLUtil
  115. .getAttributeAsInt(attributes, "y");
  116. location = new Point(x, y);
  117. }
  118. action = new GoToXYAction(id, pageIndex, location);
  119. }
  120. if (structureTreeElement != null) {
  121. action.setStructureTreeElement(structureTreeElement);
  122. }
  123. objectStack.push(action);
  124. } else if (GOTO_URI.getLocalName().equals(localName)) {
  125. String id = attributes.getValue("id");
  126. String gotoURI = attributes.getValue("uri");
  127. String showDestination = attributes.getValue("show-destination");
  128. boolean newWindow = "new".equals(showDestination);
  129. URIAction action = new URIAction(gotoURI, newWindow);
  130. if (id != null) {
  131. action.setID(id);
  132. }
  133. if (structureTreeElement != null) {
  134. action.setStructureTreeElement(structureTreeElement);
  135. }
  136. objectStack.push(action);
  137. } else {
  138. throw new SAXException(
  139. "Invalid element '" + localName + "' in namespace: " + uri);
  140. }
  141. handled = true;
  142. }
  143. if (!handled) {
  144. if (NAMESPACE.equals(uri)) {
  145. throw new SAXException("Unhandled element '" + localName + "' in namespace: "
  146. + uri);
  147. } else {
  148. log.warn("Unhandled element '" + localName + "' in namespace: " + uri);
  149. }
  150. }
  151. }
  152. /** {@inheritDoc} */
  153. public void endElement(String uri, String localName, String qName) throws SAXException {
  154. if (NAMESPACE.equals(uri)) {
  155. try {
  156. if (BOOKMARK_TREE.getLocalName().equals(localName)) {
  157. BookmarkTree tree = (BookmarkTree)objectStack.pop();
  158. if (hasNavigation()) {
  159. this.navHandler.renderBookmarkTree(tree);
  160. }
  161. } else if (BOOKMARK.getLocalName().equals(localName)) {
  162. if (objectStack.peek() instanceof AbstractAction) {
  163. AbstractAction action = (AbstractAction)objectStack.pop();
  164. Bookmark b = (Bookmark)objectStack.pop();
  165. b.setAction(action);
  166. } else {
  167. objectStack.pop();
  168. }
  169. } else if (NAMED_DESTINATION.getLocalName().equals(localName)) {
  170. AbstractAction action = (AbstractAction)objectStack.pop();
  171. NamedDestination dest = (NamedDestination)objectStack.pop();
  172. dest.setAction(action);
  173. if (hasNavigation()) {
  174. this.navHandler.renderNamedDestination(dest);
  175. }
  176. } else if (LINK.getLocalName().equals(localName)) {
  177. AbstractAction action = (AbstractAction)objectStack.pop();
  178. Link link = (Link)objectStack.pop();
  179. link.setAction(action);
  180. if (hasNavigation()) {
  181. this.navHandler.renderLink(link);
  182. }
  183. } else if (localName.startsWith("goto-")) {
  184. if (objectStack.size() == 1) {
  185. //Stand-alone action
  186. AbstractAction action = (AbstractAction)objectStack.pop();
  187. if (hasNavigation()) {
  188. this.navHandler.addResolvedAction(action);
  189. }
  190. }
  191. }
  192. } catch (IFException ife) {
  193. throw new SAXException(ife);
  194. }
  195. }
  196. content.setLength(0); // Reset text buffer (see characters())
  197. }
  198. private boolean hasNavigation() {
  199. return this.navHandler != null;
  200. }
  201. /** {@inheritDoc} */
  202. public void characters(char[] ch, int start, int length) throws SAXException {
  203. content.append(ch, start, length);
  204. }
  205. /** {@inheritDoc} */
  206. public void endDocument() throws SAXException {
  207. assert objectStack.isEmpty();
  208. }
  209. }