01: /*
02: * $Id: XMLDecoderFactory.java,v 1.4 2004/07/08 08:03:04 yuvalo Exp $
03: *
04: * (C) Copyright 2002-2004 by Yuval Oren. All rights reserved.
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: package com.bluecast.xml;
20:
21: import com.bluecast.io.CharsetDecoder;
22: import java.io.UnsupportedEncodingException;
23: import java.util.HashMap;
24:
25: /**
26: * Factory class for creating CharsetDecoders that also
27: * convert carriage returns to linefeeds and check for invalid XML characters,
28: * as per the XML 1.0 specification.
29: *
30: * @author Yuval Oren, yuval@bluecast.com
31: * @version $Revision: 1.4 $
32: */
33: public class XMLDecoderFactory {
34: private static HashMap decoders = new HashMap();
35: static {
36: UTF8XMLDecoder utf8 = new UTF8XMLDecoder();
37: ASCIIXMLDecoder ascii = new ASCIIXMLDecoder();
38: ISO8859_1XMLDecoder iso8859 = new ISO8859_1XMLDecoder();
39: UnicodeBigXMLDecoder utf16be = new UnicodeBigXMLDecoder();
40: UnicodeLittleXMLDecoder utf16le = new UnicodeLittleXMLDecoder();
41: decoders.put("UTF-8", utf8);
42: decoders.put("UTF8", utf8);
43: decoders.put("US-ASCII", ascii);
44: decoders.put("ASCII", ascii);
45: decoders.put("ISO-8859-1", iso8859);
46: decoders.put("ISO8859_1", iso8859);
47: decoders.put("UTF-16LE", utf16le);
48: decoders.put("UNICODELITTLE", utf16le);
49: decoders.put("UNICODELITTLEUNMARKED", utf16le);
50: decoders.put("UTF-16BE", utf16be);
51: decoders.put("UTF-16", utf16be);
52: decoders.put("UNICODEBIG", utf16be);
53: decoders.put("UNICODEBIGUNMARKED", utf16be);
54: }
55:
56: static public XMLDecoder createDecoder(String encoding)
57: throws UnsupportedEncodingException {
58: XMLDecoder d = (XMLDecoder) decoders
59: .get(encoding.toUpperCase());
60: if (d != null)
61: return d.newXMLDecoder();
62: else
63: throw new UnsupportedEncodingException("Encoding '"
64: + encoding + "' not supported");
65: }
66: }
|