001: /*
002: * $Id: CharacterEvent.java,v 1.2 2006/04/01 06:01:35 jeffsuttor Exp $
003: */
004:
005: /*
006: * The contents of this file are subject to the terms
007: * of the Common Development and Distribution License
008: * (the License). You may not use this file except in
009: * compliance with the License.
010: *
011: * You can obtain a copy of the license at
012: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
013: * See the License for the specific language governing
014: * permissions and limitations under the License.
015: *
016: * When distributing Covered Code, include this CDDL
017: * Header Notice in each file and include the License file
018: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
019: * If applicable, add the following below the CDDL Header,
020: * with the fields enclosed by brackets [] replaced by
021: * you own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * [Name of File] [ver.__] [Date]
025: *
026: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
027: */
028:
029: package com.sun.xml.stream.events;
030:
031: import javax.xml.stream.events.Characters;
032: import java.io.Writer;
033: import javax.xml.stream.events.XMLEvent;
034: import com.sun.xml.stream.xerces.util.XMLChar;
035:
036: /** Implementation of Character event.
037: *
038: *@author Neeraj Bajaj, Sun Microsystems
039: *@author K.Venugopal, Sun Microsystems
040: *
041: */
042:
043: public class CharacterEvent extends DummyEvent implements Characters {
044: /* data */
045: private String fData;
046: /*true if fData is CData */
047: private boolean fIsCData;
048: /* true if fData is ignorableWhitespace*/
049: private boolean fIsIgnorableWhitespace;
050: /* true if fData contet is whitespace*/
051: private boolean fIsSpace = false;
052: /*used to prevent scanning of data multiple times */
053: private boolean fCheckIfSpaceNeeded = true;
054:
055: public CharacterEvent() {
056: fIsCData = false;
057: init();
058: }
059:
060: /**
061: *
062: * @param data Character Data.
063: */
064: public CharacterEvent(String data) {
065: fIsCData = false;
066: init();
067: fData = data;
068: }
069:
070: /**
071: *
072: * @param data Character Data.
073: * @param flag true if CData
074: */
075: public CharacterEvent(String data, boolean flag) {
076: init();
077: fData = data;
078: fIsCData = flag;
079: }
080:
081: /**
082: *
083: * @param data Character Data.
084: * @param flag true if CData
085: * @param isIgnorableWhiteSpace true if data is ignorable whitespace.
086: */
087: public CharacterEvent(String data, boolean flag,
088: boolean isIgnorableWhiteSpace) {
089: init();
090: fData = data;
091: fIsCData = flag;
092: fIsIgnorableWhitespace = isIgnorableWhiteSpace;
093: }
094:
095: protected void init() {
096: setEventType(XMLEvent.CHARACTERS);
097: }
098:
099: /**
100: *
101: * @return return data.
102: */
103: public String getData() {
104: return fData;
105: }
106:
107: /**
108: *
109: * @param String data
110: */
111: public void setData(String data) {
112: fData = data;
113: fCheckIfSpaceNeeded = true;
114: }
115:
116: /**
117: *
118: * @return boolean returns true if the data is CData
119: */
120: public boolean isCData() {
121: return fIsCData;
122: }
123:
124: /**
125: *
126: * @return String return the String representation of this event.
127: */
128: public String toString() {
129: if (fIsCData)
130: return "<![CDATA[" + getData() + "]]>";
131: else
132: return fData;
133: }
134:
135: /** This method will write the XMLEvent as per the XML 1.0 specification as Unicode characters.
136: * No indentation or whitespace should be outputted.
137: *
138: * Any user defined event type SHALL have this method
139: * called when being written to on an output stream.
140: * Built in Event types MUST implement this method,
141: * but implementations MAY choose not call these methods
142: * for optimizations reasons when writing out built in
143: * Events to an output stream.
144: * The output generated MUST be equivalent in terms of the
145: * infoset expressed.
146: *
147: * @param writer The writer that will output the data
148: * @throws XMLStreamException if there is a fatal error writing the event
149: */
150: public void writeAsEncodedUnicode(Writer writer)
151: throws javax.xml.stream.XMLStreamException {
152: }
153:
154: /**
155: * Return true if this is ignorableWhiteSpace. If
156: * this event is ignorableWhiteSpace its event type will
157: * be SPACE.
158: * @return
159: */
160: public boolean isIgnorableWhiteSpace() {
161: return fIsIgnorableWhitespace;
162: }
163:
164: /**
165: * Returns true if this set of Characters
166: * is all whitespace. Whitspace inside a document
167: * is reported as CHARACTERS. This method allows
168: * checking of CHARACTERS events to see if they
169: * are composed of only whitespace characters
170: * @return
171: */
172: public boolean isWhiteSpace() {
173: //no synchronization checks made.
174: if (fCheckIfSpaceNeeded) {
175: checkWhiteSpace();
176: fCheckIfSpaceNeeded = false;
177: }
178: return fIsSpace;
179: }
180:
181: private void checkWhiteSpace() {
182: //for now - remove dependancy of XMLChar
183: if (fData != null && fData.length() > 0) {
184: fIsSpace = true;
185: for (int i = 0; i < fData.length(); i++) {
186: if (!XMLChar.isSpace(fData.charAt(i))) {
187: fIsSpace = false;
188: break;
189: }
190: }
191: }
192: }
193: }
|