001: package org.enhydra.util.ajaxforms;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.ByteArrayOutputStream;
005: import java.io.FileInputStream;
006: import java.io.InputStream;
007:
008: import javax.xml.parsers.DocumentBuilder;
009: import javax.xml.parsers.DocumentBuilderFactory;
010: import javax.xml.parsers.ParserConfigurationException;
011: import javax.xml.transform.Transformer;
012: import javax.xml.transform.dom.DOMResult;
013: import javax.xml.transform.dom.DOMSource;
014: import javax.xml.transform.stream.StreamResult;
015: import javax.xml.transform.stream.StreamSource;
016:
017: import net.ajaxforms.compiler.Context;
018: import net.ajaxforms.compiler.XDocument;
019:
020: import org.enhydra.util.DOMFormatter;
021: import org.enhydra.util.xsl.GenericXSLTPostProcessor;
022: import org.enhydra.xml.io.OutputOptions;
023: import org.w3c.dom.Document;
024: import org.w3c.dom.Node;
025:
026: import com.lutris.appserver.server.session.Session;
027: import com.lutris.logging.LogChannel;
028: import com.lutris.logging.Logger;
029: import com.lutris.util.Config;
030: import com.lutris.util.KeywordValueTable;
031:
032: public class AjaxFormsResponsePostProcessor extends
033: GenericXSLTPostProcessor {
034:
035: /*
036: * In Mime Type Parameter Name
037: */
038: protected static final String IN_MIMETYPE_PARAM_NAME = "InMimeType";
039:
040: /*
041: * Default In Mime Type Value
042: */
043: protected static final String DEFAULT_IN_MIMETYPE_VALUE = "text/xhtml+xforms";
044:
045: /*
046: * In Mime Type Value
047: */
048: public String inMimeType = DEFAULT_IN_MIMETYPE_VALUE;
049:
050: /*
051: * Out Mime Type Parameter Name
052: */
053: protected static final String OUT_MIMETYPE_PARAM_NAME = "OutMimeType";
054:
055: /*
056: * Default Out Mime Type Value
057: */
058: protected static final String DEFAULT_OUT_MIMETYPE_VALUE = "text/html";
059:
060: /*
061: * Out Mime Type Value
062: */
063: public String outMimeType = DEFAULT_OUT_MIMETYPE_VALUE;
064:
065: private static final String DEFAULT_RESOURCES_DIR = "resources";
066:
067: private static final String DEFAULT_RESOURCES_BASE = "";
068:
069: private static final String resourcesParamName = "Resources";
070:
071: private String resources = DEFAULT_RESOURCES_DIR;
072:
073: private static final String resourcesBaseParamName = "ResourcesBase";
074:
075: private String resourcesBase = DEFAULT_RESOURCES_DIR;
076:
077: private DocumentBuilder docBuilder = null;
078:
079: public AjaxFormsResponsePostProcessor() {
080: super ();
081: }
082:
083: public AjaxFormsResponsePostProcessor(LogChannel logChannel) {
084: super ();
085: this .logChannel = logChannel;
086: DocumentBuilderFactory factory = DocumentBuilderFactory
087: .newInstance();
088: factory.setNamespaceAware(true);
089: try {
090: this .docBuilder = factory.newDocumentBuilder();
091: } catch (ParserConfigurationException e) {
092: e.printStackTrace();
093: }
094: }
095:
096: public void configure(Config config) {
097: try {
098: super .configure(config);
099:
100: resources = config.getString(resourcesParamName,
101: DEFAULT_RESOURCES_DIR);
102:
103: resourcesBase = config.getString(resourcesBaseParamName,
104: DEFAULT_RESOURCES_BASE);
105:
106: if (logChannel != null) {
107: logChannel.write(Logger.DEBUG, "Ajax Forms Processor ("
108: + name + ") - Initialized successfully!");
109: }
110: } catch (Exception fe) {
111: if (logChannel != null) {
112: logChannel
113: .write(
114: Logger.WARNING,
115: "Ajax Forms Processor ("
116: + name
117: + ") - Initialized with default configuration settings!",
118: fe);
119: }
120:
121: // Applying default values
122: resourcesBase = DEFAULT_RESOURCES_BASE;
123: resources = DEFAULT_RESOURCES_DIR;
124: }
125: }
126:
127: /*
128: * (non-Javadoc)
129: *
130: * @see org.enhydra.util.ResponsePostProcessor#process(org.enhydra.xml.io.OutputOptions,
131: * org.w3c.dom.Node)
132: */
133: public Node process(OutputOptions oo, Node document) {
134:
135: try {
136: Transformer trans = tFactory.newTransformer();
137:
138: if (logChannel != null) {
139: logChannel
140: .write(Logger.DEBUG, "Generic XSLT Processor ("
141: + name
142: + ") - Processing parameters (if any)!");
143: }
144:
145: if (oo.getFreeformOption(PROPERTY_PREFIX_PARAM_NAME) != null) {
146: propsPrefix = (String) oo
147: .getFreeformOption(PROPERTY_PREFIX_PARAM_NAME);
148: }
149:
150: // this is not functional because we are actually dealing with
151: // object clones during process methods
152: // and object default property settings will not take any effect at
153: // this
154: // point!
155: if (oo.getFreeformOption(DEFAULT_TEMPLATE_PARAM) != null) {
156: String xslFilePath = (String) oo
157: .getFreeformOption(DEFAULT_TEMPLATE_PARAM);
158: defaultTemplatePath = xslFilePath;
159: }
160: //
161:
162: Document workingDoc = null;
163: // FileInputStream fis = null;
164:
165: try {
166: ByteArrayOutputStream os = new ByteArrayOutputStream();
167: StreamResult result = new StreamResult(os);
168: DOMSource source = new DOMSource(document);
169: trans.transform(source, result);
170:
171: XDocument doc = new XDocument();
172: Context ctx = Context.getInstance();
173:
174: InputStream is = new ByteArrayInputStream(os
175: .toByteArray());
176: doc.load(is);
177: doc.process();
178:
179: if (ctx.hasErrors()) {
180: throw ctx.createException();
181: }
182:
183: // Falta compactar
184: workingDoc = this .docBuilder.newDocument();
185: doc.export(workingDoc);
186: } catch (Throwable ex) {
187: ex.printStackTrace();
188: } /*finally {
189: if (fis != null) {
190: fis.close();
191: }
192: }*/
193:
194: Transformer transformer = null;
195: String templatePath = defaultTemplatePath;
196:
197: if (oo.getFreeformOption(TEMPLATE_PARAM) != null) {
198:
199: templatePath = (String) oo
200: .getFreeformOption(TEMPLATE_PARAM);
201:
202: if (logChannel != null) {
203: logChannel
204: .write(
205: Logger.DEBUG,
206: "Generic XSLT Processor ("
207: + name
208: + ") - Processing response according to template - "
209: + templatePath);
210: }
211:
212: } else {
213: if (logChannel != null) {
214: logChannel
215: .write(
216: Logger.DEBUG,
217: "Generic XSLT Processor ("
218: + name
219: + ") - Processing response according to default template - "
220: + defaultTemplatePath);
221: }
222: }
223:
224: transformer = tFactory.newTransformer(new StreamSource(
225: templatePath));
226: transformer.setParameter("resourcesdir", resourcesBase
227: + resources + "/");
228: //
229: Session session = (Session) oo
230: .getFreeformOption(sessionParam);
231: if (session != null) {
232: KeywordValueTable kwt = session.getSessionData()
233: .getSection(propsPrefix);
234: if (kwt != null) {
235: String[] keys = kwt.keys();
236: for (int i = 0; i < keys.length; i++) {
237: if (keys[i] != null) {
238: transformer.setParameter(keys[i], kwt
239: .getString(keys[i]));
240: }
241: }
242: }
243: }
244:
245: // DOMSource src = new DOMSource(workingDoc);
246:
247: Document docResult = this .docBuilder.newDocument();
248: // DOMResult res = new DOMResult(docResult);
249:
250: // Execute XSL Transformation
251: /* Alex
252: * if (transformer != null) {
253: transformer.transform(src, res);
254: } else {*/
255: if (logChannel != null) {
256: logChannel
257: .write(
258: Logger.WARNING,
259: "Ajax Forms Processor ("
260: + name
261: + ") - Problem occured during TRansformer generation!");
262: }
263: // }
264:
265: String tempMT = (String) oo.getMIMEType();
266: tempMT = outMimeType
267: + tempMT.substring(inMimeType.length());
268: oo.setMIMEType(tempMT);
269: oo.addFreeformOption(DOMFormatter.FORMATTER_NAME,
270: new AjaxFormsFormater(oo.getEncoding()));
271: return docResult;
272: } catch (Exception xfe) {
273: if (logChannel != null) {
274: logChannel
275: .write(
276: Logger.WARNING,
277: "Ajax Forms XSLT Processor ("
278: + name
279: + ") - Problem occured during XSL transformation!",
280: xfe);
281: }
282: }
283: // Failed To Execute XSL Transformation
284: // Therefore, we chose to return the original
285: return document;
286: }
287:
288: /*
289: * (non-Javadoc)
290: *
291: * @see org.enhydra.util.ResponsePostProcessor#shouldProcess(java.lang.String)
292: */
293: public boolean shouldProcess(String mimeType) {
294: if (mimeType != null && mimeType.startsWith(this .inMimeType))
295: return true;
296: return false;
297: }
298:
299: public Object clone() {
300: AjaxFormsResponsePostProcessor afrpp = new AjaxFormsResponsePostProcessor(
301: this .getLogChannel());
302:
303: // Post Processor Name
304: afrpp.name = this .name;
305:
306: // Default XSL Template
307: afrpp.defaultTemplatePath = this .defaultTemplatePath;
308:
309: // XSL Template Parameters
310: afrpp.TEMPLATE_PARAM = this .TEMPLATE_PARAM;
311: afrpp.DEFAULT_TEMPLATE_PARAM = this .DEFAULT_TEMPLATE_PARAM;
312:
313: // Transformer Factory
314: afrpp.tFactory = this .tFactory;
315:
316: // XSLTC OPtions
317: afrpp.useXsltc = this .useXsltc;
318: afrpp.transletName = this .transletName;
319: afrpp.destinationDir = this .destinationDir;
320: afrpp.packageName = this .packageName;
321: afrpp.jarName = this .jarName;
322: afrpp.generateTranslet = this .generateTranslet;
323: afrpp.autoTranslet = this .autoTranslet;
324: afrpp.useClasspath = this .useClasspath;
325: afrpp.enableInline = this .enableInline;
326: afrpp.debugXsltc = this .debugXsltc;
327:
328: // Ajax Forms Specific Parameters
329: afrpp.resources = this.resources;
330: afrpp.resourcesBase = this.resourcesBase;
331:
332: return afrpp;
333: }
334:
335: }
|