KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > serializers > encoding > XMLEncoder


1 /*
2  * Copyright 1999-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.cocoon.components.serializers.encoding;
17
18 /**
19  *
20  *
21  * @author <a HREF="mailto:pier@apache.org">Pier Fumagalli</a>, February 2003
22  * @version CVS $Id: XMLEncoder.java 30932 2004-07-29 17:35:38Z vgritsenko $
23  */

24 public class XMLEncoder extends CompiledEncoder {
25
26     private static final char ENCODE_HEX[] = "0123456789ABCDEF".toCharArray();
27     private static final char ENCODE_QUOT[] = "&quot;".toCharArray();
28     private static final char ENCODE_AMP[] = "&amp;".toCharArray();
29     private static final char ENCODE_APOS[] = "&apos;".toCharArray();
30     private static final char ENCODE_LT[] = "&lt;".toCharArray();
31     private static final char ENCODE_GT[] = "&gt;".toCharArray();
32
33     /**
34      * Create a new instance of this <code>XMLEncoder</code>.
35      */

36     public XMLEncoder() {
37         super("X-W3C-XML");
38     }
39
40     /**
41      * Create a new instance of this <code>XMLEncoder</code>.
42      *
43      * @param name A name for this <code>Encoding</code>.
44      * @throws NullPointerException If one of the arguments is <b>null</b>.
45      */

46     protected XMLEncoder(String JavaDoc name) {
47         super(name);
48     }
49     
50     /**
51      * Return true or false wether this encoding can encode the specified
52      * character or not.
53      * <p>
54      * This method will return true for the following character range:
55      * <br />
56      * <code>
57      * <nobr>#x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD]</nobr>
58      * </code>
59      * </p>
60      *
61      * @see <a HREF="http://www.w3.org/TR/REC-xml#charsets">W3C XML 1.0</a>
62      */

63     protected boolean compile(char c) {
64         if ((c == 0x09) || // [\t]
65
(c == 0x0a) || // [\n]
66
(c == 0x0d)) { // [\r]
67
return(true);
68         }
69
70         if ((c == 0x22) || // ["]
71
(c == 0x26) || // [&]
72
(c == 0x27) || // [']
73
(c == 0x3c) || // [<]
74
(c == 0x3e) || // [>]
75
(c < 0x20) || // See <http://www.w3.org/TR/REC-xml#charsets>
76
((c > 0xd7ff) && (c < 0xe000)) || (c > 0xfffd)) {
77             return(false);
78         }
79
80         return(true);
81     }
82
83     /**
84      * Return an array of characters representing the encoding for the
85      * specified character.
86      */

87     public char[] encode(char c) {
88         switch (c) {
89             case 0x22: return(ENCODE_QUOT); // (") [&quot;]
90
case 0x26: return(ENCODE_AMP); // (&) [&amp;]
91
case 0x27: return(ENCODE_APOS); // (') [&apos;]
92
case 0x3c: return(ENCODE_LT); // (<) [&lt;]
93
case 0x3e: return(ENCODE_GT); // (>) [&gt;]
94
default: {
95                 if (c > 0xfff) {
96                     char ret[] = { '&', '#', 'x',
97                         ENCODE_HEX[c >> 0xc & 0xf],
98                         ENCODE_HEX[c >> 0x8 & 0xf],
99                         ENCODE_HEX[c >> 0x4 & 0xf],
100                         ENCODE_HEX[c & 0xf], ';'
101                     };
102                     return(ret);
103                 }
104                 if (c > 0xff) {
105                     char ret[] = { '&', '#', 'x',
106                         ENCODE_HEX[c >> 0x8 & 0xf],
107                         ENCODE_HEX[c >> 0x4 & 0xf],
108                         ENCODE_HEX[c & 0xf], ';'
109                     };
110                     return(ret);
111                 }
112                 char ret[] = { '&', '#', 'x',
113                     ENCODE_HEX[c >> 0x4 & 0xf],
114                     ENCODE_HEX[c & 0xf], ';'
115                 };
116                 return(ret);
117             }
118         }
119     }
120 }
121
Popular Tags