source: FCSSimpleClient/trunk/src/main/java/eu/clarin/sru/client/fcs/DataViewParserGenericString.java @ 7280

Last change on this file since 7280 was 7280, checked in by Oliver Schonefeld, 2 years ago
  • cleanup
File size: 6.4 KB
Line 
1/**
2 * This software is copyright (c) 2012-2022 by
3 *  - Leibniz-Institut fuer Deutsche Sprache (http://www.ids-mannheim.de)
4 * This is free software. You can redistribute it
5 * and/or modify it under the terms described in
6 * the GNU General Public License v3 of which you
7 * should have received a copy. Otherwise you can download
8 * it from
9 *
10 *   http://www.gnu.org/licenses/gpl-3.0.txt
11 *
12 * @copyright Leibniz-Institut fuer Deutsche Sprache (http://www.ids-mannheim.de)
13 *
14 * @license http://www.gnu.org/licenses/gpl-3.0.txt
15 *  GNU General Public License v3
16 */
17package eu.clarin.sru.client.fcs;
18
19import java.io.StringWriter;
20
21import javax.xml.stream.XMLOutputFactory;
22import javax.xml.stream.XMLStreamConstants;
23import javax.xml.stream.XMLStreamException;
24import javax.xml.stream.XMLStreamReader;
25import javax.xml.stream.XMLStreamWriter;
26
27import eu.clarin.sru.client.SRUClientException;
28
29
30/**
31 * An implementation of a DataView parser that stores the content of a Data
32 * Views in String representation.
33 *
34 * @see DataViewGenericString
35 */
36public class DataViewParserGenericString implements DataViewParser {
37    private static final XMLOutputFactory factory =
38            XMLOutputFactory.newInstance();
39
40    @Override
41    public boolean acceptType(String type) {
42        return true;
43    }
44
45
46    @Override
47    public int getPriority() {
48        return Integer.MIN_VALUE;
49    }
50
51
52    @Override
53    public DataView parse(XMLStreamReader reader, String type, String pid,
54            String ref) throws XMLStreamException, SRUClientException {
55        XMLStreamWriter to = null;
56        try {
57            final StringWriter writer = new StringWriter();
58            to = factory.createXMLStreamWriter(writer);
59            copy(reader, to);
60            to.close();
61            return new DataViewGenericString(type, pid, ref, writer.toString());
62        } finally {
63            if (to != null) {
64                try {
65                    to.close();
66                } catch (XMLStreamException e) {
67                    /* IGNORE */
68                }
69            }
70        }
71    }
72
73
74    public static void copy(XMLStreamReader from, XMLStreamWriter to)
75            throws XMLStreamException {
76        int depth = 0;
77        while (from.hasNext()) {
78            depth = copyEvent(from, to, depth);
79            from.next();
80            if (depth <= 0) {
81                break;
82            }
83        } // while
84    }
85
86
87    private static int copyEvent(XMLStreamReader from, XMLStreamWriter to,
88            int depth) throws XMLStreamException {
89        switch (from.getEventType()) {
90        case XMLStreamConstants.START_DOCUMENT:
91            {
92                final String version = from.getVersion();
93                if ((version == null) || version.isEmpty()) {
94                    to.writeStartDocument();
95                } else {
96                    to.writeStartDocument(from.getCharacterEncodingScheme(),
97                            from.getVersion());
98                }
99                to.writeCharacters("\n");
100            }
101            return depth + 1;
102
103        case XMLStreamConstants.END_DOCUMENT:
104            to.writeCharacters("\n");
105            to.writeEndDocument();
106            return depth - 1;
107
108        case XMLStreamConstants.START_ELEMENT:
109            copyStartElement(from, to);
110            return depth + 1;
111
112        case XMLStreamConstants.END_ELEMENT:
113            to.writeEndElement();
114            return depth - 1;
115
116        case XMLStreamConstants.SPACE:
117            to.writeCharacters(from.getTextCharacters(), from.getTextStart(),
118                    from.getTextLength());
119            return depth;
120
121        case XMLStreamConstants.CDATA:
122            to.writeCData(from.getText());
123            return depth;
124
125        case XMLStreamConstants.CHARACTERS:
126            to.writeCharacters(from.getTextCharacters(), from.getTextStart(),
127                    from.getTextLength());
128            return depth;
129
130        case XMLStreamConstants.COMMENT:
131            to.writeComment(from.getText());
132            return depth;
133
134        case XMLStreamConstants.PROCESSING_INSTRUCTION:
135            to.writeProcessingInstruction(from.getPITarget(), from.getPIData());
136            return depth;
137
138        case XMLStreamConstants.DTD:
139        case XMLStreamConstants.ENTITY_REFERENCE:
140        case XMLStreamConstants.ATTRIBUTE:
141        case XMLStreamConstants.NAMESPACE:
142        case XMLStreamConstants.ENTITY_DECLARATION:
143        case XMLStreamConstants.NOTATION_DECLARATION:
144            /* FALL_TROUGH */
145        } // switch
146        throw new XMLStreamException("unsupported event type: " +
147                from.getEventType());
148    }
149
150    private static void copyStartElement(XMLStreamReader from,
151            XMLStreamWriter to) throws XMLStreamException {
152        final int nsCount = from.getNamespaceCount();
153        if (nsCount > 0) { // yup, got some...
154            for (int i = 0; i < nsCount; ++i) {
155                final String prefix = from.getNamespacePrefix(i);
156                final String uri = from.getNamespaceURI(i);
157                if ((prefix == null) || prefix.isEmpty()) { // default NS
158                    to.setDefaultNamespace(uri);
159                } else {
160                    to.setPrefix(prefix, uri);
161                }
162            }
163        }
164        to.writeStartElement(from.getPrefix(), from.getLocalName(),
165                from.getNamespaceURI());
166
167        if (nsCount > 0) {
168            // write namespace declarations
169            for (int i = 0; i < nsCount; ++i) {
170                final String prefix = from.getNamespacePrefix(i);
171                final String uri = from.getNamespaceURI(i);
172
173                if ((prefix == null) || prefix.isEmpty()) { // default NS
174                    to.writeDefaultNamespace(uri);
175                } else {
176                    to.writeNamespace(prefix, uri);
177                }
178            }
179        }
180        final int attrCount = from.getAttributeCount();
181        if (attrCount > 0) {
182            for (int i = 0; i < attrCount; ++i) {
183                to.writeAttribute(from.getAttributePrefix(i),
184                        from.getAttributeNamespace(i),
185                        from.getAttributeLocalName(i),
186                        from.getAttributeValue(i));
187            }
188        }
189    }
190
191} // class DataViewParserGenericString
Note: See TracBrowser for help on using the repository browser.