KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JTidyTask


1 /*****************************************************************************
2  * Copyright (C) The Krysalis project. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * This software is published under the terms of the Krysalis Patchy *
5  * Software License version 1.1_01, a copy of which has been included *
6  * at the bottom of this file. *
7  *****************************************************************************/

8  
9 import java.io.InputStream JavaDoc;
10 import java.io.FileInputStream JavaDoc;
11 import java.io.BufferedInputStream JavaDoc;
12 import java.io.OutputStream JavaDoc;
13 import java.io.FileOutputStream JavaDoc;
14 import java.io.Writer JavaDoc;
15 import java.io.PrintWriter JavaDoc;
16 import java.io.FileWriter JavaDoc;
17 import java.io.BufferedWriter JavaDoc;
18 import java.io.FileWriter JavaDoc;
19 import java.io.IOException JavaDoc;
20
21 import java.util.ArrayList JavaDoc;
22
23 import org.w3c.dom.NodeList JavaDoc;
24 import org.w3c.dom.Node JavaDoc;
25 import org.w3c.dom.Element JavaDoc;
26 import org.w3c.dom.Attr JavaDoc;
27 import org.w3c.dom.Document JavaDoc;
28 import org.w3c.dom.NamedNodeMap JavaDoc;
29 import org.w3c.tidy.Tidy;
30
31 import org.apache.tools.ant.BuildException;
32 import org.apache.tools.ant.taskdefs.Property;
33
34 /**
35  * Task to ask property values to the user. Uses current value as default.
36  *
37  * @author <a HREF="mailto:barozzi@nicolaken.com">Nicola Ken Barozzi</a>
38  * @created 14 January 2002
39  */

40
41 public class JTidyTask
42     extends org.apache.tools.ant.Task
43 {
44     private String JavaDoc src;
45     private String JavaDoc dest;
46     private String JavaDoc log;
47     private Tidy tidy;
48     private String JavaDoc warn = "false";
49     private String JavaDoc summary = "false";
50     PrintWriter JavaDoc pw;
51
52     /**
53      * Constructor.
54      */

55
56     public JTidyTask()
57     {
58         super();
59     }
60
61     /**
62      * Initializes the task.
63      */

64
65     public void init()
66     {
67         super.init();
68
69         // Setup an instance of Tidy.
70
tidy = new Tidy();
71         tidy.setXmlOut(true);
72         tidy.setXHTML(true);
73         tidy.setDropFontTags(true);
74         tidy.setLiteralAttribs(true);
75         tidy.setMakeClean(true);
76         tidy.setShowWarnings(Boolean.getBoolean(warn));
77         tidy.setQuiet(!Boolean.getBoolean(summary));
78     }
79
80     /**
81      * Run the task.
82      * @exception org.apache.tools.ant.BuildException The exception raised during task execution.
83      */

84
85     public void execute()
86         throws org.apache.tools.ant.BuildException
87     {
88         try
89         {
90             PrintWriter JavaDoc pw = new PrintWriter JavaDoc(new FileWriter JavaDoc(log));
91
92             tidy.setErrout(pw);
93
94             // Extract the document using JTidy and stream it.
95
BufferedInputStream JavaDoc in =
96                 new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(src));
97
98             // FileOutputStream out = new FileOutputStream(dest);
99
PrintWriter JavaDoc out =
100                 new PrintWriter JavaDoc(new FileWriter JavaDoc(dest));
101
102             // using null as output to get dom so to remove duplicate attributes
103
org.w3c.dom.Document JavaDoc domDoc = tidy.parseDOM(in, null);
104
105             domDoc.normalize();
106             stripDuplicateAttributes(domDoc, null);
107             org.apache.xml.serialize.OutputFormat format =
108                 new org.apache.xml.serialize.OutputFormat();
109
110             format.setIndenting(true);
111             format.setEncoding("ISO-8859-1");
112             format.setPreserveSpace(true);
113             format.setLineSeparator("\n");
114             org.apache.xml.serialize.XMLSerializer serializer =
115                 new org.apache.xml.serialize.XMLSerializer(out, format);
116
117             serializer.serialize(domDoc);
118             out.flush();
119             out.close();
120             in.close();
121             pw.flush();
122             pw.close();
123         }
124         catch (IOException JavaDoc ioe)
125         {
126             throw new BuildException(ioe);
127         }
128     }
129
130     public void setSrc(String JavaDoc src)
131     {
132         this.src = src;
133     }
134
135     public void setDest(String JavaDoc dest)
136     {
137         this.dest = dest;
138     }
139
140     public void setLog(String JavaDoc log)
141     {
142         this.log = log;
143     }
144
145     public void setWarn(String JavaDoc warn)
146     {
147         this.warn = warn;
148     }
149
150     public void setSummary(String JavaDoc summary)
151     {
152         this.summary = summary;
153     }
154
155     // using parent because jtidy dom is bugged, cannot get parent or delete child
156
public static void stripDuplicateAttributes(Node JavaDoc node, Node JavaDoc parent)
157     {
158
159         // The output depends on the type of the node
160
switch (node.getNodeType())
161         {
162
163             case Node.DOCUMENT_NODE :
164             {
165                 Document JavaDoc doc = ( Document JavaDoc ) node;
166                 Node JavaDoc child = doc.getFirstChild();
167
168                 while (child != null)
169                 {
170                     stripDuplicateAttributes(child, node);
171                     child = child.getNextSibling();
172                 }
173                 break;
174             }
175             case Node.ELEMENT_NODE :
176             {
177                 Element JavaDoc elt = ( Element JavaDoc ) node;
178                 NamedNodeMap JavaDoc attrs = elt.getAttributes();
179                 ArrayList JavaDoc nodesToRemove = new ArrayList JavaDoc();
180                 int nodesToRemoveNum = 0;
181
182                 for (int i = 0; i < attrs.getLength(); i++)
183                 {
184                     Node JavaDoc a = attrs.item(i);
185
186                     for (int j = 0; j < attrs.getLength(); j++)
187                     {
188                         Node JavaDoc b = attrs.item(j);
189
190                         // if there are two attributes with same name
191
if ((i != j)
192                                 && (a.getNodeName().equals(b.getNodeName())))
193                         {
194                             nodesToRemove.add(b);
195                             nodesToRemoveNum++;
196                         }
197                     }
198                 }
199                 for (int i = 0; i < nodesToRemoveNum; i++)
200                 {
201                     org.w3c.dom.Attr JavaDoc nodeToDelete =
202                         ( org.w3c.dom.Attr JavaDoc ) nodesToRemove.get(i);
203                     org.w3c.dom.Element JavaDoc nodeToDeleteParent =
204                         ( org.w3c.dom
205
JavaDoc                            .Element ) node; // nodeToDelete.getParentNode();
206

207                     nodeToDeleteParent.removeAttributeNode(nodeToDelete);
208                 }
209                 nodesToRemove.clear();
210                 Node JavaDoc child = elt.getFirstChild();
211
212                 while (child != null)
213                 {
214                     stripDuplicateAttributes(child, node);
215                     child = child.getNextSibling();
216                 }
217                 break;
218             }
219             default :
220
221                 // do nothing
222
break;
223         }
224     }
225 }
226
227 /*
228 The Krysalis Patchy Software License, Version 1.1_01
229 Copyright (c) 2002 Nicola Ken Barozzi. All rights reserved.
230
231 This Licence is compatible with the BSD licence as described and
232 approved by http://www.opensource.org/, and is based on the
233 Apache Software Licence Version 1.1.
234
235 Redistribution and use in source and binary forms, with or without
236 modification, are permitted provided that the following conditions
237 are met:
238
239  1. Redistributions of source code must retain the above copyright
240     notice, this list of conditions and the following disclaimer.
241
242 2. Redistributions in binary form must reproduce the above copyright
243    notice, this list of conditions and the following disclaimer in
244    the documentation and/or other materials provided with the
245    distribution.
246
247 3. The end-user documentation included with the redistribution,
248    if any, must include the following acknowledgment:
249       "This product includes software developed for project
250        Krysalis (http://www.krysalis.org/)."
251    Alternately, this acknowledgment may appear in the software itself,
252    if and wherever such third-party acknowledgments normally appear.
253
254 4. The names "Krysalis" and "Nicola Ken Barozzi" and
255    "Krysalis Centipede" must not be used to endorse or promote products
256    derived from this software without prior written permission. For
257    written permission, please contact krysalis@nicolaken.org.
258    
259 5. Products derived from this software may not be called "Krysalis",
260    "Krysalis Centipede", nor may "Krysalis" appear in their name,
261    without prior written permission of Nicola Ken Barozzi.
262
263 6. This software may contain voluntary contributions made by many
264    individuals, who decided to donate the code to this project in
265    respect of this licence.
266
267 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
268 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
269 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
270 DISCLAIMED. IN NO EVENT SHALL THE KRYSALIS PROJECT OR
271 ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
272 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
273 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
274 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
275 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
276 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
277 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
278 SUCH DAMAGE.
279 ====================================================================*/

280
Popular Tags