source: FCSSimpleEndpoint/trunk/src/main/java/eu/clarin/sru/server/fcs/XMLStreamWriterHelper.java @ 2751

Last change on this file since 2751 was 2751, checked in by oschonef, 11 years ago
  • initial import of sources
  • Property svn:eol-style set to native
File size: 9.8 KB
Line 
1package eu.clarin.sru.server.fcs;
2
3import javax.xml.stream.XMLStreamException;
4import javax.xml.stream.XMLStreamWriter;
5
6
7/**
8 * This class provides several helper methods for writing records in the
9 * CLARIN-FCS record schema. These methods <em>do not</em> cover the full
10 * spectrum of all variations of records that are permitted by the CLARIN FCS
11 * specification.
12 *
13 * @see <a
14 *      href="https://trac.clarin.eu/wiki/FCS-specification#SearchRetrieveOperation">
15 *      CLARIN FCS specification, section "SearchRetrieve Operation"</a>
16 */
17public final class XMLStreamWriterHelper {
18    private static final String FCS_NS          = "http://clarin.eu/fcs/1.0";
19    private static final String FCS_PREFIX      = "fcs";
20    private static final String FCS_KWIC_NS     =
21            "http://clarin.eu/fcs/1.0/kwic";
22    private static final String FCS_KWIC_PREFIX = "kwic";
23    private static final String FCS_KWIC_MIMETYPE =
24            "application/x-clarin-fcs-kwic+xml";
25
26
27    /**
28     * Write the start of a resource (i.e. the <code>&lt;Resource&gt;</code>
29     * element). Calls to this method need to be balanced with calls to the
30     * {@link #writeEndResource(XMLStreamWriter)} method.
31     *
32     *
33     * @param writer
34     *            the {@link XMLStreamWriter} to be used
35     * @param pid
36     *            the persistent identifier of this resource or
37     *            <code>null</code>, if not applicable
38     * @param ref
39     *            the reference of this resource or <code>null</code>, if not
40     *            applicable
41     * @throws XMLStreamException
42     *             if an error occurred
43     */
44    public static void writeStartResource(XMLStreamWriter writer, String pid,
45            String ref) throws XMLStreamException {
46        if (writer == null) {
47            throw new NullPointerException("writer == null");
48        }
49
50        writer.setPrefix(FCS_PREFIX, FCS_NS);
51        writer.writeStartElement(FCS_NS, "Resource");
52        writer.writeNamespace(FCS_PREFIX, FCS_NS);
53        if ((pid != null) && !pid.isEmpty()) {
54            writer.writeAttribute("pid", pid);
55        }
56        if ((ref != null) && !ref.isEmpty()) {
57            writer.writeAttribute("ref", ref);
58        }
59    }
60
61
62    /**
63     * Write the end of a resource (i.e. the <code>&lt;/Resource&gt;</code>
64     * element). Calls to this method need to be balanced with calls to the
65     * {@link #writeStartResource(XMLStreamWriter, String, String)} method.
66     *
67     * @param writer
68     *            the {@link XMLStreamWriter} to be used
69     * @throws XMLStreamException
70     *             if an error occurred
71     */
72    public static void writeEndResource(XMLStreamWriter writer)
73            throws XMLStreamException {
74        if (writer == null) {
75            throw new NullPointerException("writer == null");
76        }
77
78        writer.writeEndElement(); // "Resource" element
79    }
80
81
82    /**
83     * Write the start of a resource fragment (i.e. the
84     * <code>&lt;ResourceFragment&gt;</code> element). Calls to this method need
85     * to be balanced with calls to the
86     * {@link #writeEndResourceFragment(XMLStreamWriter)} method.
87     *
88     *
89     * @param writer
90     *            the {@link XMLStreamWriter} to be used
91     * @param pid
92     *            the persistent identifier of this resource or
93     *            <code>null</code>, if not applicable
94     * @param ref
95     *            the reference of this resource or <code>null</code>, if not
96     *            applicable
97     * @throws XMLStreamException
98     *             if an error occurred
99     */
100    public static void writeStartResourceFragment(XMLStreamWriter writer,
101            String pid, String ref) throws XMLStreamException {
102        if (writer == null) {
103            throw new NullPointerException("writer == null");
104        }
105
106        writer.writeStartElement(FCS_NS, "ResourceFragment");
107        if ((pid != null) && !pid.isEmpty()) {
108            writer.writeAttribute("pid", pid);
109        }
110        if ((ref != null) && !ref.isEmpty()) {
111            writer.writeAttribute("ref", ref);
112        }
113    }
114
115
116    /**
117     * Write the end of a resource fragment (i.e. the
118     * <code>&lt;/ResourceFragment&gt;</code> element). Calls to this method
119     * need to be balanced with calls to the
120     * {@link #writeStartResourceFragment(XMLStreamWriter, String, String)}
121     * method.
122     *
123     * @param writer
124     *            the {@link XMLStreamWriter} to be used
125     * @throws XMLStreamException
126     *             if an error occurred
127     */
128    public static void writeEndResourceFragment(XMLStreamWriter writer)
129            throws XMLStreamException {
130        if (writer == null) {
131            throw new NullPointerException("writer == null");
132        }
133
134        writer.writeEndElement(); // "ResourceFragment" element
135    }
136
137
138    /**
139     * Write the start of a data view (i.e. the
140     * <code>&lt;DataView&gt;</code> element). Calls to this method need
141     * to be balanced with calls to the
142     * {@link #writeEndResource(XMLStreamWriter)} method.
143     *
144     *
145     * @param writer
146     *            the {@link XMLStreamWriter} to be used
147     * @param mimetype
148     *            the MIME type of this data view
149     *            applicable
150     * @throws XMLStreamException
151     *             if an error occurred
152     */
153    public static void writeStartDataView(XMLStreamWriter writer,
154            String mimetype) throws XMLStreamException {
155        if (writer == null) {
156            throw new NullPointerException("writer == null");
157        }
158        if (mimetype == null) {
159            throw new NullPointerException("mimetype == null");
160        }
161        if (mimetype.isEmpty()) {
162            throw new IllegalArgumentException("mimetype is empty");
163        }
164
165        writer.writeStartElement(FCS_NS, "DataView");
166        writer.writeAttribute("type", mimetype);
167    }
168
169
170    /**
171     * Write the end of a data view (i.e. the <code>&lt;/DataView&gt;</code>
172     * element). Calls to this method need to be balanced with calls to the
173     * {@link #writeStartDataView(XMLStreamWriter, String)} method.
174     *
175     * @param writer
176     *            the {@link XMLStreamWriter} to be used
177     * @throws XMLStreamException
178     *             if an error occurred
179     */
180    public static void writeEndDataView(XMLStreamWriter writer)
181            throws XMLStreamException {
182        if (writer == null) {
183            throw new NullPointerException("writer == null");
184        }
185
186        writer.writeEndElement(); // "DataView" element
187    }
188
189
190    /**
191     * Convince method to write a KWIC data view. It automatically performs the
192     * calls to {@link #writeStartDataView(XMLStreamWriter, String)} and
193     * {@link #writeEndDataView(XMLStreamWriter)}.
194     *
195     * @param writer
196     *            the {@link XMLStreamWriter} to be used
197     * @param left
198     *            the left context of the KWIC or <code>null</code> if not
199     *            applicable
200     * @param keyword
201     *            the keyword of the KWIC
202     * @param right
203     *            the right context of the KWIC or <code>null</code> if not
204     *            applicable
205     * @throws XMLStreamException
206     *             if an error occurred
207     */
208    public static void writeKWICDataView(XMLStreamWriter writer, String left,
209            String keyword, String right) throws XMLStreamException {
210        if (writer == null) {
211            throw new NullPointerException("writer == null");
212        }
213
214        writeStartDataView(writer, FCS_KWIC_MIMETYPE);
215
216        // actual "kwic" data view
217        writer.setPrefix(FCS_KWIC_PREFIX, FCS_KWIC_NS);
218        writer.writeStartElement(FCS_KWIC_NS, "kwic");
219        writer.writeNamespace(FCS_KWIC_PREFIX, FCS_KWIC_NS);
220
221        writer.writeStartElement(FCS_KWIC_NS, "c");
222        writer.writeAttribute("type", "left");
223        if (left != null) {
224            writer.writeCharacters(left);
225        }
226        writer.writeEndElement(); // "c" element
227
228        writer.writeStartElement(FCS_KWIC_NS, "kw");
229        writer.writeCharacters(keyword);
230        writer.writeEndElement(); // "kw" element
231
232        writer.writeStartElement(FCS_KWIC_NS, "c");
233        writer.writeAttribute("type", "right");
234        if (right != null) {
235            writer.writeCharacters(right);
236        }
237        writer.writeEndElement(); // "c" element
238
239        writer.writeEndElement(); // "kwic" element
240
241        writeEndDataView(writer);
242    }
243
244
245    /**
246     * Convince method for writing a record with a KWIC data view. The following
247     * code (arguments omitted) would accomplish the same result:
248     * <pre>
249     * ...
250     * writeStartResource(...);
251     * writeKWICDataView(...);
252     * writeEndResource(...);
253     * ...
254     * </pre>
255     *
256     * @param writer
257     *            the {@link XMLStreamWriter} to be used
258     * @param pid
259     *            the persistent identifier of this resource or
260     *            <code>null</code>, if not applicable
261     * @param ref
262     *            the reference of this resource or <code>null</code>, if not
263     *            applicable
264     * @param left
265     *            the left context of the KWIC or <code>null</code> if not
266     *            applicable
267     * @param keyword
268     *            the keyword of the KWIC
269     * @param right
270     *            the right context of the KWIC or <code>null</code> if not
271     *            applicable
272     * @throws XMLStreamException
273     *             if an error occurred
274     */
275    public static void writeResourceWithKWICDataView(XMLStreamWriter writer,
276            String pid, String ref, String left, String keyword, String right)
277            throws XMLStreamException {
278        if (writer == null) {
279            throw new NullPointerException("writer == null");
280        }
281
282        writeStartResource(writer, pid, ref);
283        writeKWICDataView(writer, left, keyword, right);
284        writeEndResource(writer);
285    }
286
287} // class XMLStreamWriterHelper
Note: See TracBrowser for help on using the repository browser.