Source Code Cross Referenced for GenericXSLTPostProcessor.java in  » J2EE » Enhydra-Application-Framework » org » enhydra » util » xsl » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » Enhydra Application Framework » org.enhydra.util.xsl 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * 
003:         */package org.enhydra.util.xsl;
004:
005:        import java.io.File;
006:        import java.util.Iterator;
007:        import java.util.Map;
008:        import java.util.Properties;
009:
010:        import javax.xml.transform.ErrorListener;
011:        import javax.xml.transform.Transformer;
012:        import javax.xml.transform.TransformerException;
013:        import javax.xml.transform.TransformerFactory;
014:        import javax.xml.transform.TransformerFactoryConfigurationError;
015:        import javax.xml.transform.dom.DOMResult;
016:        import javax.xml.transform.dom.DOMSource;
017:        import javax.xml.transform.stream.StreamSource;
018:
019:        import org.enhydra.util.ResponsePostProcessor;
020:        import org.enhydra.xml.io.OutputOptions;
021:        import org.w3c.dom.Node;
022:
023:        import com.lutris.appserver.server.session.Session;
024:        import com.lutris.logging.LogChannel;
025:        import com.lutris.logging.Logger;
026:        import com.lutris.util.Config;
027:        import com.lutris.util.ConfigException;
028:        import com.lutris.util.KeywordValueTable;
029:
030:        /**
031:         * @author Slobodan Vujasinovic
032:         */
033:        public class GenericXSLTPostProcessor implements  ResponsePostProcessor {
034:
035:            /*
036:             * Name of the post processor
037:             */
038:            protected String name = null;
039:
040:            /*
041:             * LogChannel used by ResponsePostProcessor
042:             */
043:            protected LogChannel logChannel;
044:
045:            /*
046:             * In Mime Type Parameter Name
047:             */
048:            protected static final String IN_MIMETYPE_PARAM_NAME = "InMimeType";
049:
050:            /*
051:             * Default In Mime Type Value
052:             */
053:            protected static final String DEFAULT_IN_MIMETYPE_VALUE = "text/html";
054:
055:            /*
056:             * In Mime Type Value
057:             */
058:            public String inMimeType = DEFAULT_IN_MIMETYPE_VALUE;
059:
060:            /*
061:             * Out Mime Type Parameter Name
062:             */
063:            protected static final String OUT_MIMETYPE_PARAM_NAME = "OutMimeType";
064:
065:            /*
066:             * Default Out Mime Type Value
067:             */
068:            protected static final String DEFAULT_OUT_MIMETYPE_VALUE = "text/html";
069:
070:            /*
071:             * Out Mime Type Value
072:             */
073:            public String outMimeType = DEFAULT_OUT_MIMETYPE_VALUE;
074:
075:            /*
076:             * XSLT Base Directory Parameter Name
077:             */
078:            protected String BASE_DIR_PARAM = "XSLTBaseDir";
079:
080:            /*
081:             * Name of the XSLT Base Directory Parameter Name
082:             */
083:            protected static final String BASE_DIR_PARAM_NAME = "XSLTBaseDirName";
084:
085:            /*
086:             * Default value for XSLT Base Directory Parameter Name
087:             */
088:            protected static final String BASE_DIR_PARAM_NAME_VALUE = "XSLTBaseDir";
089:
090:            /*
091:             * XSLT Base Directory Value
092:             */
093:            protected static final String DEFAULT_BASE_DIR_PARAM_VALUE = ".";
094:
095:            /*
096:             * Default XSLT Base Directory Value
097:             */
098:            protected String baseDir = DEFAULT_BASE_DIR_PARAM_VALUE;
099:
100:            /*
101:             * XSL Template Parameter Name
102:             */
103:            protected String TEMPLATE_PARAM = "TemplatePath";
104:
105:            /*
106:             * Name of the XSL Template Parameter Name Enables multiple Instance
107:             * definitions in the same application which can be individually
108:             * reconfigured at runtime (using different OutputOptions property).
109:             */
110:            protected static final String TEMPLATE_PARAM_NAME = "TemplatePathName";
111:
112:            /*
113:             * Default value for XSL Template Parameter Name
114:             */
115:            protected static final String TEMPLATE_PARAM_NAME_VALUE = "TemplatePath";
116:
117:            /*
118:             * XSL Template Parameter Name
119:             */
120:            protected String DEFAULT_TEMPLATE_PARAM = "DefaultTemplatePath";
121:
122:            /*
123:             * Default XSL Template Value
124:             */
125:            protected static final String DEFAULT_TEMPLATE_PARAM_VALUE = "default.xsl";
126:
127:            /*
128:             * Name of Default XSL Template Parameter Name Enables multiple Instance
129:             * definitions in the same application which can be individually
130:             * reconfigured at runtime (using different OutputOptions property).
131:             */
132:            protected static final String DEFAULT_TEMPLATE_PARAM_NAME = "DefaultTemplateParamName";
133:
134:            /*
135:             * Default XSL Template Value
136:             */
137:            protected static final String DEFAULT_TEMPLATE_PARAM_NAME_VALUE = "DefaultTemplatePath";
138:
139:            /*
140:             * Default XSL Template Value
141:             */
142:            protected String defaultTemplatePath = DEFAULT_TEMPLATE_PARAM_VALUE;
143:
144:            /*
145:             * Parameter name for use XSLTC option
146:             */
147:            protected static final String USE_XSLTC_PARAM_NAME = "UseXsltc";
148:
149:            /*
150:             * Default value for use XSLTC option
151:             */
152:            protected static final boolean DEFAULT_USE_XSLTC_VALUE = false;
153:
154:            /*
155:             * Use XSLTC option
156:             */
157:            protected boolean useXsltc = DEFAULT_USE_XSLTC_VALUE;
158:
159:            // Set of XSLTC configuration options (disregarded if useXsltc is set to
160:            // false)
161:            /*
162:             * XSLTC 'translet-name' option
163:             */
164:            protected static final String XSLTC_TRANSLET_NAME_PARAM_NAME = "Xsltc.TransletName";
165:
166:            protected static final String DEFAULT_XSLTC_TRANSLET_NAME_VALUE = null;
167:
168:            protected String transletName = DEFAULT_XSLTC_TRANSLET_NAME_VALUE;
169:
170:            /*
171:             * XSLTC 'destination-directory' option
172:             */
173:            protected static final String XSLTC_DESTINATION_DIR_PARAM_NAME = "Xsltc.DestinationDir";
174:
175:            protected static final String DEFAULT_XSLTC_DESTINATION_DIR_VALUE = null;
176:
177:            protected String destinationDir = DEFAULT_XSLTC_DESTINATION_DIR_VALUE;
178:
179:            /*
180:             * XSLTC 'package-name' option
181:             */
182:            protected static final String XSLTC_PACKAGE_NAME_PARAM_NAME = "Xsltc.PackageName";
183:
184:            protected static final String DEFAULT_XSLTC_PACKAGE_NAME_VALUE = null;
185:
186:            protected String packageName = DEFAULT_XSLTC_PACKAGE_NAME_VALUE;
187:
188:            /*
189:             * XSLTC 'jar-name' option
190:             */
191:            protected static final String XSLTC_JAR_NAME_PARAM_NAME = "Xsltc.JarName";
192:
193:            protected static final String DEFAULT_XSLTC_JAR_NAME_VALUE = null;
194:
195:            protected String jarName = DEFAULT_XSLTC_JAR_NAME_VALUE;
196:
197:            /*
198:             * XSLTC 'generate-translet' option
199:             */
200:            protected static final String XSLTC_GENERATE_TRANSLET_PARAM_NAME = "Xsltc.GenerateTranslet";
201:
202:            protected static final boolean DEFAULT_XSLTC_GENERATE_TRANSLET_VALUE = false;
203:
204:            protected boolean generateTranslet = DEFAULT_XSLTC_GENERATE_TRANSLET_VALUE;
205:
206:            /*
207:             * XSLTC 'auto-translet' option
208:             */
209:            protected static final String XSLTC_AUTO_TRANSLET_PARAM_NAME = "Xsltc.AutoTranslet";
210:
211:            protected static final boolean DEFAULT_XSLTC_AUTO_TRANSLET_VALUE = false;
212:
213:            protected boolean autoTranslet = DEFAULT_XSLTC_AUTO_TRANSLET_VALUE;
214:
215:            /*
216:             * XSLTC 'use-classpath' option
217:             */
218:            protected static final String XSLTC_USE_CLASSPATH_PARAM_NAME = "Xsltc.UseClasspath";
219:
220:            protected static final boolean DEFAULT_XSLTC_USE_CLASSPATH_VALUE = false;
221:
222:            protected boolean useClasspath = DEFAULT_XSLTC_USE_CLASSPATH_VALUE;
223:
224:            /*
225:             * XSLTC 'enable-inlining' option
226:             */
227:            protected static final String XSLTC_INLINE_PARAM_NAME = "Xsltc.EnableInline";
228:
229:            protected static final boolean DEFAULT_XSLTC_INLINE_VALUE = false;
230:
231:            protected boolean enableInline = DEFAULT_XSLTC_INLINE_VALUE;
232:
233:            /*
234:             * XSLTC 'debug' option
235:             */
236:            protected static final String XSLTC_DEBUG_PARAM_NAME = "Xsltc.Debug";
237:
238:            protected static final boolean DEFAULT_XSLTC_DEBUG_VALUE = false;
239:
240:            protected boolean debugXsltc = DEFAULT_XSLTC_DEBUG_VALUE;
241:
242:            /*
243:             * Transformer Factory
244:             */
245:            protected TransformerFactory tFactory = null;
246:
247:            /*
248:             * Pre Processing Parameter Set
249:             */
250:            protected String PROPERTY_PREFIX_PARAM_NAME = "Prefix";
251:
252:            protected String DEFAULT_PROPERTY_PREFIX_PARAM_VALUE = "style";
253:
254:            protected String propsPrefix = DEFAULT_PROPERTY_PREFIX_PARAM_VALUE;
255:
256:            protected static final String SESSION_PARAM_NAME = "SessionParamName";
257:
258:            protected static final String DEFAULT_SESSION_PARAM_VALUE = "session";
259:
260:            protected String sessionParam = DEFAULT_SESSION_PARAM_VALUE;
261:
262:            /**
263:             * 
264:             */
265:            public GenericXSLTPostProcessor() {
266:                super ();
267:            }
268:
269:            /**
270:             * 
271:             */
272:            public GenericXSLTPostProcessor(LogChannel logChannel) {
273:                super ();
274:                this .logChannel = logChannel;
275:            }
276:
277:            /*
278:             * (non-Javadoc)
279:             * 
280:             * @see org.enhydra.util.ResponsePostProcessor#configure(com.lutris.util.Config)
281:             */
282:            public void configure(Config config) {
283:                // do some property initialization
284:                // action is triggered on application startup
285:
286:                try {
287:                    useXsltc = config.getBoolean(USE_XSLTC_PARAM_NAME,
288:                            DEFAULT_USE_XSLTC_VALUE);
289:                } catch (ConfigException e) {
290:                    useXsltc = DEFAULT_USE_XSLTC_VALUE;
291:                    logChannel.write(Logger.WARNING,
292:                            "Generic XSLT Post Processor (" + name + ") - "
293:                                    + USE_XSLTC_PARAM_NAME
294:                                    + " parameter is set on default value!", e);
295:                }
296:
297:                if (useXsltc) {
298:                    try {
299:                        transletName = config.getString(
300:                                XSLTC_TRANSLET_NAME_PARAM_NAME,
301:                                DEFAULT_XSLTC_TRANSLET_NAME_VALUE);
302:                        destinationDir = config.getString(
303:                                XSLTC_DESTINATION_DIR_PARAM_NAME,
304:                                DEFAULT_XSLTC_DESTINATION_DIR_VALUE);
305:                        packageName = config.getString(
306:                                XSLTC_PACKAGE_NAME_PARAM_NAME,
307:                                DEFAULT_XSLTC_PACKAGE_NAME_VALUE);
308:                        jarName = config.getString(XSLTC_JAR_NAME_PARAM_NAME,
309:                                DEFAULT_XSLTC_JAR_NAME_VALUE);
310:                        generateTranslet = config.getBoolean(
311:                                XSLTC_GENERATE_TRANSLET_PARAM_NAME,
312:                                DEFAULT_XSLTC_GENERATE_TRANSLET_VALUE);
313:                        autoTranslet = config.getBoolean(
314:                                XSLTC_AUTO_TRANSLET_PARAM_NAME,
315:                                DEFAULT_XSLTC_AUTO_TRANSLET_VALUE);
316:                        useClasspath = config.getBoolean(
317:                                XSLTC_USE_CLASSPATH_PARAM_NAME,
318:                                DEFAULT_XSLTC_USE_CLASSPATH_VALUE);
319:                        enableInline = config.getBoolean(
320:                                XSLTC_INLINE_PARAM_NAME,
321:                                DEFAULT_XSLTC_INLINE_VALUE);
322:                        debugXsltc = config.getBoolean(XSLTC_DEBUG_PARAM_NAME,
323:                                DEFAULT_XSLTC_DEBUG_VALUE);
324:                    } catch (ConfigException e) {
325:                        logChannel
326:                                .write(
327:                                        Logger.WARNING,
328:                                        "Generic XSLT Post Processor ("
329:                                                + name
330:                                                + ") - some parameters are set on default value!",
331:                                        e);
332:                    }
333:                }
334:
335:                initTransformerFactory();
336:
337:                try {
338:                    BASE_DIR_PARAM = config.getString(BASE_DIR_PARAM_NAME,
339:                            BASE_DIR_PARAM_NAME_VALUE);
340:                } catch (ConfigException e) {
341:                    BASE_DIR_PARAM = BASE_DIR_PARAM_NAME_VALUE;
342:                    logChannel.write(Logger.WARNING,
343:                            "Generic XSLT Post Processor (" + name + ") - "
344:                                    + BASE_DIR_PARAM_NAME
345:                                    + " parameter is set on default value!", e);
346:                }
347:
348:                try {
349:                    baseDir = config.getString(BASE_DIR_PARAM,
350:                            DEFAULT_BASE_DIR_PARAM_VALUE);
351:                } catch (ConfigException e) {
352:                    baseDir = DEFAULT_BASE_DIR_PARAM_VALUE;
353:                    logChannel.write(Logger.WARNING,
354:                            "Generic XSLT Post Processor (" + name + ") - "
355:                                    + BASE_DIR_PARAM
356:                                    + " parameter is set on default value!", e);
357:                }
358:
359:                try {
360:                    TEMPLATE_PARAM = config.getString(TEMPLATE_PARAM_NAME,
361:                            TEMPLATE_PARAM_NAME_VALUE);
362:                } catch (ConfigException e) {
363:                    TEMPLATE_PARAM = TEMPLATE_PARAM_NAME_VALUE;
364:                    logChannel.write(Logger.WARNING,
365:                            "Generic XSLT Post Processor (" + name + ") - "
366:                                    + TEMPLATE_PARAM
367:                                    + " parameter is set on default value!", e);
368:                }
369:
370:                try {
371:                    DEFAULT_TEMPLATE_PARAM = config.getString(
372:                            DEFAULT_TEMPLATE_PARAM_NAME,
373:                            DEFAULT_TEMPLATE_PARAM_NAME_VALUE);
374:                } catch (ConfigException e) {
375:                    DEFAULT_TEMPLATE_PARAM = DEFAULT_TEMPLATE_PARAM_NAME_VALUE;
376:                    logChannel.write(Logger.WARNING,
377:                            "Generic XSLT Post Processor (" + name + ") - "
378:                                    + DEFAULT_TEMPLATE_PARAM
379:                                    + " parameter is set on default value!", e);
380:                }
381:
382:                String xslFilePath = null;
383:
384:                try {
385:                    xslFilePath = config.getString(DEFAULT_TEMPLATE_PARAM,
386:                            DEFAULT_TEMPLATE_PARAM_VALUE);
387:                } catch (ConfigException e) {
388:                    xslFilePath = DEFAULT_TEMPLATE_PARAM_VALUE;
389:                    logChannel.write(Logger.WARNING,
390:                            "Generic XSLT Post Processor (" + name + ") - "
391:                                    + DEFAULT_TEMPLATE_PARAM
392:                                    + " parameter is set on default value!", e);
393:                }
394:
395:                defaultTemplatePath = resolve(baseDir, xslFilePath);
396:
397:                try {
398:                    inMimeType = config.getString(IN_MIMETYPE_PARAM_NAME,
399:                            DEFAULT_IN_MIMETYPE_VALUE);
400:                } catch (ConfigException e) {
401:                    inMimeType = DEFAULT_IN_MIMETYPE_VALUE;
402:                    logChannel.write(Logger.WARNING,
403:                            "Generic XSLT Post Processor (" + name + ") - "
404:                                    + OUT_MIMETYPE_PARAM_NAME
405:                                    + " parameter is set on default value!", e);
406:                }
407:
408:                try {
409:                    outMimeType = config.getString(OUT_MIMETYPE_PARAM_NAME,
410:                            DEFAULT_OUT_MIMETYPE_VALUE);
411:                } catch (ConfigException e) {
412:                    outMimeType = DEFAULT_OUT_MIMETYPE_VALUE;
413:                    logChannel.write(Logger.WARNING,
414:                            "Generic XSLT Post Processor (" + name + ") - "
415:                                    + OUT_MIMETYPE_PARAM_NAME
416:                                    + " parameter is set on default value!", e);
417:                }
418:
419:                try {
420:                    propsPrefix = config.getString(PROPERTY_PREFIX_PARAM_NAME,
421:                            DEFAULT_PROPERTY_PREFIX_PARAM_VALUE);
422:                    sessionParam = config.getString(SESSION_PARAM_NAME,
423:                            DEFAULT_SESSION_PARAM_VALUE);
424:                } catch (ConfigException e) {
425:                    propsPrefix = DEFAULT_PROPERTY_PREFIX_PARAM_VALUE;
426:                    logChannel.write(Logger.WARNING,
427:                            "Generic XSLT Post Processor (" + name + ") - "
428:                                    + PROPERTY_PREFIX_PARAM_NAME
429:                                    + " parameter is set on default value!", e);
430:                }
431:
432:                if (logChannel != null) {
433:                    logChannel.write(Logger.DEBUG,
434:                            "Generic XSLT Post Processor (" + name
435:                                    + ") - Initialized successfully!");
436:                }
437:
438:            }
439:
440:            /**
441:             * Prepares system props required by XSLTC Xalan option!
442:             */
443:            private void initTransformerFactory() {
444:                tFactory = null;
445:                String keyValue = null;
446:
447:                String key = "javax.xml.transform.TransformerFactory";
448:                String value = "org.apache.xalan.xsltc.trax.TransformerFactoryImpl";
449:
450:                if (useXsltc) {
451:                    keyValue = System.getProperty(key);
452:                    Properties props = System.getProperties();
453:                    props.put(key, value);
454:                    System.setProperties(props);
455:
456:                }
457:                try {
458:                    tFactory = TransformerFactory.newInstance();
459:
460:                    tFactory.setErrorListener(new ErrorListener() {
461:                        public void error(TransformerException exception) {
462:                            if (logChannel != null)
463:                                logChannel
464:                                        .write(
465:                                                Logger.ERROR,
466:                                                "Generic XSLT Post Processor (TransformerFactory) "
467:                                                        + exception
468:                                                                .getMessageAndLocation());
469:                        }
470:
471:                        public void fatalError(TransformerException exception) {
472:                            if (logChannel != null)
473:                                logChannel
474:                                        .write(
475:                                                Logger.ERROR,
476:                                                "Generic XSLT Post Processor (TransformerFactory) "
477:                                                        + exception
478:                                                                .getMessageAndLocation());
479:                        }
480:
481:                        public void warning(TransformerException exception) {
482:                            if (logChannel != null)
483:                                logChannel
484:                                        .write(
485:                                                Logger.WARNING,
486:                                                "Generic XSLT Post Processor (TransformerFactory) - "
487:                                                        + exception
488:                                                                .getMessageAndLocation());
489:                        }
490:                    });
491:                } catch (TransformerFactoryConfigurationError tfce) {
492:                    tfce.printStackTrace();
493:                }
494:
495:                if (useXsltc) {
496:                    // We are setting System properties back!
497:                    // Not forcing XSLTC option on other (JVM) Xalan transformations.
498:                    Properties props = System.getProperties();
499:                    if (keyValue != null) {
500:                        props.put(key, keyValue);
501:                    } else {
502:                        props.remove(key);
503:                    }
504:                    System.setProperties(props);
505:
506:                    // XSLTC props tuning
507:                    if (transletName != null)
508:                        tFactory.setAttribute("translet-name", transletName);
509:                    if (destinationDir != null)
510:                        tFactory.setAttribute("destination-directory",
511:                                destinationDir);
512:                    if (packageName != null)
513:                        tFactory.setAttribute("package-name", packageName);
514:                    if (jarName != null)
515:                        tFactory.setAttribute("jar-name", jarName);
516:
517:                    tFactory.setAttribute("generate-translet", Boolean
518:                            .valueOf(generateTranslet));
519:                    tFactory.setAttribute("auto-translet", Boolean
520:                            .valueOf(autoTranslet));
521:                    tFactory.setAttribute("use-classpath", Boolean
522:                            .valueOf(useClasspath));
523:                    tFactory.setAttribute("enable-inlining", Boolean
524:                            .valueOf(enableInline));
525:                    tFactory.setAttribute("debug", Boolean.valueOf(debugXsltc));
526:                }
527:            }
528:
529:            /*
530:             * (non-Javadoc)
531:             * 
532:             * @see org.enhydra.util.ResponsePostProcessor#process(org.enhydra.xml.io.OutputOptions,
533:             *      org.w3c.dom.Node)
534:             */
535:            public Node process(OutputOptions oo, Node document) {
536:                try {
537:                    if (logChannel != null) {
538:                        logChannel
539:                                .write(Logger.DEBUG, "Generic XSLT Processor ("
540:                                        + name
541:                                        + ") - Processing parameters (if any)!");
542:                    }
543:
544:                    if (oo.getFreeformOption(PROPERTY_PREFIX_PARAM_NAME) != null) {
545:                        propsPrefix = (String) oo
546:                                .getFreeformOption(PROPERTY_PREFIX_PARAM_NAME);
547:                    }
548:
549:                    // this is not functional because we are actually dealing with
550:                    // object clones
551:                    // and object property settings will not take any effect at this
552:                    // point!
553:                    if (oo.getFreeformOption(DEFAULT_TEMPLATE_PARAM) != null) {
554:                        String xslFilePath = (String) oo
555:                                .getFreeformOption(DEFAULT_TEMPLATE_PARAM);
556:                        defaultTemplatePath = xslFilePath;
557:                    }
558:                    //
559:
560:                    // Take transforme style parameters from session (if any)
561:                    Session session = (Session) oo
562:                            .getFreeformOption(sessionParam);
563:                    KeywordValueTable kwt = null;
564:                    if (session != null) {
565:                        kwt = session.getSessionData().getSection(propsPrefix);
566:                        if (kwt != null) {
567:                            String[] keys = kwt.keys();
568:                            for (int i = 0; i < keys.length; i++) {
569:
570:                                if (keys[i] != null) {
571:                                    // check if we have default template specified and
572:                                    // change it
573:                                    if (DEFAULT_TEMPLATE_PARAM
574:                                            .equalsIgnoreCase(keys[i])) {
575:                                        defaultTemplatePath = kwt
576:                                                .getString(keys[i]);
577:                                    }
578:                                    if (OUT_MIMETYPE_PARAM_NAME
579:                                            .equalsIgnoreCase(keys[i])) {
580:                                        outMimeType = kwt.getString(keys[i]);
581:                                    }
582:                                }
583:                            }
584:                        }
585:                    }
586:
587:                    Transformer transformer = null;
588:                    String templatePath = defaultTemplatePath;
589:
590:                    // check if we have special template defined in OutputOptions (set
591:                    // from PO)
592:                    if (oo
593:                            .getFreeformOption(propsPrefix + "."
594:                                    + TEMPLATE_PARAM) != null) {
595:
596:                        templatePath = (String) oo
597:                                .getFreeformOption(propsPrefix + "."
598:                                        + TEMPLATE_PARAM);
599:
600:                        if (logChannel != null) {
601:                            logChannel
602:                                    .write(
603:                                            Logger.DEBUG,
604:                                            "Generic XSLT Processor ("
605:                                                    + name
606:                                                    + ") - Processing response according to template - "
607:                                                    + templatePath);
608:                        }
609:
610:                    }
611:
612:                    // check if we have special output mimetype defined in OutputOptions
613:                    // (set from PO)
614:                    if (oo.getFreeformOption(propsPrefix + "."
615:                            + OUT_MIMETYPE_PARAM_NAME) != null) {
616:
617:                        outMimeType = (String) oo.getFreeformOption(propsPrefix
618:                                + "." + OUT_MIMETYPE_PARAM_NAME);
619:
620:                        if (logChannel != null) {
621:                            logChannel
622:                                    .write(
623:                                            Logger.DEBUG,
624:                                            "Generic XSLT Processor ("
625:                                                    + name
626:                                                    + ") - Processing response according to output mimetype - "
627:                                                    + outMimeType);
628:                        }
629:
630:                    }
631:
632:                    // check if we have special transformation parameters defined in
633:                    // OutputOptions
634:                    // (set from PO)
635:                    if (oo.getFreeformOption(propsPrefix) != null) {
636:
637:                        Map transParams = (Map) oo
638:                                .getFreeformOption(propsPrefix);
639:
640:                        // fill them in table of existing transformation parameters
641:                        if (kwt == null) {
642:                            kwt = new KeywordValueTable();
643:                        }
644:
645:                        Iterator entries = transParams.entrySet().iterator();
646:
647:                        while (entries.hasNext()) {
648:                            try {
649:                                Map.Entry me = (Map.Entry) entries.next();
650:                                String key = (String) me.getKey();
651:                                Object value = me.getValue();
652:                                if (value != null)
653:                                    kwt.set(key, value);
654:                            } catch (Exception ex) {
655:
656:                            }
657:                        }
658:
659:                        if (logChannel != null) {
660:                            logChannel
661:                                    .write(
662:                                            Logger.DEBUG,
663:                                            "Generic XSLT Processor ("
664:                                                    + name
665:                                                    + ") - Transformation parameters from OutputOptions will apply!");
666:                        }
667:
668:                    }
669:
670:                    // Initialize transformer
671:                    templatePath = resolve(baseDir, templatePath);
672:
673:                    if (templatePath != null) {
674:                        transformer = tFactory.newTransformer(new StreamSource(
675:                                templatePath));
676:                    } else {
677:                        transformer = tFactory.newTransformer();
678:                    }
679:                    // Apply style parameters to the transformer instance
680:                    if (kwt != null) {
681:                        String[] keys = kwt.keys();
682:                        for (int i = 0; i < keys.length; i++) {
683:                            if (keys[i] != null) {
684:                                transformer.setParameter(keys[i], kwt
685:                                        .getString(keys[i]));
686:                            }
687:                        }
688:                    }
689:
690:                    DOMSource src = new DOMSource(document);
691:                    DOMResult res = new DOMResult();
692:
693:                    // Execute XSL Transformation
694:                    if (transformer != null) {
695:                        transformer.transform(src, res);
696:                    } else {
697:                        if (logChannel != null) {
698:                            logChannel
699:                                    .write(
700:                                            Logger.WARNING,
701:                                            "Generic XSLT Processor ("
702:                                                    + name
703:                                                    + ") - Problem occured during TRansformer generation!");
704:                        }
705:                    }
706:
707:                    try {
708:                        String tempMT = (String) oo.getMIMEType();
709:                        tempMT = outMimeType
710:                                + tempMT.substring(inMimeType.length());
711:                        oo.setMIMEType(tempMT);
712:                    } catch (Exception ex) {
713:                        oo.setMIMEType(outMimeType);
714:                    }
715:
716:                    return res.getNode();
717:                } catch (Exception xfe) {
718:                    if (logChannel != null) {
719:                        logChannel
720:                                .write(
721:                                        Logger.WARNING,
722:                                        "Generic XSLT Processor ("
723:                                                + name
724:                                                + ") - Problem occured during XSL transformation!",
725:                                        xfe);
726:                    }
727:                }
728:                // Failed To Execute XSL Transformation
729:                // Therefore, we chose to return the original
730:                return document;
731:            }
732:
733:            /*
734:             * (non-Javadoc)
735:             * 
736:             * @see org.enhydra.util.ResponsePostProcessor#process(byte[],
737:             *      java.lang.String, java.lang.String)
738:             */
739:            public byte[] process(byte[] buteArray, String mimeEncoding,
740:                    String mimeType) {
741:                return buteArray;
742:            }
743:
744:            /*
745:             * (non-Javadoc)
746:             * 
747:             * @see org.enhydra.util.ResponsePostProcessor#setName(java.lang.String)
748:             */
749:            public void setName(String name) {
750:                this .name = name;
751:            }
752:
753:            /*
754:             * (non-Javadoc)
755:             * 
756:             * @see org.enhydra.util.ResponsePostProcessor#getName()
757:             */
758:            public String getName() {
759:                return name;
760:            }
761:
762:            /*
763:             * (non-Javadoc)
764:             * 
765:             * @see org.enhydra.util.ResponsePostProcessor#shouldProcess(java.lang.String)
766:             */
767:            public boolean shouldProcess(String mimeType) {
768:                if (mimeType != null && mimeType.startsWith(this .inMimeType))
769:                    return true;
770:                return false;
771:            }
772:
773:            /*
774:             * (non-Javadoc)
775:             * 
776:             * @see org.enhydra.util.ResponsePostProcessor#setLogChannel(com.lutris.logging.LogChannel)
777:             */
778:            public void setLogChannel(LogChannel logChannel) {
779:                this .logChannel = logChannel;
780:            }
781:
782:            /*
783:             * (non-Javadoc)
784:             * 
785:             * @see org.enhydra.util.ResponsePostProcessor#getLogChannel()
786:             */
787:            public LogChannel getLogChannel() {
788:                return logChannel;
789:            }
790:
791:            /*
792:             * (non-Javadoc)
793:             * 
794:             * @see org.enhydra.util.ResponsePostProcessor#getOutputMimeType()
795:             */
796:            public String getOutputMimeType() {
797:                return this .outMimeType;
798:            }
799:
800:            public Object clone() {
801:                GenericXSLTPostProcessor gxpp = new GenericXSLTPostProcessor(
802:                        this .getLogChannel());
803:                // Post Processor Name
804:                gxpp.name = this .name;
805:
806:                // Default XSL Template
807:                gxpp.defaultTemplatePath = this .defaultTemplatePath;
808:
809:                // MimeTypes
810:                gxpp.inMimeType = this .inMimeType;
811:                gxpp.outMimeType = this .outMimeType;
812:
813:                // XSL Template Parameters
814:                gxpp.TEMPLATE_PARAM = this .TEMPLATE_PARAM;
815:                gxpp.DEFAULT_TEMPLATE_PARAM = this .DEFAULT_TEMPLATE_PARAM;
816:
817:                // Transformer Factory
818:                gxpp.tFactory = this .tFactory;
819:
820:                // XSLTC OPtions
821:                gxpp.useXsltc = this .useXsltc;
822:                gxpp.transletName = this .transletName;
823:                gxpp.destinationDir = this .destinationDir;
824:                gxpp.packageName = this .packageName;
825:                gxpp.jarName = this .jarName;
826:                gxpp.generateTranslet = this .generateTranslet;
827:                gxpp.autoTranslet = this .autoTranslet;
828:                gxpp.useClasspath = this .useClasspath;
829:                gxpp.enableInline = this .enableInline;
830:                gxpp.debugXsltc = this .debugXsltc;
831:
832:                gxpp.baseDir = this .baseDir;
833:                gxpp.propsPrefix = this .propsPrefix;
834:
835:                return gxpp;
836:            }
837:
838:            /**
839:             * implements javax.xml.transform.URIResolver. This method is called by the
840:             * Transformer when it hits e.g. a 'document()' function or an 'import'
841:             * statement to resolve the location of files.
842:             * 
843:             * @param href
844:             *            the local href used
845:             * @param base
846:             *            the base to resolve against
847:             * @return a Source object that can be used to load a resource
848:             */
849:            public String resolve(String base, String href) {
850:                try {
851:                    File file = new File(href);
852:                    if (!file.exists())
853:                        file = new File(base, href);
854:                    return file.getCanonicalPath();
855:                } catch (Exception ex) {
856:                    ex.printStackTrace();
857:                    return null;
858:                }
859:            }
860:
861:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.