KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > charcode > CharacterSetFactory


1 package com.icl.saxon.charcode;
2 import com.icl.saxon.Loader;
3 import javax.xml.transform.OutputKeys JavaDoc;
4 import java.util.Properties JavaDoc;
5
6 /**
7 * This class creates a CharacterSet object for a given named encoding.
8 */

9
10
11 public class CharacterSetFactory {
12
13
14     /**
15     * Make a CharacterSet appropriate to the encoding
16     */

17
18     public static CharacterSet getCharacterSet(Properties JavaDoc details) {
19         String JavaDoc encoding = details.getProperty(OutputKeys.ENCODING);
20         if (encoding==null) encoding = "UTF8";
21         if (encoding.equalsIgnoreCase("utf-8")) encoding = "UTF8"; // needed for Microsoft Java VM
22

23         CharacterSet charSet = makeCharacterSet(encoding);
24         if (charSet==null) {
25             charSet = new ASCIICharacterSet();
26         }
27         return charSet;
28     }
29
30     public static CharacterSet makeCharacterSet(String JavaDoc encoding) {
31
32         if (encoding.equalsIgnoreCase("ASCII")) {
33             return new ASCIICharacterSet();
34         } else if (encoding.equalsIgnoreCase("US-ASCII")) {
35             return new ASCIICharacterSet();
36         } else if (encoding.equalsIgnoreCase("iso-8859-1")) {
37             return new Latin1CharacterSet();
38         } else if (encoding.equalsIgnoreCase("ISO8859_1")) {
39             return new Latin1CharacterSet();
40         } else if (encoding.equalsIgnoreCase("iso-8859-2")) {
41             return new Latin2CharacterSet();
42         } else if (encoding.equalsIgnoreCase("ISO8859_2")) {
43             return new Latin2CharacterSet();
44         } else if (encoding.equalsIgnoreCase("utf-8")) {
45             return new UnicodeCharacterSet();
46         } else if (encoding.equalsIgnoreCase("UTF8")) {
47             return new UnicodeCharacterSet();
48         } else if (encoding.equalsIgnoreCase("utf-16")) {
49             return new UnicodeCharacterSet();
50         } else if (encoding.equalsIgnoreCase("utf16")) {
51             return new UnicodeCharacterSet();
52         } else if (encoding.equalsIgnoreCase("KOI8-R")) {
53             return new KOI8RCharacterSet();
54         } else if (encoding.equalsIgnoreCase("cp1251")) {
55             return new CP1251CharacterSet();
56         } else if (encoding.equalsIgnoreCase("windows-1251")) {
57             return new CP1251CharacterSet();
58         } else if (encoding.equalsIgnoreCase("cp1250")) {
59             return new CP1250CharacterSet();
60         } else if (encoding.equalsIgnoreCase("windows-1250")) {
61             return new CP1250CharacterSet();
62         } else if (encoding.equalsIgnoreCase("cp852")) {
63             return new CP852CharacterSet();
64             
65         } else {
66             String JavaDoc csname = null;
67             try {
68                 // Allow an alias for the character set to be specified as a system property
69
csname = System.getProperty(OutputKeys.ENCODING + "." + encoding);
70                 if (csname == null) {
71                     csname = encoding;
72                 }
73                 Object JavaDoc obj = Loader.getInstance(csname);
74                 if (obj instanceof PluggableCharacterSet) {
75                     return (PluggableCharacterSet)obj;
76                 }
77             } catch (Exception JavaDoc err) {
78                 System.err.println("Failed to load " + csname);
79             }
80         }
81         
82         return null;
83
84     }
85 }
86
87 //
88
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
89
// you may not use this file except in compliance with the License. You may obtain a copy of the
90
// License at http://www.mozilla.org/MPL/
91
//
92
// Software distributed under the License is distributed on an "AS IS" basis,
93
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
94
// See the License for the specific language governing rights and limitations under the License.
95
//
96
// The Original Code is: all this file.
97
//
98
// The Initial Developer of the Original Code is
99
// Michael Kay of International Computers Limited (michael.h.kay@ntlworld.com).
100
//
101
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
102
//
103
// Contributor(s): none.
104
//
105
Popular Tags