1 17 18 19 20 package org.apache.fop.pdf; 21 22 import java.io.IOException ; 23 import java.io.OutputStream ; 24 import java.util.Date ; 25 26 import javax.xml.transform.TransformerConfigurationException ; 27 28 import org.apache.xmlgraphics.xmp.Metadata; 29 import org.apache.xmlgraphics.xmp.XMPSerializer; 30 import org.apache.xmlgraphics.xmp.schemas.DublinCoreAdapter; 31 import org.apache.xmlgraphics.xmp.schemas.DublinCoreSchema; 32 import org.apache.xmlgraphics.xmp.schemas.XMPBasicAdapter; 33 import org.apache.xmlgraphics.xmp.schemas.XMPBasicSchema; 34 import org.apache.xmlgraphics.xmp.schemas.pdf.AdobePDFAdapter; 35 import org.apache.xmlgraphics.xmp.schemas.pdf.AdobePDFSchema; 36 import org.apache.xmlgraphics.xmp.schemas.pdf.PDFAAdapter; 37 import org.apache.xmlgraphics.xmp.schemas.pdf.PDFAOldXMPSchema; 38 import org.apache.xmlgraphics.xmp.schemas.pdf.PDFAXMPSchema; 39 40 import org.xml.sax.SAXException ; 41 42 46 public class PDFMetadata extends PDFStream { 47 48 private Metadata xmpMetadata; 49 private boolean readOnly = true; 50 51 52 public PDFMetadata(Metadata xmp, boolean readOnly) { 53 super(); 54 if (xmp == null) { 55 throw new NullPointerException ( 56 "The parameter for the XMP Document must not be null"); 57 } 58 this.xmpMetadata = xmp; 59 this.readOnly = readOnly; 60 } 61 62 63 protected void setupFilterList() { 64 if (!getFilterList().isInitialized()) { 65 getFilterList().addDefaultFilters( 66 getDocumentSafely().getFilterMap(), 67 PDFFilterList.METADATA_FILTER); 68 } 69 super.setupFilterList(); 70 } 71 72 75 public Metadata getMetadata() { 76 return this.xmpMetadata; 77 } 78 79 84 protected int output(java.io.OutputStream stream) 85 throws java.io.IOException { 86 int length = super.output(stream); 87 this.xmpMetadata = null; return length; 89 } 90 91 92 protected void outputRawStreamData(OutputStream out) throws IOException { 93 try { 94 XMPSerializer.writeXMPPacket(xmpMetadata, out, this.readOnly); 95 } catch (TransformerConfigurationException tce) { 96 throw new IOException ("Error setting up Transformer for XMP stream serialization: " 97 + tce.getMessage()); 98 } catch (SAXException saxe) { 99 throw new IOException ("Error while serializing XMP stream: " 100 + saxe.getMessage()); 101 } 102 } 103 104 105 protected String buildStreamDict(String lengthEntry) { 106 final String filterEntry = getFilterList().buildFilterDictEntries(); 107 if (getDocumentSafely().getProfile().getPDFAMode().isPDFA1LevelB() 108 && filterEntry != null && filterEntry.length() > 0) { 109 throw new PDFConformanceException( 110 "The Filter key is prohibited when PDF/A-1 is active"); 111 } 112 final StringBuffer sb = new StringBuffer (128); 113 sb.append(getObjectID()); 114 sb.append("<< "); 115 sb.append("/Type /Metadata"); 116 sb.append("\n/Subtype /XML"); 117 sb.append("\n/Length " + lengthEntry); 118 sb.append("\n" + filterEntry); 119 sb.append("\n>>\n"); 120 return sb.toString(); 121 } 122 123 128 public static Metadata createXMPFromUserAgent(PDFDocument pdfDoc) { 129 Metadata meta = new Metadata(); 130 131 PDFInfo info = pdfDoc.getInfo(); 132 133 if (info.getCreationDate() == null) { 135 Date d = new Date (); 136 info.setCreationDate(d); 137 } 138 139 143 DublinCoreAdapter dc = DublinCoreSchema.getAdapter(meta); 145 if (info.getAuthor() != null) { 146 dc.addCreator(info.getAuthor()); 147 } 148 if (info.getTitle() != null) { 149 dc.setTitle(info.getTitle()); 150 } 151 if (info.getSubject() != null) { 152 dc.addSubject(info.getSubject()); 153 } 154 dc.addDate(info.getCreationDate()); 155 156 PDFAMode pdfaMode = pdfDoc.getProfile().getPDFAMode(); 158 if (pdfaMode.isPDFA1LevelB()) { 159 PDFAAdapter pdfa = PDFAXMPSchema.getAdapter(meta); 160 PDFAAdapter pdfaOld = PDFAOldXMPSchema.getAdapter(meta); 163 pdfa.setPart(1); 164 pdfaOld.setPart(1); 165 if (pdfaMode == PDFAMode.PDFA_1A) { 166 pdfa.setConformance("A"); pdfaOld.setConformance("A"); } else { 169 pdfa.setConformance("B"); pdfaOld.setConformance("B"); } 172 } 173 174 XMPBasicAdapter xmpBasic = XMPBasicSchema.getAdapter(meta); 176 xmpBasic.setCreateDate(info.getCreationDate()); 177 PDFProfile profile = pdfDoc.getProfile(); 178 if (profile.isModDateRequired()) { 179 xmpBasic.setModifyDate(info.getCreationDate()); 180 } 181 if (info.getCreator() != null) { 182 xmpBasic.setCreatorTool(info.getCreator()); 183 } 184 185 AdobePDFAdapter adobePDF = AdobePDFSchema.getAdapter(meta); 186 if (info.getKeywords() != null) { 187 adobePDF.setKeywords(info.getKeywords()); 188 } 189 if (info.getProducer() != null) { 190 adobePDF.setProducer(info.getProducer()); 191 } 192 adobePDF.setPDFVersion(pdfDoc.getPDFVersionString()); 193 194 195 return meta; 196 } 197 198 204 public static void updateInfoFromMetadata(Metadata meta, PDFInfo info) { 205 DublinCoreAdapter dc = DublinCoreSchema.getAdapter(meta); 206 info.setTitle(dc.getTitle()); 207 String [] creators = dc.getCreators(); 208 if (creators != null && creators.length > 0) { 209 info.setAuthor(creators[0]); 210 } else { 211 info.setAuthor(null); 212 } 213 String [] subjects = dc.getSubjects(); 214 if (subjects != null && subjects.length > 0) { 217 info.setSubject(subjects[0]); 218 } else { 219 info.setSubject(null); 220 } 221 222 AdobePDFAdapter pdf = AdobePDFSchema.getAdapter(meta); 223 info.setKeywords(pdf.getKeywords()); 224 info.setProducer(pdf.getProducer()); 225 226 XMPBasicAdapter xmpBasic = XMPBasicSchema.getAdapter(meta); 227 info.setCreator(xmpBasic.getCreatorTool()); 228 Date d; 229 d = xmpBasic.getCreateDate(); 230 xmpBasic.setCreateDate(d); info.setCreationDate(d); 234 d = xmpBasic.getModifyDate(); 235 if (d != null) { xmpBasic.setModifyDate(d); 237 info.setModDate(d); 238 } 239 } 240 } 241 | Popular Tags |