Esempio n. 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);
  }
  // file import rules
  public ActionResult importFile(File selectedFile) throws IOException {
    Timer timer = new Timer();
    TreeMap<String, Stats> types = new TreeMap<String, Stats>();

    InputStream in = new FileInputStream(selectedFile);
    MarcReader reader = new MarcPermissiveStreamReader(in, true, true);

    while (reader.hasNext()) {
      Record record = reader.next();

      String bib_id = "";
      DataField df907 = (DataField) record.getVariableField("907");

      if (df907 != null) {
        Subfield df907a = df907.getSubfield('a');
        if (df907a != null) {
          bib_id = df907a.getData();
        }
      }
      if (bib_id.startsWith(".b")) {
        bib_id = bib_id.substring(2, 9);
      }

      String key = bib_id;
      Stats stat = Generator.INSTANCE.create(key);
      types.put(stat.key, stat);

      String checksum = getChecksum(record.toString());
      stat.setVal(InventoryStatsItem.Hash_Code, checksum);
    } // end while loop

    return new ActionResult(
        selectedFile,
        selectedFile.getName(),
        this.toString(),
        details,
        types,
        true,
        timer.getDuration());
  }
Esempio n. 3
0
 @Override
 public String[] getValues(VariableField vf) {
   DataField df = (DataField) vf;
   String value = df.getSubfield('a').getData();
   String tag = df.getTag();
   if (tag.equals("100")) {
     Subfield csf = df.getSubfield('c');
     if (csf != null) {
       String data = csf.getData();
       int dlen = data.length();
       StringBuffer sb = new StringBuffer(value.length() + data.length() + 1);
       sb.append(value);
       sb.append(' ');
       char c = data.charAt(dlen - 1);
       if (c == ',' || c == '.') {
         sb.append(data, 0, dlen - 1);
       } else sb.append(data);
       value = sb.toString();
     }
   } else { // 110 & 710
     List<Subfield> sfs = df.getSubfields('b');
     if (sfs.size() > 0) {
       StringBuffer buf = new StringBuffer(value.length() + sfs.size() * 5);
       buf.append(value);
       Iterator<Subfield> sfi = sfs.iterator();
       while (sfi.hasNext()) {
         buf.append(' ');
         buf.append(sfi.next().getData());
       }
       int last = buf.length() - 1;
       char c = buf.charAt(last);
       if (c == ',' || c == '.') {
         buf.setLength(last);
       }
       value = buf.toString();
     }
   }
   return new String[] {trim(value).toString()};
 }