286N/A/*
286N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
286N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
286N/A *
286N/A * This code is free software; you can redistribute it and/or modify it
286N/A * under the terms of the GNU General Public License version 2 only, as
286N/A * published by the Free Software Foundation. Oracle designates this
286N/A * particular file as subject to the "Classpath" exception as provided
286N/A * by Oracle in the LICENSE file that accompanied this code.
286N/A *
286N/A * This code is distributed in the hope that it will be useful, but WITHOUT
286N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
286N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
286N/A * version 2 for more details (a copy is included in the LICENSE file that
286N/A * accompanied this code).
286N/A *
286N/A * You should have received a copy of the GNU General Public License version
286N/A * 2 along with this work; if not, write to the Free Software Foundation,
286N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
286N/A *
286N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
286N/A * or visit www.oracle.com if you need additional information or have any
286N/A * questions.
286N/A */
286N/A
286N/Apackage com.sun.xml.internal.stream.writers;
286N/A
286N/Aimport java.io.FileWriter;
286N/Aimport java.io.IOException;
286N/Aimport java.io.OutputStreamWriter;
286N/Aimport java.io.Writer;
286N/Aimport java.nio.charset.Charset;
286N/Aimport java.nio.charset.CharsetEncoder;
286N/Aimport com.sun.org.apache.xerces.internal.util.XMLChar;
524N/Aimport com.sun.org.apache.xerces.internal.utils.SecuritySupport;
286N/A
286N/A/**
286N/A * Implements common xml writer functions.
286N/A *
286N/A * @author Neeraj Bajaj,K.Venugopal Sun Microsystems.
286N/A */
286N/A
286N/Apublic class WriterUtility {
286N/A
286N/A
286N/A public static final String START_COMMENT = "<!--";
286N/A public static final String END_COMMENT = "-->";
286N/A public static final String DEFAULT_ENCODING = " encoding=\"utf-8\"";
286N/A public static final String DEFAULT_XMLDECL ="<?xml version=\"1.0\" ?>";
286N/A public static final String DEFAULT_XML_VERSION ="1.0";
286N/A public static final char CLOSE_START_TAG = '>';
286N/A public static final char OPEN_START_TAG = '<';
286N/A public static final String OPEN_END_TAG ="</";
286N/A public static final char CLOSE_END_TAG = '>';
286N/A public static final String START_CDATA = "<![CDATA[";
286N/A public static final String END_CDATA = "]]>";
286N/A public static final String CLOSE_EMPTY_ELEMENT = "/>";
286N/A public static final String SPACE = " ";
286N/A public static final String UTF_8 = "utf-8";
286N/A
286N/A static final boolean DEBUG_XML_CONTENT = false;
286N/A
286N/A /**XXX: This feature is only used when writing element content values.
286N/A * default value is 'true' however, if the feature is set to false
286N/A * characters wont be escaped.
286N/A * This feature has no effect when writing Attribute values, character would still be escaped.
286N/A * I can't think of any reason why this would be useful when writing attribute values.
286N/A * However, this can be reconsidered if there is any usecase.
286N/A */
286N/A boolean fEscapeCharacters = true ;
286N/A
286N/A /** Writer object*/
286N/A Writer fWriter = null;
286N/A
286N/A //CharsetEncoder
286N/A CharsetEncoder fEncoder ;
286N/A
286N/A public WriterUtility(){
286N/A fEncoder = getDefaultEncoder();
286N/A }
286N/A
286N/A
286N/A /** Creates a new instance of WriterUtility */
286N/A public WriterUtility(Writer writer) {
286N/A fWriter = writer;
286N/A if(writer instanceof OutputStreamWriter){
286N/A String charset = ((OutputStreamWriter)writer).getEncoding();
286N/A if(charset != null){
286N/A fEncoder = Charset.forName(charset).newEncoder();
286N/A }
286N/A }else if(writer instanceof FileWriter){
286N/A String charset = ((FileWriter)writer).getEncoding();
286N/A if(charset != null){
286N/A fEncoder = Charset.forName(charset).newEncoder();
286N/A }
286N/A }
286N/A else{
286N/A //attempt to retreive default fEncoderoder
286N/A fEncoder = getDefaultEncoder();
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * sets the writer object
286N/A * @param writer file to write into
286N/A */
286N/A public void setWriter(Writer writer){
286N/A fWriter = writer;
286N/A }
286N/A
286N/A public void setEscapeCharacters(boolean escape){
286N/A fEscapeCharacters = escape ;
286N/A }
286N/A
286N/A public boolean getEscapeCharacters(){
286N/A return fEscapeCharacters;
286N/A }
286N/A
286N/A /**
286N/A * writes xml content (characters and element content
286N/A * @param content
286N/A */
286N/A public void writeXMLContent(char[] content, int start, int length) throws IOException{
286N/A writeXMLContent(content, start, length, getEscapeCharacters());
286N/A }
286N/A
286N/A /**
286N/A * writes xml content (characters and element content
286N/A * @param content
286N/A */
286N/A private void writeXMLContent(char[] content, int start, int length, boolean escapeCharacter) throws IOException{
286N/A if(DEBUG_XML_CONTENT){
286N/A System.out.println("content to write is " + new String(content, start, length));
286N/A }
286N/A int index;
286N/A char ch;
286N/A int sc;
286N/A final int end = start + length ;
286N/A //define startWritePos to track the position from where the character array data needs to be written
286N/A //initialize this variable to start pos. indicating that no data has been written
286N/A int startWritePos = start;
286N/A
286N/A for ( index = start ; index < end ; index++ ) {
286N/A ch = content[ index ];
286N/A
286N/A if(fEncoder != null && !fEncoder.canEncode(ch)){
286N/A //- write the data to the point we get this character
286N/A fWriter.write(content, startWritePos, index - startWritePos );
286N/A
286N/A //escape this character
286N/A fWriter.write( "&#x" );
286N/A fWriter.write(Integer.toHexString(ch));
286N/A fWriter.write( ';' );
286N/A //increase the startWritePos by 1 indicating that next write should start from
286N/A //one position ahead
286N/A startWritePos = index + 1;
286N/A
286N/A }
286N/A if(DEBUG_XML_CONTENT){
286N/A System.out.println("startWritePos = " + startWritePos);
286N/A System.out.println("index = " + index);
286N/A System.out.println("start = " + start);
286N/A System.out.println("end = " + end);
286N/A }
286N/A
286N/A switch(ch){
286N/A case '<' :{
286N/A if(escapeCharacter){
286N/A //this character needs to be escaped, write the data from the last write pos
286N/A fWriter.write(content, startWritePos, index - startWritePos);
286N/A fWriter.write("&lt;");
286N/A if(DEBUG_XML_CONTENT){
286N/A System.out.print(new String(content, startWritePos, index - startWritePos));
286N/A System.out.println("&lt;");
286N/A }
286N/A //increase the startWritePos by 1 indicating that next write should start from
286N/A //one position ahead
286N/A startWritePos = index + 1;
286N/A }
286N/A break;
286N/A }
286N/A case '&' :{
286N/A if(escapeCharacter){
286N/A //this character needs to be escaped, write the data from the last write pos
286N/A fWriter.write(content, startWritePos, index - startWritePos);
286N/A fWriter.write("&amp;");
286N/A if(DEBUG_XML_CONTENT){
286N/A System.out.print(new String(content,startWritePos, index - startWritePos));
286N/A System.out.println("&amp;");
286N/A }
286N/A //increase the startWritePos by 1 indicating that next write should start from
286N/A //one position ahead
286N/A startWritePos = index + 1;
286N/A }
286N/A break;
286N/A }
286N/A
286N/A case '>': {
286N/A if(escapeCharacter){
286N/A //this character needs to be escaped, write the data from the last write pos
286N/A fWriter.write(content, startWritePos, index - startWritePos);
286N/A fWriter.write("&gt;");
286N/A if(DEBUG_XML_CONTENT){
286N/A System.out.print(new String(content,startWritePos, index - startWritePos));
286N/A System.out.println("&gt;");
286N/A }
286N/A //increase the startWritePos by 1 indicating that next write should start from
286N/A //one position ahead
286N/A startWritePos = index + 1;
286N/A }
286N/A break;
286N/A }
286N/A }
286N/A }
286N/A if(DEBUG_XML_CONTENT){
286N/A System.out.println("out of the loop, writing " + new String(content, startWritePos, end - startWritePos));
286N/A }
286N/A //write any pending data
286N/A fWriter.write(content, startWritePos, end - startWritePos);
286N/A }
286N/A
286N/A /**
286N/A * writes xml content (characters and element content
286N/A * @param content
286N/A */
286N/A public void writeXMLContent(String content) throws IOException{
286N/A if(content == null || content.length() == 0) return ;
286N/A writeXMLContent(content.toCharArray(), 0, content.length());
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Write Attribute value to the underlying stream.
286N/A *
286N/A * @param value
286N/A */
286N/A
286N/A public void writeXMLAttributeValue(String value)throws IOException{
286N/A writeXMLContent(value.toCharArray(), 0, value.length(), true);
286N/A }
286N/A
286N/A private CharsetEncoder getDefaultEncoder(){
286N/A try{
524N/A String encoding = SecuritySupport.getSystemProperty("file.encoding");
286N/A if(encoding != null){
286N/A return Charset.forName(encoding).newEncoder();
286N/A }
286N/A }
286N/A catch(Exception ex){
286N/A //for any exception thrown , catch and continue
286N/A }
286N/A return null;
286N/A }
286N/A}