X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Forg%2Fmarc4j%2FMarcXmlWriter.java;h=43066f31e3374ad0455b174cf723bd470527f5eb;hb=fa5b153a201efe044d9823cbea25d43123500083;hp=a28981ad57afd24cb702416e320aead4f2766206;hpb=0c435d66d7ffac54d3abbf9d949c5cb6f3f9baa4;p=marc4j.git diff --git a/src/org/marc4j/MarcXmlWriter.java b/src/org/marc4j/MarcXmlWriter.java index a28981a..43066f3 100644 --- a/src/org/marc4j/MarcXmlWriter.java +++ b/src/org/marc4j/MarcXmlWriter.java @@ -94,8 +94,8 @@ import com.ibm.icu.text.Normalizer; *

* In addition you can perform Unicode normalization. This is for example not * done by the MARC-8 to UCS/Unicode converter. With Unicode normalization text - * is transformed into the canonical composed form. For example "a´bc" - * is normalized to "ábc". To perform normalization set Unicode + * is transformed into the canonical composed form. For example "a�bc" + * is normalized to "�bc". To perform normalization set Unicode * normalization to true: *

* @@ -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; @@ -218,7 +226,7 @@ public class MarcXmlWriter implements MarcWriter { /** * Character encoding. Default is UTF-8. */ - private String encoding = "UTF8"; + //private String encoding = "UTF8"; private CharConverter converter = null; @@ -273,7 +281,7 @@ public class MarcXmlWriter implements MarcWriter { setIndent(indent); writer = new OutputStreamWriter(out, encoding); writer = new BufferedWriter(writer); - this.encoding = encoding; + // this.encoding = encoding; setHandler(new StreamResult(writer), null); } catch (UnsupportedEncodingException e) { throw new MarcException(e.getMessage(), e); @@ -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,20 +486,20 @@ 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 i = record.getControlFields().iterator(); - while (i.hasNext()) { - ControlField field = (ControlField) i.next(); + Iterator ci = record.getControlFields().iterator(); + while (ci.hasNext()) { + ControlField field = (ControlField) ci.next(); atts = new AttributesImpl(); atts.addAttribute("", "tag", "tag", "CDATA", field.getTag()); @@ -497,16 +507,16 @@ 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); } - i = record.getDataFields().iterator(); - while (i.hasNext()) { - DataField field = (DataField) i.next(); + Iterator di = record.getDataFields().iterator(); + while (di.hasNext()) { + DataField field = di.next(); atts = new AttributesImpl(); atts.addAttribute("", "tag", "tag", "CDATA", field.getTag()); atts.addAttribute("", "ind1", "ind1", "CDATA", String.valueOf(field @@ -518,10 +528,10 @@ public class MarcXmlWriter implements MarcWriter { handler.ignorableWhitespace("\n ".toCharArray(), 0, 5); handler.startElement(Constants.MARCXML_NS_URI, DATA_FIELD, - DATA_FIELD, atts); - Iterator j = field.getSubfields().iterator(); - while (j.hasNext()) { - Subfield subfield = (Subfield) j.next(); + Q_DATA_FIELD, atts); + Iterator si = field.getSubfields().iterator(); + while (si.hasNext()) { + Subfield subfield = (Subfield) si.next(); atts = new AttributesImpl(); atts.addAttribute("", "code", "code", "CDATA", String .valueOf(subfield.getCode())); @@ -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) {