KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > components > encoding > UTF8Encoder


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.i18n.Messages;
19
20 import java.io.IOException JavaDoc;
21 import java.io.Writer JavaDoc;
22
23 /**
24  * UTF-8 Encoder.
25  *
26  * @author <a HREF="mailto:jens@void.fm">Jens Schumann</a>
27  * @see <a HREF="http://encoding.org">encoding.org</a>
28  * @see <a HREF="http://czyborra.com/utf/#UTF-8">UTF 8 explained</a>
29  */

30 class UTF8Encoder extends AbstractXMLEncoder {
31     /**
32      * gets the encoding supported by this encoder
33      *
34      * @return string
35      */

36     public String JavaDoc getEncoding() {
37         return XMLEncoderFactory.ENCODING_UTF_8;
38     }
39
40     /**
41      * write the encoded version of a given string
42      *
43      * @param writer writer to write this string to
44      * @param xmlString string to be encoded
45      */

46     public void writeEncoded(Writer JavaDoc writer, String JavaDoc xmlString)
47             throws IOException JavaDoc {
48         if (xmlString == null) {
49             return;
50         }
51         int length = xmlString.length();
52         char character;
53         for (int i = 0; i < length; i++) {
54             character = xmlString.charAt( i );
55             switch (character) {
56                 // we don't care about single quotes since axis will
57
// use double quotes anyway
58
case '&':
59                     writer.write(AMP);
60                     break;
61                 case '"':
62                     writer.write(QUOTE);
63                     break;
64                 case '<':
65                     writer.write(LESS);
66                     break;
67                 case '>':
68                     writer.write(GREATER);
69                     break;
70                 case '\n':
71                     writer.write(LF);
72                     break;
73                 case '\r':
74                     writer.write(CR);
75                     break;
76                 case '\t':
77                     writer.write(TAB);
78                     break;
79                 default:
80                     if (character < 0x20) {
81                         throw new IllegalArgumentException JavaDoc(Messages.getMessage(
82                                 "invalidXmlCharacter00",
83                                 Integer.toHexString(character),
84                                 xmlString));
85                     } else if (character > 0x7F) {
86                         writer.write("&#x");
87                         writer.write(Integer.toHexString(character).toUpperCase());
88                         writer.write(";");
89                     } else {
90                         writer.write(character);
91                     }
92                     break;
93             }
94         }
95     }
96 }
97
Popular Tags