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 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); }
/** * @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(); }