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