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.

PDFAMetadataTestCase.java 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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.pdf;
  19. import java.util.ArrayList;
  20. import java.util.Calendar;
  21. import java.util.List;
  22. import java.util.Locale;
  23. import java.util.TimeZone;
  24. import org.junit.Test;
  25. import static org.junit.Assert.assertEquals;
  26. import static org.junit.Assert.assertFalse;
  27. import static org.junit.Assert.assertNotNull;
  28. import static org.junit.Assert.assertNull;
  29. import static org.junit.Assert.assertTrue;
  30. import org.apache.xmlgraphics.util.QName;
  31. import org.apache.xmlgraphics.xmp.Metadata;
  32. import org.apache.xmlgraphics.xmp.XMPArray;
  33. import org.apache.xmlgraphics.xmp.XMPArrayType;
  34. import org.apache.xmlgraphics.xmp.XMPConstants;
  35. import org.apache.xmlgraphics.xmp.XMPProperty;
  36. import org.apache.xmlgraphics.xmp.schemas.DublinCoreAdapter;
  37. import org.apache.xmlgraphics.xmp.schemas.DublinCoreSchema;
  38. import org.apache.xmlgraphics.xmp.schemas.XMPBasicAdapter;
  39. import org.apache.xmlgraphics.xmp.schemas.XMPBasicSchema;
  40. import org.apache.xmlgraphics.xmp.schemas.pdf.AdobePDFAdapter;
  41. import org.apache.xmlgraphics.xmp.schemas.pdf.AdobePDFSchema;
  42. import org.apache.fop.pdf.PDFAMode;
  43. import org.apache.fop.pdf.PDFDocument;
  44. import org.apache.fop.pdf.PDFInfo;
  45. import org.apache.fop.pdf.PDFMetadata;
  46. import org.apache.fop.pdf.PDFUAMode;
  47. /**
  48. * Test case for PDF/A metadata handling.
  49. */
  50. public class PDFAMetadataTestCase {
  51. @Test
  52. public void testInfoUpdate() throws Exception {
  53. Metadata meta = new Metadata();
  54. DublinCoreAdapter dc = DublinCoreSchema.getAdapter(meta);
  55. dc.setTitle("MyTitle");
  56. dc.setDescription(null, "MySubject");
  57. dc.addCreator("That's me");
  58. AdobePDFAdapter pdf = AdobePDFSchema.getAdapter(meta);
  59. pdf.setKeywords("XSL-FO XML");
  60. pdf.setProducer("SuperFOP");
  61. XMPBasicAdapter xmp = XMPBasicSchema.getAdapter(meta);
  62. xmp.setCreatorTool("WonderFOP");
  63. Calendar cal1 = Calendar.getInstance(TimeZone.getTimeZone("Europe/Zurich"));
  64. cal1.set(2007, Calendar.JUNE, 5, 21, 49, 13);
  65. cal1.set(Calendar.MILLISECOND, 0);
  66. xmp.setCreateDate(cal1.getTime());
  67. Calendar cal2 = Calendar.getInstance(TimeZone.getTimeZone("Europe/Zurich"));
  68. cal2.set(2007, Calendar.JUNE, 6, 8, 15, 59);
  69. cal2.set(Calendar.MILLISECOND, 0);
  70. xmp.setModifyDate(cal2.getTime());
  71. PDFInfo info = new PDFInfo();
  72. assertNull(info.getTitle());
  73. PDFMetadata.updateInfoFromMetadata(meta, info);
  74. assertEquals("MyTitle", info.getTitle());
  75. assertEquals("MySubject", info.getSubject());
  76. assertEquals("That's me", info.getAuthor());
  77. assertEquals("XSL-FO XML", info.getKeywords());
  78. assertEquals("SuperFOP", info.getProducer());
  79. assertEquals("WonderFOP", info.getCreator());
  80. assertEquals(cal1.getTime(), info.getCreationDate());
  81. assertEquals(cal2.getTime(), info.getModDate());
  82. }
  83. @Test
  84. public void testXMPUpdate() throws Exception {
  85. PDFDocument doc = new PDFDocument("SuperFOP");
  86. PDFInfo info = doc.getInfo();
  87. info.setTitle("MyTitle");
  88. info.setSubject("MySubject");
  89. info.setAuthor("That's me");
  90. info.setKeywords("XSL-FO XML");
  91. //info.setProducer("SuperFOP");
  92. info.setCreator("WonderFOP");
  93. Calendar cal1 = Calendar.getInstance(TimeZone.getTimeZone("Europe/Zurich"));
  94. cal1.set(2007, Calendar.JUNE, 5, 21, 49, 13);
  95. cal1.set(Calendar.MILLISECOND, 0);
  96. info.setCreationDate(cal1.getTime());
  97. Calendar cal2 = Calendar.getInstance(TimeZone.getTimeZone("Europe/Zurich"));
  98. cal2.set(2007, Calendar.JUNE, 6, 8, 15, 59);
  99. cal2.set(Calendar.MILLISECOND, 0);
  100. info.setModDate(cal2.getTime());
  101. Metadata meta = PDFMetadata.createXMPFromPDFDocument(doc);
  102. DublinCoreAdapter dc = DublinCoreSchema.getAdapter(meta);
  103. assertEquals("MyTitle", dc.getTitle());
  104. assertEquals("MySubject", dc.getDescription());
  105. assertEquals(1, dc.getCreators().length);
  106. assertEquals("That's me", dc.getCreators()[0]);
  107. AdobePDFAdapter pdf = AdobePDFSchema.getAdapter(meta);
  108. assertEquals("XSL-FO XML", pdf.getKeywords());
  109. assertEquals("SuperFOP", pdf.getProducer());
  110. XMPBasicAdapter xmp = XMPBasicSchema.getAdapter(meta);
  111. assertEquals("WonderFOP", xmp.getCreatorTool());
  112. assertEquals(cal1.getTime(), xmp.getCreateDate());
  113. assertEquals(cal2.getTime(), xmp.getModifyDate());
  114. }
  115. @Test
  116. public void testXMPMetaDataForLanguageAndDateForPDF2A() throws Exception {
  117. PDFDocument doc = new PDFDocument("SuperFOP");
  118. doc.getRoot().setLanguage(new Locale("en"));
  119. doc.getProfile().setPDFAMode(PDFAMode.PDFA_2A);
  120. Metadata meta = PDFMetadata.createXMPFromPDFDocument(doc);
  121. assertTrue(meta.getProperty("http://purl.org/dc/elements/1.1/", "language").getValue().toString()
  122. .contains("rdf:Bag"));
  123. assertTrue(meta.getProperty("http://purl.org/dc/elements/1.1/", "date").getValue().toString()
  124. .contains("rdf:Seq"));
  125. }
  126. @Test
  127. public void testXMPMetaDataForLanguageAndDateForPDF1A() throws Exception {
  128. PDFDocument doc = new PDFDocument("SuperFOP");
  129. doc.getRoot().setLanguage(new Locale("en"));
  130. doc.getProfile().setPDFAMode(PDFAMode.PDFA_1A);
  131. Metadata meta = PDFMetadata.createXMPFromPDFDocument(doc);
  132. assertFalse(meta.getProperty("http://purl.org/dc/elements/1.1/", "language").getValue().toString()
  133. .contains("rdf:Bag"));
  134. assertFalse(meta.getProperty("http://purl.org/dc/elements/1.1/", "date").getValue().toString()
  135. .contains("rdf:Seq"));
  136. }
  137. @Test
  138. public void testPDFAExtensionSchema() {
  139. PDFDocument doc = new PDFDocument("SuperFOP");
  140. doc.getProfile().setPDFAMode(PDFAMode.PDFA_1A);
  141. doc.getProfile().setPDFUAMode(PDFUAMode.PDFUA_1);
  142. Metadata meta = PDFMetadata.createXMPFromPDFDocument(doc);
  143. XMPProperty schemas = meta.getProperty(XMPConstants.PDF_A_EXTENSION, "schemas");
  144. assertProperties(schemas, XMPConstants.PDF_A_EXTENSION, "schemas", null, null);
  145. assertNotNull("When PDF/A and PDF/UA are both active, we need to add an "
  146. + "extension element to avoid validation errors from PDF/A validators", schemas);
  147. List<XMPProperty> schemasArrayList = assertArrayValue(schemas, XMPArrayType.BAG);
  148. assertProperties(schemasArrayList.get(0), XMPConstants.PDF_A_SCHEMA, "schema",
  149. "pdfaSchema", "PDF/UA identification schema");
  150. assertProperties(schemasArrayList.get(1), XMPConstants.PDF_A_SCHEMA, "namespaceURI",
  151. "pdfaSchema", "http://www.aiim.org/pdfua/ns/id/");
  152. assertProperties(schemasArrayList.get(2), XMPConstants.PDF_A_SCHEMA, "prefix",
  153. "pdfaSchema", "pdfuaid");
  154. assertProperties(schemasArrayList.get(3), XMPConstants.PDF_A_SCHEMA, "property",
  155. "pdfaSchema", null);
  156. List<XMPProperty> propertyArrayList = assertArrayValue(schemasArrayList.get(3), XMPArrayType.SEQ);
  157. assertProperties(propertyArrayList.get(0), XMPConstants.PDF_A_PROPERTY, "name",
  158. "pdfaProperty", "part");
  159. assertProperties(propertyArrayList.get(1), XMPConstants.PDF_A_PROPERTY, "valueType",
  160. "pdfaProperty", "Integer");
  161. assertProperties(propertyArrayList.get(2), XMPConstants.PDF_A_PROPERTY, "category",
  162. "pdfaProperty", "internal");
  163. assertProperties(propertyArrayList.get(3), XMPConstants.PDF_A_PROPERTY, "description",
  164. "pdfaProperty", "Indicates, which part of ISO 14289 standard is followed");
  165. }
  166. private void assertProperties(XMPProperty prop, String ns, String localName, String prefix,
  167. String value) {
  168. QName name = prop.getName();
  169. assertEquals("Property must have expected value or the validator might fail",
  170. ns, name.getNamespaceURI());
  171. assertEquals("Property must have expected value or the validator might fail",
  172. localName, name.getLocalName());
  173. assertEquals("Property must have expected value or the validator might fail",
  174. prefix, name.getPrefix());
  175. if (value != null) {
  176. assertEquals("Property must have expected value or the validator might fail",
  177. value, prop.getValue());
  178. }
  179. }
  180. private List<XMPProperty> assertArrayValue(XMPProperty prop, XMPArrayType type) {
  181. Object value = prop.getValue();
  182. assertEquals("Property value must be an array", XMPArray.class, value.getClass());
  183. XMPArray array = (XMPArray) value;
  184. assertEquals("The property expects an array of the given type",
  185. type, array.getType());
  186. assertEquals("Array must only have 1 element with 4 properties inside",
  187. 1, array.getSize());
  188. Object arrayValue = array.getValue(0);
  189. assertEquals("Array must only have 1 element with 4 properties inside",
  190. ArrayList.class, arrayValue.getClass());
  191. List<XMPProperty> arrayList = (List<XMPProperty>) arrayValue;
  192. assertEquals("Array must only have 1 element with 4 properties inside",
  193. 4, arrayList.size());
  194. return arrayList;
  195. }
  196. }