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.

PDFStructureTreeTestCase.java 4.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.pdf;
  19. import java.io.ByteArrayInputStream;
  20. import java.io.ByteArrayOutputStream;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import javax.xml.transform.Result;
  24. import javax.xml.transform.Source;
  25. import javax.xml.transform.Transformer;
  26. import javax.xml.transform.TransformerException;
  27. import javax.xml.transform.TransformerFactory;
  28. import javax.xml.transform.sax.SAXResult;
  29. import javax.xml.transform.stream.StreamSource;
  30. import org.junit.Assert;
  31. import org.junit.Test;
  32. import org.xml.sax.SAXException;
  33. import org.apache.fop.apps.FOUserAgent;
  34. import org.apache.fop.apps.Fop;
  35. import org.apache.fop.apps.FopFactory;
  36. import org.apache.fop.apps.MimeConstants;
  37. public class PDFStructureTreeTestCase {
  38. @Test
  39. public void testRemoveUnusedStructs() throws Exception {
  40. String fo = "<fo:root xmlns:fo=\"http://www.w3.org/1999/XSL/Format\">\n"
  41. + " <fo:layout-master-set>\n"
  42. + " <fo:simple-page-master page-width=\"8.5in\" page-height=\"11in\" master-name=\"First\">\n"
  43. + " <fo:region-body region-name=\"Body\"/>\n"
  44. + " <fo:region-before extent=\"1in\" region-name=\"Header\"/>\n"
  45. + " </fo:simple-page-master>\n"
  46. + " <fo:simple-page-master page-width=\"8.5in\" page-height=\"11in\" master-name=\"Rest\">\n"
  47. + " <fo:region-body region-name=\"Body\"/>\n"
  48. + " <fo:region-before extent=\"1in\" region-name=\"Header Rest\"/>\n"
  49. + " </fo:simple-page-master>\n"
  50. + " <fo:page-sequence-master master-name=\"PSM\">\n"
  51. + " <fo:repeatable-page-master-alternatives>\n"
  52. + " <fo:conditional-page-master-reference page-position=\"first\" master-reference=\"First\"/>\n"
  53. + " <fo:conditional-page-master-reference page-position=\"rest\" master-reference=\"Rest\"/>\n"
  54. + " </fo:repeatable-page-master-alternatives>\n"
  55. + " </fo:page-sequence-master>\n"
  56. + " </fo:layout-master-set>\n"
  57. + " <fo:page-sequence master-reference=\"PSM\">\n"
  58. + " <fo:static-content flow-name=\"Header\">\n"
  59. + " <fo:block><fo:external-graphic src=\"test/resources/fop/image/logo.jpg\" /></fo:block>\n"
  60. + " </fo:static-content>\n"
  61. + " <fo:static-content flow-name=\"Header Rest\">\n"
  62. + " <fo:block><fo:external-graphic src=\"test/resources/fop/svg/logo.jpg\" /></fo:block>\n"
  63. + " </fo:static-content>\n"
  64. + " <fo:flow flow-name=\"Body\">\n"
  65. + " <fo:block>test</fo:block>\n"
  66. + " </fo:flow>\n"
  67. + " </fo:page-sequence>\n"
  68. + "</fo:root>";
  69. ByteArrayOutputStream bos = foToOutput(fo);
  70. String pdf = bos.toString();
  71. Assert.assertEquals(pdf.split("/S /Figure").length, 2);
  72. Assert.assertEquals(pdf.split("/S /").length, 11);
  73. }
  74. private ByteArrayOutputStream foToOutput(String fo)
  75. throws IOException, SAXException, TransformerException {
  76. FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
  77. FOUserAgent userAgent = fopFactory.newFOUserAgent();
  78. userAgent.setAccessibility(true);
  79. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  80. Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, userAgent, bos);
  81. Transformer transformer = TransformerFactory.newInstance().newTransformer();
  82. Source src = new StreamSource(new ByteArrayInputStream(fo.getBytes("UTF-8")));
  83. Result res = new SAXResult(fop.getDefaultHandler());
  84. transformer.transform(src, res);
  85. return bos;
  86. }
  87. }