Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/com/sun/xml/internal/stream/writers/WriterUtility.java
48527 views
/*1* Copyright (c) 2005, 2013, 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.writers;2627import java.io.FileWriter;28import java.io.IOException;29import java.io.OutputStreamWriter;30import java.io.Writer;31import java.nio.charset.Charset;32import java.nio.charset.CharsetEncoder;33import com.sun.org.apache.xerces.internal.util.XMLChar;34import com.sun.org.apache.xerces.internal.utils.SecuritySupport;3536/**37* Implements common xml writer functions.38*39* @author Neeraj Bajaj,K.Venugopal Sun Microsystems.40*/4142public class WriterUtility {434445public static final String START_COMMENT = "<!--";46public static final String END_COMMENT = "-->";47public static final String DEFAULT_ENCODING = " encoding=\"utf-8\"";48public static final String DEFAULT_XMLDECL ="<?xml version=\"1.0\" ?>";49public static final String DEFAULT_XML_VERSION ="1.0";50public static final char CLOSE_START_TAG = '>';51public static final char OPEN_START_TAG = '<';52public static final String OPEN_END_TAG ="</";53public static final char CLOSE_END_TAG = '>';54public static final String START_CDATA = "<![CDATA[";55public static final String END_CDATA = "]]>";56public static final String CLOSE_EMPTY_ELEMENT = "/>";57public static final String SPACE = " ";58public static final String UTF_8 = "utf-8";5960static final boolean DEBUG_XML_CONTENT = false;6162/**XXX: This feature is only used when writing element content values.63* default value is 'true' however, if the feature is set to false64* characters wont be escaped.65* This feature has no effect when writing Attribute values, character would still be escaped.66* I can't think of any reason why this would be useful when writing attribute values.67* However, this can be reconsidered if there is any usecase.68*/69boolean fEscapeCharacters = true ;7071/** Writer object*/72Writer fWriter = null;7374//CharsetEncoder75CharsetEncoder fEncoder ;7677public WriterUtility(){78fEncoder = getDefaultEncoder();79}808182/** Creates a new instance of WriterUtility */83public WriterUtility(Writer writer) {84fWriter = writer;85if(writer instanceof OutputStreamWriter){86String charset = ((OutputStreamWriter)writer).getEncoding();87if(charset != null){88fEncoder = Charset.forName(charset).newEncoder();89}90}else if(writer instanceof FileWriter){91String charset = ((FileWriter)writer).getEncoding();92if(charset != null){93fEncoder = Charset.forName(charset).newEncoder();94}95}96else{97//attempt to retreive default fEncoderoder98fEncoder = getDefaultEncoder();99}100}101102/**103* sets the writer object104* @param writer file to write into105*/106public void setWriter(Writer writer){107fWriter = writer;108}109110public void setEscapeCharacters(boolean escape){111fEscapeCharacters = escape ;112}113114public boolean getEscapeCharacters(){115return fEscapeCharacters;116}117118/**119* writes xml content (characters and element content120* @param content121*/122public void writeXMLContent(char[] content, int start, int length) throws IOException{123writeXMLContent(content, start, length, getEscapeCharacters());124}125126/**127* writes xml content (characters and element content128* @param content129*/130private void writeXMLContent(char[] content, int start, int length, boolean escapeCharacter) throws IOException{131if(DEBUG_XML_CONTENT){132System.out.println("content to write is " + new String(content, start, length));133}134int index;135char ch;136int sc;137final int end = start + length ;138//define startWritePos to track the position from where the character array data needs to be written139//initialize this variable to start pos. indicating that no data has been written140int startWritePos = start;141142for ( index = start ; index < end ; index++ ) {143ch = content[ index ];144145if(fEncoder != null && !fEncoder.canEncode(ch)){146//- write the data to the point we get this character147fWriter.write(content, startWritePos, index - startWritePos );148149//escape this character150fWriter.write( "&#x" );151fWriter.write(Integer.toHexString(ch));152fWriter.write( ';' );153//increase the startWritePos by 1 indicating that next write should start from154//one position ahead155startWritePos = index + 1;156157}158if(DEBUG_XML_CONTENT){159System.out.println("startWritePos = " + startWritePos);160System.out.println("index = " + index);161System.out.println("start = " + start);162System.out.println("end = " + end);163}164165switch(ch){166case '<' :{167if(escapeCharacter){168//this character needs to be escaped, write the data from the last write pos169fWriter.write(content, startWritePos, index - startWritePos);170fWriter.write("<");171if(DEBUG_XML_CONTENT){172System.out.print(new String(content, startWritePos, index - startWritePos));173System.out.println("<");174}175//increase the startWritePos by 1 indicating that next write should start from176//one position ahead177startWritePos = index + 1;178}179break;180}181case '&' :{182if(escapeCharacter){183//this character needs to be escaped, write the data from the last write pos184fWriter.write(content, startWritePos, index - startWritePos);185fWriter.write("&");186if(DEBUG_XML_CONTENT){187System.out.print(new String(content,startWritePos, index - startWritePos));188System.out.println("&");189}190//increase the startWritePos by 1 indicating that next write should start from191//one position ahead192startWritePos = index + 1;193}194break;195}196197case '>': {198if(escapeCharacter){199//this character needs to be escaped, write the data from the last write pos200fWriter.write(content, startWritePos, index - startWritePos);201fWriter.write(">");202if(DEBUG_XML_CONTENT){203System.out.print(new String(content,startWritePos, index - startWritePos));204System.out.println(">");205}206//increase the startWritePos by 1 indicating that next write should start from207//one position ahead208startWritePos = index + 1;209}210break;211}212}213}214if(DEBUG_XML_CONTENT){215System.out.println("out of the loop, writing " + new String(content, startWritePos, end - startWritePos));216}217//write any pending data218fWriter.write(content, startWritePos, end - startWritePos);219}220221/**222* writes xml content (characters and element content223* @param content224*/225public void writeXMLContent(String content) throws IOException{226if(content == null || content.length() == 0) return ;227writeXMLContent(content.toCharArray(), 0, content.length());228}229230231/**232* Write Attribute value to the underlying stream.233*234* @param value235*/236237public void writeXMLAttributeValue(String value)throws IOException{238writeXMLContent(value.toCharArray(), 0, value.length(), true);239}240241private CharsetEncoder getDefaultEncoder(){242try{243String encoding = SecuritySupport.getSystemProperty("file.encoding");244if(encoding != null){245return Charset.forName(encoding).newEncoder();246}247}248catch(Exception ex){249//for any exception thrown , catch and continue250}251return null;252}253}254255256