Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/com/sun/xml/internal/stream/events/CharacterEvent.java
86414 views
/*1* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.xml.internal.stream.events ;2627import javax.xml.stream.events.Characters;28import java.io.Writer;29import java.io.IOException;30import javax.xml.stream.events.XMLEvent;31import com.sun.org.apache.xerces.internal.util.XMLChar;3233/** Implementation of Character event.34*35*@author Neeraj Bajaj, Sun Microsystems36*@author K.Venugopal, Sun Microsystems37*38*/3940public class CharacterEvent extends DummyEvent41implements Characters {42/* data */43private String fData;44/*true if fData is CData */45private boolean fIsCData;46/* true if fData is ignorableWhitespace*/47private boolean fIsIgnorableWhitespace;48/* true if fData contet is whitespace*/49private boolean fIsSpace = false;50/*used to prevent scanning of data multiple times */51private boolean fCheckIfSpaceNeeded = true;5253public CharacterEvent() {54fIsCData = false;55init();56}5758/**59*60* @param data Character Data.61*/62public CharacterEvent(String data) {63fIsCData = false;64init();65fData = data;66}6768/**69*70* @param data Character Data.71* @param flag true if CData72*/73public CharacterEvent(String data, boolean flag) {74init();75fData = data;76fIsCData = flag;77}7879/**80*81* @param data Character Data.82* @param flag true if CData83* @param isIgnorableWhiteSpace true if data is ignorable whitespace.84*/85public CharacterEvent(String data, boolean flag, boolean isIgnorableWhiteSpace) {86init();87fData = data;88fIsCData = flag;89fIsIgnorableWhitespace = isIgnorableWhiteSpace ;90}9192protected void init() {93setEventType(XMLEvent.CHARACTERS);94}9596/**97*98* @return return data.99*/100public String getData() {101return fData;102}103104/**105*106* @param String data107*/108public void setData(String data){109fData = data;110fCheckIfSpaceNeeded = true;111}112113/**114*115* @return boolean returns true if the data is CData116*/117public boolean isCData() {118return fIsCData;119}120121/**122*123* @return String return the String representation of this event.124*/125public String toString() {126if(fIsCData)127return "<![CDATA[" + getData() + "]]>";128else129return fData;130}131132/** 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 method136* 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 methods139* for optimizations reasons when writing out built in140* Events to an output stream.141* The output generated MUST be equivalent in terms of the142* infoset expressed.143*144* @param writer The writer that will output the data145* @throws XMLStreamException if there is a fatal error writing the event146*/147protected void writeAsEncodedUnicodeEx(Writer writer) throws IOException148{149if (fIsCData) {150writer.write("<![CDATA[" + getData() + "]]>");151} else {152charEncode(writer, fData);153}154}155156/**157* Return true if this is ignorableWhiteSpace. If158* this event is ignorableWhiteSpace its event type will159* be SPACE.160* @return161*/162public boolean isIgnorableWhiteSpace() {163return fIsIgnorableWhitespace;164}165166/**167* Returns true if this set of Characters168* is all whitespace. Whitspace inside a document169* is reported as CHARACTERS. This method allows170* checking of CHARACTERS events to see if they171* are composed of only whitespace characters172* @return173*/174public boolean isWhiteSpace() {175//no synchronization checks made.176if(fCheckIfSpaceNeeded){177checkWhiteSpace();178fCheckIfSpaceNeeded = false;179}180return fIsSpace;181}182183private void checkWhiteSpace(){184//for now - remove dependancy of XMLChar185if(fData != null && fData.length() >0 ){186fIsSpace = true;187for(int i=0;i<fData.length();i++){188if(!XMLChar.isSpace(fData.charAt(i))){189fIsSpace = false;190break;191}192}193}194}195}196197198