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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache POI" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache POI", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.Writer;
import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.tidy.Tidy;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.Property;
/**
* Task to ask property values to the user. Uses current value as default.
*
* @author <a href="mailto:barozzi@nicolaken.com">Nicola Ken Barozzi</a>
* @created 14 January 2002
*/
public class JTidyTask
extends org.apache.tools.ant.Task
{
private String src;
private String dest;
private String log;
private Tidy tidy;
private String warn = "false";
private String summary = "false";
PrintWriter pw;
/**
* Constructor.
*/
public JTidyTask()
{
super();
}
/**
* Initializes the task.
*/
public void init()
{
super.init();
// Setup an instance of Tidy.
tidy = new Tidy();
tidy.setXmlOut(true);
tidy.setXHTML(true);
tidy.setDropFontTags(true);
tidy.setLiteralAttribs(true);
tidy.setMakeClean(true);
tidy.setShowWarnings(Boolean.getBoolean(warn));
tidy.setQuiet(!Boolean.getBoolean(summary));
}
/**
* Run the task.
* @exception org.apache.tools.ant.BuildException The exception raised during task execution.
*/
public void execute()
throws org.apache.tools.ant.BuildException
{
try
{
PrintWriter pw = new PrintWriter(new FileWriter(log));
tidy.setErrout(pw);
// Extract the document using JTidy and stream it.
BufferedInputStream in =
new BufferedInputStream(new FileInputStream(src));
// FileOutputStream out = new FileOutputStream(dest);
PrintWriter out =
new PrintWriter(new FileWriter(dest));
// using null as output to get dom so to remove duplicate attributes
org.w3c.dom.Document domDoc = tidy.parseDOM(in, null);
domDoc.normalize();
stripDuplicateAttributes(domDoc, null);
org.apache.xml.serialize.OutputFormat format =
new org.apache.xml.serialize.OutputFormat();
format.setIndenting(true);
format.setEncoding("ISO-8859-1");
format.setPreserveSpace(true);
format.setLineSeparator("\n");
org.apache.xml.serialize.XMLSerializer serializer =
new org.apache.xml.serialize.XMLSerializer(out, format);
serializer.serialize(domDoc);
out.flush();
out.close();
in.close();
pw.flush();
pw.close();
}
catch (IOException ioe)
{
throw new BuildException(ioe);
}
}
public void setSrc(String src)
{
this.src = src;
}
public void setDest(String dest)
{
this.dest = dest;
}
public void setLog(String log)
{
this.log = log;
}
public void setWarn(String warn)
{
this.warn = warn;
}
public void setSummary(String summary)
{
this.summary = summary;
}
// using parent because jtidy dom is bugged, cannot get parent or delete child
public static void stripDuplicateAttributes(Node node, Node parent)
{
// The output depends on the type of the node
switch (node.getNodeType())
{
case Node.DOCUMENT_NODE :
{
Document doc = ( Document ) node;
Node child = doc.getFirstChild();
while (child != null)
{
stripDuplicateAttributes(child, node);
child = child.getNextSibling();
}
break;
}
case Node.ELEMENT_NODE :
{
Element elt = ( Element ) node;
NamedNodeMap attrs = elt.getAttributes();
ArrayList nodesToRemove = new ArrayList();
int nodesToRemoveNum = 0;
for (int i = 0; i < attrs.getLength(); i++)
{
Node a = attrs.item(i);
for (int j = 0; j < attrs.getLength(); j++)
{
Node b = attrs.item(j);
// if there are two attributes with same name
if ((i != j)
&& (a.getNodeName().equals(b.getNodeName())))
{
nodesToRemove.add(b);
nodesToRemoveNum++;
}
}
}
for (int i = 0; i < nodesToRemoveNum; i++)
{
org.w3c.dom.Attr nodeToDelete =
( org.w3c.dom.Attr ) nodesToRemove.get(i);
org.w3c.dom.Element nodeToDeleteParent =
( org.w3c.dom
.Element ) node; // nodeToDelete.getParentNode();
nodeToDeleteParent.removeAttributeNode(nodeToDelete);
}
nodesToRemove.clear();
Node child = elt.getFirstChild();
while (child != null)
{
stripDuplicateAttributes(child, node);
child = child.getNextSibling();
}
break;
}
default :
// do nothing
break;
}
}
}
|