1/*
2 * Copyright (c) 2005, Oracle and/or its affiliates. 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.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.xml.internal.stream.events ;
27
28import javax.xml.stream.events.Characters;
29import java.io.Writer;
30import java.io.IOException;
31import javax.xml.stream.events.XMLEvent;
32import com.sun.org.apache.xerces.internal.util.XMLChar;
33
34/** Implementation of Character event.
35 *
36 *@author Neeraj Bajaj, Sun Microsystems
37 *@author K.Venugopal, Sun Microsystems
38 *
39 */
40
41public class CharacterEvent extends DummyEvent
42implements Characters {
43    /* data */
44    private String fData;
45    /*true if fData is CData */
46    private boolean fIsCData;
47    /* true if fData is ignorableWhitespace*/
48    private boolean fIsIgnorableWhitespace;
49    /* true if fData contet is whitespace*/
50    private boolean fIsSpace = false;
51    /*used to prevent scanning of  data multiple times */
52    private boolean fCheckIfSpaceNeeded = true;
53
54    public CharacterEvent() {
55        fIsCData = false;
56        init();
57    }
58
59    /**
60     *
61     * @param data Character Data.
62     */
63    public CharacterEvent(String data) {
64        fIsCData = false;
65        init();
66        fData = data;
67    }
68
69    /**
70     *
71     * @param data Character Data.
72     * @param flag true if CData
73     */
74    public CharacterEvent(String data, boolean flag) {
75        init();
76        fData = data;
77        fIsCData = flag;
78    }
79
80    /**
81     *
82     * @param data Character Data.
83     * @param flag true if CData
84     * @param isIgnorableWhiteSpace true if data is ignorable whitespace.
85     */
86    public CharacterEvent(String data, boolean flag, boolean isIgnorableWhiteSpace) {
87        init();
88        fData = data;
89        fIsCData = flag;
90        fIsIgnorableWhitespace = isIgnorableWhiteSpace ;
91    }
92
93    protected void init() {
94        setEventType(XMLEvent.CHARACTERS);
95    }
96
97    /**
98     *
99     * @return return data.
100     */
101    public String getData() {
102        return fData;
103    }
104
105    /**
106     *
107     * @param String data
108     */
109    public void setData(String data){
110        fData = data;
111        fCheckIfSpaceNeeded = true;
112    }
113
114    /**
115     *
116     * @return boolean returns true if the data is CData
117     */
118    public boolean isCData() {
119        return fIsCData;
120    }
121
122    /**
123     *
124     * @return String return the String representation of this event.
125     */
126    public String toString() {
127        if(fIsCData)
128            return "<![CDATA[" + getData() + "]]>";
129        else
130            return fData;
131    }
132
133    /** This method will write the XMLEvent as per the XML 1.0 specification as Unicode characters.
134     * No indentation or whitespace should be outputted.
135     *
136     * Any user defined event type SHALL have this method
137     * called when being written to on an output stream.
138     * Built in Event types MUST implement this method,
139     * but implementations MAY choose not call these methods
140     * for optimizations reasons when writing out built in
141     * Events to an output stream.
142     * The output generated MUST be equivalent in terms of the
143     * infoset expressed.
144     *
145     * @param writer The writer that will output the data
146     * @throws XMLStreamException if there is a fatal error writing the event
147     */
148    protected void writeAsEncodedUnicodeEx(Writer writer) throws IOException
149    {
150        if (fIsCData) {
151            writer.write("<![CDATA[" + getData() + "]]>");
152        } else {
153            charEncode(writer, fData);
154        }
155     }
156
157    /**
158     * Return true if this is ignorableWhiteSpace.  If
159     * this event is ignorableWhiteSpace its event type will
160     * be SPACE.
161     * @return
162     */
163    public boolean isIgnorableWhiteSpace() {
164        return fIsIgnorableWhitespace;
165    }
166
167    /**
168     * Returns true if this set of Characters
169     * is all whitespace.  Whitspace inside a document
170     * is reported as CHARACTERS.  This method allows
171     * checking of CHARACTERS events to see if they
172     * are composed of only whitespace characters
173     * @return
174     */
175    public boolean isWhiteSpace() {
176        //no synchronization checks made.
177        if(fCheckIfSpaceNeeded){
178            checkWhiteSpace();
179            fCheckIfSpaceNeeded = false;
180        }
181        return fIsSpace;
182    }
183
184    private void checkWhiteSpace(){
185        //for now - remove dependancy of XMLChar
186        if(fData != null && fData.length() >0 ){
187            fIsSpace = true;
188            for(int i=0;i<fData.length();i++){
189                if(!XMLChar.isSpace(fData.charAt(i))){
190                    fIsSpace = false;
191                    break;
192                }
193            }
194        }
195    }
196}
197