Add prefix support. Need switch to on/off them
authorDennis Schafroth <dennis@indexdata.com>
Wed, 1 Feb 2012 15:37:33 +0000 (16:37 +0100)
committerDennis Schafroth <dennis@indexdata.com>
Wed, 1 Feb 2012 15:37:33 +0000 (16:37 +0100)
src/org/marc4j/TurboMarcXmlWriter.java

index 789410d..d5ec784 100644 (file)
@@ -196,17 +196,25 @@ import com.ibm.icu.text.Normalizer;
  */\r
 public class TurboMarcXmlWriter implements MarcWriter {\r
 \r
+    protected static final String prefix = "tmarc:";\r
+    \r
     protected static final String CONTROL_FIELD = "c";\r
+    protected static final String Q_CONTROL_FIELD = prefix + "c";\r
 \r
     protected static final String DATA_FIELD = "d";\r
+    protected static final String Q_DATA_FIELD = prefix + "d";\r
 \r
     protected static final String SUBFIELD = "s";\r
+    protected static final String Q_SUBFIELD = prefix + "s";\r
 \r
     protected static final String COLLECTION = "c";\r
+    protected static final String Q_COLLECTION = prefix + "c";\r
 \r
     protected static final String RECORD = "r";\r
+    protected static final String Q_RECORD = prefix + "r";\r
 \r
     protected static final String LEADER = "l";\r
+    protected static final String Q_LEADER = prefix + "l";\r
 \r
     private boolean indent = false;\r
 \r
@@ -322,7 +330,8 @@ public class TurboMarcXmlWriter 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
@@ -405,11 +414,11 @@ public class TurboMarcXmlWriter 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("tmarc", Constants.MARCXML_NS_URI);\r
             // add namespace declaration using attribute - need better solution\r
-            atts.addAttribute(Constants.TURBO_MARCXML_NS_URI, "xmlns", "xmlns",\r
+            atts.addAttribute(Constants.TURBO_MARCXML_NS_URI, "xmlns", "xmlns:tmarc",\r
                               "CDATA", Constants.TURBO_MARCXML_NS_URI);            \r
-            handler.startElement(Constants.TURBO_MARCXML_NS_URI, COLLECTION, COLLECTION, atts);\r
+            handler.startElement(Constants.TURBO_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 +437,7 @@ public class TurboMarcXmlWriter implements MarcWriter {
 \r
             handler\r
                     .endElement(Constants.TURBO_MARCXML_NS_URI, COLLECTION,\r
-                            COLLECTION);\r
+                       Q_COLLECTION);\r
             handler.endPrefixMapping("");\r
             handler.endDocument();\r
         } catch (SAXException e) {\r
@@ -476,16 +485,16 @@ public class TurboMarcXmlWriter implements MarcWriter {
         if (indent)\r
             handler.ignorableWhitespace("\n  ".toCharArray(), 0, 3);\r
 \r
-        handler.startElement(Constants.TURBO_MARCXML_NS_URI, RECORD, RECORD, atts);\r
+        handler.startElement(Constants.TURBO_MARCXML_NS_URI, RECORD, Q_RECORD, atts);\r
 \r
         if (indent)\r
             handler.ignorableWhitespace("\n    ".toCharArray(), 0, 5);\r
 \r
-        handler.startElement(Constants.TURBO_MARCXML_NS_URI, LEADER, LEADER, atts);\r
+        handler.startElement(Constants.TURBO_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.TURBO_MARCXML_NS_URI, LEADER, LEADER);\r
+        handler.endElement(Constants.TURBO_MARCXML_NS_URI, LEADER, Q_LEADER);\r
 \r
         Iterator<ControlField> ci = record.getControlFields().iterator();\r
         while (ci.hasNext()) {\r
@@ -496,10 +505,11 @@ public class TurboMarcXmlWriter implements MarcWriter {
             if (indent)\r
                 handler.ignorableWhitespace("\n    ".toCharArray(), 0, 5);\r
             String elementName = CONTROL_FIELD + field.getTag();\r
-            handler.startElement(Constants.TURBO_MARCXML_NS_URI, elementName, elementName, atts);\r
+            String qElementName = prefix + elementName;\r
+            handler.startElement(Constants.TURBO_MARCXML_NS_URI, elementName,  qElementName, atts);\r
             temp = getDataElement(field.getData());\r
             handler.characters(temp, 0, temp.length);\r
-            handler.endElement(Constants.TURBO_MARCXML_NS_URI, elementName, elementName);\r
+            handler.endElement(Constants.TURBO_MARCXML_NS_URI, elementName, qElementName);\r
         }\r
 \r
         Iterator<DataField> di = record.getDataFields().iterator();\r
@@ -515,20 +525,26 @@ public class TurboMarcXmlWriter implements MarcWriter {
             if (indent)\r
                 handler.ignorableWhitespace("\n    ".toCharArray(), 0, 5);\r
             StringBuffer elementName = new StringBuffer(DATA_FIELD);\r
+            StringBuffer qElementName = new StringBuffer(prefix);\r
             elementName.append(field.getTag());\r
-            handler.startElement(Constants.TURBO_MARCXML_NS_URI, elementName.toString(), elementName.toString(), atts);\r
+            qElementName.append(elementName);\r
+            handler.startElement(Constants.TURBO_MARCXML_NS_URI, elementName.toString(), \r
+                       qElementName.toString(), atts);\r
 \r
             Iterator<Subfield> si = field.getSubfields().iterator();\r
             while (si.hasNext()) {\r
                 Subfield subfield = (Subfield) si.next();\r
                 StringBuffer subfieldName = new StringBuffer(SUBFIELD); \r
-  \r
+                StringBuffer qSubfieldName = new StringBuffer(prefix);\r
+                qSubfieldName.append(subfieldName);\r
+                \r
                 char code = subfield.getCode(); \r
                 // if [a-zA-Z0-9] append to  elementName, otherwise use a attribute\r
                 if (code >= '0' && code <= '9' ||\r
                     code >= 'a' && code <= 'z' ||\r
                     code >= 'A' && code <= 'Z') {\r
                   subfieldName.append(code);\r
+                  qSubfieldName.append(code);\r
                 }\r
                 else {\r
                   atts = new AttributesImpl();\r
@@ -539,25 +555,26 @@ public class TurboMarcXmlWriter implements MarcWriter {
                     handler.ignorableWhitespace("\n      ".toCharArray(), 0, 7);\r
 \r
                 handler.startElement(Constants.TURBO_MARCXML_NS_URI, subfieldName.toString(),\r
-                    subfieldName.toString(), atts);\r
+                    qSubfieldName.toString(), atts);\r
                 temp = getDataElement(subfield.getData());\r
                 handler.characters(temp, 0, temp.length);\r
                 handler\r
                         .endElement(Constants.TURBO_MARCXML_NS_URI, subfieldName.toString(),\r
-                            subfieldName.toString());\r
+                            qSubfieldName.toString());\r
             }\r
 \r
             if (indent)\r
                 handler.ignorableWhitespace("\n    ".toCharArray(), 0, 5);\r
 \r
             handler\r
-                    .endElement(Constants.TURBO_MARCXML_NS_URI, elementName.toString(), elementName.toString());\r
+                    .endElement(Constants.TURBO_MARCXML_NS_URI, \r
+                       elementName.toString(), qElementName.toString());\r
         }\r
 \r
         if (indent)\r
             handler.ignorableWhitespace("\n  ".toCharArray(), 0, 3);\r
 \r
-        handler.endElement(Constants.TURBO_MARCXML_NS_URI, RECORD, RECORD);\r
+        handler.endElement(Constants.TURBO_MARCXML_NS_URI, RECORD, Q_RECORD);\r
     }\r
 \r
     protected char[] getDataElement(String data) {\r