@Override
 public void endRecord() {
   if (!recordOpen) {
     return;
   }
   try {
     if (listener != null) {
       listener.endRecord();
     }
     if (contentHandler != null) {
       contentHandler.endElement(nsUri, RECORD, RECORD);
     }
     if (listener != null) {
       // emit trailer event, drives record output segmentation
       listener.trailer(null);
     }
     this.recordOpen = false;
   } catch (Exception ex) {
     if (fatalerrors) {
       throw new RuntimeException(ex);
     } else if (!silenterrors) {
       logger.warn(designator + ": " + ex.getMessage(), ex);
     }
   }
 }
 @Override
 public void endSubField(Field designator) {
   if (designator == null) {
     return;
   }
   try {
     if (listener != null) {
       listener.endSubField(designator);
     }
     if (designator != null) {
       if (contentHandler != null) {
         String value = designator.data();
         if (!value.isEmpty()) {
           value = normalizeValue(value);
           contentHandler.characters(value.toCharArray(), 0, value.length());
         }
       }
     }
     if (contentHandler != null) {
       contentHandler.endElement(NS_URI, SUBFIELD, SUBFIELD);
     }
   } catch (Exception ex) {
     if (fatalerrors) {
       throw new RuntimeException(ex);
     } else if (!silenterrors) {
       logger.warn(designator + ": " + ex.getMessage(), ex);
     }
   }
 }
 @Override
 public void beginSubField(Field designator) {
   if (designator == null) {
     return;
   }
   try {
     AttributesImpl attrs = new AttributesImpl();
     String subfieldId = designator.subfieldId();
     if (subfieldId == null || subfieldId.length() == 0) {
       subfieldId = "a"; // fallback
     }
     attrs.addAttribute(nsUri, CODE, CODE, "CDATA", subfieldId);
     if (contentHandler != null) {
       contentHandler.startElement(nsUri, SUBFIELD, SUBFIELD, attrs);
     }
     if (listener != null) {
       listener.beginSubField(designator);
     }
   } catch (Exception ex) {
     if (fatalerrors) {
       throw new RuntimeException(ex);
     } else if (!silenterrors) {
       logger.warn(designator + ": " + ex.getMessage(), ex);
     }
   }
 }
 @Override
 public void endDataField(Field designator) {
   try {
     if (!datafieldOpen) {
       return;
     }
     if (listener != null) {
       listener.endDataField(designator);
     }
     if (designator != null) {
       String value = designator.data();
       if (value != null && !value.isEmpty()) {
         value = normalizeValue(value);
         // write data field per default into a subfield with code 'a'
         AttributesImpl attrs = new AttributesImpl();
         attrs.addAttribute(nsUri, CODE, CODE, "CDATA", "a");
         if (contentHandler != null) {
           contentHandler.startElement(nsUri, SUBFIELD, SUBFIELD, attrs);
           contentHandler.characters(value.toCharArray(), 0, value.length());
           contentHandler.endElement(nsUri, SUBFIELD, SUBFIELD);
         }
       }
     }
     if (contentHandler != null) {
       contentHandler.endElement(NS_URI, DATAFIELD, DATAFIELD);
     }
     datafieldOpen = false;
   } catch (Exception ex) {
     if (fatalerrors) {
       throw new RuntimeException(ex);
     } else if (!silenterrors) {
       logger.warn(designator + ": " + ex.getMessage(), ex);
     }
   }
 }
 @Override
 public void beginRecord(String format, String type) {
   if (recordOpen) {
     return;
   }
   try {
     AttributesImpl attrs = new AttributesImpl();
     if (format != null && !"MARC21".equalsIgnoreCase(schema)) {
       attrs.addAttribute(nsUri, FORMAT, FORMAT, "CDATA", format);
     }
     if (type != null) {
       attrs.addAttribute(nsUri, TYPE, TYPE, "CDATA", type);
     }
     if (contentHandler != null) {
       contentHandler.startElement(nsUri, RECORD, RECORD, attrs);
     }
     if (listener != null) {
       listener.beginRecord(format, type);
     }
     this.recordOpen = true;
   } catch (Exception ex) {
     if (fatalerrors) {
       throw new RuntimeException(ex);
     } else if (!silenterrors) {
       logger.warn(designator + ": " + ex.getMessage(), ex);
     }
   }
 }
 @Override
 public void beginDataField(Field designator) {
   if (designator == null) {
     return;
   }
   try {
     if (designator.isControlField()) {
       beginControlField(designator);
       endControlField(designator);
       return;
     }
     if (datafieldOpen) {
       return;
     }
     AttributesImpl attrs = new AttributesImpl();
     String tag = designator.tag();
     if (tag == null || tag.length() == 0) {
       tag = Field.NULL_TAG; // fallback
       designator.tag(tag);
     }
     attrs.addAttribute(nsUri, TAG, TAG, "CDATA", tag);
     int ind = designator.indicator() != null ? designator.indicator().length() : 0;
     // force at least two default blank indicators if schema is Marc 21
     if ("MARC21".equalsIgnoreCase(schema)) {
       for (int i = (ind == 0 ? 1 : ind); i <= 2; i++) {
         attrs.addAttribute(null, IND + i, IND + i, "CDATA", " ");
       }
     }
     // set indicators
     for (int i = 1; i <= ind; i++) {
       attrs.addAttribute(
           null, IND + i, IND + i, "CDATA", designator.indicator().substring(i - 1, i));
     }
     if (contentHandler != null) {
       contentHandler.startElement(nsUri, DATAFIELD, DATAFIELD, attrs);
     }
     if (listener != null) {
       listener.beginDataField(designator);
     }
     datafieldOpen = true;
   } catch (Exception ex) {
     if (fatalerrors) {
       throw new RuntimeException(ex);
     } else if (!silenterrors) {
       logger.warn(designator + ": " + ex.getMessage(), ex);
     }
   }
 }
 @Override
 public void beginControlField(Field designator) {
   if (designator == null) {
     return;
   }
   try {
     AttributesImpl attrs = new AttributesImpl();
     attrs.addAttribute(nsUri, TAG, TAG, "CDATA", designator.tag());
     if (contentHandler != null) {
       contentHandler.startElement(nsUri, CONTROLFIELD, CONTROLFIELD, attrs);
     }
     if (listener != null) {
       listener.beginControlField(designator);
     }
   } catch (Exception ex) {
     if (fatalerrors) {
       throw new RuntimeException(ex);
     } else if (!silenterrors) {
       logger.warn(designator + ": " + ex.getMessage(), ex);
     }
   }
 }
 @Override
 public void leader(String value) {
   if (value == null) {
     return;
   }
   try {
     if (contentHandler != null) {
       contentHandler.startElement(nsUri, LEADER, LEADER, EMPTY_ATTRIBUTES);
       contentHandler.characters(value.toCharArray(), 0, value.length());
       contentHandler.endElement(nsUri, LEADER, LEADER);
     }
     if (listener != null) {
       listener.leader(value);
     }
   } catch (Exception ex) {
     if (fatalerrors) {
       throw new RuntimeException(ex);
     } else if (!silenterrors) {
       logger.warn(designator + ": " + ex.getMessage(), ex);
     }
   }
 }
 @Override
 public void endControlField(Field designator) {
   try {
     if (listener != null) {
       listener.endControlField(designator);
     }
     if (designator != null) {
       String value = designator.data();
       if (!value.isEmpty()) {
         switch (designator.tag()) {
           case "001":
             this.id = value;
             break;
           case "006":
           case "007":
           case "008":
             // fix fill characters here
             value = value.replace('^', '|');
             break;
         }
         if (contentHandler != null) {
           contentHandler.characters(value.toCharArray(), 0, value.length());
         }
       }
     }
     if (contentHandler != null) {
       contentHandler.endElement(nsUri, CONTROLFIELD, CONTROLFIELD);
     }
   } catch (Exception ex) {
     if (fatalerrors) {
       throw new RuntimeException(ex);
     } else if (!silenterrors) {
       logger.warn(designator + ": " + ex.getMessage(), ex);
     }
   }
 }