From: Dennis Schafroth Date: Wed, 1 Feb 2012 15:37:33 +0000 (+0100) Subject: Add prefix support. Need switch to on/off them X-Git-Tag: v2.5~23^2~3 X-Git-Url: http://lists.indexdata.dk/?a=commitdiff_plain;h=52e9333d0e4033c5c85612f41771d73423380af1;p=marc4j.git Add prefix support. Need switch to on/off them --- diff --git a/src/org/marc4j/TurboMarcXmlWriter.java b/src/org/marc4j/TurboMarcXmlWriter.java index 789410d..d5ec784 100644 --- a/src/org/marc4j/TurboMarcXmlWriter.java +++ b/src/org/marc4j/TurboMarcXmlWriter.java @@ -196,17 +196,25 @@ import com.ibm.icu.text.Normalizer; */ public class TurboMarcXmlWriter implements MarcWriter { + protected static final String prefix = "tmarc:"; + protected static final String CONTROL_FIELD = "c"; + protected static final String Q_CONTROL_FIELD = prefix + "c"; protected static final String DATA_FIELD = "d"; + protected static final String Q_DATA_FIELD = prefix + "d"; protected static final String SUBFIELD = "s"; + protected static final String Q_SUBFIELD = prefix + "s"; protected static final String COLLECTION = "c"; + protected static final String Q_COLLECTION = prefix + "c"; protected static final String RECORD = "r"; + protected static final String Q_RECORD = prefix + "r"; protected static final String LEADER = "l"; + protected static final String Q_LEADER = prefix + "l"; private boolean indent = false; @@ -322,7 +330,8 @@ public class TurboMarcXmlWriter implements MarcWriter { public void close() { writeEndDocument(); try { - writer.close(); + if (writer != null) + writer.close(); } catch (IOException e) { throw new MarcException(e.getMessage(), e); } @@ -405,11 +414,11 @@ public class TurboMarcXmlWriter 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("tmarc", Constants.MARCXML_NS_URI); // add namespace declaration using attribute - need better solution - atts.addAttribute(Constants.TURBO_MARCXML_NS_URI, "xmlns", "xmlns", + atts.addAttribute(Constants.TURBO_MARCXML_NS_URI, "xmlns", "xmlns:tmarc", "CDATA", Constants.TURBO_MARCXML_NS_URI); - handler.startElement(Constants.TURBO_MARCXML_NS_URI, COLLECTION, COLLECTION, atts); + handler.startElement(Constants.TURBO_MARCXML_NS_URI, COLLECTION, Q_COLLECTION, atts); } catch (SAXException e) { throw new MarcException( "SAX error occured while writing start document", e); @@ -428,7 +437,7 @@ public class TurboMarcXmlWriter implements MarcWriter { handler .endElement(Constants.TURBO_MARCXML_NS_URI, COLLECTION, - COLLECTION); + Q_COLLECTION); handler.endPrefixMapping(""); handler.endDocument(); } catch (SAXException e) { @@ -476,16 +485,16 @@ public class TurboMarcXmlWriter implements MarcWriter { if (indent) handler.ignorableWhitespace("\n ".toCharArray(), 0, 3); - handler.startElement(Constants.TURBO_MARCXML_NS_URI, RECORD, RECORD, atts); + handler.startElement(Constants.TURBO_MARCXML_NS_URI, RECORD, Q_RECORD, atts); if (indent) handler.ignorableWhitespace("\n ".toCharArray(), 0, 5); - handler.startElement(Constants.TURBO_MARCXML_NS_URI, LEADER, LEADER, atts); + handler.startElement(Constants.TURBO_MARCXML_NS_URI, LEADER, Q_LEADER, atts); Leader leader = record.getLeader(); temp = leader.toString().toCharArray(); handler.characters(temp, 0, temp.length); - handler.endElement(Constants.TURBO_MARCXML_NS_URI, LEADER, LEADER); + handler.endElement(Constants.TURBO_MARCXML_NS_URI, LEADER, Q_LEADER); Iterator ci = record.getControlFields().iterator(); while (ci.hasNext()) { @@ -496,10 +505,11 @@ public class TurboMarcXmlWriter implements MarcWriter { if (indent) handler.ignorableWhitespace("\n ".toCharArray(), 0, 5); String elementName = CONTROL_FIELD + field.getTag(); - handler.startElement(Constants.TURBO_MARCXML_NS_URI, elementName, elementName, atts); + String qElementName = prefix + elementName; + handler.startElement(Constants.TURBO_MARCXML_NS_URI, elementName, qElementName, atts); temp = getDataElement(field.getData()); handler.characters(temp, 0, temp.length); - handler.endElement(Constants.TURBO_MARCXML_NS_URI, elementName, elementName); + handler.endElement(Constants.TURBO_MARCXML_NS_URI, elementName, qElementName); } Iterator di = record.getDataFields().iterator(); @@ -515,20 +525,26 @@ public class TurboMarcXmlWriter implements MarcWriter { if (indent) handler.ignorableWhitespace("\n ".toCharArray(), 0, 5); StringBuffer elementName = new StringBuffer(DATA_FIELD); + StringBuffer qElementName = new StringBuffer(prefix); elementName.append(field.getTag()); - handler.startElement(Constants.TURBO_MARCXML_NS_URI, elementName.toString(), elementName.toString(), atts); + qElementName.append(elementName); + handler.startElement(Constants.TURBO_MARCXML_NS_URI, elementName.toString(), + qElementName.toString(), atts); Iterator si = field.getSubfields().iterator(); while (si.hasNext()) { Subfield subfield = (Subfield) si.next(); StringBuffer subfieldName = new StringBuffer(SUBFIELD); - + StringBuffer qSubfieldName = new StringBuffer(prefix); + qSubfieldName.append(subfieldName); + char code = subfield.getCode(); // if [a-zA-Z0-9] append to elementName, otherwise use a attribute if (code >= '0' && code <= '9' || code >= 'a' && code <= 'z' || code >= 'A' && code <= 'Z') { subfieldName.append(code); + qSubfieldName.append(code); } else { atts = new AttributesImpl(); @@ -539,25 +555,26 @@ public class TurboMarcXmlWriter implements MarcWriter { handler.ignorableWhitespace("\n ".toCharArray(), 0, 7); handler.startElement(Constants.TURBO_MARCXML_NS_URI, subfieldName.toString(), - subfieldName.toString(), atts); + qSubfieldName.toString(), atts); temp = getDataElement(subfield.getData()); handler.characters(temp, 0, temp.length); handler .endElement(Constants.TURBO_MARCXML_NS_URI, subfieldName.toString(), - subfieldName.toString()); + qSubfieldName.toString()); } if (indent) handler.ignorableWhitespace("\n ".toCharArray(), 0, 5); handler - .endElement(Constants.TURBO_MARCXML_NS_URI, elementName.toString(), elementName.toString()); + .endElement(Constants.TURBO_MARCXML_NS_URI, + elementName.toString(), qElementName.toString()); } if (indent) handler.ignorableWhitespace("\n ".toCharArray(), 0, 3); - handler.endElement(Constants.TURBO_MARCXML_NS_URI, RECORD, RECORD); + handler.endElement(Constants.TURBO_MARCXML_NS_URI, RECORD, Q_RECORD); } protected char[] getDataElement(String data) {