From: Dennis Schafroth Date: Wed, 1 Feb 2012 15:36:55 +0000 (+0100) Subject: Add prefixes. Need switch to on/off them X-Git-Tag: v2.5~23^2~4 X-Git-Url: http://sru.miketaylor.org.uk/cgi-bin?a=commitdiff_plain;h=fa5b153a201efe044d9823cbea25d43123500083;p=marc4j.git Add prefixes. Need switch to on/off them --- diff --git a/src/org/marc4j/MarcXmlWriter.java b/src/org/marc4j/MarcXmlWriter.java index 7e52fc2..43066f3 100644 --- a/src/org/marc4j/MarcXmlWriter.java +++ b/src/org/marc4j/MarcXmlWriter.java @@ -196,17 +196,25 @@ import com.ibm.icu.text.Normalizer; */ public class MarcXmlWriter implements MarcWriter { + protected static final String prefix = "marc:"; protected static final String CONTROL_FIELD = "controlfield"; + protected static final String Q_CONTROL_FIELD = prefix + "controlfield"; protected static final String DATA_FIELD = "datafield"; + protected static final String Q_DATA_FIELD = prefix + "datafield"; + protected static final String SUBFIELD = "subfield"; + protected static final String Q_SUBFIELD = prefix + "subfield"; protected static final String COLLECTION = "collection"; + protected static final String Q_COLLECTION = prefix + "collection"; protected static final String RECORD = "record"; + protected static final String Q_RECORD = prefix + "record"; protected static final String LEADER = "leader"; + protected static final String Q_LEADER = prefix + "leader"; private boolean indent = false; @@ -322,7 +330,8 @@ public class MarcXmlWriter implements MarcWriter { public void close() { writeEndDocument(); try { - writer.close(); + if (writer != null) + writer.close(); } catch (IOException e) { throw new MarcException(e.getMessage(), e); } @@ -382,6 +391,7 @@ public class MarcXmlWriter implements MarcWriter { "SAXTransformerFactory is not supported"); SAXTransformerFactory saxFactory = (SAXTransformerFactory) factory; + //saxFactory.setFeature("http://xml.org/sax/features/namespaces", false); if (stylesheet == null) handler = saxFactory.newTransformerHandler(); else @@ -405,11 +415,11 @@ public class MarcXmlWriter implements MarcWriter { AttributesImpl atts = new AttributesImpl(); handler.startDocument(); // The next line duplicates the namespace declaration for Marc XML - // handler.startPrefixMapping("", Constants.MARCXML_NS_URI); + handler.startPrefixMapping("marc", Constants.MARCXML_NS_URI); // add namespace declaration using attribute - need better solution - atts.addAttribute(Constants.MARCXML_NS_URI, "xmlns", "xmlns", + atts.addAttribute(Constants.MARCXML_NS_URI, "xmlns", "xmlns:marc", "CDATA", Constants.MARCXML_NS_URI); - handler.startElement(Constants.MARCXML_NS_URI, COLLECTION, COLLECTION, atts); + handler.startElement(Constants.MARCXML_NS_URI, COLLECTION, Q_COLLECTION, atts); } catch (SAXException e) { throw new MarcException( "SAX error occured while writing start document", e); @@ -428,7 +438,7 @@ public class MarcXmlWriter implements MarcWriter { handler .endElement(Constants.MARCXML_NS_URI, COLLECTION, - COLLECTION); + Q_COLLECTION); handler.endPrefixMapping(""); handler.endDocument(); } catch (SAXException e) { @@ -476,16 +486,16 @@ public class MarcXmlWriter implements MarcWriter { if (indent) handler.ignorableWhitespace("\n ".toCharArray(), 0, 3); - handler.startElement(Constants.MARCXML_NS_URI, RECORD, RECORD, atts); + handler.startElement(Constants.MARCXML_NS_URI, RECORD, Q_RECORD, atts); if (indent) handler.ignorableWhitespace("\n ".toCharArray(), 0, 5); - handler.startElement(Constants.MARCXML_NS_URI, LEADER, LEADER, atts); + handler.startElement(Constants.MARCXML_NS_URI, LEADER, Q_LEADER, atts); Leader leader = record.getLeader(); temp = leader.toString().toCharArray(); handler.characters(temp, 0, temp.length); - handler.endElement(Constants.MARCXML_NS_URI, LEADER, LEADER); + handler.endElement(Constants.MARCXML_NS_URI, LEADER, Q_LEADER); Iterator ci = record.getControlFields().iterator(); while (ci.hasNext()) { @@ -497,11 +507,11 @@ public class MarcXmlWriter implements MarcWriter { handler.ignorableWhitespace("\n ".toCharArray(), 0, 5); handler.startElement(Constants.MARCXML_NS_URI, CONTROL_FIELD, - CONTROL_FIELD, atts); + Q_CONTROL_FIELD, atts); temp = getDataElement(field.getData()); handler.characters(temp, 0, temp.length); handler.endElement(Constants.MARCXML_NS_URI, CONTROL_FIELD, - CONTROL_FIELD); + Q_CONTROL_FIELD); } Iterator di = record.getDataFields().iterator(); @@ -518,7 +528,7 @@ public class MarcXmlWriter implements MarcWriter { handler.ignorableWhitespace("\n ".toCharArray(), 0, 5); handler.startElement(Constants.MARCXML_NS_URI, DATA_FIELD, - DATA_FIELD, atts); + Q_DATA_FIELD, atts); Iterator si = field.getSubfields().iterator(); while (si.hasNext()) { Subfield subfield = (Subfield) si.next(); @@ -530,12 +540,12 @@ public class MarcXmlWriter implements MarcWriter { handler.ignorableWhitespace("\n ".toCharArray(), 0, 7); handler.startElement(Constants.MARCXML_NS_URI, SUBFIELD, - SUBFIELD, atts); + Q_SUBFIELD, atts); temp = getDataElement(subfield.getData()); handler.characters(temp, 0, temp.length); handler .endElement(Constants.MARCXML_NS_URI, SUBFIELD, - SUBFIELD); + Q_SUBFIELD); } if (indent) @@ -543,13 +553,13 @@ public class MarcXmlWriter implements MarcWriter { handler .endElement(Constants.MARCXML_NS_URI, DATA_FIELD, - DATA_FIELD); + Q_DATA_FIELD); } if (indent) handler.ignorableWhitespace("\n ".toCharArray(), 0, 3); - handler.endElement(Constants.MARCXML_NS_URI, RECORD, RECORD); + handler.endElement(Constants.MARCXML_NS_URI, RECORD, Q_RECORD); } protected char[] getDataElement(String data) {