Make List type safe. Remove/suppress all warnings.
[marc4j.git] / src / org / marc4j / converter / impl / ReverseCodeTableHandler.java
1 // $Id: ReverseCodeTableHandler.java,v 1.2 2005/05/04 11:39:12 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.util.Hashtable;\r
26 import java.util.Vector;\r
27 \r
28 import javax.xml.parsers.SAXParser;\r
29 import javax.xml.parsers.SAXParserFactory;\r
30 \r
31 import org.xml.sax.Attributes;\r
32 import org.xml.sax.InputSource;\r
33 import org.xml.sax.Locator;\r
34 import org.xml.sax.SAXParseException;\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>ReverseCodeTableHandler</code> is a SAX2 <code>ContentHandler</code>\r
41  * that builds a data structure to facilitate <code>UnicodeToAnsel</code>\r
42  * character conversion.\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 ReverseCodeTableHandler extends DefaultHandler {\r
50   @SuppressWarnings("rawtypes")\r
51   private Hashtable charset;\r
52 \r
53   @SuppressWarnings("rawtypes")\r
54   private Vector combiningchars;\r
55 \r
56   private boolean useAlt = false;\r
57   \r
58   /** Data element identifier */\r
59   private Integer isocode;\r
60 \r
61   private char[] marc;\r
62 \r
63   private Character ucs;\r
64 \r
65   private boolean combining;\r
66 \r
67   /** Tag name */\r
68   @SuppressWarnings("unused")\r
69   private String tag;\r
70 \r
71   /** StringBuffer to store data */\r
72   private StringBuffer data;\r
73 \r
74   /** Locator object */\r
75   @SuppressWarnings("unused")\r
76   private Locator locator;\r
77 \r
78   @SuppressWarnings("rawtypes")\r
79   public Hashtable getCharSets() {\r
80     return charset;\r
81   }\r
82 \r
83   @SuppressWarnings("rawtypes")\r
84   public Vector getCombiningChars() {\r
85     return combiningchars;\r
86   }\r
87 \r
88   /**\r
89    * <p>\r
90    * Registers the SAX2 <code>Locator</code> object.\r
91    * </p>\r
92    * \r
93    * @param locator\r
94    *          the {@link Locator}object\r
95    */\r
96   public void setDocumentLocator(Locator locator) {\r
97     this.locator = locator;\r
98   }\r
99 \r
100   @SuppressWarnings("rawtypes")\r
101   public void startElement(String uri, String name, String qName,\r
102       Attributes atts) throws SAXParseException {\r
103     if (name.equals("characterSet"))\r
104       isocode = Integer.valueOf(atts.getValue("ISOcode"), 16);\r
105     else if (name.equals("marc"))\r
106       data = new StringBuffer();\r
107     else if (name.equals("codeTables")) {\r
108       charset = new Hashtable();\r
109       combiningchars = new Vector();\r
110     } else if (name.equals("ucs"))\r
111       data = new StringBuffer();\r
112     else if (name.equals("alt"))\r
113       data = new StringBuffer();\r
114     else if (name.equals("code"))\r
115       combining = false;\r
116     else if (name.equals("isCombining"))\r
117       data = new StringBuffer();\r
118 \r
119   }\r
120 \r
121   public void characters(char[] ch, int start, int length) {\r
122     if (data != null) {\r
123       data.append(ch, start, length);\r
124     }\r
125   }\r
126 \r
127   @SuppressWarnings({ "unchecked", "rawtypes" })\r
128   public void endElement(String uri, String name, String qName)\r
129       throws SAXParseException {\r
130     if (name.equals("marc")) {\r
131       String marcstr = data.toString();\r
132       if (marcstr.length() == 6) {\r
133         marc = new char[3];\r
134         marc[0] = (char) Integer.parseInt(marcstr.substring(0, 2), 16);\r
135         marc[1] = (char) Integer.parseInt(marcstr.substring(2, 4), 16);\r
136         marc[2] = (char) Integer.parseInt(marcstr.substring(4, 6), 16);\r
137       } else {\r
138         marc = new char[1];\r
139         marc[0] = (char) Integer.parseInt(marcstr, 16);\r
140       }\r
141     } else if (name.equals("ucs")) {\r
142       if (data.length() > 0)\r
143         ucs = new Character((char) Integer.parseInt(data.toString(), 16));\r
144       else\r
145         useAlt = true;\r
146     } else if (name.equals("alt")) {\r
147       if (useAlt && data.length() > 0) {\r
148         ucs = new Character((char) Integer.parseInt(data.toString(), 16));\r
149         useAlt = false;\r
150       }      \r
151     } else if (name.equals("code")) {\r
152       if (combining) {\r
153         combiningchars.add(ucs);\r
154       }\r
155 \r
156       if (charset.get(ucs) == null) {\r
157         Hashtable h = new Hashtable(1);\r
158         h.put(isocode, marc);\r
159         charset.put(ucs, h);\r
160       } else {\r
161         Hashtable h = (Hashtable) charset.get(ucs);\r
162         h.put(isocode, marc);\r
163       }\r
164     } else if (name.equals("isCombining")) {\r
165       if (data.toString().equals("true"))\r
166         combining = true;\r
167     }\r
168     data = null;\r
169   }\r
170 \r
171   @SuppressWarnings({ "rawtypes", "unused" })\r
172   public static void main(String[] args) {\r
173     Hashtable charsets = null;\r
174 \r
175     try {\r
176 \r
177       SAXParserFactory factory = SAXParserFactory.newInstance();\r
178       factory.setNamespaceAware(true);\r
179       factory.setValidating(false);\r
180       SAXParser saxParser = factory.newSAXParser();\r
181       XMLReader rdr = saxParser.getXMLReader();\r
182 \r
183       File file = new File(\r
184           "C:\\Documents and Settings\\ckeith\\Desktop\\Projects\\Code Tables\\codetables.xml");\r
185       InputSource src = new InputSource(new FileInputStream(file));\r
186 \r
187       ReverseCodeTableHandler saxUms = new ReverseCodeTableHandler();\r
188 \r
189       rdr.setContentHandler(saxUms);\r
190       rdr.parse(src);\r
191     } catch (Exception exc) {\r
192       exc.printStackTrace(System.out);\r
193       System.err.println("Exception: " + exc);\r
194     }\r
195   }\r
196 }