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.Locator;
\r
35 import org.xml.sax.SAXException;
\r
38 * Creates <code>Record</code> objects from SAX events and pushes each item
\r
39 * onto the top of the <code>RecordStack</code>.
\r
41 * @author Bas Peters
\r
42 * @version $Revision: 1.1 $
\r
44 public class MarcXmlHandler implements ContentHandler {
\r
46 private RecordStack queue;
\r
48 //private InputSource input;
\r
50 private StringBuffer sb;
\r
52 private Subfield subfield;
\r
54 private ControlField controlField;
\r
56 private DataField dataField;
\r
58 private Record record;
\r
62 /** Constants representing each valid tag type */
\r
63 private static final int COLLECTION_ID = 1;
\r
65 private static final int LEADER_ID = 2;
\r
67 private static final int RECORD_ID = 3;
\r
69 private static final int CONTROLFIELD_ID = 4;
\r
71 private static final int DATAFIELD_ID = 5;
\r
73 private static final int SUBFIELD_ID = 6;
\r
75 /** The tag attribute name string */
\r
76 private static final String TAG_ATTR = "tag";
\r
78 /** The code attribute name string */
\r
79 private static final String CODE_ATTR = "code";
\r
81 /** The first indicator attribute name string */
\r
82 private static final String IND_1_ATTR = "ind1";
\r
84 /** The second indicator attribute name string */
\r
85 private static final String IND_2_ATTR = "ind2";
\r
87 /** Hashset for mapping of element strings to constants (Integer) */
\r
88 private static final HashMap<String, Integer> elementMap;
\r
90 private MarcFactory factory = null;
\r
93 elementMap = new HashMap<String, Integer>();
\r
94 elementMap.put("collection", new Integer(COLLECTION_ID));
\r
95 elementMap.put("leader", new Integer(LEADER_ID));
\r
96 elementMap.put("record", new Integer(RECORD_ID));
\r
97 elementMap.put("controlfield", new Integer(CONTROLFIELD_ID));
\r
98 elementMap.put("datafield", new Integer(DATAFIELD_ID));
\r
99 elementMap.put("subfield", new Integer(SUBFIELD_ID));
\r
103 * Default constructor.
\r
107 public MarcXmlHandler(RecordStack queue) {
\r
108 this.queue = queue;
\r
109 factory = MarcFactory.newInstance();
\r
112 public void startDocument() throws SAXException {
\r
115 public void startElement(String uri, String name, String qName,
\r
116 Attributes atts) throws SAXException {
\r
118 String realname = (name.length() == 0) ? qName : name;
\r
119 Integer elementType = (Integer) elementMap.get(realname);
\r
121 if (elementType == null)
\r
124 switch (elementType.intValue()) {
\r
125 case COLLECTION_ID:
\r
128 record = factory.newRecord();
\r
131 sb = new StringBuffer();
\r
133 case CONTROLFIELD_ID:
\r
134 sb = new StringBuffer();
\r
135 tag = atts.getValue(TAG_ATTR);
\r
136 controlField = factory.newControlField(tag);
\r
139 tag = atts.getValue(TAG_ATTR);
\r
140 String ind1 = atts.getValue(IND_1_ATTR);
\r
141 String ind2 = atts.getValue(IND_2_ATTR);
\r
142 dataField = factory.newDataField(tag, ind1.charAt(0), ind2.charAt(0));
\r
145 sb = new StringBuffer();
\r
146 String code = atts.getValue(CODE_ATTR);
\r
147 subfield = factory.newSubfield(code.charAt(0));
\r
151 public void characters(char[] ch, int start, int length) throws SAXException {
\r
153 sb.append(ch, start, length);
\r
156 public void endElement(String uri, String name, String qName)
\r
157 throws SAXException {
\r
159 String realname = (name.length() == 0) ? qName : name;
\r
160 Integer elementType = (Integer) elementMap.get(realname);
\r
162 if (elementType == null)
\r
165 switch (elementType.intValue()) {
\r
166 case COLLECTION_ID:
\r
169 queue.push(record);
\r
172 Leader leader = factory.newLeader(sb.toString());
\r
173 record.setLeader(leader);
\r
175 case CONTROLFIELD_ID:
\r
176 controlField.setData(sb.toString());
\r
177 record.addVariableField(controlField);
\r
180 record.addVariableField(dataField);
\r
183 subfield.setData(sb.toString());
\r
184 dataField.addSubfield(subfield);
\r
189 public void endDocument() throws SAXException {
\r
193 public void ignorableWhitespace(char[] data, int offset, int length)
\r
194 throws SAXException {
\r
198 public void endPrefixMapping(String prefix) throws SAXException {
\r
201 public void skippedEntity(String name) throws SAXException {
\r
205 public void setDocumentLocator(Locator locator) {
\r
209 public void processingInstruction(String target, String data)
\r
210 throws SAXException {
\r
214 public void startPrefixMapping(String prefix, String uri) throws SAXException {
\r