3
3
import java .io .IOException ;
4
4
import java .io .Writer ;
5
5
import java .util .Locale ;
6
-
7
6
import org .apache .xml .serialize .ElementState ;
8
7
import org .apache .xml .serialize .HTMLdtd ;
9
8
import org .apache .xml .serialize .OutputFormat ;
@@ -39,44 +38,38 @@ protected String getEntityRef(int charToPrint) {
39
38
}
40
39
41
40
/**
42
- * Called to serialize a DOM element. Equivalent to calling {@link
43
- * #startElement}, {@link #endElement} and serializing everything
44
- * inbetween, but better optimized.
41
+ * Called to serialize a DOM element. Equivalent to calling {@link #startElement}, {@link
42
+ * #endElement} and serializing everything inbetween, but better optimized.
45
43
*/
46
44
@ Override
47
- protected void serializeElement (Element elem )
48
- throws IOException
49
- {
45
+ protected void serializeElement (Element elem ) throws IOException {
50
46
Attr attr ;
51
47
NamedNodeMap attrMap ;
52
- int i ;
48
+ int i ;
53
49
Node child ;
54
50
ElementState state ;
55
- boolean preserveSpace ;
56
- String name ;
57
- String value ;
58
- String tagName ;
51
+ boolean preserveSpace ;
52
+ String name ;
53
+ String value ;
54
+ String tagName ;
59
55
60
56
tagName = elem .getTagName ();
61
57
state = getElementState ();
62
- if ( isDocumentState () ) {
58
+ if (isDocumentState ()) {
63
59
// If this is the root element handle it differently.
64
60
// If the first root element in the document, serialize
65
61
// the document's DOCTYPE. Space preserving defaults
66
62
// to that of the output format.
67
- if ( ! _started )
68
- startDocument ( tagName );
63
+ if (!_started ) startDocument (tagName );
69
64
} else {
70
65
// For any other element, if first in parent, then
71
66
// close parent's opening tag and use the parnet's
72
67
// space preserving.
73
- if ( state .empty )
74
- _printer .printText ( '>' );
68
+ if (state .empty ) _printer .printText ('>' );
75
69
// Indent this element on a new line if the first
76
70
// content of the parent element or immediately
77
71
// following an element.
78
- if ( _indenting && ! state .preserveSpace &&
79
- ( state .empty || state .afterElement ) )
72
+ if (_indenting && !state .preserveSpace && (state .empty || state .afterElement ))
80
73
_printer .breakLine ();
81
74
}
82
75
preserveSpace = state .preserveSpace ;
@@ -85,8 +78,8 @@ protected void serializeElement(Element elem )
85
78
// This only happens in endElement().
86
79
87
80
// XHTML: element names are lower case, DOM will be different
88
- _printer .printText ( '<' );
89
- _printer .printText ( tagName );
81
+ _printer .printText ('<' );
82
+ _printer .printText (tagName );
90
83
_printer .indent ();
91
84
92
85
// Lookup the element's attribute, but only print specified
@@ -95,79 +88,74 @@ protected void serializeElement(Element elem )
95
88
// separated with a space so the element can be broken on
96
89
// multiple lines.
97
90
attrMap = elem .getAttributes ();
98
- if ( attrMap != null ) {
99
- for ( i = 0 ; i < attrMap .getLength () ; ++i ) {
100
- attr = (Attr ) attrMap .item ( i );
91
+ if (attrMap != null ) {
92
+ for (i = 0 ; i < attrMap .getLength (); ++i ) {
93
+ attr = (Attr ) attrMap .item (i );
101
94
name = attr .getName ().toLowerCase (Locale .ENGLISH );
102
95
value = attr .getValue ();
103
- if ( attr .getSpecified () ) {
96
+ if (attr .getSpecified ()) {
104
97
_printer .printSpace ();
105
98
// HTML: Empty values print as attribute name, no value.
106
99
// HTML: URI attributes will print unescaped
107
- if ( value == null ) {
100
+ if (value == null ) {
108
101
value = "" ;
109
102
}
110
- if ( !_format .getPreserveEmptyAttributes () && value .length () == 0 )
111
- _printer .printText ( name );
112
- else if ( HTMLdtd .isURI ( tagName , name ) ) {
113
- _printer .printText ( name );
114
- _printer .printText ( "=\" " );
115
- _printer .printText ( escapeURI ( value ) );
116
- _printer .printText ( '"' );
117
- } else if ( HTMLdtd .isBoolean ( tagName , name ) )
118
- _printer .printText ( name );
103
+ if (!_format .getPreserveEmptyAttributes () && value .length () == 0 )
104
+ _printer .printText (name );
105
+ else if (HTMLdtd .isURI (tagName , name )) {
106
+ _printer .printText (name );
107
+ _printer .printText ("=\" " );
108
+ _printer .printText (escapeURI (value ));
109
+ _printer .printText ('"' );
110
+ } else if (HTMLdtd .isBoolean (tagName , name )) _printer .printText (name );
119
111
else {
120
- _printer .printText ( name );
121
- _printer .printText ( "=\" " );
122
- printEscaped ( value );
123
- _printer .printText ( '"' );
112
+ _printer .printText (name );
113
+ _printer .printText ("=\" " );
114
+ printEscaped (value );
115
+ _printer .printText ('"' );
124
116
}
125
117
}
126
118
}
127
119
}
128
- if ( HTMLdtd .isPreserveSpace ( tagName ) )
129
- preserveSpace = true ;
120
+ if (HTMLdtd .isPreserveSpace (tagName )) preserveSpace = true ;
130
121
131
122
// If element has children, or if element is not an empty tag,
132
123
// serialize an opening tag.
133
- if ( elem .hasChildNodes () || ! HTMLdtd .isEmptyTag ( tagName ) ) {
124
+ if (elem .hasChildNodes () || !HTMLdtd .isEmptyTag (tagName ) ) {
134
125
// Enter an element state, and serialize the children
135
126
// one by one. Finally, end the element.
136
- state = enterElementState ( null , null , tagName , preserveSpace );
127
+ state = enterElementState (null , null , tagName , preserveSpace );
137
128
138
129
// Prevents line breaks inside A/TD
139
- if ( tagName .equalsIgnoreCase ( "A" ) || tagName .equalsIgnoreCase ( "TD" ) ) {
130
+ if (tagName .equalsIgnoreCase ("A" ) || tagName .equalsIgnoreCase ("TD" ) ) {
140
131
state .empty = false ;
141
- _printer .printText ( '>' );
132
+ _printer .printText ('>' );
142
133
}
143
134
144
135
// Handle SCRIPT and STYLE specifically by changing the
145
136
// state of the current element to CDATA (XHTML) or
146
137
// unescaped (HTML).
147
- if ( tagName .equalsIgnoreCase ( "SCRIPT" ) ||
148
- tagName .equalsIgnoreCase ( "STYLE" ) ) {
138
+ if (tagName .equalsIgnoreCase ("SCRIPT" ) || tagName .equalsIgnoreCase ("STYLE" )) {
149
139
// HTML: Print contents unescaped
150
140
state .unescaped = true ;
151
141
}
152
142
child = elem .getFirstChild ();
153
- while ( child != null ) {
154
- serializeNode ( child );
143
+ while (child != null ) {
144
+ serializeNode (child );
155
145
child = child .getNextSibling ();
156
146
}
157
- endElementIO ( null , null , tagName );
147
+ endElementIO (null , null , tagName );
158
148
} else {
159
149
_printer .unindent ();
160
150
// XHTML: Close empty tag with ' />' so it's XML and HTML compatible.
161
151
// HTML: Empty tags are defined as such in DTD no in document.
162
152
if (!elem .hasChildNodes () && isAllowedEmptyTag (tagName ) && !requiresClosingTag (tagName ))
163
- _printer .printText ( "/>" );
164
- else
165
- _printer .printText ( '>' );
153
+ _printer .printText ("/>" );
154
+ else _printer .printText ('>' );
166
155
// After element but parent element is no longer empty.
167
156
state .afterElement = true ;
168
157
state .empty = false ;
169
- if ( isDocumentState () )
170
- _printer .flush ();
158
+ if (isDocumentState ()) _printer .flush ();
171
159
}
172
160
}
173
161
@@ -185,11 +173,13 @@ public void endElementIO(String namespaceURI, String localName, String rawName)
185
173
if (state .empty && isAllowedEmptyTag (rawName ) && !requiresClosingTag (rawName )) { //
186
174
_printer .printText ("/>" );
187
175
} else {
188
- if (state .empty ) _printer .printText ('>' );
176
+ if (state .empty ) _printer .printText ('>' );
189
177
// This element is not empty and that last content was another element, so print a line break
190
178
// before that last element and this element's closing tag. [keith] Provided this is not an
191
179
// anchor. HTML: some elements do not print closing tag (e.g. LI)
192
- if (rawName == null || !HTMLdtd .isOnlyOpening (rawName ) || HTMLdtd .isOptionalClosing (rawName )) {
180
+ if (rawName == null
181
+ || !HTMLdtd .isOnlyOpening (rawName )
182
+ || HTMLdtd .isOptionalClosing (rawName )) {
193
183
if (_indenting && !state .preserveSpace && state .afterElement ) _printer .breakLine ();
194
184
// Must leave CData section first (Illegal in HTML, but still)
195
185
if (state .inCData ) _printer .printText ("]]>" );
@@ -230,6 +220,6 @@ private boolean requiresClosingTag(String tagName) {
230
220
}
231
221
232
222
private boolean isAllowedEmptyTag (String tagName ) {
233
- return "head" .equals (tagName ) || allowedEmptyTags .matches ( tagName );
223
+ return "head" .equals (tagName ) || allowedEmptyTags .matches (tagName );
234
224
}
235
225
}
0 commit comments