jaxp/src/com/sun/xml/internal/stream/writers/WriterUtility.java
changeset 12457 c348e06f0e82
parent 12005 a754d69d5e60
child 16953 a44e04deb948
equal deleted inserted replaced
12324:1d7e6da6adc8 12457:c348e06f0e82
       
     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 
       
    26 package com.sun.xml.internal.stream.writers;
       
    27 
       
    28 import java.io.FileWriter;
       
    29 import java.io.IOException;
       
    30 import java.io.OutputStreamWriter;
       
    31 import java.io.Writer;
       
    32 import java.nio.charset.Charset;
       
    33 import java.nio.charset.CharsetEncoder;
       
    34 import com.sun.org.apache.xerces.internal.util.XMLChar;
       
    35 
       
    36 /**
       
    37  * Implements common xml writer functions.
       
    38  *
       
    39  * @author Neeraj Bajaj,K.Venugopal Sun Microsystems.
       
    40  */
       
    41 
       
    42 public class WriterUtility {
       
    43 
       
    44 
       
    45     public static final String START_COMMENT = "<!--";
       
    46     public static final String END_COMMENT = "-->";
       
    47     public static final String DEFAULT_ENCODING = " encoding=\"utf-8\"";
       
    48     public static final String DEFAULT_XMLDECL ="<?xml version=\"1.0\" ?>";
       
    49     public static final String DEFAULT_XML_VERSION ="1.0";
       
    50     public static final char CLOSE_START_TAG = '>';
       
    51     public static final char OPEN_START_TAG = '<';
       
    52     public static final String OPEN_END_TAG ="</";
       
    53     public static final char CLOSE_END_TAG = '>';
       
    54     public static final String START_CDATA = "<![CDATA[";
       
    55     public static final String END_CDATA = "]]>";
       
    56     public static final String CLOSE_EMPTY_ELEMENT = "/>";
       
    57     public static final String SPACE = " ";
       
    58     public static final String UTF_8 = "utf-8";
       
    59 
       
    60     static final boolean DEBUG_XML_CONTENT = false;
       
    61 
       
    62     /**XXX: This feature is only used when writing element content values.
       
    63      * default value is 'true' however, if the feature is set to false
       
    64      * 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      */
       
    69     boolean fEscapeCharacters = true ;
       
    70 
       
    71     /** Writer object*/
       
    72     Writer fWriter = null;
       
    73 
       
    74     //CharsetEncoder
       
    75     CharsetEncoder fEncoder ;
       
    76 
       
    77     public WriterUtility(){
       
    78         fEncoder = getDefaultEncoder();
       
    79     }
       
    80 
       
    81 
       
    82     /** Creates a new instance of WriterUtility */
       
    83     public WriterUtility(Writer writer) {
       
    84         fWriter = writer;
       
    85         if(writer instanceof OutputStreamWriter){
       
    86             String charset = ((OutputStreamWriter)writer).getEncoding();
       
    87             if(charset != null){
       
    88                 fEncoder = Charset.forName(charset).newEncoder();
       
    89             }
       
    90         }else if(writer instanceof FileWriter){
       
    91             String charset = ((FileWriter)writer).getEncoding();
       
    92             if(charset != null){
       
    93                 fEncoder = Charset.forName(charset).newEncoder();
       
    94             }
       
    95         }
       
    96         else{
       
    97             //attempt to retreive default fEncoderoder
       
    98             fEncoder = getDefaultEncoder();
       
    99         }
       
   100     }
       
   101 
       
   102     /**
       
   103      * sets the writer object
       
   104      * @param writer file to write into
       
   105      */
       
   106     public void  setWriter(Writer writer){
       
   107         fWriter = writer;
       
   108     }
       
   109 
       
   110     public void setEscapeCharacters(boolean escape){
       
   111         fEscapeCharacters = escape ;
       
   112     }
       
   113 
       
   114     public boolean getEscapeCharacters(){
       
   115         return fEscapeCharacters;
       
   116     }
       
   117 
       
   118     /**
       
   119      * writes xml content (characters and element content
       
   120      * @param content
       
   121      */
       
   122     public void writeXMLContent(char[] content, int start, int length) throws IOException{
       
   123         writeXMLContent(content, start, length, getEscapeCharacters());
       
   124     }
       
   125 
       
   126     /**
       
   127      * writes xml content (characters and element content
       
   128      * @param content
       
   129      */
       
   130     private void writeXMLContent(char[] content, int start, int length, boolean escapeCharacter) throws IOException{
       
   131         if(DEBUG_XML_CONTENT){
       
   132             System.out.println("content to write is " + new String(content, start, length));
       
   133         }
       
   134         int index;
       
   135         char ch;
       
   136         int sc;
       
   137         final int end = start + length ;
       
   138         //define startWritePos to track the position from where the character array data needs to be written
       
   139         //initialize this variable to start pos. indicating that no data has been written
       
   140         int startWritePos = start;
       
   141 
       
   142         for ( index = start ; index < end ; index++ ) {
       
   143             ch = content[ index ];
       
   144 
       
   145             if(fEncoder != null && !fEncoder.canEncode(ch)){
       
   146                 //- write the data to the point we get this character
       
   147                 fWriter.write(content, startWritePos, index - startWritePos );
       
   148 
       
   149                 //escape this character
       
   150                 fWriter.write( "&#x" );
       
   151                 fWriter.write(Integer.toHexString(ch));
       
   152                 fWriter.write( ';' );
       
   153                 //increase the startWritePos by 1 indicating that next write should start from
       
   154                 //one position ahead
       
   155                 startWritePos = index + 1;
       
   156 
       
   157             }
       
   158             if(DEBUG_XML_CONTENT){
       
   159                 System.out.println("startWritePos = " + startWritePos);
       
   160                 System.out.println("index = " + index);
       
   161                 System.out.println("start = " + start);
       
   162                 System.out.println("end = " + end);
       
   163             }
       
   164 
       
   165             switch(ch){
       
   166                 case '<' :{
       
   167                     if(escapeCharacter){
       
   168                         //this character needs to be escaped, write the data from the last write pos
       
   169                         fWriter.write(content, startWritePos, index - startWritePos);
       
   170                         fWriter.write("&lt;");
       
   171                         if(DEBUG_XML_CONTENT){
       
   172                             System.out.print(new String(content, startWritePos, index - startWritePos));
       
   173                             System.out.println("&lt;");
       
   174                         }
       
   175                         //increase the startWritePos by 1 indicating that next write should start from
       
   176                         //one position ahead
       
   177                         startWritePos = index + 1;
       
   178                     }
       
   179                     break;
       
   180                 }
       
   181                 case '&' :{
       
   182                     if(escapeCharacter){
       
   183                         //this character needs to be escaped, write the data from the last write pos
       
   184                         fWriter.write(content, startWritePos, index - startWritePos);
       
   185                         fWriter.write("&amp;");
       
   186                         if(DEBUG_XML_CONTENT){
       
   187                             System.out.print(new String(content,startWritePos, index - startWritePos));
       
   188                             System.out.println("&amp;");
       
   189                         }
       
   190                         //increase the startWritePos by 1 indicating that next write should start from
       
   191                         //one position ahead
       
   192                         startWritePos = index + 1;
       
   193                     }
       
   194                     break;
       
   195                 }
       
   196 
       
   197                 case '>': {
       
   198                     if(escapeCharacter){
       
   199                         //this character needs to be escaped, write the data from the last write pos
       
   200                         fWriter.write(content, startWritePos, index - startWritePos);
       
   201                         fWriter.write("&gt;");
       
   202                         if(DEBUG_XML_CONTENT){
       
   203                             System.out.print(new String(content,startWritePos, index - startWritePos));
       
   204                             System.out.println("&gt;");
       
   205                         }
       
   206                         //increase the startWritePos by 1 indicating that next write should start from
       
   207                         //one position ahead
       
   208                         startWritePos = index + 1;
       
   209                     }
       
   210                     break;
       
   211                 }
       
   212             }
       
   213         }
       
   214         if(DEBUG_XML_CONTENT){
       
   215             System.out.println("out of the loop, writing " + new String(content, startWritePos, end - startWritePos));
       
   216         }
       
   217         //write any pending data
       
   218         fWriter.write(content, startWritePos, end - startWritePos);
       
   219     }
       
   220 
       
   221     /**
       
   222      * writes xml content (characters and element content
       
   223      * @param content
       
   224      */
       
   225     public void writeXMLContent(String content) throws IOException{
       
   226         if(content == null || content.length() == 0) return ;
       
   227         writeXMLContent(content.toCharArray(), 0, content.length());
       
   228     }
       
   229 
       
   230 
       
   231     /**
       
   232      * Write Attribute value to the underlying stream.
       
   233      *
       
   234      * @param value
       
   235      */
       
   236 
       
   237     public void  writeXMLAttributeValue(String value)throws IOException{
       
   238         writeXMLContent(value.toCharArray(), 0, value.length(), true);
       
   239     }
       
   240 
       
   241     private CharsetEncoder getDefaultEncoder(){
       
   242         try{
       
   243             String encoding = System.getProperty("file.encoding");
       
   244             if(encoding != null){
       
   245                 return Charset.forName(encoding).newEncoder();
       
   246             }
       
   247         }
       
   248         catch(Exception ex){
       
   249             //for any exception thrown , catch and continue
       
   250         }
       
   251         return null;
       
   252     }
       
   253 }