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