Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/axis/components/encoding/AbstractXMLEncoder.java


1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.axis.components.encoding;
17  
18  import org.apache.axis.utils.Messages;
19  
20  /**
21   *
22   * Abstract class for XML String encoders.
23   *
24   * The new encoding mechanism fixes the following bugs/issues:
25   *   http://nagoya.apache.org/bugzilla/show_bug.cgi?id=15133
26   *   http://nagoya.apache.org/bugzilla/show_bug.cgi?id=15494
27   *   http://nagoya.apache.org/bugzilla/show_bug.cgi?id=19327
28   *
29   * @author <a href="mailto:jens@void.fm">Jens Schumann</a>
30   * @author <a href="mailto:dims@yahoo.com">Davanum Srinivas</a>
31   *
32   */
33  public abstract class AbstractXMLEncoder implements XMLEncoder {
34      protected static final String AMP = "&amp;";
35      protected static final String QUOTE = "&quot;";
36      protected static final String LESS = "&lt;";
37      protected static final String GREATER = "&gt;";
38      protected static final String LF = "\n";
39      protected static final String CR = "\r";
40      protected static final String TAB = "\t";
41  
42      /**
43       * gets the encoding supported by this encoder
44       * @return string
45       */
46      public abstract String getEncoding();
47  
48      /**
49       * Encode a string
50       * @param xmlString string to be encoded
51       * @return encoded string
52       */
53      public String encode(String xmlString) {
54          if(xmlString == null) {
55              return "";
56          }
57          char[] characters = xmlString.toCharArray();
58          StringBuffer out = null;
59          char character;
60  
61          for (int i = 0; i < characters.length; i++) {
62              character = characters[i];
63              switch (character) {
64                  // we don't care about single quotes since axis will
65                  // use double quotes anyway
66                  case '&':
67                      if (out == null) {
68                          out = getInitialByteArray(xmlString, i);
69                      }
70                      out.append(AMP);
71                      break;
72                  case '"':
73                      if (out == null) {
74                          out = getInitialByteArray(xmlString, i);
75                      }
76                      out.append(QUOTE);
77                      break;
78                  case '<':
79                      if (out == null) {
80                          out = getInitialByteArray(xmlString, i);
81                      }
82                      out.append(LESS);
83                      break;
84                  case '>':
85                      if (out == null) {
86                          out = getInitialByteArray(xmlString, i);
87                      }
88                      out.append(GREATER);
89                      break;
90                  case '\n':
91                      if (out == null) {
92                          out = getInitialByteArray(xmlString, i);
93                      }
94                      out.append(LF);
95                      break;
96                  case '\r':
97                      if (out == null) {
98                          out = getInitialByteArray(xmlString, i);
99                      }
100                     out.append(CR);
101                     break;
102                 case '\t':
103                     if (out == null) {
104                         out = getInitialByteArray(xmlString, i);
105                     }
106                     out.append(TAB);
107                     break;
108                 default:
109                     if (character < 0x20) {
110                         throw new IllegalArgumentException(Messages.getMessage("invalidXmlCharacter00", Integer.toHexString(character), xmlString));
111                     } else {
112                         if (out != null) {
113                             out.append(character);
114                         }
115                     }
116                     break;
117             }
118         }
119         if (out == null) {
120             return xmlString;
121         }
122         return out.toString();
123     }
124 
125     protected StringBuffer getInitialByteArray(String aXmlString, int pos) {
126         return new StringBuffer(aXmlString.substring(0, pos));
127     }
128 }