KickJava   Java API By Example, From Geeks To Geeks.

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


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-16 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-16">UTF 16 explained</a>
29  */

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

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

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