KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > components > encoding > AbstractXMLEncoder


1 /*
2
3  * Copyright 2001-2004 The Apache Software Foundation.
4
5  *
6
7  * Licensed under the Apache License, Version 2.0 (the "License");
8
9  * you may not use this file except in compliance with the License.
10
11  * You may obtain a copy of the License at
12
13  *
14
15  * http://www.apache.org/licenses/LICENSE-2.0
16
17  *
18
19  * Unless required by applicable law or agreed to in writing, software
20
21  * distributed under the License is distributed on an "AS IS" BASIS,
22
23  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
25  * See the License for the specific language governing permissions and
26
27  * limitations under the License.
28
29  */

30
31 package org.jboss.axis.components.encoding;
32
33
34 import org.jboss.axis.utils.Messages;
35
36 import java.io.UnsupportedEncodingException JavaDoc;
37
38
39 /**
40  * Abstract class for XML String encoders.
41  * <p/>
42  * <p/>
43  * <p/>
44  * The new encoding mechanism fixes the following bugs/issues:
45  * <p/>
46  * http://nagoya.apache.org/bugzilla/show_bug.cgi?id=15133
47  * <p/>
48  * http://nagoya.apache.org/bugzilla/show_bug.cgi?id=15494
49  * <p/>
50  * http://nagoya.apache.org/bugzilla/show_bug.cgi?id=19327
51  *
52  * @author <a HREF="mailto:jens@void.fm">Jens Schumann</a>
53  * @author <a HREF="mailto:dims@yahoo.com">Davanum Srinivas</a>
54  */

55
56 public abstract class AbstractXMLEncoder implements XMLEncoder
57 {
58
59    private static final byte[] AMP = "&amp;".getBytes();
60
61    private static final byte[] QUOTE = "&quot;".getBytes();
62
63    private static final byte[] LESS = "&lt;".getBytes();
64
65    private static final byte[] GREATER = "&gt;".getBytes();
66
67    private static final byte[] LF = "\n".getBytes();
68
69    private static final byte[] CR = "\r".getBytes();
70
71    private static final byte[] TAB = "\t".getBytes();
72
73
74    /**
75     * Encode a string
76     *
77     * @param xmlString string to be encoded
78     * @return encoded string
79     */

80
81    public String JavaDoc encode(String JavaDoc xmlString)
82    {
83
84       if (xmlString == null)
85       {
86
87          return "";
88
89       }
90
91       char[] characters = xmlString.toCharArray();
92
93       EncodedByteArray out = null;
94
95       char character;
96
97
98       for (int i = 0; i < characters.length; i++)
99       {
100
101          character = characters[i];
102
103          switch (character)
104          {
105
106             // we don't care about single quotes since axis will
107

108             // use double quotes anyway
109

110             case '&':
111
112                if (out == null)
113                {
114
115                   out = getInitialByteArray(xmlString, i);
116
117                }
118
119                out.append(AMP);
120
121                break;
122
123             case '"':
124
125                if (out == null)
126                {
127
128                   out = getInitialByteArray(xmlString, i);
129
130                }
131
132                out.append(QUOTE);
133
134                break;
135
136             case '<':
137
138                if (out == null)
139                {
140
141                   out = getInitialByteArray(xmlString, i);
142
143                }
144
145                out.append(LESS);
146
147                break;
148
149             case '>':
150
151                if (out == null)
152                {
153
154                   out = getInitialByteArray(xmlString, i);
155
156                }
157
158                out.append(GREATER);
159
160                break;
161
162             case '\n':
163
164                if (out == null)
165                {
166
167                   out = getInitialByteArray(xmlString, i);
168
169                }
170
171                out.append(LF);
172
173                break;
174
175             case '\r':
176
177                if (out == null)
178                {
179
180                   out = getInitialByteArray(xmlString, i);
181
182                }
183
184                out.append(CR);
185
186                break;
187
188             case '\t':
189
190                if (out == null)
191                {
192
193                   out = getInitialByteArray(xmlString, i);
194
195                }
196
197                out.append(TAB);
198
199                break;
200
201             default:
202
203                if (character < 0x20)
204                {
205
206                   throw new IllegalArgumentException JavaDoc(Messages.getMessage("invalidXmlCharacter00", Integer.toHexString(character), xmlString));
207
208                }
209                else if (needsEncoding(character))
210                {
211
212                   if (out == null)
213                   {
214
215                      out = getInitialByteArray(xmlString, i);
216
217                   }
218
219                   appendEncoded(out, character);
220
221                }
222                else
223                {
224
225                   if (out != null)
226                   {
227
228                      out.append(character);
229
230                   }
231
232                }
233
234                break;
235
236          }
237
238       }
239
240       if (out == null)
241       {
242
243          return xmlString;
244
245       }
246
247       try
248       {
249
250          return out.toString(getEncoding());
251
252       }
253       catch (UnsupportedEncodingException JavaDoc e)
254       {
255
256          // we tested it ealier, should work.
257

258          throw new IllegalStateException JavaDoc(Messages.getMessage("encodingDisappeared00", getEncoding()));
259
260       }
261
262    }
263
264
265    public abstract String JavaDoc getEncoding();
266
267
268    public abstract boolean needsEncoding(char c);
269
270
271    public abstract void appendEncoded(EncodedByteArray out, char c);
272
273
274    private EncodedByteArray getInitialByteArray(String JavaDoc aXmlString, int pos)
275    {
276
277       try
278       {
279
280          return new EncodedByteArray(aXmlString.getBytes(getEncoding()), 0, pos);
281
282       }
283       catch (UnsupportedEncodingException JavaDoc e)
284       {
285
286          // we tested it ealier, should work.
287

288          throw new IllegalStateException JavaDoc(Messages.getMessage("encodingDisappeared00", getEncoding()));
289
290       }
291
292    }
293
294 }
295
296
Popular Tags