Пример #1
0
  protected void toXml(Record record) throws SAXException {
    if (!MarcFactory.newInstance().validateRecord(record)) {
      throw new MarcException("Marc record didn't validate");
    }
    char temp[];
    AttributesImpl atts = new AttributesImpl();
    if (indent) handler.ignorableWhitespace("\n  ".toCharArray(), 0, 3);

    handler.startElement(Constants.MARCXML_NS_URI, RECORD, RECORD, atts);

    if (indent) handler.ignorableWhitespace("\n    ".toCharArray(), 0, 5);

    handler.startElement(Constants.MARCXML_NS_URI, LEADER, LEADER, atts);
    Leader leader = record.getLeader();
    temp = leader.toString().toCharArray();
    handler.characters(temp, 0, temp.length);
    handler.endElement(Constants.MARCXML_NS_URI, LEADER, LEADER);

    for (ControlField field : record.getControlFields()) {
      atts = new AttributesImpl();
      atts.addAttribute("", "tag", "tag", "CDATA", field.getTag());

      if (indent) handler.ignorableWhitespace("\n    ".toCharArray(), 0, 5);

      handler.startElement(Constants.MARCXML_NS_URI, CONTROL_FIELD, CONTROL_FIELD, atts);
      temp = getDataElement(field.getData());
      handler.characters(temp, 0, temp.length);
      handler.endElement(Constants.MARCXML_NS_URI, CONTROL_FIELD, CONTROL_FIELD);
    }

    for (DataField field : record.getDataFields()) {
      atts = new AttributesImpl();
      atts.addAttribute("", "tag", "tag", "CDATA", field.getTag());
      atts.addAttribute("", "ind1", "ind1", "CDATA", String.valueOf(field.getIndicator1()));
      atts.addAttribute("", "ind2", "ind2", "CDATA", String.valueOf(field.getIndicator2()));

      if (indent) handler.ignorableWhitespace("\n    ".toCharArray(), 0, 5);

      handler.startElement(Constants.MARCXML_NS_URI, DATA_FIELD, DATA_FIELD, atts);
      for (Subfield subfield : field.getSubfields()) {
        atts = new AttributesImpl();
        atts.addAttribute("", "code", "code", "CDATA", String.valueOf(subfield.getCode()));

        if (indent) handler.ignorableWhitespace("\n      ".toCharArray(), 0, 7);

        handler.startElement(Constants.MARCXML_NS_URI, SUBFIELD, SUBFIELD, atts);
        temp = getDataElement(subfield.getData());
        handler.characters(temp, 0, temp.length);
        handler.endElement(Constants.MARCXML_NS_URI, SUBFIELD, SUBFIELD);
      }

      if (indent) handler.ignorableWhitespace("\n    ".toCharArray(), 0, 5);

      handler.endElement(Constants.MARCXML_NS_URI, DATA_FIELD, DATA_FIELD);
    }

    if (indent) handler.ignorableWhitespace("\n  ".toCharArray(), 0, 3);

    handler.endElement(Constants.MARCXML_NS_URI, RECORD, RECORD);
  }
Пример #2
0
  /** Tests the various ways to get fields from a {@link Record}. */
  @Test
  public void testGetFields() {
    String cn = record.getControlNumber();
    assertEquals("12883376", cn);

    ControlField cf = record.getControlNumberField();
    assertEquals("001", cf.getTag());
    assertEquals("12883376", cf.getData());

    List<? extends VariableField> fieldList = record.getVariableFields();
    assertEquals(15, fieldList.size());

    fieldList = record.getControlFields();
    assertEquals(3, fieldList.size());

    fieldList = record.getDataFields();
    assertEquals(12, fieldList.size());

    VariableField field = record.getVariableField("245");
    assertEquals("245", field.getTag());

    fieldList = record.getVariableFields("650");
    assertEquals(3, fieldList.size());

    String[] fields = {"245", "260", "300"};
    fieldList = record.getVariableFields(fields);
    assertEquals(3, fieldList.size());
  }