Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

DocumentNavigationHandlerTestCase.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.extensions;
  19. import java.awt.Dimension;
  20. import java.io.ByteArrayOutputStream;
  21. import java.io.File;
  22. import java.util.ArrayList;
  23. import java.util.Arrays;
  24. import java.util.HashMap;
  25. import java.util.Iterator;
  26. import java.util.List;
  27. import javax.xml.transform.stream.StreamResult;
  28. import org.junit.Assert;
  29. import org.junit.Test;
  30. import org.xml.sax.Attributes;
  31. import org.xml.sax.SAXException;
  32. import static org.mockito.Mockito.mock;
  33. import static org.mockito.Mockito.when;
  34. import org.apache.xmlgraphics.util.QName;
  35. import org.apache.fop.accessibility.StructureTreeElement;
  36. import org.apache.fop.apps.FOUserAgent;
  37. import org.apache.fop.apps.FopFactory;
  38. import org.apache.fop.fonts.FontInfo;
  39. import org.apache.fop.render.intermediate.IFContext;
  40. import org.apache.fop.render.intermediate.IFException;
  41. import org.apache.fop.render.intermediate.extensions.AbstractAction;
  42. import org.apache.fop.render.intermediate.extensions.Bookmark;
  43. import org.apache.fop.render.intermediate.extensions.BookmarkTree;
  44. import org.apache.fop.render.intermediate.extensions.DocumentNavigationExtensionConstants;
  45. import org.apache.fop.render.intermediate.extensions.DocumentNavigationHandler;
  46. import org.apache.fop.render.intermediate.extensions.GoToXYAction;
  47. import org.apache.fop.render.pdf.PDFDocumentHandler;
  48. import org.apache.fop.render.pdf.PDFDocumentNavigationHandler;
  49. public class DocumentNavigationHandlerTestCase {
  50. @Test
  51. public void testGotoXY() throws SAXException, IFException {
  52. FOUserAgent ua = FopFactory.newInstance(new File(".").toURI()).newFOUserAgent();
  53. PDFDocumentHandler documentHandler = new PDFDocumentHandler(new IFContext(ua));
  54. documentHandler.setResult(new StreamResult(new ByteArrayOutputStream()));
  55. documentHandler.setFontInfo(new FontInfo());
  56. documentHandler.startDocument();
  57. documentHandler.startPage(0, "", "", new Dimension());
  58. documentHandler.endPage();
  59. int currentPage = 1;
  60. documentHandler.startPage(currentPage, "", "", new Dimension());
  61. final List<GoToXYAction> goToXYActions = new ArrayList<GoToXYAction>();
  62. PDFDocumentNavigationHandler pdfDocumentNavigationHandler = new PDFDocumentNavigationHandler(documentHandler) {
  63. public void addResolvedAction(AbstractAction action) throws IFException {
  64. super.addResolvedAction(action);
  65. goToXYActions.add((GoToXYAction) action);
  66. }
  67. };
  68. DocumentNavigationHandler navigationHandler = new DocumentNavigationHandler(pdfDocumentNavigationHandler,
  69. new HashMap<String, StructureTreeElement>());
  70. QName xy = DocumentNavigationExtensionConstants.GOTO_XY;
  71. Attributes attributes = mock(Attributes.class);
  72. when(attributes.getValue("page-index")).thenReturn("0");
  73. when(attributes.getValue("x")).thenReturn("0");
  74. when(attributes.getValue("y")).thenReturn("0");
  75. navigationHandler.startElement(xy.getNamespaceURI(), xy.getLocalName(), null, attributes);
  76. navigationHandler.endElement(xy.getNamespaceURI(), xy.getLocalName(), null);
  77. documentHandler.endPage();
  78. //Since user may merge IF files we want to use current page
  79. Assert.assertEquals(goToXYActions.get(0).getPageIndex(), currentPage);
  80. }
  81. @Test
  82. public void testGotoXYUniqueLinks() throws IFException, SAXException {
  83. FOUserAgent ua = FopFactory.newInstance(new File(".").toURI()).newFOUserAgent();
  84. PDFDocumentHandler documentHandler = new PDFDocumentHandler(new IFContext(ua));
  85. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  86. documentHandler.setResult(new StreamResult(bos));
  87. documentHandler.setFontInfo(new FontInfo());
  88. documentHandler.startDocument();
  89. PDFDocumentNavigationHandler pdfDocumentNavigationHandler = new PDFDocumentNavigationHandler(documentHandler);
  90. DocumentNavigationHandler navigationHandler = new DocumentNavigationHandler(pdfDocumentNavigationHandler,
  91. new HashMap<String, StructureTreeElement>());
  92. QName xy = DocumentNavigationExtensionConstants.GOTO_XY;
  93. Attributes attributes = mock(Attributes.class);
  94. when(attributes.getValue("page-index")).thenReturn("0");
  95. when(attributes.getValue("x")).thenReturn("0");
  96. when(attributes.getValue("y")).thenReturn("0");
  97. documentHandler.startPage(0, "", "", new Dimension());
  98. navigationHandler.startElement(xy.getNamespaceURI(), xy.getLocalName(), null, attributes);
  99. navigationHandler.endElement(xy.getNamespaceURI(), xy.getLocalName(), null);
  100. documentHandler.endPage();
  101. documentHandler.startPage(1, "", "", new Dimension());
  102. navigationHandler.startElement(xy.getNamespaceURI(), xy.getLocalName(), null, attributes);
  103. navigationHandler.endElement(xy.getNamespaceURI(), xy.getLocalName(), null);
  104. documentHandler.endPage();
  105. Iterator<String> i = Arrays.asList(bos.toString().split("\n")).iterator();
  106. List<String> pageLink = new ArrayList<String>();
  107. while (i.hasNext()) {
  108. if (i.next().equals("/S /GoTo")) {
  109. pageLink.add(i.next());
  110. }
  111. }
  112. Assert.assertEquals(pageLink.size(), 2);
  113. Assert.assertFalse(pageLink.get(0).equals(pageLink.get(1)));
  114. }
  115. @Test
  116. public void testBookmarkGotoXY() throws SAXException, IFException {
  117. FOUserAgent ua = FopFactory.newInstance(new File(".").toURI()).newFOUserAgent();
  118. PDFDocumentHandler documentHandler = new PDFDocumentHandler(new IFContext(ua));
  119. documentHandler.setResult(new StreamResult(new ByteArrayOutputStream()));
  120. documentHandler.setFontInfo(new FontInfo());
  121. documentHandler.startDocument();
  122. documentHandler.startPage(0, "", "", new Dimension());
  123. documentHandler.endPage();
  124. int currentPage = 1;
  125. documentHandler.startPage(currentPage, "", "", new Dimension());
  126. final List<BookmarkTree> trees = new ArrayList<BookmarkTree>();
  127. PDFDocumentNavigationHandler pdfDocumentNavigationHandler = new PDFDocumentNavigationHandler(documentHandler) {
  128. public void renderBookmarkTree(BookmarkTree tree) throws IFException {
  129. trees.add(tree);
  130. }
  131. };
  132. DocumentNavigationHandler navigationHandler = new DocumentNavigationHandler(pdfDocumentNavigationHandler,
  133. new HashMap<String, StructureTreeElement>());
  134. Attributes attributes = mock(Attributes.class);
  135. when(attributes.getValue("page-index")).thenReturn("0");
  136. when(attributes.getValue("x")).thenReturn("0");
  137. when(attributes.getValue("y")).thenReturn("0");
  138. for (QName q : Arrays.asList(DocumentNavigationExtensionConstants.BOOKMARK_TREE,
  139. DocumentNavigationExtensionConstants.BOOKMARK,
  140. DocumentNavigationExtensionConstants.GOTO_XY)) {
  141. navigationHandler.startElement(q.getNamespaceURI(), q.getLocalName(), null, attributes);
  142. }
  143. for (QName q : Arrays.asList(DocumentNavigationExtensionConstants.GOTO_XY,
  144. DocumentNavigationExtensionConstants.BOOKMARK,
  145. DocumentNavigationExtensionConstants.BOOKMARK_TREE)) {
  146. navigationHandler.endElement(q.getNamespaceURI(), q.getLocalName(), null);
  147. }
  148. documentHandler.endPage();
  149. Bookmark b = (Bookmark) trees.get(0).getBookmarks().get(0);
  150. GoToXYAction a = (GoToXYAction) b.getAction();
  151. Assert.assertEquals(a.getPageIndex(), 0);
  152. }
  153. }