KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > util > CharEncoding


1 /*
2  * (c) Copyright 2005 Hewlett-Packard Development Company, LP
3  * [See end of file]
4  */

5
6 package com.hp.hpl.jena.util;
7
8 import java.nio.charset.Charset JavaDoc;
9 import java.util.*;
10 /**
11  *
12  * This class provides a number of static methods which interact with
13  * java.nio.charset.Charset to analyze and transform the strings identifing
14  * character encodings.
15  *
16  * @author Jeremy J. Carroll
17  *
18  */

19 abstract public class CharEncoding {
20     static Set macEncodings = new HashSet();
21     static {
22 macEncodings.add("MacArabic");
23 macEncodings.add("MacCentralEurope");
24 macEncodings.add("MacCroatian");
25 macEncodings.add("MacCyrillic");
26 macEncodings.add("MacDingbat");
27 macEncodings.add("MacGreek");
28 macEncodings.add("MacHebrew");
29 macEncodings.add("MacIceland");
30 macEncodings.add("MacRoman");
31 macEncodings.add("MacRomania");
32 macEncodings.add("MacSymbol");
33 macEncodings.add("MacThai");
34 macEncodings.add("MacTurkish");
35 macEncodings.add("MacUkraine");
36     }
37     private String JavaDoc name;
38     private CharEncoding(){}
39     private CharEncoding(String JavaDoc name) {
40       this.name = name;
41     }
42     /**
43      * Gives the canonical name for this charset.
44      * If {@link #isIANA()} returns true, then
45      * this is the name registered at IANA.
46      * If {@link #isInNIO()} returns true, and
47      * {@link #isIANA()} returns false, then this name
48      * will start with "x-".
49      * The name is case insensitive, but not case
50      * normalized.
51      * @return Canonical name.
52      */

53     public String JavaDoc name() {
54         return name;
55     }
56     /**
57      * Returns true if this charset
58      * registered at IANA.
59      * Since the registry may change, the results of this
60      * method may not be entirely up-to-date,
61      * and draws from the knowledge in
62      * the Java java.nio.charset.Charset class.
63      * If {@link #isInNIO()} returns false, no information
64      * is known, and this method returns false.
65      * @return true if this character encoding is IANA registered.
66      */

67  
68     abstract public boolean isIANA();
69     /**
70      * Returns true if this charset is supported by
71      * java.nio.charset.Charset.
72      * Without this support {@link #isIANA()}
73      * does not work correctly.
74      * @return true if this charset is supported by
75      * java.nio.charset.Charset.
76      */

77     abstract public boolean isInNIO();
78     
79     /**
80      * If {@link #isIANA} or {@link #isInNIO}
81      * return false, this returns a suggested warning
82      * message. If {@link #isIANA} is true, then this
83      * returns null.
84      * @return A message (or null)
85      */

86     abstract public String JavaDoc warningMessage();
87     static private class IANAnioEncoding extends CharEncoding {
88         IANAnioEncoding(String JavaDoc name) {
89             super(name);
90         }
91         public boolean isIANA() {
92             return true;
93         }
94         public boolean isInNIO() {
95             return true;
96         }
97         public String JavaDoc warningMessage() {
98             return null;
99         }
100     }
101     static private class NonIANAnioEncoding extends CharEncoding {
102         NonIANAnioEncoding(String JavaDoc name) {
103             super(name);
104         }
105         public boolean isIANA() {
106             return false;
107         }
108         public boolean isInNIO() {
109             return true;
110         }
111         public String JavaDoc warningMessage() {
112             return "The encoding \"" + name() + "\" is not registered with IANA, and hence not suitable for Web content.";
113         }
114     }
115     static private class NotNioEncoding extends CharEncoding {
116         NotNioEncoding(String JavaDoc name) {
117             super(name);
118         }
119         public boolean isIANA() {
120             return false;
121         }
122         public boolean isInNIO() {
123             return false;
124         }
125         public String JavaDoc warningMessage() {
126             return "The encoding \"" + name() + "\" is not fully supported; maybe try using Java 1.5 or higher (if you are not already).";
127         }
128     }
129
130     /**
131      * Create a new CharacterEncoding object,
132      * given a name of a character encoding
133      * identifying it.
134      * @param enc A name.
135      * @return The corresponding CharacterEncoding object.
136      */

137     static public CharEncoding create(String JavaDoc enc){
138         if (Charset.isSupported(enc)) {
139             String JavaDoc nm = Charset.forName(enc).name();
140             if (nm.charAt(1)=='-'
141                 && (nm.charAt(0)=='x' || nm.charAt(0)=='X') )
142                 return new NonIANAnioEncoding(nm);
143             else if (nm.startsWith("Mac") &&
144                macEncodings.contains(nm) )
145                 return new NonIANAnioEncoding(nm);
146             else
147                 return new IANAnioEncoding(nm);
148         } else {
149             return new NotNioEncoding(enc);
150         }
151     }
152 }
153
154 /*
155  * (c) Copyright 2005 Hewlett-Packard Development Company, LP All rights
156  * reserved.
157  *
158  * Redistribution and use in source and binary forms, with or without
159  * modification, are permitted provided that the following conditions are met:
160  * 1. Redistributions of source code must retain the above copyright notice,
161  * this list of conditions and the following disclaimer. 2. Redistributions in
162  * binary form must reproduce the above copyright notice, this list of
163  * conditions and the following disclaimer in the documentation and/or other
164  * materials provided with the distribution. 3. The name of the author may not
165  * be used to endorse or promote products derived from this software without
166  * specific prior written permission.
167  *
168  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
169  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
170  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
171  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
172  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
173  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
174  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
175  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
176  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
177  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
178  */

179
180
Popular Tags