001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.module;
011:
012: import java.util.*;
013: import java.io.*;
014:
015: import org.mmbase.module.core.MMBaseContext;
016: import org.mmbase.util.*;
017: import org.mmbase.util.logging.Logger;
018: import org.mmbase.util.logging.Logging;
019:
020: /**
021: * XSL conversion module
022: *
023: * Right now, only the replace() method is defined. It is called as:
024: * $MOD-XSLCONVERT-xmlPath-xslFile
025: * where xmlPath is the path relative to mmbase.config and xslFile is
026: * and xsl file located in the subdirectory xslt of mmbase.config.
027: *
028: * @application XSL or Tools
029: * @move org.mmbase.util.xml
030: * @author Case Roole, cjr@dds.nl
031: * @version $Id: XSLConvert.java,v 1.13 2007/06/19 13:59:30 michiel Exp $
032: */
033: public class XSLConvert extends ProcessorModule {
034:
035: private static final Logger log = Logging
036: .getLoggerInstance(XSLConvert.class);
037:
038: private String configpath;
039:
040: public void init() {
041: configpath = MMBaseContext.getConfigPath();
042: }
043:
044: public XSLConvert(String name) {
045: super (name);
046: }
047:
048: /**
049: * Generate a list of values from a command to the processor
050: *
051: * NOT IMPLEMENTED FOR XSLConvert
052: */
053: public Vector getList(PageInfo sp, StringTagger tagger, String value) {
054: return null;
055: }
056:
057: /**
058: * Execute the commands provided in the form values
059: */
060: public boolean process(PageInfo sp, Hashtable cmds, Hashtable vars) {
061: return false;
062: }
063:
064: /**
065: * Handle a $MOD command
066: *
067: * It is called as:
068: * $MOD-XSLCONVERT-xmlPath-xslFile
069: * where:
070: * - xmlPath is the path relative to mmbase.config and,
071: * - xslFile is xsl file located in the subdirectory xslt of mmbase.config.
072: */
073: public String replace(PageInfo sp, String cmds) {
074: StringTokenizer tok = new StringTokenizer(cmds, "-\n\r");
075: int count = tok.countTokens();
076: String[] argv = new String[count];
077: for (int i = 0; i < count; i++) {
078: argv[i] = tok.nextToken();
079: }
080:
081: if (argv.length != 2) {
082: return "$MOD-XSLCONVERT should have two arguments, e.g. $MOD-XSLCONVERT-xmlPath-xslFile";
083: } else {
084: String xmlPath = configpath + File.separator + argv[0];
085: String xslPath = configpath + File.separator + "xslt"
086: + File.separator + argv[1];
087:
088: if (!xslPath.endsWith(".xsl")) {
089: xslPath = xslPath + ".xsl";
090: }
091: if (!xmlPath.endsWith(".xml")) {
092: xmlPath = xmlPath + ".xml";
093: }
094:
095: return transform(xmlPath, xslPath);
096: }
097: //return "[no command defined]";
098: }
099:
100: /**
101: * Transform XML file using an XSL file
102: *
103: * @param xmlPath Path to XML file
104: * @param xslPath Path to XSL file
105: * @return Converted document
106: */
107: public String transform(String xmlPath, String xslPath) {
108: // Do nothing for the time being
109: if (log.isDebugEnabled()) {
110: log.debug("XML file = " + xmlPath);
111: log.debug("XSL file = " + xslPath);
112: }
113: return XSLTransformer.transform(xmlPath, xslPath);
114: }
115:
116: public String getModuleInfo() {
117: return "Support XSL transformations of XML files, cjr@dds.nl";
118: }
119:
120: }
|