001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.ant;
024:
025: import java.io.File;
026: import java.io.FileInputStream;
027: import java.io.IOException;
028: import java.util.ArrayList;
029: import java.util.Collection;
030: import java.util.Iterator;
031:
032: import javax.xml.parsers.ParserConfigurationException;
033: import javax.xml.transform.TransformerException;
034:
035: import org.apache.tools.ant.BuildException;
036: import org.apache.tools.ant.DirectoryScanner;
037: import org.apache.tools.ant.Task;
038: import org.apache.tools.ant.types.FileSet;
039: import org.apache.xpath.XPathAPI;
040: import org.w3c.dom.Document;
041: import org.xml.sax.SAXException;
042:
043: import biz.hammurapi.xml.dom.DOMUtils;
044:
045: /**
046: * This task applies a stylesheet to XML files. Output file name is calculated from XPath expression.
047: * @author Pavel Vlasov
048: * @revision $Revision$
049: */
050: public class StyleTask extends Task {
051:
052: private File outputDir;
053:
054: private String fileName;
055:
056: private Collection fileSets = new ArrayList();
057:
058: private File style;
059:
060: /**
061: * Output directory.
062: * @param outputDir
063: */
064: public void setOutputDir(File outputDir) {
065: this .outputDir = outputDir;
066: }
067:
068: /**
069: * XPath expression for file name. Use / for file separator.
070: * @param fileName
071: */
072: public void setFileName(String fileName) {
073: this .fileName = fileName;
074: }
075:
076: public void addFileSet(FileSet fileSet) {
077: fileSets.add(fileSet);
078: }
079:
080: public void setStyle(File style) {
081: this .style = style;
082: }
083:
084: public void execute() throws BuildException {
085: try {
086: Iterator it = fileSets.iterator();
087: while (it.hasNext()) {
088: FileSet fs = (FileSet) it.next();
089: DirectoryScanner ds = fs
090: .getDirectoryScanner(getProject());
091: String[] files = ds.getIncludedFiles();
092: for (int i = 0; i < files.length; i++) {
093: File in = new File(ds.getBasedir(), files[i]);
094: Document doc = DOMUtils.parse(in);
095:
096: String fn = XPathAPI.eval(doc.getDocumentElement(),
097: fileName).toString().replace('/',
098: File.separatorChar);
099: File out = new File(outputDir, fn);
100:
101: File parentDir = out.getParentFile();
102: if (!parentDir.exists()) {
103: if (!parentDir.mkdirs()) {
104: throw new BuildException(
105: "Could not create directory: "
106: + parentDir
107: .getAbsolutePath());
108: }
109: }
110:
111: DOMUtils.style(doc, out,
112: new FileInputStream(style), null);
113: }
114: }
115: } catch (IOException e) {
116: throw new BuildException(e);
117: } catch (SAXException e) {
118: throw new BuildException(e);
119: } catch (ParserConfigurationException e) {
120: throw new BuildException(e);
121: } catch (TransformerException e) {
122: throw new BuildException(e);
123: }
124: }
125:
126: }
|