001: /*
002: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * [See end of file]
004: */
005:
006: package com.hp.hpl.jena.util;
007:
008: import java.nio.charset.Charset;
009: import java.util.*;
010:
011: /**
012: *
013: * This class provides a number of static methods which interact with
014: * java.nio.charset.Charset to analyze and transform the strings identifing
015: * character encodings.
016: *
017: * @author Jeremy J. Carroll
018: *
019: */
020: abstract public class CharEncoding {
021: static Set macEncodings = new HashSet();
022: static {
023: macEncodings.add("MacArabic");
024: macEncodings.add("MacCentralEurope");
025: macEncodings.add("MacCroatian");
026: macEncodings.add("MacCyrillic");
027: macEncodings.add("MacDingbat");
028: macEncodings.add("MacGreek");
029: macEncodings.add("MacHebrew");
030: macEncodings.add("MacIceland");
031: macEncodings.add("MacRoman");
032: macEncodings.add("MacRomania");
033: macEncodings.add("MacSymbol");
034: macEncodings.add("MacThai");
035: macEncodings.add("MacTurkish");
036: macEncodings.add("MacUkraine");
037: }
038: private String name;
039:
040: private CharEncoding() {
041: }
042:
043: private CharEncoding(String name) {
044: this .name = name;
045: }
046:
047: /**
048: * Gives the canonical name for this charset.
049: * If {@link #isIANA()} returns true, then
050: * this is the name registered at IANA.
051: * If {@link #isInNIO()} returns true, and
052: * {@link #isIANA()} returns false, then this name
053: * will start with "x-".
054: * The name is case insensitive, but not case
055: * normalized.
056: * @return Canonical name.
057: */
058: public String name() {
059: return name;
060: }
061:
062: /**
063: * Returns true if this charset
064: * registered at IANA.
065: * Since the registry may change, the results of this
066: * method may not be entirely up-to-date,
067: * and draws from the knowledge in
068: * the Java java.nio.charset.Charset class.
069: * If {@link #isInNIO()} returns false, no information
070: * is known, and this method returns false.
071: * @return true if this character encoding is IANA registered.
072: */
073:
074: abstract public boolean isIANA();
075:
076: /**
077: * Returns true if this charset is supported by
078: * java.nio.charset.Charset.
079: * Without this support {@link #isIANA()}
080: * does not work correctly.
081: * @return true if this charset is supported by
082: * java.nio.charset.Charset.
083: */
084: abstract public boolean isInNIO();
085:
086: /**
087: * If {@link #isIANA} or {@link #isInNIO}
088: * return false, this returns a suggested warning
089: * message. If {@link #isIANA} is true, then this
090: * returns null.
091: * @return A message (or null)
092: */
093: abstract public String warningMessage();
094:
095: static private class IANAnioEncoding extends CharEncoding {
096: IANAnioEncoding(String name) {
097: super (name);
098: }
099:
100: public boolean isIANA() {
101: return true;
102: }
103:
104: public boolean isInNIO() {
105: return true;
106: }
107:
108: public String warningMessage() {
109: return null;
110: }
111: }
112:
113: static private class NonIANAnioEncoding extends CharEncoding {
114: NonIANAnioEncoding(String name) {
115: super (name);
116: }
117:
118: public boolean isIANA() {
119: return false;
120: }
121:
122: public boolean isInNIO() {
123: return true;
124: }
125:
126: public String warningMessage() {
127: return "The encoding \""
128: + name()
129: + "\" is not registered with IANA, and hence not suitable for Web content.";
130: }
131: }
132:
133: static private class NotNioEncoding extends CharEncoding {
134: NotNioEncoding(String name) {
135: super (name);
136: }
137:
138: public boolean isIANA() {
139: return false;
140: }
141:
142: public boolean isInNIO() {
143: return false;
144: }
145:
146: public String warningMessage() {
147: return "The encoding \""
148: + name()
149: + "\" is not fully supported; maybe try using Java 1.5 or higher (if you are not already).";
150: }
151: }
152:
153: /**
154: * Create a new CharacterEncoding object,
155: * given a name of a character encoding
156: * identifying it.
157: * @param enc A name.
158: * @return The corresponding CharacterEncoding object.
159: */
160: static public CharEncoding create(String enc) {
161: if (Charset.isSupported(enc)) {
162: String nm = Charset.forName(enc).name();
163: if (nm.charAt(1) == '-'
164: && (nm.charAt(0) == 'x' || nm.charAt(0) == 'X'))
165: return new NonIANAnioEncoding(nm);
166: else if (nm.startsWith("Mac") && macEncodings.contains(nm))
167: return new NonIANAnioEncoding(nm);
168: else
169: return new IANAnioEncoding(nm);
170: } else {
171: return new NotNioEncoding(enc);
172: }
173: }
174: }
175:
176: /*
177: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP All rights
178: * reserved.
179: *
180: * Redistribution and use in source and binary forms, with or without
181: * modification, are permitted provided that the following conditions are met:
182: * 1. Redistributions of source code must retain the above copyright notice,
183: * this list of conditions and the following disclaimer. 2. Redistributions in
184: * binary form must reproduce the above copyright notice, this list of
185: * conditions and the following disclaimer in the documentation and/or other
186: * materials provided with the distribution. 3. The name of the author may not
187: * be used to endorse or promote products derived from this software without
188: * specific prior written permission.
189: *
190: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
191: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
192: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
193: * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
194: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
195: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
196: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
197: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
198: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
199: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
200: */
|