KickJava   Java API By Example, From Geeks To Geeks.

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


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: CompiledCharset.java 30932 2004-07-29 17:35:38Z vgritsenko $
23  */

24 public abstract class CompiledCharset extends AbstractCharset {
25
26     /** The encoding table of this <code>Charset</code>. */
27     protected byte encoding[];
28
29     /**
30      * Create a new instance of this <code>CompiledCharset</code>.
31      * <p>
32      * After construction, the <code>compile()</code> method will have to
33      * be called for proper operation of this <code>Charset</code>.
34      *
35      * @param name This <code>Charset</code> name.
36      * @param aliases This <code>Charset</code> alias names.
37      * @throws NullPointerException If one of the arguments is <b>null</b>.
38      */

39     protected CompiledCharset(String JavaDoc name, String JavaDoc aliases[]) {
40         super(name, aliases);
41         this.encoding = new byte[8192];
42         for (int x = 0; x < this.encoding.length; x++) this.encoding[x] = 0;
43     }
44
45     /**
46      * Create a new instance of this <code>CompiledCharset</code>.
47      * <p>
48      * The encodings table passed to this constructor <b>needs</b> to be 8192
49      * bytes long, or (in other words), must contain exactly 65536 bits.
50      * </p>
51      * <p>
52      * As in the Java Programming Language a <code>char</code> can assume
53      * values between 0 (zero) and 65535 (inclusive), each bit in the specified
54      * array refers to a specific <code>char</code> value.
55      * </p>
56      * <p>
57      * When this specific bit is set to 1 (one or true) we assume that the
58      * charset <b>can</b> encode the given character, while when the bit is
59      * set to 0 (zero or false), the character cannot be represented using
60      * this <code>Charset</code>.
61      * </p>
62      * <p>
63      * For example, the <b>US-ASCII</b> <code>Charset</code> can represent
64      * only Java characters between 0 (zero) and 255 (inclusive), therefore
65      * the specified byte array will contain only 256 true bits.
66      * </p>
67      * <p>
68      * To check if a character can be encoded by this <code>Charset</code>,
69      * given &quot;<code>c</code>&quot; as the character to verify, one
70      * can write this simple formula:
71      * </p>
72      * <p>
73      * <nobr><code>((encoding[c >> 3] & (1 << (c & 0x07))) > 0)
74      * </p>
75      * <p>
76      * If the result of this operation is 0 (zero) the bit was set to zero,
77      * and therefore &quot;<code>c</code>&quot; cannot be represented in
78      * this <code>Charset</code>, while if the result is greater than 0 (zero)
79      * the character &quot;<code>c</code>&quot; can actually be represented
80      * by this <code>Charset</code>
81      * </p>
82      *
83      * @param name This <code>Charset</code> name.
84      * @param aliases This <code>Charset</code> alias names.
85      * @param encoding This <code>Charset</code> encoding table as specified
86      * above.
87      * @throws NullPointerException If one of the arguments is <b>null</b>.
88      * @throws IllegalArgumentException If the length of the encoding table
89      * is <b>not</b> 8192 precisely.
90      */

91     protected CompiledCharset(String JavaDoc name, String JavaDoc aliases[], byte encoding[])
92     throws NullPointerException JavaDoc, IllegalArgumentException JavaDoc {
93         super(name, aliases);
94         if (encoding == null) throw new NullPointerException JavaDoc("Invalid table");
95         if (encoding.length != 8192) {
96             throw new IllegalArgumentException JavaDoc("Invalid encoding table size: "
97                 + "current length is " + encoding.length + ", required 8192.");
98         }
99         this.encoding = encoding;
100     }
101
102     /**
103      * Check if the specified character is representable by this specifiec
104      * <code>Charset</code> instance.
105      * </p>
106      */

107     public boolean allows(char c) {
108         /* This is tied to haw the compiler does stuff. */
109         return((this.encoding[c >> 3] & (1 << (c & 0x07))) > 0);
110     }
111
112     /**
113      * Compile the encoding table of this <code>CompiledCharset</code>.
114      * <p>
115      * This method will invoke the <code>compile(...)</code> method for any
116      * possible value of a Java character (65536 times, from 0, zero, to
117      * 65535 inclusive), building the encoding table of the characters this
118      * <code>Charset</code> can successfully represent.
119      */

120     protected final void compile() {
121         for (int x = 0; x <= Character.MAX_VALUE; x ++) {
122             if (this.compile((char)x)) {
123                 int pos = x >> 3;
124                 encoding[pos] = (byte) (encoding[pos] | (1 << (x & 0x07)));
125             }
126         }
127     }
128
129     /**
130      * Return true or false wether this encoding can encode the specified
131      * character or not.
132      * <p>
133      * This method is equivalent to the <code>allows(...)</code> method, but
134      * it will be called upon construction of the encoding table.
135      * </p>
136      */

137     protected abstract boolean compile(char c);
138 }
139
Popular Tags