Mercurial > hg > mpdl-group
comparison software/eXist/mpdl-modules/src/org/exist/xquery/modules/mpdldoc/Html2Pdf.java @ 0:408254cf2f1d
Erstellung
author | Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de> |
---|---|
date | Wed, 24 Nov 2010 17:24:23 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:408254cf2f1d |
---|---|
1 /* | |
2 * eXist Open Source Native XML Database: Extension module | |
3 * Copyright (C) 2008 Josef Willenborg | |
4 * jwillenborg@mpiwg-berlin.mpg.de | |
5 * http://www.mpiwg-berlin.mpg.de | |
6 * | |
7 * This program is free software; you can redistribute it and/or | |
8 * modify it under the terms of the GNU Lesser General Public License | |
9 * as published by the Free Software Foundation; either version 2 | |
10 * of the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 * GNU Lesser General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Lesser General Public License | |
18 * along with this program; if not, write to the Free Software | |
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
20 * | |
21 * $Id: $ | |
22 */ | |
23 package org.exist.xquery.modules.mpdldoc; | |
24 | |
25 import org.exist.dom.QName; | |
26 import org.exist.storage.serializers.Serializer; | |
27 import org.exist.xquery.BasicFunction; | |
28 import org.exist.xquery.Cardinality; | |
29 import org.exist.xquery.FunctionSignature; | |
30 import org.exist.xquery.XPathException; | |
31 import org.exist.xquery.XQueryContext; | |
32 import org.exist.xquery.value.Base64Binary; | |
33 import org.exist.xquery.value.NodeValue; | |
34 import org.exist.xquery.value.Sequence; | |
35 import org.exist.xquery.value.SequenceType; | |
36 import org.exist.xquery.value.Type; | |
37 import org.xml.sax.SAXException; | |
38 | |
39 import de.mpg.mpiwg.berlin.mpdl.exception.ApplicationException; | |
40 import de.mpg.mpiwg.berlin.mpdl.util.MpdlITextRenderer; | |
41 import de.mpg.mpiwg.berlin.mpdl.util.StringUtilEscapeChars; | |
42 | |
43 /** | |
44 * @author Josef Willenborg (jwillenborg@mpiwg-berlin.mpg.de) | |
45 */ | |
46 public class Html2Pdf extends BasicFunction { | |
47 | |
48 public final static FunctionSignature signature = | |
49 new FunctionSignature( | |
50 new QName("html2pdf", MPDLDocModule.NAMESPACE_URI, MPDLDocModule.PREFIX), | |
51 "A function which converts the input HTML fragment to pdf", | |
52 new SequenceType[] { | |
53 new SequenceType(Type.NODE, Cardinality.EXACTLY_ONE), | |
54 new SequenceType(Type.STRING, Cardinality.EXACTLY_ONE), | |
55 new SequenceType(Type.STRING, Cardinality.EXACTLY_ONE), | |
56 new SequenceType(Type.STRING, Cardinality.EXACTLY_ONE), | |
57 new SequenceType(Type.STRING, Cardinality.EXACTLY_ONE), | |
58 new SequenceType(Type.STRING, Cardinality.EXACTLY_ONE) | |
59 }, | |
60 new SequenceType(Type.BYTE, Cardinality.EXACTLY_ONE)); | |
61 | |
62 public Html2Pdf(XQueryContext context) { | |
63 super(context, signature); | |
64 } | |
65 | |
66 public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException { | |
67 try { | |
68 Sequence firstSeq = args[0]; | |
69 Sequence secondSeq = args[1]; | |
70 Sequence thirdSeq = args[2]; | |
71 Sequence fourthSeq = args[3]; | |
72 Sequence fifthSeq = args[4]; | |
73 Sequence sixthSeq = args[5]; | |
74 if (firstSeq.isEmpty() || secondSeq.isEmpty() || thirdSeq.isEmpty() || fourthSeq.isEmpty() || fifthSeq.isEmpty() || sixthSeq.isEmpty()) | |
75 return Sequence.EMPTY_SEQUENCE; | |
76 NodeValue nodeValue= (NodeValue) firstSeq.itemAt(0); | |
77 Serializer serializer = context.getBroker().getSerializer(); | |
78 serializer.reset(); | |
79 String nodeValueStr = serializer.serialize(nodeValue); | |
80 | |
81 String language = secondSeq.getStringValue(); | |
82 String topLeftStrTmp = thirdSeq.getStringValue(); | |
83 String topRightStrTmp = fourthSeq.getStringValue(); | |
84 String bottomLeftStrTmp = fifthSeq.getStringValue(); | |
85 String bottomRightStrTmp = sixthSeq.getStringValue(); | |
86 String topLeftStr = """ + StringUtilEscapeChars.deresolveXmlEntities(topLeftStrTmp) + """; | |
87 String topRightStr = """ + StringUtilEscapeChars.deresolveXmlEntities(topRightStrTmp) + """; | |
88 String bottomLeftStr = """ + StringUtilEscapeChars.deresolveXmlEntities(bottomLeftStrTmp) + """; | |
89 String bottomRightStr = """ + StringUtilEscapeChars.deresolveXmlEntities(bottomRightStrTmp) + """; | |
90 | |
91 MpdlITextRenderer mpdlRenderer = MpdlITextRenderer.getInstance(); | |
92 String singlePageStr = nodeValueStr.replaceAll("class=\"page\">", "class=\"singlePage\">"); | |
93 byte[] pdfBytes = mpdlRenderer.createPdf(singlePageStr, language, topLeftStr, topRightStr, bottomLeftStr, bottomRightStr); | |
94 | |
95 return new Base64Binary(pdfBytes); | |
96 } catch (ApplicationException e) { | |
97 throw new XPathException(e.getMessage()); | |
98 } catch (SAXException e) { | |
99 throw new XPathException(e); | |
100 } | |
101 } | |
102 } |