StyleTask.java

biz/hammurapi/ant/StyleTask.java

Violations

Inspector Message Severity Location
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 049 Use a Collection instead of arrays Object[] 2 97:39
Java Inspector 085 Do not declare runtime exceptions in the throws clause. 2 91:9
Java Inspector 089 Method documentation is too short. It is only 2 words. Should be at least 3 words. 2 60:9
Java Inspector 089 Parameter outputDir is not documented 2 60:9
Java Inspector 089 Parameter fileName is not documented 2 69:9
Java Inspector 089 Parameter fileSet is not documented 2 78:9
Java Inspector 089 Method documentation is too short. It is only 1 words. Should be at least 3 words. 2 87:9
Java Inspector 089 Parameter style is not documented 2 87:9
Java Inspector 089 Undocumented method 2 91:9
Java Inspector 024 Avoid hardwired character literals 3 102:122
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 108:82

Source code

1/*
2 * hgcommons 9
3 * Hammurapi Group Common Library
4 * Copyright (C) 2003 Hammurapi Group
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21 * e-Mail: support@hammurapi.biz
22 */
23package biz.hammurapi.ant;
24
25import java.io.File;
26import java.io.FileInputStream;
27import java.util.ArrayList;
28import java.util.Collection;
29import java.util.Iterator;
30
31import org.apache.tools.ant.BuildException;
32import org.apache.tools.ant.DirectoryScanner;
33import org.apache.tools.ant.Task;
34import org.apache.tools.ant.types.FileSet;
35import org.w3c.dom.Document;
36
37import biz.hammurapi.xml.dom.DOMUtils;
38
39/**
40 * This task applies a stylesheet to XML files. Output file name is calculated from XPath expression.
41 * @author Pavel Vlasov
42 * @ant.task name="Style" category="Common"
43 * @revision $Revision$
44 */
45public class StyleTask extends Task {
46
47 private File outputDir;
48
49 private String fileName;
50
51 private Collection fileSets = new ArrayList();
52
53 private File style;
54
55 /**
56 * Output directory.
57 * @ant.required
58 * @param outputDir
59 */
60 public void setOutputDir(File outputDir) {
61 this.outputDir = outputDir;
62 }
63
64 /**
65 * XPath expression for file name. Use / for file separator.
66 * @ant.required
67 * @param fileName
68 */
69 public void setFileName(String fileName) {
70 this.fileName = fileName;
71 }
72
73 /**
74 * Adds file set
75 * @ant.not-required
76 * @param fileSet
77 */
78 public void addFileSet(FileSet fileSet) {
79 fileSets.add(fileSet);
80 }
81
82 /**
83 * Style
84 * @ant.required
85 * @param style
86 */
87 public void setStyle(File style) {
88 this.style = style;
89 }
90
91 public void execute() throws BuildException {
92 try {
93 Iterator it=fileSets.iterator();
94 while (it.hasNext()) {
95 FileSet fs=(FileSet) it.next();
96 DirectoryScanner ds = fs.getDirectoryScanner(getProject());
97 String[] files=ds.getIncludedFiles();
98 for (int i=0; i<files.length; i++) {
99 File in = new File(ds.getBasedir(), files[i]);
100 Document doc = DOMUtils.parse(in);
101
102 String fn = DOMUtils.eval(doc.getDocumentElement(), fileName).toString().replace('/', File.separatorChar);
103 File out = new File(outputDir, fn);
104
105 File parentDir = out.getParentFile();
106 if (!parentDir.exists()) {
107 if (!parentDir.mkdirs()) {
108 throw new BuildException("Could not create directory: "+parentDir.getAbsolutePath());
109 }
110 }
111
112 DOMUtils.style(doc, out, new FileInputStream(style), null);
113 }
114 }
115 } catch (Exception e) {
116 throw new BuildException(e);
117 }
118 }
119
120}
121