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); }
/** * Tests {@link Record#find(String)} and {@link Record#find(String, String)} and {@link * Record#find(String[], String)}. * * @throws Exception */ @Test public void testFind() throws Exception { VariableField field = record.getVariableField("245"); assertEquals(true, field.find("Summerland")); assertEquals(true, field.find("Sum*erland")); assertEquals(true, field.find("[Cc]habo[a-z]")); field = record.getVariableField("008"); assertEquals(true, field.find("eng")); List<? extends VariableField> result = record.find("Summerland"); assertEquals(1, result.size()); field = (VariableField) result.get(0); assertEquals("245", field.getTag()); result = record.find("Chabon"); assertEquals(2, result.size()); result = record.find("100", "Chabon"); assertEquals(1, result.size()); String[] tags = {"100", "260", "300"}; result = record.find(tags, "Chabon"); assertEquals(1, result.size()); result = record.find("040", "DLC"); assertTrue(result.size() > 0); DataField df = (DataField) result.get(0); String agency = df.getSubfield('a').getData(); assertTrue(agency.matches("DLC")); }
public boolean execute() { List dataFields = record.getDataFieldList(); if (dataFields == null) return false; Iterator dit = dataFields.iterator(); // to make sure remove() operation is supported boolean ret = false; while (dit.hasNext()) { DataField dataField = (DataField) dit.next(); if (fieldMatches(dataField)) { List subfields = dataField.getSubfieldList(); if (subfields == null) continue; Iterator sit = subfields.iterator(); while (sit.hasNext()) { Subfield subfield = (Subfield) sit.next(); if (subfieldMatches(subfield)) { sit.remove(); ret = true; } } if (subfields.size() == 0) { dit.remove(); } } } return ret; }
/** * Tests creating a new {@link Record}. * * @throws Exception */ @Test public void testCreateRecord() throws Exception { MarcFactory factory = MarcFactory.newInstance(); Record record = factory.newRecord("00000cam a2200000 a 4500"); assertEquals("00000cam a2200000 a 4500", record.getLeader().marshal()); record.addVariableField(factory.newControlField("001", "12883376")); DataField df = factory.newDataField("245", '1', '0'); df.addSubfield(factory.newSubfield('a', "Summerland /")); df.addSubfield(factory.newSubfield('c', "Michael Chabon.")); record.addVariableField(df); }
public String getPublishDateDisplay(MarcFunctionContext ctx) { for (String tag : new String[] {"260", "264"}) { for (DataField df : ctx.record().getDataFields(tag)) { if (tag.equals("264") && df.getIndicator2() != '1') { continue; } if (df.getSubfield('c') != null) { String subC = df.getSubfield('c').getData(); if (SINGLE_YEAR_PATTERN.matcher(subC).matches()) { return subC; } } } } return null; }
/** * Creates a new data field with the given tag and indicators and subfields and returns the * instance. * * @return DataField */ @Override public DataField newDataField( final String tag, final char ind1, final char ind2, final String... subfieldCodesAndData) { final DataField df = new DataFieldImpl(tag, ind1, ind2); if (subfieldCodesAndData.length % 2 == 1) { throw new MarcException( "Error: must provide even number of parameters for subfields: code, data, code, data, ..."); } for (int i = 0; i < subfieldCodesAndData.length; i += 2) { if (subfieldCodesAndData[i].length() != 1) { throw new MarcException("Error: subfieldCode must be a single character"); } final Subfield sf = newSubfield(subfieldCodesAndData[i].charAt(0), subfieldCodesAndData[i + 1]); df.addSubfield(sf); } return (df); }
public static void main(String args[]) throws Exception { InputStream input = ReadMarcExample.class.getResourceAsStream("resources/summerland.mrc"); MarcReader reader = new MarcStreamReader(input); while (reader.hasNext()) { Record record = reader.next(); // check if the cataloging agency is DLC List result = record.find("040", "DLC"); if (result.size() > 0) System.out.println("Agency for this record is DLC"); // there is no specific find for a specific subfield // so to check if it is the orignal cataloging agency DataField field = (DataField) result.get(0); String agency = field.getSubfield('a').getData(); if (agency.matches("DLC")) System.out.println("DLC is the original agency"); } }
public Set<Integer> getPublishDate(MarcFunctionContext ctx) { MarcRecord record = ctx.record(); Set<Integer> years = new TreeSet<Integer>(); for (DataField datafield : record.getDataFields("264")) { if (datafield.getIndicator2() == '1') { if (datafield.getSubfield('c') != null) { years.addAll(getPublishDateFromItem(datafield.getSubfield('c').getData())); } } } years.addAll(getPublishDateFromItems(ctx, "260", 'c')); years.addAll(getPublishDateFromItems(ctx, "773", '9')); years.addAll(getPublishDateFromItems(ctx, "996", 'y')); String field008 = record.getControlField("008"); years.addAll(parsePublishDateFrom008(field008)); return years; }
// 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()); }
@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()}; }
@Override public Set<SkatKey> process(Long item) throws Exception { Set<SkatKey> result = new HashSet<>(); HarvestedRecord hr = harvestedRecordDao.get(item); if (hr.getRawRecord() == null) { return result; } MarcRecord marc = null; InputStream is = new ByteArrayInputStream(hr.getRawRecord()); try { marc = marcXmlParser.parseRecord(is); } catch (Exception e) { return result; } for (DataField df : marc.getDataFields("996")) { if (df.getSubfield('e') == null) { continue; } if (df.getSubfield('w') == null) { continue; } String sigla = df.getSubfield('e').getData(); String recordId = df.getSubfield('w').getData(); if (recordId.length() > 100 || sigla.length() > 20) { // ignore garbage continue; } SkatKey key = new SkatKey(new SkatKeyCompositeId(hr.getId(), sigla, recordId)); result.add(key); } // ignore records having not enough information return result.size() < 2 ? Collections.emptySet() : result; }
private void makeMaregdRecord( int databaseType, Record record, String controlNumber, CustomStorageRecord storageRecord) { // making latin record String entryTag = StorageUtils.findEntryTagForAuthority((AuthorityRecord) storageRecord); String entryRelatedTag = "7" + entryTag.substring(1); record = storageRecord.getRecord(); Record engRecord = MarcUtils.parseContent(storageRecord.getContent()); engRecord.getControlField(Tags.ID).setData("0"); // exchanging fields DataField df200 = engRecord.getDataField(entryTag); DataField df700 = engRecord.getDataField(entryRelatedTag); if (df700 != null) { df700.setTag(entryTag); if (df200 != null) { df200.setTag(entryRelatedTag); df200.add(new Subfield('3', String.valueOf(storageRecord.getId()))); } CustomStorageRecord engStorageRecord = StorageUtils.createNewStorageRecord( StorageUtils.getStorageClassByClassType(databaseType)); engStorageRecord.setId(0); engStorageRecord.setContent(MarcUtils.marshalContent(engRecord)); engStorageRecord.setLanguageOfCataloging(MarcConsts.LANG_ENGLISH); engStorageRecord.setCharacterSets("50"); engStorageRecord.setSubfieldRangeData(Tags.GENERAL_PROCESSING_DATA, 'a', "ba", 21, 22); engStorageRecord.setSubfieldRangeData(Tags.GENERAL_PROCESSING_DATA, 'a', "0", 23, 23); engStorageRecord.setSubfieldData(Tags.LANGUAGE_OF_THE_ITEM, 'a', MarcConsts.LANG_PERSIAN); try { engStorageRecord = save(engStorageRecord); logger.info(" ^^ english record added.", null); df700 = record.getDataField(entryRelatedTag); if (df700 != null) { df700.add(new Subfield('3', String.valueOf(engStorageRecord.getId()))); storageRecord.setContent(MarcUtils.marshalContent(record)); storageRecord = save(storageRecord); logger.info(" ^^ farsi record changed.", null); } } catch (Exception ex) { logger.error("error: RecordNo = " + controlNumber + " english making error!! **", ex); } } }
/** * @param study - study that we are exporting * @param out * @throws IOException */ @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) public void exportStudy(Study study, OutputStream out) throws IOException { if (study.getReleasedVersion() == null) { throw new IllegalArgumentException( "Study does not have released version, study.id = " + study.getId()); } Metadata metadata = study.getReleasedVersion().getMetadata(); MarcFactory factory = MarcFactory.newInstance(); Record record = factory.newRecord(); DataField title = factory.newDataField("245", '0', ' '); title.addSubfield(factory.newSubfield('a', metadata.getTitle())); if (!StringUtil.isEmpty(metadata.getAuthorsStr())) { title.addSubfield(factory.newSubfield('c', metadata.getAuthorsStr())); } if (!StringUtil.isEmpty(metadata.getDistributionDate())) { title.addSubfield(factory.newSubfield('s', metadata.getDistributionDate())); } record.addVariableField(title); DataField globalId = factory.newDataField("440", ' ', ' '); globalId.addSubfield(factory.newSubfield('v', study.getGlobalId())); record.addVariableField(globalId); for (StudyOtherId studyOtherId : metadata.getStudyOtherIds()) { DataField otherId = factory.newDataField("440", ' ', ' '); otherId.addSubfield(factory.newSubfield('v', studyOtherId.getOtherId())); record.addVariableField(otherId); } for (StudyAbstract studyAbstract : metadata.getStudyAbstracts()) { DataField abstractField = factory.newDataField("520", ' ', ' '); abstractField.addSubfield(factory.newSubfield('a', studyAbstract.getText())); record.addVariableField(abstractField); } DataField handle = factory.newDataField("856", ' ', ' '); handle.addSubfield(factory.newSubfield('u', study.getHandleURL())); record.addVariableField(handle); DataField dataverseUrl = factory.newDataField("535", ' ', ' '); dataverseUrl.addSubfield( factory.newSubfield( 'd', "http://" + PropertyUtil.getHostUrl() + "/dvn/study?globalId=" + study.getGlobalId())); record.addVariableField(dataverseUrl); for (StudyTopicClass studyTopicClass : metadata.getStudyTopicClasses()) { DataField topicClass = factory.newDataField("650", ' ', ' '); topicClass.addSubfield(factory.newSubfield('a', studyTopicClass.getValue())); record.addVariableField(topicClass); } MarcStreamWriter writer = new MarcStreamWriter(out); writer.write(record); out.flush(); }