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.1KB

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