6
|
1 |
/*
|
|
2 |
* Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package com.sun.xml.internal.stream.events ;
|
|
27 |
|
|
28 |
import javax.xml.stream.events.Characters;
|
|
29 |
import java.io.Writer;
|
|
30 |
import javax.xml.stream.events.XMLEvent;
|
|
31 |
import com.sun.org.apache.xerces.internal.util.XMLChar;
|
|
32 |
|
|
33 |
/** Implementation of Character event.
|
|
34 |
*
|
|
35 |
*@author Neeraj Bajaj, Sun Microsystems
|
|
36 |
*@author K.Venugopal, Sun Microsystems
|
|
37 |
*
|
|
38 |
*/
|
|
39 |
|
|
40 |
public class CharacterEvent extends DummyEvent
|
|
41 |
implements Characters {
|
|
42 |
/* data */
|
|
43 |
private String fData;
|
|
44 |
/*true if fData is CData */
|
|
45 |
private boolean fIsCData;
|
|
46 |
/* true if fData is ignorableWhitespace*/
|
|
47 |
private boolean fIsIgnorableWhitespace;
|
|
48 |
/* true if fData contet is whitespace*/
|
|
49 |
private boolean fIsSpace = false;
|
|
50 |
/*used to prevent scanning of data multiple times */
|
|
51 |
private boolean fCheckIfSpaceNeeded = true;
|
|
52 |
|
|
53 |
public CharacterEvent() {
|
|
54 |
fIsCData = false;
|
|
55 |
init();
|
|
56 |
}
|
|
57 |
|
|
58 |
/**
|
|
59 |
*
|
|
60 |
* @param data Character Data.
|
|
61 |
*/
|
|
62 |
public CharacterEvent(String data) {
|
|
63 |
fIsCData = false;
|
|
64 |
init();
|
|
65 |
fData = data;
|
|
66 |
}
|
|
67 |
|
|
68 |
/**
|
|
69 |
*
|
|
70 |
* @param data Character Data.
|
|
71 |
* @param flag true if CData
|
|
72 |
*/
|
|
73 |
public CharacterEvent(String data, boolean flag) {
|
|
74 |
init();
|
|
75 |
fData = data;
|
|
76 |
fIsCData = flag;
|
|
77 |
}
|
|
78 |
|
|
79 |
/**
|
|
80 |
*
|
|
81 |
* @param data Character Data.
|
|
82 |
* @param flag true if CData
|
|
83 |
* @param isIgnorableWhiteSpace true if data is ignorable whitespace.
|
|
84 |
*/
|
|
85 |
public CharacterEvent(String data, boolean flag, boolean isIgnorableWhiteSpace) {
|
|
86 |
init();
|
|
87 |
fData = data;
|
|
88 |
fIsCData = flag;
|
|
89 |
fIsIgnorableWhitespace = isIgnorableWhiteSpace ;
|
|
90 |
}
|
|
91 |
|
|
92 |
protected void init() {
|
|
93 |
setEventType(XMLEvent.CHARACTERS);
|
|
94 |
}
|
|
95 |
|
|
96 |
/**
|
|
97 |
*
|
|
98 |
* @return return data.
|
|
99 |
*/
|
|
100 |
public String getData() {
|
|
101 |
return fData;
|
|
102 |
}
|
|
103 |
|
|
104 |
/**
|
|
105 |
*
|
|
106 |
* @param String data
|
|
107 |
*/
|
|
108 |
public void setData(String data){
|
|
109 |
fData = data;
|
|
110 |
fCheckIfSpaceNeeded = true;
|
|
111 |
}
|
|
112 |
|
|
113 |
/**
|
|
114 |
*
|
|
115 |
* @return boolean returns true if the data is CData
|
|
116 |
*/
|
|
117 |
public boolean isCData() {
|
|
118 |
return fIsCData;
|
|
119 |
}
|
|
120 |
|
|
121 |
/**
|
|
122 |
*
|
|
123 |
* @return String return the String representation of this event.
|
|
124 |
*/
|
|
125 |
public String toString() {
|
|
126 |
if(fIsCData)
|
|
127 |
return "<![CDATA[" + getData() + "]]>";
|
|
128 |
else
|
|
129 |
return fData;
|
|
130 |
}
|
|
131 |
|
|
132 |
/** This method will write the XMLEvent as per the XML 1.0 specification as Unicode characters.
|
|
133 |
* No indentation or whitespace should be outputted.
|
|
134 |
*
|
|
135 |
* Any user defined event type SHALL have this method
|
|
136 |
* called when being written to on an output stream.
|
|
137 |
* Built in Event types MUST implement this method,
|
|
138 |
* but implementations MAY choose not call these methods
|
|
139 |
* for optimizations reasons when writing out built in
|
|
140 |
* Events to an output stream.
|
|
141 |
* The output generated MUST be equivalent in terms of the
|
|
142 |
* infoset expressed.
|
|
143 |
*
|
|
144 |
* @param writer The writer that will output the data
|
|
145 |
* @throws XMLStreamException if there is a fatal error writing the event
|
|
146 |
*/
|
|
147 |
public void writeAsEncodedUnicode(Writer writer) throws javax.xml.stream.XMLStreamException {
|
|
148 |
}
|
|
149 |
|
|
150 |
/**
|
|
151 |
* Return true if this is ignorableWhiteSpace. If
|
|
152 |
* this event is ignorableWhiteSpace its event type will
|
|
153 |
* be SPACE.
|
|
154 |
* @return
|
|
155 |
*/
|
|
156 |
public boolean isIgnorableWhiteSpace() {
|
|
157 |
return fIsIgnorableWhitespace;
|
|
158 |
}
|
|
159 |
|
|
160 |
/**
|
|
161 |
* Returns true if this set of Characters
|
|
162 |
* is all whitespace. Whitspace inside a document
|
|
163 |
* is reported as CHARACTERS. This method allows
|
|
164 |
* checking of CHARACTERS events to see if they
|
|
165 |
* are composed of only whitespace characters
|
|
166 |
* @return
|
|
167 |
*/
|
|
168 |
public boolean isWhiteSpace() {
|
|
169 |
//no synchronization checks made.
|
|
170 |
if(fCheckIfSpaceNeeded){
|
|
171 |
checkWhiteSpace();
|
|
172 |
fCheckIfSpaceNeeded = false;
|
|
173 |
}
|
|
174 |
return fIsSpace;
|
|
175 |
}
|
|
176 |
|
|
177 |
private void checkWhiteSpace(){
|
|
178 |
//for now - remove dependancy of XMLChar
|
|
179 |
if(fData != null && fData.length() >0 ){
|
|
180 |
fIsSpace = true;
|
|
181 |
for(int i=0;i<fData.length();i++){
|
|
182 |
if(!XMLChar.isSpace(fData.charAt(i))){
|
|
183 |
fIsSpace = false;
|
|
184 |
break;
|
|
185 |
}
|
|
186 |
}
|
|
187 |
}
|
|
188 |
}
|
|
189 |
}
|