1 // NAnt - A .NET build tool
\r
2 // Copyright (C) 2001 Gerry Shaw
\r
4 // This program is free software; you can redistribute it and/or modify
\r
5 // it under the terms of the GNU General Public License as published by
\r
6 // the Free Software Foundation; either version 2 of the License, or
\r
7 // (at your option) any later version.
\r
9 // This program is distributed in the hope that it will be useful,
\r
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
12 // GNU General Public License for more details.
\r
14 // You should have received a copy of the GNU General Public License
\r
15 // along with this program; if not, write to the Free Software
\r
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
18 // Serge (serge@wildwestsoftware.com)
\r
19 // Gerry Shaw (gerry_shaw@yahoo.com)
\r
21 namespace SourceForge.NAnt {
\r
26 using System.Xml.Xsl;
\r
27 using System.Xml.XPath;
\r
28 using System.Text.RegularExpressions;
\r
31 public class StyleTask : Task {
\r
33 // TODO: consider prefixing private fields with _ to stay consistent (gs)
\r
35 [TaskAttribute("basedir", Required=false)]
\r
36 string baseDir = null;
\r
38 [TaskAttribute("destdir", Required=false)]
\r
39 string destDir = null;
\r
41 [TaskAttribute("extension", Required=false)]
\r
42 string extension = "html";
\r
44 [TaskAttribute("style", Required=true)]
\r
45 string xsltFile = null;
\r
47 [TaskAttribute("in", Required=true)]
\r
48 string srcFile = null;
\r
50 [TaskAttribute("out", Required=false)]
\r
51 string destFile = null;
\r
53 private static string GetPath(string dir, string file) {
\r
54 // TODO: remove platform dependencies by using System.IO.Path (gs)
\r
55 string d = (dir == null)
\r
57 : Regex.Replace(dir, "/", "\\");
\r
59 return (d==null || d=="")
\r
60 ? (file==null || file=="") ? "" : file
\r
62 ? d +file : d + "\\" + file;
\r
65 private XmlReader CreateXmlReader(string dir, string file) {
\r
66 string xmlPath = GetPath(dir, file);
\r
67 XmlTextReader xmlReader = null;
\r
70 xmlReader = new XmlTextReader(new FileStream(xmlPath, FileMode.Open));
\r
71 } catch (Exception) {
\r
78 private XmlWriter CreateXmlWriter(string dir, string file) {
\r
79 string xmlPath = GetPath(dir, file);
\r
81 XmlWriter xmlWriter = null;
\r
83 string targetDir = Path.GetDirectoryName(Path.GetFullPath(xmlPath));
\r
84 if (targetDir != null && targetDir != "" && !Directory.Exists(targetDir)) {
\r
85 Directory.CreateDirectory(targetDir);
\r
89 // UTF-8 encoding will be used
\r
90 xmlWriter = new XmlTextWriter(xmlPath, null);
\r
91 } catch (Exception) {
\r
98 protected override void ExecuteTask() {
\r
99 string destFile = this.destFile;
\r
101 if (destFile == null || destFile == "") {
\r
102 // TODO: use System.IO.Path (gs)
\r
103 string ext = extension[0]=='.'
\r
107 int extPos = srcFile.LastIndexOf('.');
\r
109 if (extPos == -1) {
\r
110 destFile = srcFile + ext;
\r
112 destFile = srcFile.Substring(0, extPos) + ext;
\r
116 string srcPath = GetPath(baseDir, srcFile);
\r
117 string destPath = GetPath(destDir, destFile);
\r
118 string xsltPath = GetPath(baseDir, xsltFile);
\r
120 FileInfo srcInfo = new FileInfo(srcPath);
\r
121 FileInfo destInfo = new FileInfo(destPath);
\r
122 FileInfo xsltInfo = new FileInfo(xsltPath);
\r
124 if (!srcInfo.Exists) {
\r
125 throw new BuildException("Unable to find source xml file.");
\r
127 if (!xsltInfo.Exists) {
\r
128 throw new BuildException("Unable to find stylesheet file.");
\r
131 bool destOutdated = !destInfo.Exists
\r
132 || srcInfo.LastWriteTime > destInfo.LastWriteTime
\r
133 || xsltInfo.LastWriteTime > destInfo.LastWriteTime;
\r
135 if (destOutdated) {
\r
136 XmlReader xmlReader = CreateXmlReader(baseDir, srcFile);
\r
137 XmlReader xslReader = CreateXmlReader(baseDir, xsltFile);
\r
138 XmlWriter xmlWriter = CreateXmlWriter(destDir, destFile);
\r
140 Log.WriteLine(LogPrefix + "Transforming into " + Path.GetFullPath(destDir));
\r
142 // TODO: remove assignments from conditional statement (gs)
\r
143 if (xmlReader != null && xslReader != null && xmlWriter != null) {
\r
144 XslTransform xslt = new XslTransform();
\r
145 XPathDocument xml = new XPathDocument(xmlReader);
\r
147 Log.WriteLine(LogPrefix + "Loading stylesheet " + Path.GetFullPath(xsltPath));
\r
149 xslt.Load(xslReader);
\r
150 } catch (XsltCompileException xce) {
\r
151 throw new BuildException(xce.Message, xce);
\r
152 } catch (Exception e) {
\r
153 throw new BuildException(e.Message, e);
\r
156 Log.WriteLine(LogPrefix + "Processing " + Path.GetFullPath(srcPath) + " to " + Path.GetFullPath(destPath));
\r
158 xslt.Transform(xml, null, xmlWriter);
\r
159 } catch (Exception e) {
\r
160 throw new BuildException(e.Message, e);
\r
163 // not sure how to deal with this...
\r
164 // TODO: remove this statement or do something useful (gs)
\r
165 // Can this condition occur? I would have thought
\r
166 // that an exception would be thrown. (gs)
\r