1 // $Id: CodeTable.java,v 1.4 2008/10/16 14:51:44 haschart Exp $
\r
3 * Copyright (C) 2002 Bas Peters
\r
5 * This file is part of MARC4J
\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
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
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
21 package org.marc4j.converter.impl;
\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
30 import javax.xml.parsers.SAXParser;
\r
31 import javax.xml.parsers.SAXParserFactory;
\r
33 import org.marc4j.MarcException;
\r
34 import org.xml.sax.InputSource;
\r
35 import org.xml.sax.XMLReader;
\r
39 * <code>CodeTable</code> defines a data structure to facilitate
\r
40 * <code>AnselToUnicode</code> character conversion.
\r
43 * @author Corey Keith
\r
44 * @version $Revision: 1.4 $
\r
47 public class CodeTable implements CodeTableInterface {
\r
48 @SuppressWarnings("rawtypes")
\r
49 protected static HashMap charsets = null;
\r
51 @SuppressWarnings("rawtypes")
\r
52 protected static HashMap combining = null;
\r
54 @SuppressWarnings("rawtypes")
\r
55 public boolean isCombining(int i, int g0, int g1) {
\r
57 Vector v = (Vector) combining.get(new Integer(g0));
\r
58 return (v != null && v.contains(new Integer(i)));
\r
60 Vector v = (Vector) combining.get(new Integer(g1));
\r
61 return (v != null && v.contains(new Integer(i)));
\r
65 @SuppressWarnings("rawtypes")
\r
66 public char getChar(int c, int mode) {
\r
70 HashMap charset = (HashMap) charsets.get(new Integer(mode));
\r
72 if (charset == null) {
\r
73 // System.err.println("Hashtable not found: "
\r
74 // + Integer.toHexString(mode));
\r
77 Character ch = (Character) charset.get(new Integer(c));
\r
79 int newc = (c < 0x80) ? c + 0x80 : c - 0x80;
\r
80 ch = (Character) charset.get(new Integer(newc));
\r
82 // System.err.println("Character not found: "
\r
83 // + Integer.toHexString(c) + " in Code Table: "
\r
84 // + Integer.toHexString(mode));
\r
87 return ch.charValue();
\r
89 return ch.charValue();
\r
94 public CodeTable(InputStream byteStream) {
\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
103 InputSource src = new InputSource(byteStream);
\r
105 CodeTableHandler saxUms = new CodeTableHandler();
\r
107 rdr.setContentHandler(saxUms);
\r
110 charsets = saxUms.getCharSets();
\r
111 combining = saxUms.getCombiningChars();
\r
112 } catch (Exception e) {
\r
113 throw new MarcException("Failed to setup Codetables. Resources missing?", e);
\r
117 public CodeTable(String filename) {
\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
126 File file = new File(filename);
\r
127 InputSource src = new InputSource(new FileInputStream(file));
\r
129 CodeTableHandler saxUms = new CodeTableHandler();
\r
131 rdr.setContentHandler(saxUms);
\r
134 charsets = saxUms.getCharSets();
\r
135 combining = saxUms.getCombiningChars();
\r
136 } catch (Exception e) {
\r
137 throw new MarcException(e.getMessage(), e);
\r
141 public CodeTable(URI uri) {
\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
150 InputSource src = new InputSource(uri.toURL().openStream());
\r
152 CodeTableHandler saxUms = new CodeTableHandler();
\r
154 rdr.setContentHandler(saxUms);
\r
157 charsets = saxUms.getCharSets();
\r
158 combining = saxUms.getCombiningChars();
\r
159 } catch (Exception e) {
\r
160 throw new MarcException(e.getMessage(), e);
\r