-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathNormalOutput.java
More file actions
203 lines (185 loc) · 6.51 KB
/
NormalOutput.java
File metadata and controls
203 lines (185 loc) · 6.51 KB
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
/*
* NormalOutput.java
* Copyright (c) 2005-2007 Radek Burget
*
* CSSBox is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CSSBox 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with CSSBox. If not, see <http://www.gnu.org/licenses/>.
*
* Created on 30. leden 2005, 19:02
*/
package org.fit.cssbox.css;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import org.unbescape.html.HtmlEscape;
import org.w3c.dom.*;
/**
* An output generator that outputs the DOM tree in a standard (xml) way
*
* @author radek
*/
public class NormalOutput extends Output
{
private boolean filterStyles = true;
public NormalOutput(Node root)
{
super(root);
}
public NormalOutput(Node root, boolean filterStyles)
{
super(root);
this.filterStyles = filterStyles;
}
/**
* Formats the complete tag tree to an output stream.
* @param out The output stream to be used for the output.
*/
public void dumpTo(OutputStream out)
{
PrintWriter writer;
try {
writer = new PrintWriter(new OutputStreamWriter(out, "utf-8"));
} catch (UnsupportedEncodingException e) {
writer = new PrintWriter(out);
}
recursiveDump(root, 0, writer);
writer.close();
}
/**
* Formats the complete tag tree and prints using a writer.
* @param writer The writer to be used for printing the ouput.
*/
public void dumpTo(PrintWriter writer)
{
recursiveDump(root, 0, writer);
}
//========================================================================
private void recursiveDump(Node n, int level, PrintWriter p)
{
//Opening tag
if (n.getNodeType() == Node.ELEMENT_NODE)
{
String tag = "";
Element el = (Element) n;
//do not dump original style definitions
if (filterStyles)
{
if (el.getTagName().equals("style"))
return;
if (el.getTagName().equals("link") &&
("stylesheet".equalsIgnoreCase(el.getAttribute("rel")) ||
"text/css".equalsIgnoreCase(el.getAttribute("type"))))
return;
}
//Replace meta generator
if (el.getTagName().equals("meta") && "generator".equalsIgnoreCase(el.getAttribute("name")))
el.setAttribute("content", "CSS Transformer by Radek Burget, burgetr@fit.vutbr.cz");
//Change encoding to utf-8
if (el.getTagName().equals("meta") && "content-type".equalsIgnoreCase(el.getAttribute("http-equiv")))
el.setAttribute("content", "text/html; charset=utf-8");
//Dump the tag
tag = tag + "<" + el.getTagName();
NamedNodeMap attrs = el.getAttributes();
for (int i = 0; i < attrs.getLength(); i++)
{
Node attr = attrs.item(i);
tag = tag + " " + attr.getNodeName() + "=\"" + attr.getNodeValue() + "\"";
}
if (n.getChildNodes().getLength() == 0) {
// Use self-closing tag for empty node
tag = tag + "/>";
p.print(tag);
return;
}
tag = tag + ">";
p.print(tag);
}
else if (n.getNodeType() == Node.TEXT_NODE)
{
p.print(HtmlEscape.escapeHtml4Xml(n.getNodeValue()));
}
else if (n.getNodeType() == Node.COMMENT_NODE)
{
p.print("<!--");
p.print(n.getNodeValue());
p.print("-->");
}
NodeList child = n.getChildNodes();
for (int i = 0; i < child.getLength(); i++)
recursiveDump(child.item(i), level+1, p);
//Closing tag
if (n.getNodeType() == Node.ELEMENT_NODE)
{
//if (n.getNodeName().equals("head"))
// p.print("<script type=\"text/javascript\" src=\"visual.js\"></script>");
p.print("</" + n.getNodeName() + ">");
}
}
@SuppressWarnings("unused")
private void recursiveDumpNice(Node n, int level, PrintWriter p)
{
//Opening tag
if (n.getNodeType() == Node.ELEMENT_NODE)
{
String tag = "";
Element el = (Element) n;
if (el.getTagName().equals("style"))
return;
tag = tag + "<" + el.getTagName();
NamedNodeMap attrs = el.getAttributes();
for (int i = 0; i < attrs.getLength(); i++)
{
Node attr = attrs.item(i);
tag = tag + " " + attr.getNodeName() + "=\"" + attr.getNodeValue() + "\"";
}
if (n.getChildNodes().getLength() == 0) {
// Use self-closing tag for empty node
tag = tag + "/>";
indent(level, p);
p.print(tag);
return;
}
tag = tag + ">";
indent(level, p);
p.println(tag);
}
else if (n.getNodeType() == Node.TEXT_NODE)
{
indent(level, p);
p.print(HtmlEscape.escapeHtml4Xml(n.getNodeValue()));
}
else if (n.getNodeType() == Node.COMMENT_NODE)
{
indent(level, p);
p.print("<!--");
p.print(n.getNodeValue());
p.print("-->");
}
NodeList child = n.getChildNodes();
for (int i = 0; i < child.getLength(); i++)
recursiveDumpNice(child.item(i), level+1, p);
//Closing tag
if (n.getNodeType() == Node.ELEMENT_NODE)
{
indent(level, p);
p.println("</" + n.getNodeName() + ">");
}
}
private void indent(int level, PrintWriter p)
{
String ind = "";
for (int i = 0; i < level*4; i++) ind = ind + ' ';
p.print(ind);
}
}