1 // $Id: ReverseCodeTable.java,v 1.2 2005/12/14 17:11:30 bpeters 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.Hashtable;
\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
36 import org.xml.sax.helpers.DefaultHandler;
\r
40 * <code>ReverseCodeTable</code> defines a data structure to facilitate
\r
41 * UnicodeToAnsel character conversion.
\r
44 * @author Corey Keith
\r
45 * @version $Revision: 1.2 $
\r
47 * @see DefaultHandler
\r
49 public class ReverseCodeTable {
\r
50 @SuppressWarnings("rawtypes")
\r
51 protected static Hashtable charset = null;
\r
53 @SuppressWarnings("rawtypes")
\r
54 protected static Vector combining = null;
\r
56 public boolean isCombining(Character c) {
\r
57 return combining.contains(c);
\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
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
79 if ((chars.get(ctt.getPrevious(CodeTableTracker.G0)) != null)
\r
80 || (chars.get(ctt.getPrevious(CodeTableTracker.G1)) != null)) {
\r
81 ctt.makePreviousCurrent();
\r
89 @SuppressWarnings("rawtypes")
\r
90 public static char getChar(Character c, CodeTableTracker ctt) {
\r
91 Hashtable chars = (Hashtable) charset.get(c);
\r
93 Integer marc = (Integer) chars.get(ctt.getCurrent(CodeTableTracker.G0));
\r
96 return (char) marc.intValue();
\r
98 marc = (Integer) chars.get(ctt.getCurrent(CodeTableTracker.G1));
\r
100 return (char) marc.intValue();
\r
105 public ReverseCodeTable(InputStream byteStream) {
\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
113 InputSource src = new InputSource(byteStream);
\r
115 ReverseCodeTableHandler saxUms = new ReverseCodeTableHandler();
\r
117 rdr.setContentHandler(saxUms);
\r
120 charset = saxUms.getCharSets();
\r
121 combining = saxUms.getCombiningChars();
\r
123 } catch (Exception e) {
\r
124 throw new MarcException(e.getMessage(), e);
\r
129 public ReverseCodeTable(String filename) {
\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
137 File file = new File(filename);
\r
138 InputSource src = new InputSource(new FileInputStream(file));
\r
140 ReverseCodeTableHandler saxUms = new ReverseCodeTableHandler();
\r
142 rdr.setContentHandler(saxUms);
\r
145 charset = saxUms.getCharSets();
\r
146 combining = saxUms.getCombiningChars();
\r
148 } catch (Exception e) {
\r
149 throw new MarcException(e.getMessage(), e);
\r
153 public ReverseCodeTable(URI uri) {
\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
161 InputSource src = new InputSource(uri.toURL().openStream());
\r
163 ReverseCodeTableHandler saxUms = new ReverseCodeTableHandler();
\r
165 rdr.setContentHandler(saxUms);
\r
168 charset = saxUms.getCharSets();
\r
169 combining = saxUms.getCombiningChars();
\r
171 } catch (Exception e) {
\r
172 throw new MarcException(e.getMessage(), e);
\r