1 // $Id: MarcXmlHandler.java,v 1.1 2005/05/04 10:06:46 bpeters Exp $
\r
3 * Copyright (C) 2004 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
24 import java.util.HashMap;
\r
26 import org.marc4j.marc.ControlField;
\r
27 import org.marc4j.marc.DataField;
\r
28 import org.marc4j.marc.Leader;
\r
29 import org.marc4j.marc.MarcFactory;
\r
30 import org.marc4j.marc.Record;
\r
31 import org.marc4j.marc.Subfield;
\r
32 import org.xml.sax.Attributes;
\r
33 import org.xml.sax.ContentHandler;
\r
34 import org.xml.sax.InputSource;
\r
35 import org.xml.sax.Locator;
\r
36 import org.xml.sax.SAXException;
\r
39 * Creates <code>Record</code> objects from SAX events and pushes each item
\r
40 * onto the top of the <code>RecordStack</code>.
\r
42 * @author Bas Peters
\r
43 * @version $Revision: 1.1 $
\r
45 public class MarcXmlHandler implements ContentHandler {
\r
47 private RecordStack queue;
\r
49 private InputSource input;
\r
51 private StringBuffer sb;
\r
53 private Subfield subfield;
\r
55 private ControlField controlField;
\r
57 private DataField dataField;
\r
59 private Record record;
\r
63 /** Constants representing each valid tag type */
\r
64 private static final int COLLECTION_ID = 1;
\r
66 private static final int LEADER_ID = 2;
\r
68 private static final int RECORD_ID = 3;
\r
70 private static final int CONTROLFIELD_ID = 4;
\r
72 private static final int DATAFIELD_ID = 5;
\r
74 private static final int SUBFIELD_ID = 6;
\r
76 /** The tag attribute name string */
\r
77 private static final String TAG_ATTR = "tag";
\r
79 /** The code attribute name string */
\r
80 private static final String CODE_ATTR = "code";
\r
82 /** The first indicator attribute name string */
\r
83 private static final String IND_1_ATTR = "ind1";
\r
85 /** The second indicator attribute name string */
\r
86 private static final String IND_2_ATTR = "ind2";
\r
88 /** Hashset for mapping of element strings to constants (Integer) */
\r
89 private static final HashMap elementMap;
\r
91 private MarcFactory factory = null;
\r
94 elementMap = new HashMap();
\r
95 elementMap.put("collection", new Integer(COLLECTION_ID));
\r
96 elementMap.put("leader", new Integer(LEADER_ID));
\r
97 elementMap.put("record", new Integer(RECORD_ID));
\r
98 elementMap.put("controlfield", new Integer(CONTROLFIELD_ID));
\r
99 elementMap.put("datafield", new Integer(DATAFIELD_ID));
\r
100 elementMap.put("subfield", new Integer(SUBFIELD_ID));
\r
104 * Default constructor.
\r
108 public MarcXmlHandler(RecordStack queue) {
\r
109 this.queue = queue;
\r
110 factory = MarcFactory.newInstance();
\r
113 public void startDocument() throws SAXException {
\r
116 public void startElement(String uri, String name, String qName,
\r
117 Attributes atts) throws SAXException {
\r
119 String realname = (name.length() == 0) ? qName : name;
\r
120 Integer elementType = (Integer) elementMap.get(realname);
\r
122 if (elementType == null)
\r
125 switch (elementType.intValue()) {
\r
126 case COLLECTION_ID:
\r
129 record = factory.newRecord();
\r
132 sb = new StringBuffer();
\r
134 case CONTROLFIELD_ID:
\r
135 sb = new StringBuffer();
\r
136 tag = atts.getValue(TAG_ATTR);
\r
137 controlField = factory.newControlField(tag);
\r
140 tag = atts.getValue(TAG_ATTR);
\r
141 String ind1 = atts.getValue(IND_1_ATTR);
\r
142 String ind2 = atts.getValue(IND_2_ATTR);
\r
143 dataField = factory.newDataField(tag, ind1.charAt(0), ind2.charAt(0));
\r
146 sb = new StringBuffer();
\r
147 String code = atts.getValue(CODE_ATTR);
\r
148 subfield = factory.newSubfield(code.charAt(0));
\r
152 public void characters(char[] ch, int start, int length) throws SAXException {
\r
154 sb.append(ch, start, length);
\r
157 public void endElement(String uri, String name, String qName)
\r
158 throws SAXException {
\r
160 String realname = (name.length() == 0) ? qName : name;
\r
161 Integer elementType = (Integer) elementMap.get(realname);
\r
163 if (elementType == null)
\r
166 switch (elementType.intValue()) {
\r
167 case COLLECTION_ID:
\r
170 queue.push(record);
\r
173 Leader leader = factory.newLeader(sb.toString());
\r
174 record.setLeader(leader);
\r
176 case CONTROLFIELD_ID:
\r
177 controlField.setData(sb.toString());
\r
178 record.addVariableField(controlField);
\r
181 record.addVariableField(dataField);
\r
184 subfield.setData(sb.toString());
\r
185 dataField.addSubfield(subfield);
\r
190 public void endDocument() throws SAXException {
\r
194 public void ignorableWhitespace(char[] data, int offset, int length)
\r
195 throws SAXException {
\r
199 public void endPrefixMapping(String prefix) throws SAXException {
\r
202 public void skippedEntity(String name) throws SAXException {
\r
206 public void setDocumentLocator(Locator locator) {
\r
210 public void processingInstruction(String target, String data)
\r
211 throws SAXException {
\r
215 public void startPrefixMapping(String prefix, String uri) throws SAXException {
\r