Welcome to mirror list, hosted at ThFree Co, Russian Federation.

StyleTask.cs « Tasks « src « nant « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 05117217d0b8b4e30c1783a09f00bb7046f88e1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// NAnt - A .NET build tool
// Copyright (C) 2001 Gerry Shaw
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Serge (serge@wildwestsoftware.com)
// Gerry Shaw (gerry_shaw@yahoo.com)

namespace SourceForge.NAnt {

    using System;
    using System.IO;
    using System.Xml;
    using System.Xml.Xsl;
    using System.Xml.XPath;
    using System.Text.RegularExpressions;

    [TaskName("style")]
    public class StyleTask : Task {

        // TODO: consider prefixing private fields with _ to stay consistent (gs)

        [TaskAttribute("basedir", Required=false)]
        string baseDir = null;

        [TaskAttribute("destdir", Required=false)]
        string destDir = null;

        [TaskAttribute("extension", Required=false)]
        string extension = "html";

        [TaskAttribute("style", Required=true)]
        string xsltFile = null;

        [TaskAttribute("in", Required=true)]
        string srcFile = null;

        [TaskAttribute("out", Required=false)]
        string destFile = null;

        private static string GetPath(string dir, string file) {
            // TODO: remove platform dependencies by using System.IO.Path (gs)
            string d = (dir == null)
                ? ""
                : Regex.Replace(dir, "/", "\\");

            return (d==null || d=="")
                ? (file==null || file=="") ? "" : file
                : d.EndsWith("\\")
                ? d +file : d + "\\" + file;
        }

        private XmlReader CreateXmlReader(string dir, string file) {
            string xmlPath = GetPath(dir, file);
            XmlTextReader xmlReader = null;

            try {
                xmlReader = new XmlTextReader(new FileStream(xmlPath, FileMode.Open));
            } catch (Exception) {
                xmlReader = null;
            }

            return xmlReader;
        }

        private XmlWriter CreateXmlWriter(string dir, string file) {
            string xmlPath = GetPath(dir, file);

            XmlWriter xmlWriter = null;

            string targetDir = Path.GetDirectoryName(Path.GetFullPath(xmlPath));
            if (targetDir != null && targetDir != "" && !Directory.Exists(targetDir)) {
                Directory.CreateDirectory(targetDir);
            }

            try {
                // UTF-8 encoding will be used
                xmlWriter = new XmlTextWriter(xmlPath, null);
            } catch (Exception) {
                xmlWriter = null;
            }

            return xmlWriter;
        }

        protected override void ExecuteTask() {
            string destFile = this.destFile;

            if (destFile == null || destFile == "") {
                // TODO: use System.IO.Path (gs)
                string ext = extension[0]=='.'
                    ? extension
                    : "." + extension;

                int extPos = srcFile.LastIndexOf('.');

                if (extPos == -1) {
                    destFile = srcFile + ext;
                } else {
                    destFile = srcFile.Substring(0, extPos) + ext;
                }
            }

            string srcPath = GetPath(baseDir, srcFile);
            string destPath = GetPath(destDir, destFile);
            string xsltPath = GetPath(baseDir, xsltFile);

            FileInfo srcInfo = new FileInfo(srcPath);
            FileInfo destInfo = new FileInfo(destPath);
            FileInfo xsltInfo = new FileInfo(xsltPath);

            if (!srcInfo.Exists) {
                throw new BuildException("Unable to find source xml file.");
            }
            if (!xsltInfo.Exists) {
                throw new BuildException("Unable to find stylesheet file.");
            }

            bool destOutdated = !destInfo.Exists
                || srcInfo.LastWriteTime > destInfo.LastWriteTime
                || xsltInfo.LastWriteTime > destInfo.LastWriteTime;

            if (destOutdated) {
                XmlReader xmlReader = CreateXmlReader(baseDir, srcFile);
                XmlReader xslReader = CreateXmlReader(baseDir, xsltFile);
                XmlWriter xmlWriter = CreateXmlWriter(destDir, destFile);

                Log.WriteLine(LogPrefix + "Transforming into " + Path.GetFullPath(destDir));

                // TODO: remove assignments from conditional statement (gs)
                if (xmlReader != null && xslReader != null && xmlWriter != null) {
                    XslTransform xslt = new XslTransform();
                    XPathDocument xml = new XPathDocument(xmlReader);

                    Log.WriteLine(LogPrefix + "Loading stylesheet " + Path.GetFullPath(xsltPath));
                    try {
                        xslt.Load(xslReader);
                    } catch (XsltCompileException xce) {
                        throw new BuildException(xce.Message, xce);
                    } catch (Exception e) {
                        throw new BuildException(e.Message, e);
                    }

                    Log.WriteLine(LogPrefix + "Processing " + Path.GetFullPath(srcPath) + " to " + Path.GetFullPath(destPath));
                    try {
                        xslt.Transform(xml, null, xmlWriter);
                    } catch (Exception e) {
                        throw new BuildException(e.Message, e);
                    }
                } else {
                    // not sure how to deal with this...
                    // TODO: remove this statement or do something useful (gs)
                    // Can this condition occur? I would have thought
                    // that an exception would be thrown. (gs)
                }
            }
        }
    }
}