Search in sources :

Example 1 with RecordBuilderException

use of org.jzkit.search.util.RecordBuilder.RecordBuilderException in project z3950-server by mol-pl.

the class Iso2709Utf8RecordFactory method getCanonicalXML.

/**
 * Tworzy marcxml z obiektu binarnego
 *
 * @param native_object dokument binarny
 * @return marcxml
 * @throws RecordBuilderException
 */
@Override
public Document getCanonicalXML(Object native_object) throws RecordBuilderException {
    org.w3c.dom.Document retval = null;
    try {
        MarcReader reader = new MarcStreamReader(new ByteArrayInputStream((byte[]) native_object), "UTF8");
        javax.xml.transform.dom.DOMResult result = new javax.xml.transform.dom.DOMResult();
        MarcXmlWriter writer = new MarcXmlWriter(result);
        if (reader.hasNext()) {
            Record record = (Record) reader.next();
            writer.write(record);
        } else {
            log.warn("No marc record found in reader stream");
        }
        writer.close();
        retval = (Document) result.getNode();
    } catch (Exception e) {
        throw new RecordBuilderException("Problem creating marcxml from iso2709", e);
    }
    return retval;
}
Also used : RecordBuilderException(org.jzkit.search.util.RecordBuilder.RecordBuilderException) MarcStreamReader(org.marc4j.MarcStreamReader) RecordBuilderException(org.jzkit.search.util.RecordBuilder.RecordBuilderException) Document(org.w3c.dom.Document) MarcReader(org.marc4j.MarcReader) Record(org.marc4j.marc.Record)

Example 2 with RecordBuilderException

use of org.jzkit.search.util.RecordBuilder.RecordBuilderException in project z3950-server by mol-pl.

the class Iso2709Utf8RecordFactory method createFrom.

/**
 * Tworzy dokument binarny z marcxml
 *
 * @param input_dom marcxml
 * @param esn Optional Element Set Name (F,B, For XML, a schema to request);
 * @return native_object
 * @throws org.jzkit.search.util.RecordBuilder.RecordBuilderException
 */
@Override
public Object createFrom(Document input_dom, String esn) throws RecordBuilderException {
    byte[] result = null;
    log.debug("iso2709 from marcxml");
    try {
        log.debug("open pip output stream");
        PipedOutputStream pos = new PipedOutputStream();
        XMLSerializer serializer = new XMLSerializer();
        MarcXmlReader reader = new MarcXmlReader(new PipedInputStream(pos));
        serializer.setOutputByteStream(pos);
        log.debug("Serialize dom to output stream");
        serializer.serialize(input_dom);
        log.debug("flush");
        pos.flush();
        pos.close();
        log.debug("attempt to read marcxml from pipe");
        if (reader.hasNext()) {
            Record record = reader.next();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            MarcWriter writer = new MarcStreamWriter(baos, "UTF8");
            writer.write(record);
            result = baos.toByteArray();
            log.debug("Result of transform to marc: " + new String(result));
        } else {
            log.warn("No marc record found in reader stream");
        }
    } catch (java.io.IOException ioe) {
        throw new RecordBuilderException("Problem converting marcxml to iso2709", ioe);
    }
    log.debug("result=" + result);
    return result;
}
Also used : XMLSerializer(org.apache.xml.serialize.XMLSerializer) java.io(java.io) RecordBuilderException(org.jzkit.search.util.RecordBuilder.RecordBuilderException) MarcWriter(org.marc4j.MarcWriter) Record(org.marc4j.marc.Record) MarcXmlReader(org.marc4j.MarcXmlReader) MarcStreamWriter(org.marc4j.MarcStreamWriter)

Aggregations

RecordBuilderException (org.jzkit.search.util.RecordBuilder.RecordBuilderException)2 Record (org.marc4j.marc.Record)2 java.io (java.io)1 XMLSerializer (org.apache.xml.serialize.XMLSerializer)1 MarcReader (org.marc4j.MarcReader)1 MarcStreamReader (org.marc4j.MarcStreamReader)1 MarcStreamWriter (org.marc4j.MarcStreamWriter)1 MarcWriter (org.marc4j.MarcWriter)1 MarcXmlReader (org.marc4j.MarcXmlReader)1 Document (org.w3c.dom.Document)1