KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > io > CharacterSet


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: CharacterSet.java,v 1.1.1.1 2003/03/10 16:36:16 taweili Exp $
22  */

23 package org.enhydra.xml.io;
24
25 /**
26  * Information and operations associated with a specific character set.
27  * A global table of these objects is built by the Encodings class.
28  * Instances are immutable, classes can be derived to define override
29  * the operations that can't be handled by the default static parameters.
30  */

31 class CharacterSet {
32     /** Encoding name */
33     private final String JavaDoc fName;
34
35     /** Size of characters, in bits */
36     private final int fCharSize;
37
38     /** The maximum value of a character */
39     private final int fMaxCharValue;
40
41     /** MIME prefered name, null if not specified. */
42     private final String JavaDoc fMimePreferred;
43
44     /** List of aliases, zero length if none */
45     private final String JavaDoc[] fAliases;
46
47     /** Constructor */
48     public CharacterSet(String JavaDoc name,
49                         int charSize,
50                         String JavaDoc mimePreferred,
51                         String JavaDoc[] aliases) {
52         fName = name;
53         fCharSize = charSize;
54         fMimePreferred = mimePreferred;
55         fAliases = aliases;
56
57         // Roughly determine the maximum value of a character.
58
int maxCharValue;
59         if (charSize == 7) {
60             maxCharValue = 0x7e;
61         } else if (charSize == 8) {
62             maxCharValue = 0xFF;
63         } else {
64             maxCharValue = 0xFFFF;
65         }
66         fMaxCharValue = maxCharValue;
67     }
68
69     /** Get the name */
70     public final String JavaDoc getName() {
71         return fName;
72     }
73
74     /** Get the character size, in bits */
75     public final int getCharSize() {
76         return fCharSize;
77     }
78
79     /** Get the maximum value for a character */
80     public final int getMaxCharValue() {
81         return fMaxCharValue;
82     }
83
84     /** Get the MIME preferred name or null if unspecified */
85     public final String JavaDoc getMIMEPreferred() {
86         return fMimePreferred;
87     }
88
89     /** Get the aliases */
90     public final String JavaDoc[] getAliases() {
91         return fAliases;
92     }
93
94     // FIXME: do this correctly, but requires a com.sun class...
95
/**
96      * Determine if a unicode character has a valid mapping to this
97      * character set.
98      * <P> WARNING: This is only well implemented for a few character sets
99      * on an as-needed basis; the rest just go by the character size.
100      */

101     public boolean isValid(char ch) {
102         return (ch <= fMaxCharValue);
103     }
104
105     /**
106      * Determine if another character set has the same valid range of
107      * character codes as this character set. That is, will
108      * <code>isValid()</code> return the same value for any arbitrary
109      * unicode character.
110      * <P> WARNING: This is only well implemented for a few character sets
111      * on an as-needed basis; the rest just go by the character size.
112      * @deprecated use {qlink #isCompatible} instead
113      */

114     public boolean sameValidCharRange(CharacterSet otherSet) {
115         return isCompatible(otherSet);
116     }
117
118     /**
119      * Determine if another character set is compatible to this character set.
120      * "Compatible" means that for every character where
121      * <code>otherSet.isValid()</code> return <code>true</code>,
122      * <code>this.isValid()</code> will return <code>true</code> as well.
123      * <P> WARNING: This is only well implemented for a few character sets
124      * on an as-needed basis; the rest just go by the character set size.
125      */

126     public boolean isCompatible(CharacterSet otherSet) {
127         return (otherSet.getMaxCharValue() == fMaxCharValue);
128     }
129
130     /** Get string representation of object */
131     public String JavaDoc toString() {
132         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(256); // larger than default
133
buf.append(fName);
134         buf.append(": ");
135         buf.append(fCharSize);
136         buf.append(' ');
137         buf.append(fMimePreferred);
138         for (int i = 1; i < fAliases.length; i++) {
139             buf.append(' ');
140             buf.append(fAliases[i]);
141         }
142         return buf.toString();
143     }
144 }
145
146
Popular Tags