[5474] | 1 | package eu.clarin.sru.server.fcs; |
---|
| 2 | |
---|
| 3 | import javax.xml.stream.XMLStreamException; |
---|
| 4 | import javax.xml.stream.XMLStreamWriter; |
---|
| 5 | |
---|
[5477] | 6 | public class SimpleFcsResourceWriter { |
---|
[5474] | 7 | private static final String FCS_NS = "http://clarin.eu/fcs/resource"; |
---|
| 8 | private static final String FCS_PREFIX = "fcs"; |
---|
| 9 | private static final String FCS_DATAVIEW_MIMETYPE = |
---|
| 10 | "application/x-clarin-fcs-hits+xml"; |
---|
| 11 | |
---|
| 12 | private static final String HITS_NS = "http://clarin.eu/fcs/dataview/hits"; |
---|
| 13 | private static final String HITS_PREFIX = "hits"; |
---|
| 14 | |
---|
| 15 | public static void writeResource(XMLStreamWriter writer, String pid, String ref, |
---|
| 16 | String leftContext, String keyword, String rightContext) |
---|
| 17 | throws XMLStreamException { |
---|
| 18 | |
---|
| 19 | if (writer == null) { |
---|
| 20 | throw new NullPointerException("writer == null"); |
---|
| 21 | } |
---|
| 22 | |
---|
| 23 | writer.setPrefix(FCS_PREFIX, FCS_NS); |
---|
| 24 | writer.writeStartElement(FCS_NS, "Resource"); |
---|
| 25 | writer.writeNamespace(FCS_PREFIX, FCS_NS); |
---|
| 26 | if ((pid != null) && !pid.isEmpty()) { |
---|
| 27 | writer.writeAttribute("pid", pid); |
---|
| 28 | } |
---|
| 29 | if ((ref != null) && !ref.isEmpty()) { |
---|
| 30 | writer.writeAttribute("ref", ref); |
---|
| 31 | } |
---|
| 32 | writeDataView(writer, leftContext, keyword, rightContext); |
---|
| 33 | writer.writeEndElement(); |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | public static void writeDataView(XMLStreamWriter writer, String leftContext, |
---|
| 37 | String keyword, String rightContext) throws XMLStreamException { |
---|
| 38 | |
---|
| 39 | if (writer == null) { |
---|
| 40 | throw new NullPointerException("writer == null"); |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | writer.writeStartElement(FCS_NS, "DataView"); |
---|
| 44 | writer.writeAttribute("type", FCS_DATAVIEW_MIMETYPE); |
---|
| 45 | writeHits(writer, leftContext, keyword, rightContext); |
---|
| 46 | writer.writeEndElement(); |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | public static void writeHits(XMLStreamWriter writer, String leftContext, |
---|
| 50 | String keyword, String rightContext) throws XMLStreamException { |
---|
| 51 | |
---|
| 52 | if (writer == null) { |
---|
| 53 | throw new NullPointerException("writer == null"); |
---|
| 54 | } |
---|
| 55 | writer.setPrefix(HITS_PREFIX, HITS_NS); |
---|
| 56 | writer.writeStartElement(HITS_NS, "Result"); |
---|
| 57 | writer.writeNamespace(HITS_PREFIX, HITS_NS); |
---|
| 58 | |
---|
| 59 | if (leftContext != null && !leftContext.isEmpty()) { |
---|
| 60 | writer.writeCharacters(leftContext); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | writer.writeStartElement(HITS_NS, "Hit"); |
---|
| 64 | writer.writeCharacters(keyword); |
---|
| 65 | writer.writeEndElement(); // Hit |
---|
| 66 | |
---|
| 67 | if (rightContext != null && !rightContext.isEmpty()) { |
---|
| 68 | writer.writeCharacters(rightContext); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | writer.writeEndElement(); // Result |
---|
| 72 | } |
---|
| 73 | } |
---|