Make List type safe. Remove/suppress all warnings.
[marc4j.git] / src / org / marc4j / converter / impl / ReverseCodeTable.java
1 // $Id: ReverseCodeTable.java,v 1.2 2005/12/14 17:11:30 bpeters 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.Hashtable;\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 import org.xml.sax.helpers.DefaultHandler;\r
37 \r
38 /**\r
39  * <p>\r
40  * <code>ReverseCodeTable</code> defines a data structure to facilitate\r
41  * UnicodeToAnsel character conversion.\r
42  * </p>\r
43  * \r
44  * @author Corey Keith\r
45  * @version $Revision: 1.2 $\r
46  * \r
47  * @see DefaultHandler\r
48  */\r
49 public class ReverseCodeTable {\r
50   @SuppressWarnings("rawtypes")\r
51   protected static Hashtable charset = null;\r
52 \r
53   @SuppressWarnings("rawtypes")\r
54   protected static Vector combining = null;\r
55 \r
56   public boolean isCombining(Character c) {\r
57     return combining.contains(c);\r
58   }\r
59 \r
60   @SuppressWarnings("rawtypes")\r
61   public Hashtable codeTableHash(Character c) {\r
62     Hashtable chars = (Hashtable) charset.get(c);\r
63     if (chars == null) {\r
64       System.err.println("Not Found: " + c);\r
65       return chars;\r
66     } else\r
67       return chars;\r
68   }\r
69 \r
70   @SuppressWarnings("rawtypes")\r
71   public static boolean inPreviousCharCodeTable(Character c,\r
72       CodeTableTracker ctt) {\r
73     Hashtable chars = (Hashtable) charset.get(c);\r
74     if (chars == null) {\r
75       System.out.println("Not Found: " + c);\r
76       return false;\r
77     } else {\r
78 \r
79       if ((chars.get(ctt.getPrevious(CodeTableTracker.G0)) != null)\r
80           || (chars.get(ctt.getPrevious(CodeTableTracker.G1)) != null)) {\r
81         ctt.makePreviousCurrent();\r
82         return true;\r
83       } else\r
84         return false;\r
85 \r
86     }\r
87   }\r
88 \r
89   @SuppressWarnings("rawtypes")\r
90   public static char getChar(Character c, CodeTableTracker ctt) {\r
91     Hashtable chars = (Hashtable) charset.get(c);\r
92 \r
93     Integer marc = (Integer) chars.get(ctt.getCurrent(CodeTableTracker.G0));\r
94 \r
95     if (marc != null) {\r
96       return (char) marc.intValue();\r
97     }\r
98     marc = (Integer) chars.get(ctt.getCurrent(CodeTableTracker.G1));\r
99     if (marc != null) {\r
100       return (char) marc.intValue();\r
101     }\r
102     return 0x20;\r
103   }\r
104 \r
105   public ReverseCodeTable(InputStream byteStream) {\r
106     try {\r
107       SAXParserFactory factory = SAXParserFactory.newInstance();\r
108       factory.setNamespaceAware(true);\r
109       factory.setValidating(false);\r
110       SAXParser saxParser = factory.newSAXParser();\r
111       XMLReader rdr = saxParser.getXMLReader();\r
112 \r
113       InputSource src = new InputSource(byteStream);\r
114 \r
115       ReverseCodeTableHandler saxUms = new ReverseCodeTableHandler();\r
116 \r
117       rdr.setContentHandler(saxUms);\r
118       rdr.parse(src);\r
119 \r
120       charset = saxUms.getCharSets();\r
121       combining = saxUms.getCombiningChars();\r
122 \r
123     } catch (Exception e) {\r
124         throw new MarcException(e.getMessage(), e);\r
125     }\r
126 \r
127   }\r
128 \r
129   public ReverseCodeTable(String filename) {\r
130     try {\r
131       SAXParserFactory factory = SAXParserFactory.newInstance();\r
132       factory.setNamespaceAware(true);\r
133       factory.setValidating(false);\r
134       SAXParser saxParser = factory.newSAXParser();\r
135       XMLReader rdr = saxParser.getXMLReader();\r
136 \r
137       File file = new File(filename);\r
138       InputSource src = new InputSource(new FileInputStream(file));\r
139 \r
140       ReverseCodeTableHandler saxUms = new ReverseCodeTableHandler();\r
141 \r
142       rdr.setContentHandler(saxUms);\r
143       rdr.parse(src);\r
144 \r
145       charset = saxUms.getCharSets();\r
146       combining = saxUms.getCombiningChars();\r
147 \r
148     } catch (Exception e) {\r
149         throw new MarcException(e.getMessage(), e);\r
150     }\r
151   }\r
152 \r
153   public ReverseCodeTable(URI uri) {\r
154     try {\r
155       SAXParserFactory factory = SAXParserFactory.newInstance();\r
156       factory.setNamespaceAware(true);\r
157       factory.setValidating(false);\r
158       SAXParser saxParser = factory.newSAXParser();\r
159       XMLReader rdr = saxParser.getXMLReader();\r
160 \r
161       InputSource src = new InputSource(uri.toURL().openStream());\r
162 \r
163       ReverseCodeTableHandler saxUms = new ReverseCodeTableHandler();\r
164 \r
165       rdr.setContentHandler(saxUms);\r
166       rdr.parse(src);\r
167 \r
168       charset = saxUms.getCharSets();\r
169       combining = saxUms.getCombiningChars();\r
170 \r
171     } catch (Exception e) {\r
172         throw new MarcException(e.getMessage(), e);\r
173     }\r
174   }\r
175 }\r
176 \r