1 package org.kuali.ole.batch.marc;
2
3 import org.apache.commons.lang3.StringUtils;
4 import org.apache.log4j.Logger;
5 import org.marc4j.ErrorHandler;
6 import org.marc4j.MarcXmlHandler;
7 import org.marc4j.RecordStack;
8 import org.xml.sax.Attributes;
9 import org.xml.sax.SAXException;
10
11 import java.util.HashMap;
12 import java.util.Map;
13
14
15
16
17
18
19
20
21 public class OLEMarcXmlHandler extends MarcXmlHandler {
22
23 private final static String DATAFIELD = "datafield";
24 private final static String SUBFIELD = "subfield";
25 private static final String TAG_ATTR = "tag";
26 private static final String CONTROLFIELD = "controlfield";
27 private static final String LEADER = "leader";
28 private static final String OO1 = "001";
29 private boolean isRecordId = false;
30 private String recordId;
31 private String dataField;
32 private String subField;
33 private ErrorHandler errors;
34 private String qName;
35 private Attributes atts;
36 private boolean elementHasErrors=false;
37 private String errMsg;
38 private String value;
39 Logger LOG = Logger.getLogger(OLEMarcXmlHandler.class);
40
41
42
43
44 private static final String CODE_ATTR = "code";
45
46 private RecordStack queue;
47
48 public OLEMarcXmlHandler(RecordStack queue, ErrorHandler errors) {
49 super(queue);
50 this.queue = queue;
51 this.errors = errors;
52 }
53
54 public void startElement(String uri, String name, String qName,
55 Attributes atts) throws SAXException {
56 try {
57 this.qName=qName;
58 this.atts = atts;
59 elementHasErrors=false;
60 if (qName.equalsIgnoreCase(CONTROLFIELD) && atts.getValue(TAG_ATTR).equalsIgnoreCase(OO1)) {
61 isRecordId = true;
62 } else {
63 isRecordId = false;
64 }
65 if (qName.equalsIgnoreCase(DATAFIELD)) {
66 dataField = atts.getValue(TAG_ATTR);
67 } else if (qName.equalsIgnoreCase(SUBFIELD)) {
68 subField = atts.getValue(CODE_ATTR);
69 }
70 super.startElement(uri, name, qName, atts);
71 } catch (Exception e) {
72 elementHasErrors=true;
73 errMsg=e.getMessage();
74 }
75 }
76
77 public void endElement(String uri, String name, String qName)
78 throws SAXException {
79 try {
80 super.endElement(uri, name, qName);
81 } catch (Exception e) {
82 setError(value);
83 }
84
85 }
86
87 public void characters(char[] ch, int start, int length) throws SAXException {
88 super.characters(ch, start, length);
89 if (isRecordId) {
90 recordId = new StringBuffer().append(ch, start, length).toString().trim();
91 isRecordId = false;
92 }
93 value = new StringBuffer().append(ch, start, length).toString().trim();
94 if(elementHasErrors)
95 setError(value);
96 }
97
98 public ErrorHandler getErrors() {
99 return errors;
100 }
101
102 private void setError(String value){
103 OLEMarcErrorHandler errorHandler = (OLEMarcErrorHandler)errors;
104 if(qName.equalsIgnoreCase(LEADER)){
105 errorHandler.getErrorMap().put(qName,value);
106 }
107 else if(qName.equalsIgnoreCase(CONTROLFIELD) || qName.equalsIgnoreCase(DATAFIELD)){
108 errorHandler.getErrorMap().put(qName+" "+TAG_ATTR+"="+atts.getValue(TAG_ATTR),value);
109 }
110 else if(qName.equalsIgnoreCase(SUBFIELD)){
111 errorHandler.getErrorMap().put(qName+" "+CODE_ATTR+"="+atts.getValue(CODE_ATTR),value);
112 }
113 LOG.info("Error while coverting marcxml to xml for element:: "+errorHandler.getErrorMap().get(qName));
114 }
115
116 public void endDocument() throws SAXException {
117 super.endDocument();
118 OLEMarcErrorHandler errorHandler = (OLEMarcErrorHandler)errors;
119 if(!errorHandler.getErrorMap().isEmpty()){
120 errors.addError(recordId,dataField,subField,ErrorHandler.MAJOR_ERROR,"Error occurred while converting marcxml to mrc");
121 }
122 }
123 }