Make List type safe. Remove/suppress all warnings.
[marc4j.git] / src / org / marc4j / converter / impl / CodeTable.java
1 // $Id: CodeTable.java,v 1.4 2008/10/16 14:51:44 haschart Exp $\r
2 /**\r
3  * Copyright (C) 2002 Bas Peters\r
4  *\r
5  * This file is part of MARC4J\r
6  *\r
7  * MARC4J is free software; you can redistribute it and/or\r
8  * modify it under the terms of the GNU Lesser General Public\r
9  * License as published by the Free Software Foundation; either\r
10  * version 2.1 of the License, or (at your option) any later version.\r
11  *\r
12  * MARC4J is distributed in the hope that it will be useful,\r
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
15  * Lesser General Public License for more details.\r
16  *\r
17  * You should have received a copy of the GNU Lesser General Public\r
18  * License along with MARC4J; if not, write to the Free Software\r
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
20  */\r
21 package org.marc4j.converter.impl;\r
22 \r
23 import java.io.File;\r
24 import java.io.FileInputStream;\r
25 import java.io.InputStream;\r
26 import java.net.URI;\r
27 import java.util.HashMap;\r
28 import java.util.Vector;\r
29 \r
30 import javax.xml.parsers.SAXParser;\r
31 import javax.xml.parsers.SAXParserFactory;\r
32 \r
33 import org.marc4j.MarcException;\r
34 import org.xml.sax.InputSource;\r
35 import org.xml.sax.XMLReader;\r
36 \r
37 /**\r
38  * <p>\r
39  * <code>CodeTable</code> defines a data structure to facilitate\r
40  * <code>AnselToUnicode</code> character conversion.\r
41  * </p>\r
42  * \r
43  * @author Corey Keith\r
44  * @version $Revision: 1.4 $\r
45  *  \r
46  */\r
47 public class CodeTable implements CodeTableInterface {\r
48     @SuppressWarnings("rawtypes")\r
49     protected static HashMap charsets = null;\r
50 \r
51     @SuppressWarnings("rawtypes")\r
52     protected static HashMap combining = null;\r
53 \r
54     @SuppressWarnings("rawtypes")\r
55     public boolean isCombining(int i, int g0, int g1) {\r
56         if (i <= 0x7E) {\r
57             Vector v = (Vector) combining.get(new Integer(g0));\r
58             return (v != null && v.contains(new Integer(i)));\r
59         } else {\r
60             Vector v = (Vector) combining.get(new Integer(g1));\r
61             return (v != null && v.contains(new Integer(i)));\r
62         }\r
63     }\r
64 \r
65     @SuppressWarnings("rawtypes")\r
66     public char getChar(int c, int mode) {\r
67         if (c == 0x20)\r
68             return (char) c;\r
69         else {\r
70             HashMap charset = (HashMap) charsets.get(new Integer(mode));\r
71 \r
72             if (charset == null) {\r
73 //                System.err.println("Hashtable not found: "\r
74 //                        + Integer.toHexString(mode));\r
75                 return (char) c;\r
76             } else {\r
77                 Character ch = (Character) charset.get(new Integer(c));\r
78                 if (ch == null) {\r
79                     int newc = (c < 0x80) ? c + 0x80 : c - 0x80;\r
80                     ch = (Character) charset.get(new Integer(newc));\r
81                     if (ch == null) {\r
82 //                        System.err.println("Character not found: "\r
83 //                                + Integer.toHexString(c) + " in Code Table: "\r
84 //                                + Integer.toHexString(mode));\r
85                         return (char) 0;\r
86                     } else\r
87                         return ch.charValue();\r
88                 } else\r
89                     return ch.charValue();\r
90             }\r
91         }\r
92     }\r
93 \r
94     public CodeTable(InputStream byteStream) {\r
95         try {\r
96 \r
97             SAXParserFactory factory = SAXParserFactory.newInstance();\r
98             factory.setNamespaceAware(true);\r
99             factory.setValidating(false);\r
100             SAXParser saxParser = factory.newSAXParser();\r
101             XMLReader rdr = saxParser.getXMLReader();\r
102 \r
103             InputSource src = new InputSource(byteStream);\r
104 \r
105             CodeTableHandler saxUms = new CodeTableHandler();\r
106 \r
107             rdr.setContentHandler(saxUms);\r
108             rdr.parse(src);\r
109 \r
110             charsets = saxUms.getCharSets();\r
111             combining = saxUms.getCombiningChars();\r
112         } catch (Exception e) {\r
113             throw new MarcException(e.getMessage(), e);\r
114         }\r
115     }\r
116 \r
117     public CodeTable(String filename) {\r
118         try {\r
119 \r
120             SAXParserFactory factory = SAXParserFactory.newInstance();\r
121             factory.setNamespaceAware(true);\r
122             factory.setValidating(false);\r
123             SAXParser saxParser = factory.newSAXParser();\r
124             XMLReader rdr = saxParser.getXMLReader();\r
125 \r
126             File file = new File(filename);\r
127             InputSource src = new InputSource(new FileInputStream(file));\r
128 \r
129             CodeTableHandler saxUms = new CodeTableHandler();\r
130 \r
131             rdr.setContentHandler(saxUms);\r
132             rdr.parse(src);\r
133 \r
134             charsets = saxUms.getCharSets();\r
135             combining = saxUms.getCombiningChars();\r
136         } catch (Exception e) {\r
137             throw new MarcException(e.getMessage(), e);\r
138         }\r
139     }\r
140 \r
141     public CodeTable(URI uri) {\r
142         try {\r
143 \r
144             SAXParserFactory factory = SAXParserFactory.newInstance();\r
145             factory.setNamespaceAware(true);\r
146             factory.setValidating(false);\r
147             SAXParser saxParser = factory.newSAXParser();\r
148             XMLReader rdr = saxParser.getXMLReader();\r
149 \r
150             InputSource src = new InputSource(uri.toURL().openStream());\r
151 \r
152             CodeTableHandler saxUms = new CodeTableHandler();\r
153 \r
154             rdr.setContentHandler(saxUms);\r
155             rdr.parse(src);\r
156 \r
157             charsets = saxUms.getCharSets();\r
158             combining = saxUms.getCombiningChars();\r
159         } catch (Exception e) {\r
160             throw new MarcException(e.getMessage(), e);\r
161         }\r
162     }   \r
163 }