001: /**
002: *
003: */package org.enhydra.util.fo;
004:
005: import java.io.File;
006:
007: import javax.xml.transform.Source;
008: import javax.xml.transform.Transformer;
009: import javax.xml.transform.TransformerFactory;
010: import javax.xml.transform.dom.DOMResult;
011: import javax.xml.transform.dom.DOMSource;
012: import javax.xml.transform.stream.StreamSource;
013:
014: import org.enhydra.util.ResponsePostProcessor;
015: import org.enhydra.xml.io.OutputOptions;
016: import org.w3c.dom.Node;
017:
018: import com.lutris.logging.LogChannel;
019: import com.lutris.logging.Logger;
020: import com.lutris.util.Config;
021: import com.lutris.util.ConfigException;
022:
023: /**
024: * @author Slobodan Vujasinovic
025: *
026: */
027: public class Fo2HtmlResponsePostProcessor implements
028: ResponsePostProcessor {
029:
030: /*
031: * Name of the post processor
032: */
033: private String name = null;
034:
035: /**
036: * LogChannel used by ResponsePostProcessor
037: */
038: private LogChannel logChannel;
039:
040: /*
041: * Name of the post processor supported mime type
042: */
043: public static final String mimeType = "text/fo+html";
044:
045: /*
046: * Name of the post processor generated mime type
047: */
048: public static final String outMimeType = "text/html";
049:
050: // Chiba Adaptor attributes
051: private static final String TEMPLATE_PARAM_NAME = "Fo2HtmlTemplatePath";
052:
053: private static final String DEFAULT_TEMPLATE_PARAM_VALUE = "fo2html.xsl";
054:
055: private String templatePath = null;
056:
057: public Fo2HtmlResponsePostProcessor() {
058: super ();
059: }
060:
061: public Fo2HtmlResponsePostProcessor(LogChannel logChannel) {
062: super ();
063: this .logChannel = logChannel;
064: }
065:
066: /*
067: * (non-Javadoc)
068: *
069: * @see org.enhydra.util.ResponsePostProcessor#configure(com.lutris.util.Config)
070: */
071: public void configure(Config config) {
072: // do some property initialization
073: // action is triggered on application startup
074:
075: // templatePath = config.getString(TEMPLATE_PARAM_NAME,
076: // DEFAULT_TEMPLATE_PARAM_VALUE);
077: templatePath = getAbsolutePath(TEMPLATE_PARAM_NAME, config,
078: DEFAULT_TEMPLATE_PARAM_VALUE, false);
079:
080: if (logChannel != null) {
081: logChannel.write(Logger.DEBUG, "Fo2Html Post Processor ("
082: + name + ") - Initialized successfully!");
083: }
084: }
085:
086: /*
087: * (non-Javadoc)
088: *
089: * @see org.enhydra.util.ResponsePostProcessor#process(java.util.Map,
090: * org.w3c.dom.Node)
091: */
092: public Node process(OutputOptions oo, Node document) {
093: try {
094:
095: if (logChannel != null) {
096: logChannel
097: .write(Logger.DEBUG, "Fo2Html Post Processor ("
098: + name
099: + ") - Processing parameters (if any)!");
100: }
101:
102: if (oo.getFreeformOption(TEMPLATE_PARAM_NAME) != null)
103: templatePath = (String) oo
104: .getFreeformOption(TEMPLATE_PARAM_NAME);
105:
106: if (logChannel != null) {
107: logChannel
108: .write(
109: Logger.DEBUG,
110: "Fo2Html Post Processor ("
111: + name
112: + ") - Processing response according to template - "
113: + templatePath);
114: }
115:
116: File xslFile = new File(templatePath);
117:
118: // Setup XSLT
119: TransformerFactory factory = TransformerFactory
120: .newInstance();
121: Transformer transformer = factory
122: .newTransformer(new StreamSource(xslFile));
123:
124: Source src = new DOMSource(document);
125:
126: DOMResult res = new DOMResult();
127:
128: // Start XSLT transformation and FOP processing
129: transformer.transform(src, res);
130:
131: oo.setMIMEType("text/html");
132:
133: return res.getNode();
134: } catch (Exception xfe) {
135: if (logChannel != null) {
136: logChannel
137: .write(
138: Logger.WARNING,
139: "Fo2Html Post Processor ("
140: + name
141: + ") - Problem occured during Html generation!",
142: xfe);
143: }
144: }
145: // Failed to additionally process this document.
146: // Therefore, we chose to return original one!
147: return document;
148:
149: }
150:
151: /*
152: * (non-Javadoc)
153: *
154: * @see org.enhydra.util.ResponsePostProcessor#process(byte [],
155: * java.lang.String, java.lang.String)
156: */
157: public byte[] process(byte[] buteArray, String mimeEncoding,
158: String mimeType) {
159: return buteArray;
160: }
161:
162: /*
163: * (non-Javadoc)
164: *
165: * @see org.enhydra.util.ResponsePostProcessor#setName(java.lang.String)
166: */
167: public void setName(String name) {
168: this .name = name;
169: }
170:
171: /*
172: * (non-Javadoc)
173: *
174: * @see org.enhydra.util.ResponsePostProcessor#getName()
175: */
176: public String getName() {
177: return name;
178: }
179:
180: /*
181: * (non-Javadoc)
182: *
183: * @see org.enhydra.util.RequestPreProcessor#setLogChannel()
184: */
185: public void setLogChannel(LogChannel logChannel) {
186: this .logChannel = logChannel;
187: }
188:
189: /*
190: * (non-Javadoc)
191: *
192: * @see org.enhydra.util.RequestPreProcessor#getLogChannel()
193: */
194: public LogChannel getLogChannel() {
195: return logChannel;
196: }
197:
198: /*
199: * (non-Javadoc)
200: *
201: * @see org.enhydra.util.ResponsePostProcessor#shouldProcess(java.lang.String)
202: */
203: public boolean shouldProcess(String mimeType) {
204: if (mimeType != null
205: && mimeType
206: .startsWith(Fo2HtmlResponsePostProcessor.mimeType))
207: return true;
208: return false;
209: }
210:
211: /*
212: * (non-Javadoc)
213: *
214: * @see org.enhydra.util.ResponsePostProcessor#getOutputMimeType()
215: */
216: public String getOutputMimeType() {
217: return this .outMimeType;
218: }
219:
220: /**
221: * Method returns absolute path to file (directory)!
222: *
223: * @param Property
224: * Name
225: * @param Configuration
226: * Object
227: * @param Default
228: * Property Value
229: * @param If
230: * This Is Directory
231: * @return Absolute File Path
232: */
233: private String getAbsolutePath(String propName, Config config,
234: String defaultValue, boolean isDirectory) {
235: String absoultePath = null;
236: try {
237: absoultePath = config.getString(propName, defaultValue);
238:
239: if (absoultePath != null) {
240: File temp = new File(absoultePath);
241:
242: if ((!temp.isDirectory() && isDirectory)
243: || !temp.exists()) {
244: temp = new File(config.getConfigFile().getFile()
245: .getParentFile().getAbsolutePath()
246: + File.separator + absoultePath);
247:
248: if ((!temp.isDirectory() && isDirectory)
249: || !temp.exists()) {
250: if (logChannel != null)
251: logChannel
252: .write(
253: Logger.WARNING,
254: propName
255: + " application parameter isn't properly initialized!");
256:
257: } else {
258: try {
259: absoultePath = temp.getCanonicalPath();
260: } catch (Exception e) {
261: }
262: }
263: } else {
264: try {
265: absoultePath = temp.getCanonicalPath();
266: } catch (Exception e) {
267: }
268: }
269: }
270: } catch (ConfigException e) {
271: if (logChannel != null) {
272: logChannel
273: .write(
274: Logger.WARNING,
275: propName
276: + " application parameter isn't properly initialized!");
277:
278: }
279: }
280:
281: return absoultePath;
282: }
283:
284: public Object clone() {
285: Fo2HtmlResponsePostProcessor f2hrpp = new Fo2HtmlResponsePostProcessor(
286: this.getLogChannel());
287: f2hrpp.templatePath = this.templatePath;
288: return f2hrpp;
289: }
290:
291: }
|