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

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