KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > module > XSLConvert


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.module;
11
12 import java.util.*;
13 import java.io.*;
14
15 import org.mmbase.module.core.MMBaseContext;
16 import org.mmbase.util.*;
17 import org.mmbase.util.logging.Logger;
18 import org.mmbase.util.logging.Logging;
19
20 /**
21  * XSL conversion module
22  *
23  * Right now, only the replace() method is defined. It is called as:
24  * $MOD-XSLCONVERT-xmlPath-xslFile
25  * where xmlPath is the path relative to mmbase.config and xslFile is
26  * and xsl file located in the subdirectory xslt of mmbase.config.
27  *
28  * @application XSL or Tools
29  * @move org.mmbase.util.xml
30  * @author Case Roole, cjr@dds.nl
31  * @version $Id: XSLConvert.java,v 1.12 2004/10/25 08:08:36 pierre Exp $
32  */

33 public class XSLConvert extends ProcessorModule {
34
35     private static final Logger log = Logging.getLoggerInstance(XSLConvert.class);
36
37     private String JavaDoc configpath;
38
39     public void init() {
40         configpath = MMBaseContext.getConfigPath();
41     }
42
43     public XSLConvert() {}
44
45     /**
46      * Generate a list of values from a command to the processor
47      *
48      * NOT IMPLEMENTED FOR XSLConvert
49      */

50     public Vector getList(PageInfo sp,StringTagger tagger, String JavaDoc value) {
51         return null;
52     }
53
54     /**
55      * Execute the commands provided in the form values
56      */

57     public boolean process(PageInfo sp, Hashtable cmds,Hashtable vars) {
58         return false;
59     }
60
61     /**
62     * Handle a $MOD command
63     *
64     * It is called as:
65     * $MOD-XSLCONVERT-xmlPath-xslFile
66     * where:
67     * - xmlPath is the path relative to mmbase.config and,
68     * - xslFile is xsl file located in the subdirectory xslt of mmbase.config.
69     */

70     public String JavaDoc replace(PageInfo sp, String JavaDoc cmds) {
71         StringTokenizer tok = new StringTokenizer(cmds,"-\n\r");
72         int count = tok.countTokens();
73         String JavaDoc[] argv = new String JavaDoc[count];
74         for (int i=0; i<count; i++) {
75             argv[i] = tok.nextToken();
76         }
77
78         if (argv.length != 2) {
79             return "$MOD-XSLCONVERT should have two arguments, e.g. $MOD-XSLCONVERT-xmlPath-xslFile";
80         } else {
81             String JavaDoc xmlPath = configpath+File.separator+argv[0];
82             String JavaDoc xslPath = configpath+File.separator+"xslt"+File.separator+argv[1];
83
84             if (!xslPath.endsWith(".xsl")) {
85                 xslPath = xslPath+".xsl";
86             }
87             if (!xmlPath.endsWith(".xml")) {
88                 xmlPath = xmlPath+".xml";
89             }
90
91             return transform(xmlPath,xslPath);
92         }
93         //return "[no command defined]";
94
}
95
96     /**
97      * Transform XML file using an XSL file
98      *
99      * @param xmlPath Path to XML file
100      * @param xslPath Path to XSL file
101      * @return Converted document
102      */

103     public String JavaDoc transform(String JavaDoc xmlPath, String JavaDoc xslPath) {
104         // Do nothing for the time being
105
if (log.isDebugEnabled()) {
106             log.debug("XML file = "+xmlPath);
107             log.debug("XSL file = "+xslPath);
108         }
109         return XSLTransformer.transform(xmlPath,xslPath);
110     }
111
112     public String JavaDoc getModuleInfo() {
113         return "Support XSL transformations of XML files, cjr@dds.nl";
114     }
115
116 }
117
Popular Tags