1 // $Id: ReverseCodeTableHandler.java,v 1.2 2005/05/04 11:39:12 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.util.Hashtable;
\r
26 import java.util.Vector;
\r
28 import javax.xml.parsers.SAXParser;
\r
29 import javax.xml.parsers.SAXParserFactory;
\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
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
44 * @author Corey Keith
\r
45 * @version $Revision: 1.2 $
\r
47 * @see DefaultHandler
\r
49 public class ReverseCodeTableHandler extends DefaultHandler {
\r
50 @SuppressWarnings("rawtypes")
\r
51 private Hashtable charset;
\r
53 @SuppressWarnings("rawtypes")
\r
54 private Vector combiningchars;
\r
56 private boolean useAlt = false;
\r
58 /** Data element identifier */
\r
59 private Integer isocode;
\r
61 private char[] marc;
\r
63 private Character ucs;
\r
65 private boolean combining;
\r
68 @SuppressWarnings("unused")
\r
71 /** StringBuffer to store data */
\r
72 private StringBuffer data;
\r
74 /** Locator object */
\r
75 @SuppressWarnings("unused")
\r
76 private Locator locator;
\r
78 @SuppressWarnings("rawtypes")
\r
79 public Hashtable getCharSets() {
\r
83 @SuppressWarnings("rawtypes")
\r
84 public Vector getCombiningChars() {
\r
85 return combiningchars;
\r
90 * Registers the SAX2 <code>Locator</code> object.
\r
94 * the {@link Locator}object
\r
96 public void setDocumentLocator(Locator locator) {
\r
97 this.locator = locator;
\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
116 else if (name.equals("isCombining"))
\r
117 data = new StringBuffer();
\r
121 public void characters(char[] ch, int start, int length) {
\r
122 if (data != null) {
\r
123 data.append(ch, start, length);
\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
138 marc = new char[1];
\r
139 marc[0] = (char) Integer.parseInt(marcstr, 16);
\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
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
151 } else if (name.equals("code")) {
\r
153 combiningchars.add(ucs);
\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
161 Hashtable h = (Hashtable) charset.get(ucs);
\r
162 h.put(isocode, marc);
\r
164 } else if (name.equals("isCombining")) {
\r
165 if (data.toString().equals("true"))
\r
171 @SuppressWarnings({ "rawtypes", "unused" })
\r
172 public static void main(String[] args) {
\r
173 Hashtable charsets = null;
\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
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
187 ReverseCodeTableHandler saxUms = new ReverseCodeTableHandler();
\r
189 rdr.setContentHandler(saxUms);
\r
191 } catch (Exception exc) {
\r
192 exc.printStackTrace(System.out);
\r
193 System.err.println("Exception: " + exc);
\r