001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019: package de.schlund.pfixcore.util;
020:
021: import gnu.getopt.Getopt;
022:
023: import java.io.File;
024: import java.util.HashMap;
025: import java.util.Iterator;
026: import java.util.LinkedList;
027: import java.util.List;
028:
029: import javax.xml.transform.Templates;
030: import javax.xml.transform.Transformer;
031: import javax.xml.transform.TransformerConfigurationException;
032: import javax.xml.transform.TransformerException;
033: import javax.xml.transform.TransformerFactory;
034: import javax.xml.transform.dom.DOMSource;
035: import javax.xml.transform.stream.StreamResult;
036: import javax.xml.transform.stream.StreamSource;
037:
038: import org.apache.log4j.BasicConfigurator;
039: import org.w3c.dom.Document;
040: import org.w3c.dom.Element;
041: import org.w3c.dom.NodeList;
042:
043: import de.schlund.pfixxml.util.Xml;
044:
045: /**
046: * MultiTransform.java
047: *
048: *
049: * Created: Wed Apr 24 20:07:52 2002
050: *
051: * @author <a href="mailto:jtl@schlund.de">Jens Lautenbacher </a>
052: *
053: *
054: */
055: public class MultiTransform {
056: private static TransformerFactory trfac;
057: private String srcdir = null;
058: private String xslfile = null;
059: private String action = null;
060: private String out_ext = null;
061: private HashMap<String, String> params = new HashMap<String, String>();
062: private List<String> infiles = new LinkedList<String>();
063:
064: private MultiTransform() {
065: BasicConfigurator.configure();
066: trfac = TransformerFactory.newInstance();
067: }
068:
069: public static void main(String[] args) throws Exception {
070: if (args == null || args.length == 0) {
071: System.out.println("Usage:");
072: System.out
073: .println(">>> java de.schlund.pfixcore.util.MultiTransform -a action [std|iwrp] -x XSL-File");
074: System.out
075: .println(" (-s sourcedir)? (-o out_extension)?");
076: System.out
077: .println(" (-p key=val)* XML-Infile (XML-Infile)*");
078: System.exit(-1);
079: }
080: MultiTransform instance = new MultiTransform();
081: instance.scanOptions(args);
082: instance.callAction();
083: }
084:
085: public void callAction() throws Exception {
086: boolean error = false;
087: if (action != null) {
088: if (action.equals("std")) {
089: doStdfiles();
090: } else if (action.equals("iwrp")) {
091: doIWrappers();
092: } else if (action.equals("zoneprops")) {
093: doServletZone();
094: } else if (action.equals("docu")) {
095: doDocumentation();
096: } else {
097: error = true;
098: }
099: } else {
100: error = true;
101: }
102: if (error) {
103: System.out
104: .println("action needs to be one of 'std', 'zoneprops' or 'iwrp'");
105: System.exit(-1);
106: }
107: System.out.print("\n");
108: }
109:
110: private void doDocumentation() throws Exception {
111: Templates xsl = trfac.newTemplates(new StreamSource(xslfile));
112: for (Iterator<String> i = infiles.iterator(); i.hasNext();) {
113: String infile = i.next();
114: // Remove last extension xsl.in
115: String outfile = infile.substring(0, infile
116: .lastIndexOf("xsl"));
117: // Add doku.xml for generating documentation files
118: outfile = outfile + "doku.xml";
119: doTransformMaybe(null, xsl, infile, outfile, xslfile);
120: }
121: }
122:
123: private void doStdfiles() throws Exception {
124: File f = new File(xslfile);
125: Templates xsl = trfac.newTemplates(new StreamSource(f));
126: for (Iterator<String> i = infiles.iterator(); i.hasNext();) {
127: String infile = i.next();
128: // Remove the last extension part: .foo.in => .foo
129: String outfile = infile.substring(0, infile
130: .lastIndexOf("."));
131: // maybe add an output extension
132: if ((out_ext != null)) {
133: outfile = outfile + "." + out_ext;
134: }
135: doTransformMaybe(null, xsl, infile, outfile, xslfile);
136: }
137: }
138:
139: private void doIWrappers() throws Exception {
140: File f = new File(xslfile);
141: Templates xsl = trfac.newTemplates(new StreamSource(f));
142: for (Iterator<String> i = infiles.iterator(); i.hasNext();) {
143: String iwrp = i.next();
144: String pkg = iwrp.substring(0, iwrp.lastIndexOf("/"));
145: if (srcdir != null && pkg.startsWith(srcdir)) {
146: pkg = pkg.substring(srcdir.length());
147: }
148: pkg = pkg.replace(File.separatorChar, '.');
149: String outfile = iwrp.substring(0, iwrp.lastIndexOf("."));
150: outfile = outfile + ".java";
151: String klass = iwrp.substring(iwrp.lastIndexOf("/") + 1,
152: iwrp.lastIndexOf("."));
153: params.put("classname", klass);
154: params.put("package", pkg);
155: doTransformMaybe(null, xsl, iwrp, outfile, xslfile);
156: }
157: }
158:
159: private void doServletZone() throws Exception {
160: String outdir_tomcat = (String) params.get("outdir-tomcat");
161: String xslfile_tomcat = (String) params.get("xslfile-tomcat");
162: if (outdir_tomcat == null) {
163: System.out
164: .println("*** Need to have a parameter 'outdir-tomcat'");
165: System.exit(-1);
166: }
167: if (xslfile_tomcat == null) {
168: System.out
169: .println("*** Need to have a parameter 'xslfile-tomcat'");
170: System.exit(-1);
171: }
172: Templates xsltomcat = trfac.newTemplates(new StreamSource(
173: xslfile_tomcat));
174: Document doc = Xml.parseMutable((String) infiles.get(0));
175: NodeList nl = doc.getElementsByTagName("project");
176: for (int i = 0; i < nl.getLength(); i++) {
177: Element prj = (Element) nl.item(i);
178: String name = prj.getAttribute("name");
179: params.put("prjname", name);
180: File dir_tomcat = new File(outdir_tomcat + "/webapps/"
181: + name + "/WEB-INF");
182: dir_tomcat.mkdirs();
183: String outfile_tomcat = dir_tomcat.getAbsolutePath()
184: + "/web.xml";
185: doTransformMaybe(doc, xsltomcat, (String) infiles.get(0),
186: outfile_tomcat, xslfile_tomcat);
187: }
188: }
189:
190: private void doTransformMaybe(Document indoc, Templates tmpl,
191: String infile, String outfile, String xslfile)
192: throws TransformerConfigurationException,
193: TransformerException {
194: File in = new File(infile);
195: File out = new File(outfile);
196: File xsl = new File(xslfile);
197: if (in.exists() && in.isFile() && in.canRead()
198: && (!out.exists() || out.canWrite())) {
199: if ((out.exists() && ((in.lastModified() > out
200: .lastModified()) || xsl.lastModified() > out
201: .lastModified()))
202: || !out.exists()) {
203: System.out.println(">>> " + infile + " ==> " + outfile);
204: Transformer trafo = tmpl.newTransformer();
205: for (Iterator<String> i = params.keySet().iterator(); i
206: .hasNext();) {
207: String key = i.next();
208: String val = params.get(key);
209: trafo.setParameter(key, val);
210: }
211: if (indoc != null) {
212: trafo.transform(new DOMSource(indoc),
213: new StreamResult(outfile));
214: } else {
215: File f = new File(infile);
216: trafo.transform(new StreamSource(f),
217: new StreamResult(outfile));
218: }
219: } else {
220: System.out.print(".");
221: }
222: } else {
223: StringBuffer buf = new StringBuffer();
224: buf.append("File error: ").append(infile).append(" ==> ")
225: .append(outfile).append("\n");
226: buf.append(infile + " isFile: ").append(in.isFile())
227: .append("\n");
228: buf.append(infile + " canRead: ").append(in.canRead())
229: .append("\n");
230: buf.append(outfile + " exists: ").append(in.exists())
231: .append("\n");
232: buf.append(outfile + " canWrite: ").append(in.exists())
233: .append("\n");
234: throw new RuntimeException(buf.toString());
235: }
236: }
237:
238: public void scanOptions(String[] args) throws Exception {
239: Getopt g = new Getopt("MultiTransform", args, "s:x:p:a:");
240: int c;
241: while ((c = g.getopt()) != -1) {
242: switch (c) {
243: case 'x':
244: xslfile = g.getOptarg();
245: break;
246: case 's':
247: srcdir = g.getOptarg();
248: break;
249: case 'o':
250: out_ext = g.getOptarg();
251: break;
252: case 'a':
253: action = g.getOptarg();
254: break;
255: case 'p':
256: String par = g.getOptarg();
257: if (par.indexOf("=") <= 0) {
258: }
259: String key = par.substring(0, par.indexOf("="));
260: String val = par.substring(par.indexOf("=") + 1);
261: params.put(key, val);
262: break;
263: case '?':
264: System.exit(-1);
265: }
266: }
267: boolean error = false;
268: if (xslfile == null) {
269: System.out.println("Error: Need to give a xsl file");
270: error = true;
271: }
272: if (action == null) {
273: System.out.println("Error: Need to give an action");
274: error = true;
275: }
276: if (g.getOptind() == args.length) {
277: System.out.println("Error: Need at last one input file");
278: error = true;
279: }
280: if (error)
281: System.exit(-1);
282: for (int i = g.getOptind(); i < args.length; i++) {
283: infiles.add(args[i]);
284: }
285: }
286: } // MultiTransform
|