Original 2.4
[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.io.PrintStream;\r
27 import java.net.URI;\r
28 import java.util.Arrays;\r
29 import java.util.HashMap;\r
30 import java.util.Iterator;\r
31 import java.util.Vector;\r
32 \r
33 import javax.xml.parsers.SAXParser;\r
34 import javax.xml.parsers.SAXParserFactory;\r
35 \r
36 import org.marc4j.MarcException;\r
37 import org.xml.sax.InputSource;\r
38 import org.xml.sax.XMLReader;\r
39 \r
40 /**\r
41  * <p>\r
42  * <code>CodeTable</code> defines a data structure to facilitate\r
43  * <code>AnselToUnicode</code> character conversion.\r
44  * </p>\r
45  * \r
46  * @author Corey Keith\r
47  * @version $Revision: 1.4 $\r
48  *  \r
49  */\r
50 public class CodeTable implements CodeTableInterface {\r
51     protected static HashMap charsets = null;\r
52 \r
53     protected static HashMap combining = null;\r
54 \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     public char getChar(int c, int mode) {\r
66         if (c == 0x20)\r
67             return (char) c;\r
68         else {\r
69             HashMap charset = (HashMap) charsets.get(new Integer(mode));\r
70 \r
71             if (charset == null) {\r
72 //                System.err.println("Hashtable not found: "\r
73 //                        + Integer.toHexString(mode));\r
74                 return (char) c;\r
75             } else {\r
76                 Character ch = (Character) charset.get(new Integer(c));\r
77                 if (ch == null) {\r
78                     int newc = (c < 0x80) ? c + 0x80 : c - 0x80;\r
79                     ch = (Character) charset.get(new Integer(newc));\r
80                     if (ch == null) {\r
81 //                        System.err.println("Character not found: "\r
82 //                                + Integer.toHexString(c) + " in Code Table: "\r
83 //                                + Integer.toHexString(mode));\r
84                         return (char) 0;\r
85                     } else\r
86                         return ch.charValue();\r
87                 } else\r
88                     return ch.charValue();\r
89             }\r
90         }\r
91     }\r
92 \r
93     public CodeTable(InputStream byteStream) {\r
94         try {\r
95 \r
96             SAXParserFactory factory = SAXParserFactory.newInstance();\r
97             factory.setNamespaceAware(true);\r
98             factory.setValidating(false);\r
99             SAXParser saxParser = factory.newSAXParser();\r
100             XMLReader rdr = saxParser.getXMLReader();\r
101 \r
102             InputSource src = new InputSource(byteStream);\r
103 \r
104             CodeTableHandler saxUms = new CodeTableHandler();\r
105 \r
106             rdr.setContentHandler(saxUms);\r
107             rdr.parse(src);\r
108 \r
109             charsets = saxUms.getCharSets();\r
110             combining = saxUms.getCombiningChars();\r
111         } catch (Exception e) {\r
112             throw new MarcException(e.getMessage(), e);\r
113         }\r
114     }\r
115 \r
116     public CodeTable(String filename) {\r
117         try {\r
118 \r
119             SAXParserFactory factory = SAXParserFactory.newInstance();\r
120             factory.setNamespaceAware(true);\r
121             factory.setValidating(false);\r
122             SAXParser saxParser = factory.newSAXParser();\r
123             XMLReader rdr = saxParser.getXMLReader();\r
124 \r
125             File file = new File(filename);\r
126             InputSource src = new InputSource(new FileInputStream(file));\r
127 \r
128             CodeTableHandler saxUms = new CodeTableHandler();\r
129 \r
130             rdr.setContentHandler(saxUms);\r
131             rdr.parse(src);\r
132 \r
133             charsets = saxUms.getCharSets();\r
134             combining = saxUms.getCombiningChars();\r
135         } catch (Exception e) {\r
136             throw new MarcException(e.getMessage(), e);\r
137         }\r
138     }\r
139 \r
140     public CodeTable(URI uri) {\r
141         try {\r
142 \r
143             SAXParserFactory factory = SAXParserFactory.newInstance();\r
144             factory.setNamespaceAware(true);\r
145             factory.setValidating(false);\r
146             SAXParser saxParser = factory.newSAXParser();\r
147             XMLReader rdr = saxParser.getXMLReader();\r
148 \r
149             InputSource src = new InputSource(uri.toURL().openStream());\r
150 \r
151             CodeTableHandler saxUms = new CodeTableHandler();\r
152 \r
153             rdr.setContentHandler(saxUms);\r
154             rdr.parse(src);\r
155 \r
156             charsets = saxUms.getCharSets();\r
157             combining = saxUms.getCombiningChars();\r
158         } catch (Exception e) {\r
159             throw new MarcException(e.getMessage(), e);\r
160         }\r
161     }   \r
162 }