Add prefixes. Need switch to on/off them
authorDennis Schafroth <dennis@indexdata.com>
Wed, 1 Feb 2012 15:36:55 +0000 (16:36 +0100)
committerDennis Schafroth <dennis@indexdata.com>
Wed, 1 Feb 2012 15:36:55 +0000 (16:36 +0100)
src/org/marc4j/MarcXmlWriter.java

index 7e52fc2..43066f3 100644 (file)
@@ -196,17 +196,25 @@ import com.ibm.icu.text.Normalizer;
  */\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
@@ -322,7 +330,8 @@ public class MarcXmlWriter implements MarcWriter {
     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
@@ -382,6 +391,7 @@ public class MarcXmlWriter implements MarcWriter {
                         "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
@@ -405,11 +415,11 @@ public class MarcXmlWriter implements MarcWriter {
             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
@@ -428,7 +438,7 @@ public class MarcXmlWriter implements MarcWriter {
 \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
@@ -476,16 +486,16 @@ public class MarcXmlWriter implements MarcWriter {
         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
@@ -497,11 +507,11 @@ public class MarcXmlWriter implements MarcWriter {
                 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
@@ -518,7 +528,7 @@ public class MarcXmlWriter implements MarcWriter {
                 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
@@ -530,12 +540,12 @@ public class MarcXmlWriter implements MarcWriter {
                     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
@@ -543,13 +553,13 @@ public class MarcXmlWriter implements MarcWriter {
 \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