1 // $Id: CodeTableHandler.java,v 1.2 2008/09/26 21:17:42 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.util.HashMap;
\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>CodeTableHandler</code> is a SAX2 <code>ContentHandler</code> that
\r
41 * builds a data structure to facilitate AnselToUnicode character conversion.
\r
43 * @author Corey Keith
\r
44 * @version $Revision: 1.2 $
\r
46 * @see DefaultHandler
\r
48 public class CodeTableHandler extends DefaultHandler {
\r
50 private HashMap sets;
\r
52 private HashMap charset;
\r
54 private HashMap combiningchars;
\r
56 /** Data element identifier */
\r
57 private Integer isocode;
\r
59 private Integer marc;
\r
61 private Character ucs;
\r
63 private boolean useAlt = false;
\r
65 private boolean iscombining;
\r
67 private Vector combining;
\r
72 /** StringBuffer to store data */
\r
73 private StringBuffer data;
\r
75 /** Locator object */
\r
76 private Locator locator;
\r
78 public HashMap getCharSets() {
\r
82 public HashMap getCombiningChars() {
\r
83 return combiningchars;
\r
88 * Registers the SAX2 <code>Locator</code> object.
\r
92 * the {@link Locator}object
\r
94 public void setDocumentLocator(Locator locator) {
\r
95 this.locator = locator;
\r
98 public void startElement(String uri, String name, String qName,
\r
99 Attributes atts) throws SAXParseException {
\r
100 if (name.equals("characterSet")) {
\r
101 charset = new HashMap();
\r
102 isocode = Integer.valueOf(atts.getValue("ISOcode"), 16);
\r
103 combining = new Vector();
\r
104 } else if (name.equals("marc"))
\r
105 data = new StringBuffer();
\r
106 else if (name.equals("codeTables")) {
\r
107 sets = new HashMap();
\r
108 combiningchars = new HashMap();
\r
109 } else if (name.equals("ucs"))
\r
110 data = new StringBuffer();
\r
111 else if (name.equals("alt"))
\r
112 data = new StringBuffer();
\r
113 else if (name.equals("isCombining"))
\r
114 data = new StringBuffer();
\r
115 else if (name.equals("code"))
\r
116 iscombining = false;
\r
119 public void characters(char[] ch, int start, int length) {
\r
120 if (data != null) {
\r
121 data.append(ch, start, length);
\r
125 public void endElement(String uri, String name, String qName)
\r
126 throws SAXParseException {
\r
127 if (name.equals("characterSet")) {
\r
128 sets.put(isocode, charset);
\r
129 combiningchars.put(isocode, combining);
\r
132 } else if (name.equals("marc")) {
\r
133 marc = Integer.valueOf(data.toString(), 16);
\r
134 } else if (name.equals("ucs")) {
\r
135 if (data.length() > 0)
\r
136 ucs = new Character((char) Integer.parseInt(data.toString(), 16));
\r
139 } else if (name.equals("alt")) {
\r
140 if (useAlt && data.length() > 0) {
\r
141 ucs = new Character((char) Integer.parseInt(data.toString(), 16));
\r
144 } else if (name.equals("code")) {
\r
146 combining.add(marc);
\r
148 charset.put(marc, ucs);
\r
149 } else if (name.equals("isCombining")) {
\r
150 if (data.toString().equals("true"))
\r
151 iscombining = true;
\r
157 public static void main(String[] args) {
\r
158 HashMap charsets = null;
\r
162 SAXParserFactory factory = SAXParserFactory.newInstance();
\r
163 factory.setNamespaceAware(true);
\r
164 factory.setValidating(false);
\r
165 SAXParser saxParser = factory.newSAXParser();
\r
166 XMLReader rdr = saxParser.getXMLReader();
\r
168 File file = new File(
\r
169 "C:\\Documents and Settings\\ckeith\\Desktop\\Projects\\Code Tables\\codetables.xml");
\r
170 InputSource src = new InputSource(new FileInputStream(file));
\r
172 CodeTableHandler saxUms = new CodeTableHandler();
\r
174 rdr.setContentHandler(saxUms);
\r
177 charsets = saxUms.getCharSets();
\r
179 //System.out.println( charsets.toString() );
\r
180 System.out.println(saxUms.getCombiningChars());
\r
182 } catch (Exception exc) {
\r
183 exc.printStackTrace(System.out);
\r
184 // System.err.println( "Exception: " + exc );
\r