Changeset 6939 for FCSSimpleEndpoint


Ignore:
Timestamp:
02/04/16 14:26:04 (8 years ago)
Author:
Oliver Schonefeld
Message:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • FCSSimpleEndpoint/trunk/src/main/java/eu/clarin/sru/server/fcs/AdvancedDataViewWriter.java

    r6937 r6939  
    2828
    2929/**
    30  * Helper class for serializing Advanced data views. It can be used for writing
    31  * more than one, but it is <em>not thread-save</em>.
     30 * Helper class for serializing Advanced Data Views. It can be used for writing
     31 * more than one, but it is <em>not thread-save</em>. This helper can also
     32 * serialize HITS Data Views.
    3233 */
    3334public class AdvancedDataViewWriter {
     
    3839    public static final int NO_HIGHLIGHT = -1;
    3940    private static final String ADV_PREFIX = "adv";
    40     private static final String ADV_NS = "http://clarin.eu/fcs/dataview/advanced";
    41     private static final String ADV_MIME_TYPE = "application/x-clarin-fcs-adv+xml";
     41    private static final String ADV_NS =
     42            "http://clarin.eu/fcs/dataview/advanced";
     43    private static final String ADV_MIME_TYPE =
     44            "application/x-clarin-fcs-adv+xml";
     45    private static final String HITS_MIME_TYPE =
     46            "application/x-clarin-fcs-hits+xml";
     47    private static final String FCS_HITS_PREFIX = "hits";
     48    private static final String FCS_HITS_NS =
     49            "http://clarin.eu/fcs/dataview/hits";
    4250    private final Unit unit;
    4351    private final List<Segment> segments = new ArrayList<Segment>();
     
    198206
    199207    /**
    200      * Write the Advanced data view to the output stream.
     208     * Write the Advanced Data View to the output stream.
    201209     *
    202210     * @param writer
    203211     *            the writer to write to
    204212     * @throws XMLStreamException
    205      *             if an error occurs
     213     *             if an error occurred
    206214     */
    207215    public void writeAdvancedDataView(XMLStreamWriter writer)
     
    271279    }
    272280
     281
     282    /**
     283     * Convenience method to write HITS Data View.
     284     *
     285     * @param writer
     286     *            the writer to write to
     287     * @param layerId
     288     *            the layer id of the layer to be serialized as HITS Data View
     289     * @throws XMLStreamException
     290     *             if an error occurred
     291     * @throws IllegalArgumentException
     292     *             if an invalid layer id was provided
     293     */
     294    public void writeHitsDataView(XMLStreamWriter writer, URI layerId)
     295            throws XMLStreamException {
     296        if (writer == null) {
     297            throw new NullPointerException("writer == null");
     298        }
     299        if (layerId == null) {
     300            throw new NullPointerException("layerId == null");
     301        }
     302
     303        final List<Span> spans = layers.get(layerId);
     304        if (spans == null) {
     305            throw new IllegalArgumentException(
     306                    "layer with id'" + layerId + "' does not exist");
     307        }
     308        XMLStreamWriterHelper.writeStartDataView(writer, HITS_MIME_TYPE);
     309        writer.setPrefix(FCS_HITS_PREFIX, FCS_HITS_NS);
     310        writer.writeStartElement(FCS_HITS_NS, "Result");
     311        writer.writeNamespace(FCS_HITS_PREFIX, FCS_HITS_NS);
     312        boolean needSpace = false;
     313        for (Span span : spans) {
     314            if (span.value.length() > 0) {
     315                if (needSpace) {
     316                    writer.writeCharacters(" ");
     317                    needSpace = false;
     318                }
     319                if (span.highlight != null) {
     320                    writer.writeStartElement(FCS_HITS_NS, "Hit");
     321                    writer.writeCharacters(span.value);
     322                    writer.writeEndElement(); // "Hit" element
     323                    needSpace = true;
     324                } else {
     325                    writer.writeCharacters(span.value);
     326                    if (!Character.isWhitespace(
     327                            (span.value.charAt(span.value.length() - 1)))) {
     328                        needSpace = true;
     329                    }
     330                }
     331            }
     332        }
     333        writer.writeEndElement(); // "Result" element
     334        XMLStreamWriterHelper.writeEndDataView(writer);
     335    }
     336
    273337    private static final class Segment {
    274338        private final String id;
     
    282346            this.start = start;
    283347            this.end = end;
     348            /*
     349             * FIXME: add API to set reference
     350             */
    284351            this.ref = null;
    285352        }
     
    306373    }
    307374
    308 } // class AdvancedDataViewHelper
     375} // class AdvancedDataViewWriter
Note: See TracChangeset for help on using the changeset viewer.