Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
../../../../../img/srcFileCovDistChart0.png 48% of files have more coverage
43   180   18   10,75
22   89   0,42   4
4     4,5  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  PebblePDFCreationListener       Line # 61 43 0% 18 69 0% 0.0
 
No Tests
 
1    /*
2    * Copyright (c) 2003-2006, Simon Brown
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    *
8    * - Redistributions of source code must retain the above copyright
9    * notice, this list of conditions and the following disclaimer.
10    *
11    * - Redistributions in binary form must reproduce the above copyright
12    * notice, this list of conditions and the following disclaimer in
13    * the documentation and/or other materials provided with the
14    * distribution.
15    *
16    * - Neither the name of Pebble nor the names of its contributors may
17    * be used to endorse or promote products derived from this software
18    * without specific prior written permission.
19    *
20    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21    * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22    * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23    * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24    * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28    * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30    * POSSIBILITY OF SUCH DAMAGE.
31    */
32    package net.sourceforge.pebble.web.listener;
33   
34    import org.xhtmlrenderer.pdf.ITextRenderer;
35    import org.xhtmlrenderer.pdf.PDFCreationListener;
36   
37    import java.util.Properties;
38    import java.util.Enumeration;
39   
40    import org.w3c.dom.Document;
41    import org.w3c.dom.Element;
42    import org.w3c.dom.Node;
43    import org.w3c.dom.NodeList;
44   
45    import com.lowagie.text.pdf.PdfName;
46    import com.lowagie.text.pdf.PdfString;
47    import com.lowagie.text.pdf.PdfObject;
48    import com.lowagie.text.pdf.PdfWriter;
49    import com.lowagie.text.pdf.PdfDictionary;
50   
51    import org.apache.commons.logging.Log;
52    import org.apache.commons.logging.LogFactory;
53   
54    /**
55    * Add ability to manipulate PDF output document before it's closed and opened.
56    *
57    * Soltuion was adopted from: https://xhtmlrenderer.dev.java.net/servlets/ReadMsg?listName=users&msgNo=1908
58    *
59    * @author Alexander Zagniotov
60    */
 
61    public class PebblePDFCreationListener implements PDFCreationListener {
62   
63    /** the log used by this class */
64    private static Log log = LogFactory.getLog(PebblePDFCreationListener.class);
65   
66    Properties headerTags = null;
67   
 
68  0 toggle public PebblePDFCreationListener() {
69  0 headerTags = new Properties();
70    }
71   
 
72  0 toggle public void parseMetaTags(Document sourceXHTML) {
73   
74  0 try {
75   
76  0 NodeList e = sourceXHTML.getDocumentElement().getElementsByTagName("head");
77  0 Element e1 = (Element) e.item(0);
78  0 NodeList retVal = e1.getElementsByTagName("meta");
79   
80  0 for (int i = 0; i < retVal.getLength(); i++) {
81   
82  0 Element thisNode = (Element) retVal.item(i);
83  0 String name = thisNode.getAttribute("name");
84  0 String content = thisNode.getAttribute("content");
85   
86    //<meta name="XXXXX" content="XXXXX" />
87  0 if (name.length() != 0 && content.length() != 0) {
88  0 headerTags.setProperty(name,content);
89    }
90    }
91    }
92   
93    catch (Exception e) {
94  0 log.warn("Could not parse header meta tags: " + e);
95    }
96    }
97   
98   
99    /**
100    * Called directly before calling open() on the Document.
101    * That allows you to e.g. modify headers before the document is created.
102    *
103    */
 
104  0 toggle public void preOpen(ITextRenderer renderer) {
105   
106  0 try {
107  0 Enumeration e = headerTags.propertyNames();
108   
109  0 PdfWriter writer = renderer.getWriter();
110   
111  0 if (writer == null) {
112  0 log.warn("PdfWriter is null, not able to set header meta tags to PDF document");
113  0 return;
114    }
115   
116    /*
117    Available versions:
118    PdfWriter.VERSION_1_2,
119    PdfWriter.VERSION_1_3,
120    PdfWriter.VERSION_1_4, //default
121    PdfWriter.VERSION_1_5,
122    PdfWriter.VERSION_1_6,
123    PdfWriter.VERSION_1_7
124    */
125  0 writer.setPdfVersion(PdfWriter.VERSION_1_5);
126   
127    //Full compression means that not only page streams are compressed,
128    //but some other objects as well, such as the cross-reference table.
129    //This is only possible since PDF-1.5
130  0 writer.setFullCompression();
131   
132  0 while (e.hasMoreElements()) {
133   
134  0 String key = (String) e.nextElement() ;
135  0 PdfString val = new PdfString(headerTags.getProperty(key), PdfObject.TEXT_UNICODE);
136   
137  0 PdfDictionary dictionary = writer.getInfo();
138   
139  0 if (key.equals("title" )) {
140  0 dictionary.put(PdfName.TITLE, val);
141    }
142  0 else if (key.equals("author")) {
143  0 dictionary.put(PdfName.AUTHOR, val);
144    }
145  0 else if (key.equals("subject")) {
146  0 dictionary.put(PdfName.SUBJECT, val);
147    }
148  0 else if (key.equals("keywords")) {
149  0 dictionary.put(PdfName.KEYWORDS, val);
150    }
151  0 else if (key.equals("creator")) {
152  0 dictionary.put(PdfName.CREATOR, val);
153    }
154  0 else if (key.equals("producer")) {
155  0 dictionary.put(PdfName.PRODUCER, val);
156    }
157    else {
158  0 log.warn("Unexpected header meta tag: " + key + ", value: " + val);
159    }
160    }
161    }
162    catch (Exception e) {
163  0 log.warn("Could not set header meta tags to PDF document: " + e);
164    }
165    }
166   
 
167  0 toggle public void onClose(ITextRenderer renderer) {
168   
169  0 PdfWriter writer = renderer.getWriter();
170   
171  0 if (writer == null) {
172  0 log.warn("PdfWriter is null, not able to set header meta tags to PDF document");
173  0 return;
174    }
175   
176    //Display doc title in the title bar instead of filename,
177  0 writer.setViewerPreferences(PdfWriter.DisplayDocTitle);
178   
179    }
180    }