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.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
33 import javax.xml.parsers.SAXParser;
\r
34 import javax.xml.parsers.SAXParserFactory;
\r
36 import org.marc4j.MarcException;
\r
37 import org.xml.sax.InputSource;
\r
38 import org.xml.sax.XMLReader;
\r
42 * <code>CodeTable</code> defines a data structure to facilitate
\r
43 * <code>AnselToUnicode</code> character conversion.
\r
46 * @author Corey Keith
\r
47 * @version $Revision: 1.4 $
\r
50 public class CodeTable implements CodeTableInterface {
\r
51 protected static HashMap charsets = null;
\r
53 protected static HashMap combining = null;
\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 public char getChar(int c, int mode) {
\r
69 HashMap charset = (HashMap) charsets.get(new Integer(mode));
\r
71 if (charset == null) {
\r
72 // System.err.println("Hashtable not found: "
\r
73 // + Integer.toHexString(mode));
\r
76 Character ch = (Character) charset.get(new Integer(c));
\r
78 int newc = (c < 0x80) ? c + 0x80 : c - 0x80;
\r
79 ch = (Character) charset.get(new Integer(newc));
\r
81 // System.err.println("Character not found: "
\r
82 // + Integer.toHexString(c) + " in Code Table: "
\r
83 // + Integer.toHexString(mode));
\r
86 return ch.charValue();
\r
88 return ch.charValue();
\r
93 public CodeTable(InputStream byteStream) {
\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
102 InputSource src = new InputSource(byteStream);
\r
104 CodeTableHandler saxUms = new CodeTableHandler();
\r
106 rdr.setContentHandler(saxUms);
\r
109 charsets = saxUms.getCharSets();
\r
110 combining = saxUms.getCombiningChars();
\r
111 } catch (Exception e) {
\r
112 throw new MarcException(e.getMessage(), e);
\r
116 public CodeTable(String filename) {
\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
125 File file = new File(filename);
\r
126 InputSource src = new InputSource(new FileInputStream(file));
\r
128 CodeTableHandler saxUms = new CodeTableHandler();
\r
130 rdr.setContentHandler(saxUms);
\r
133 charsets = saxUms.getCharSets();
\r
134 combining = saxUms.getCombiningChars();
\r
135 } catch (Exception e) {
\r
136 throw new MarcException(e.getMessage(), e);
\r
140 public CodeTable(URI uri) {
\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
149 InputSource src = new InputSource(uri.toURL().openStream());
\r
151 CodeTableHandler saxUms = new CodeTableHandler();
\r
153 rdr.setContentHandler(saxUms);
\r
156 charsets = saxUms.getCharSets();
\r
157 combining = saxUms.getCombiningChars();
\r
158 } catch (Exception e) {
\r
159 throw new MarcException(e.getMessage(), e);
\r