/
PDFResource.java
187 lines (158 loc) · 6.52 KB
/
PDFResource.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* Copyright 2006 Wyona
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.wyona.org/licenses/APACHE-LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wyona.yanel.impl.resources;
import org.wyona.yanel.core.Path;
import org.wyona.yanel.core.Resource;
import org.wyona.yanel.core.api.attributes.ViewableV2;
import org.wyona.yanel.core.attributes.viewable.View;
import org.wyona.yanel.core.attributes.viewable.ViewDescriptor;
import org.wyona.yanel.core.source.SourceResolver;
import org.wyona.yarep.core.Repository;
import org.wyona.yanel.core.util.WildcardReplacerHelper;
import org.apache.log4j.Category;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.MimeConstants;
import java.io.File;
import java.io.InputStream;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Source;
import javax.xml.transform.Result;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;
/**
*
*/
public class PDFResource extends Resource implements ViewableV2 {
private static Category log = Category.getInstance(PDFResource.class);
/**
*
*/
public PDFResource() {
}
/**
*
*/
public ViewDescriptor[] getViewDescriptors() {
return null;
}
/**
*
*/
public View getView(String viewId) throws Exception {
if (!exists()) {
log.warn("No such XML '" + getDataPath() + "' in order to generate PDF!");
throw new org.wyona.yanel.core.ResourceNotFoundException("No such XML '" + getDataPath() + "' in order to generate PDF!");
}
View defaultView = new View();
defaultView.setMimeType(getMimeType(viewId));
defaultView.setResponse(false); // This resource writes directly into the response output stream (because of memory it's better to stream large documents)
try {
String yanelPath = getDataPath();
Repository repo = getRealm().getRepository();
InputStream docSource = null;
if (yanelPath.startsWith("yanelrepo:") || yanelPath.startsWith("yanelresource:")) {
SourceResolver resolver = new SourceResolver(this);
Source source = resolver.resolve(yanelPath, null);
docSource = ((StreamSource) source).getInputStream();
} else {
docSource = repo.getNode(yanelPath).getInputStream();
}
// Step 1: Construct a FopFactory
// (reuse if you plan to render multiple documents!)
FopFactory fopFactory = FopFactory.newInstance();
// Step 2: Construct fop with desired output format
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, getResponse().getOutputStream());
SourceResolver srcResolver = new SourceResolver(this);
fopFactory.setURIResolver(srcResolver);
/* Only for debugging ...
java.io.FileOutputStream fout = new java.io.FileOutputStream("/home/michi/Desktop/yanel.pdf");
driver.setOutputStream(fout);
*/
// TODO: This doesn't seem to work properly (see below)
//java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
//driver.setOutputStream(baos);
Transformer transformer = TransformerFactory.newInstance().newTransformer(getXSLTStreamSource(getPath(),repo));
org.xml.sax.XMLReader xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
xmlReader.setEntityResolver(new org.apache.xml.resolver.tools.CatalogResolver());
Source src = new SAXSource(xmlReader, new org.xml.sax.InputSource(docSource));
Result res = new SAXResult(fop.getDefaultHandler());
transformer.transform(src,res);
// TODO: For some strange reason the stream seems to be truncated after a certain length ...!
//log.debug("Result Size"+ baos.size());
//defaultView.setInputStream(new java.io.ByteArrayInputStream(baos.toByteArray()));
} catch(Exception e) {
log.error(e, e);
}
return defaultView;
}
/**
* convenient method to allow easy overriding of how the dataPath is generated
* @return String datapath
*/
public String getDataPath() throws Exception {
WildcardReplacerHelper dataPath = new WildcardReplacerHelper(getResourceConfigProperty("yanel-path"), getResourceConfigProperty("yanel-path-matcher"));
return dataPath.getReplacedString(getPath());
}
/**
* Check if data actually exists within repository
*/
public boolean exists() throws Exception {
String yanelPath = getDataPath();
Repository repo = getRealm().getRepository();
if (yanelPath.startsWith("yanelrepo:") || yanelPath.startsWith("yanelresource:")) {
log.warn("Not implemented yet!");
return true;
}
return repo.existsNode(yanelPath);
}
/**
*
*/
public long getSize() throws Exception {
log.warn("Not implemented yet!");
return -1;
}
/**
*
*/
private StreamSource getXSLTStreamSource(String path, Repository repo) throws Exception {
String xsltPath = getXSLTPath(path);
if(xsltPath != null)
return new StreamSource(repo.getInputStream(new Path(xsltPath)));
File xsltFile = org.wyona.commons.io.FileUtil.file( rtd.getConfigFile().getParentFile().getAbsolutePath(), "xslt" + File.separator + "xml2fo.xsl");
if( log.isDebugEnabled() )
log.debug("XSLT file: " + xsltFile);
return new StreamSource(xsltFile);
}
/**
*
*/
private String getXSLTPath(String path) throws Exception {
String xsltPath = getResourceConfigProperty("xslt");
if (xsltPath != null) return xsltPath;
log.info("No XSLT Path within: " + path);
return null;
}
/**
*
*/
public String getMimeType(String viewId) {
return "application/pdf";
}
}