*/\r
public class MarcXmlWriter implements MarcWriter {\r
\r
+ protected static final String prefix = "marc:";\r
protected static final String CONTROL_FIELD = "controlfield";\r
+ protected static final String Q_CONTROL_FIELD = prefix + "controlfield";\r
\r
protected static final String DATA_FIELD = "datafield";\r
+ protected static final String Q_DATA_FIELD = prefix + "datafield";\r
+ \r
\r
protected static final String SUBFIELD = "subfield";\r
+ protected static final String Q_SUBFIELD = prefix + "subfield";\r
\r
protected static final String COLLECTION = "collection";\r
+ protected static final String Q_COLLECTION = prefix + "collection";\r
\r
protected static final String RECORD = "record";\r
+ protected static final String Q_RECORD = prefix + "record";\r
\r
protected static final String LEADER = "leader";\r
+ protected static final String Q_LEADER = prefix + "leader";\r
\r
private boolean indent = false;\r
\r
public void close() {\r
writeEndDocument();\r
try {\r
- writer.close();\r
+ if (writer != null)\r
+ writer.close();\r
} catch (IOException e) {\r
throw new MarcException(e.getMessage(), e);\r
}\r
"SAXTransformerFactory is not supported");\r
\r
SAXTransformerFactory saxFactory = (SAXTransformerFactory) factory;\r
+ //saxFactory.setFeature("http://xml.org/sax/features/namespaces", false);\r
if (stylesheet == null)\r
handler = saxFactory.newTransformerHandler();\r
else\r
AttributesImpl atts = new AttributesImpl();\r
handler.startDocument();\r
// The next line duplicates the namespace declaration for Marc XML\r
- // handler.startPrefixMapping("", Constants.MARCXML_NS_URI);\r
+ handler.startPrefixMapping("marc", Constants.MARCXML_NS_URI);\r
// add namespace declaration using attribute - need better solution\r
- atts.addAttribute(Constants.MARCXML_NS_URI, "xmlns", "xmlns",\r
+ atts.addAttribute(Constants.MARCXML_NS_URI, "xmlns", "xmlns:marc",\r
"CDATA", Constants.MARCXML_NS_URI); \r
- handler.startElement(Constants.MARCXML_NS_URI, COLLECTION, COLLECTION, atts);\r
+ handler.startElement(Constants.MARCXML_NS_URI, COLLECTION, Q_COLLECTION, atts);\r
} catch (SAXException e) {\r
throw new MarcException(\r
"SAX error occured while writing start document", e);\r
\r
handler\r
.endElement(Constants.MARCXML_NS_URI, COLLECTION,\r
- COLLECTION);\r
+ Q_COLLECTION);\r
handler.endPrefixMapping("");\r
handler.endDocument();\r
} catch (SAXException e) {\r
if (indent)\r
handler.ignorableWhitespace("\n ".toCharArray(), 0, 3);\r
\r
- handler.startElement(Constants.MARCXML_NS_URI, RECORD, RECORD, atts);\r
+ handler.startElement(Constants.MARCXML_NS_URI, RECORD, Q_RECORD, atts);\r
\r
if (indent)\r
handler.ignorableWhitespace("\n ".toCharArray(), 0, 5);\r
\r
- handler.startElement(Constants.MARCXML_NS_URI, LEADER, LEADER, atts);\r
+ handler.startElement(Constants.MARCXML_NS_URI, LEADER, Q_LEADER, atts);\r
Leader leader = record.getLeader();\r
temp = leader.toString().toCharArray();\r
handler.characters(temp, 0, temp.length);\r
- handler.endElement(Constants.MARCXML_NS_URI, LEADER, LEADER);\r
+ handler.endElement(Constants.MARCXML_NS_URI, LEADER, Q_LEADER);\r
\r
Iterator<ControlField> ci = record.getControlFields().iterator();\r
while (ci.hasNext()) {\r
handler.ignorableWhitespace("\n ".toCharArray(), 0, 5);\r
\r
handler.startElement(Constants.MARCXML_NS_URI, CONTROL_FIELD,\r
- CONTROL_FIELD, atts);\r
+ Q_CONTROL_FIELD, atts);\r
temp = getDataElement(field.getData());\r
handler.characters(temp, 0, temp.length);\r
handler.endElement(Constants.MARCXML_NS_URI, CONTROL_FIELD,\r
- CONTROL_FIELD);\r
+ Q_CONTROL_FIELD);\r
}\r
\r
Iterator<DataField> di = record.getDataFields().iterator();\r
handler.ignorableWhitespace("\n ".toCharArray(), 0, 5);\r
\r
handler.startElement(Constants.MARCXML_NS_URI, DATA_FIELD,\r
- DATA_FIELD, atts);\r
+ Q_DATA_FIELD, atts);\r
Iterator<Subfield> si = field.getSubfields().iterator();\r
while (si.hasNext()) {\r
Subfield subfield = (Subfield) si.next();\r
handler.ignorableWhitespace("\n ".toCharArray(), 0, 7);\r
\r
handler.startElement(Constants.MARCXML_NS_URI, SUBFIELD,\r
- SUBFIELD, atts);\r
+ Q_SUBFIELD, atts);\r
temp = getDataElement(subfield.getData());\r
handler.characters(temp, 0, temp.length);\r
handler\r
.endElement(Constants.MARCXML_NS_URI, SUBFIELD,\r
- SUBFIELD);\r
+ Q_SUBFIELD);\r
}\r
\r
if (indent)\r
\r
handler\r
.endElement(Constants.MARCXML_NS_URI, DATA_FIELD,\r
- DATA_FIELD);\r
+ Q_DATA_FIELD);\r
}\r
\r
if (indent)\r
handler.ignorableWhitespace("\n ".toCharArray(), 0, 3);\r
\r
- handler.endElement(Constants.MARCXML_NS_URI, RECORD, RECORD);\r
+ handler.endElement(Constants.MARCXML_NS_URI, RECORD, Q_RECORD);\r
}\r
\r
protected char[] getDataElement(String data) {\r