source: SRUClient/trunk/src/main/java/eu/clarin/sru/client/XmlStreamReaderUtils.java @ 2466

Last change on this file since 2466 was 2466, checked in by oschonef, 11 years ago
  • update copyright (2/2)
File size: 8.5 KB
Line 
1/**
2 * This software is copyright (c) 2011-2013 by
3 *  - 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 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;
18
19import javax.xml.namespace.QName;
20import javax.xml.stream.XMLStreamException;
21import javax.xml.stream.XMLStreamReader;
22
23
24/**
25 * Helper class for dealing with {@link XMLStreamReader}.
26 * <p>
27 * <em>Note: this class is semi-public API and may be change or removed in the future.</em>
28 * </p>
29 */
30public final class XmlStreamReaderUtils {
31    private XmlStreamReaderUtils() {
32    }
33
34    public static boolean readStart(XMLStreamReader reader, String namespaceURI, String localName, boolean required)
35            throws XMLStreamException {
36        return readStart(reader, namespaceURI, localName, required, false);
37    }
38
39
40    public static boolean readStart(XMLStreamReader reader, String namespaceURI, String localName, boolean required,
41            boolean attributes) throws XMLStreamException {
42        // System.err.println("readStart (" + localName + ", required = " +
43        // required + ") @ " + toReadable(reader));
44        if (!reader.isEndElement()) {
45            while (reader.hasNext()) {
46                // System.err.println("  LOOP: " + dumpState());
47                if (reader.isWhiteSpace()) {
48                    reader.next();
49                    continue;
50                }
51                if (reader.isStartElement()) {
52                    if (namespaceURI.equals(reader.getNamespaceURI()) &&
53                            localName.equals(reader.getLocalName())) {
54                        // System.err.print("--> found ");
55                        if (!attributes) {
56                            // System.err.print("and consumed ");
57                            reader.next(); // skip to next event
58                        }
59                        // System.err.println("@ " + toReadable(reader));
60                        return true;
61                    }
62                    break;
63                }
64                if (reader.isCharacters() || reader.isEndElement()) {
65                    break;
66                }
67                reader.next();
68            } // while
69        }
70        if (required) {
71            // System.err.println("--> error, not found @ " +
72            // toReadable(reader));
73            throw new XMLStreamException("expected element '" +
74                    new QName(namespaceURI, localName) + "', but found '" +
75                    reader.getName() + "'", reader.getLocation());
76        }
77        // System.err.println("--> not found @ " + toReadable(reader));
78        return false;
79    }
80
81
82    public static void readEnd(XMLStreamReader reader, String namespaceURI, String localName)
83            throws XMLStreamException {
84        readEnd(reader, namespaceURI, localName, false);
85    }
86
87
88    public static void readEnd(XMLStreamReader reader, String namespaceURI, String localName, boolean skipContent)
89            throws XMLStreamException {
90        // System.err.println("readEnd (" + localName + ") @ " + dumpState() +
91        // ", skipContent = " + skipContent);
92        int level = 1;
93        while (reader.hasNext()) {
94            // System.err.println("  LOOP " + dumpState() + " [" +
95            // level + "]");
96            if (reader.isWhiteSpace()) {
97                reader.next();
98                continue;
99            }
100            if (skipContent) {
101                if (reader.isCharacters()) {
102                    reader.next();
103                    continue;
104                }
105                if (reader.isStartElement()) {
106                    if (!(namespaceURI.equals(reader.getNamespaceURI()) && localName
107                            .equals(reader.getLocalName()))) {
108                        level++;
109                    }
110                    reader.next();
111                    continue;
112                }
113            }
114            if (reader.isEndElement()) {
115                level--;
116                // System.err.println("   @END-TAG: " + dumpState() + " [" +
117                // level + "]");
118                if (level == 0) {
119                    if (namespaceURI.equals(reader.getNamespaceURI()) &&
120                            localName.equals(reader.getLocalName())) {
121                        reader.next(); // consume tag
122                        break;
123                    } else {
124                        throw new XMLStreamException("expected end tag for '" +
125                                new QName(namespaceURI, localName) +
126                                "', but found '" + reader.getName() + "'",
127                                reader.getLocation());
128                    }
129                }
130            }
131            reader.next();
132        }
133        // System.err.println("--> ok @ " + dumpState());
134    }
135
136
137    public static String readContent(XMLStreamReader reader, String namespaceURI, String localName, boolean required)
138            throws XMLStreamException {
139        String result = null;
140        if (readStart(reader, namespaceURI, localName, required)) {
141            result = readString(reader, true);
142            readEnd(reader, namespaceURI, localName);
143        }
144        return result;
145    }
146
147
148    public static int readContent(XMLStreamReader reader, String namespaceURI, String localName, boolean required,
149            int defaultValue) throws XMLStreamException {
150        if (readStart(reader, namespaceURI, localName, required)) {
151            String s = readString(reader, true);
152            readEnd(reader, namespaceURI, localName);
153            try {
154                return Integer.parseInt(s);
155            } catch (NumberFormatException e) {
156                throw new XMLStreamException(
157                        "expected a xs:integer value: s", e);
158            }
159        }
160        return defaultValue;
161    }
162
163
164    public static String readString(XMLStreamReader reader, boolean required) throws XMLStreamException {
165        // System.err.println("readString @ " + toReadable(reader));
166        String s = null;
167        if (reader.isCharacters()) {
168            s = reader.getText();
169            if (s != null) {
170                s = s.trim();
171            }
172            reader.next();
173        }
174        if (required && ((s == null) || s.isEmpty())) {
175            throw new XMLStreamException("expected character content "
176                    + "at position", reader.getLocation());
177        }
178        // System.err.println("--> ok @ " + toReadable(reader));
179        return s;
180    }
181
182
183    public static String readAttributeValue(XMLStreamReader reader, String namespaceURI, String localName)
184            throws XMLStreamException {
185        if (!reader.isStartElement()) {
186            throw new XMLStreamException("not at a start elment event",
187                    reader.getLocation());
188        }
189        String attr = reader.getAttributeValue(namespaceURI, localName);
190        if (attr != null) {
191            attr = attr.trim().intern();
192        }
193        return attr;
194    }
195
196
197    public static String readNamespaceURI(XMLStreamReader reader) throws XMLStreamException {
198        if (!reader.isStartElement()) {
199            throw new XMLStreamException("not at a start elment event",
200                    reader.getLocation());
201        }
202        return reader.getNamespaceURI();
203    }
204
205
206    public static String peekElementLocalName(XMLStreamReader reader) throws XMLStreamException {
207        if (!reader.isStartElement()) {
208            throw new XMLStreamException("not at a start elment event",
209                    reader.getLocation());
210        }
211        return reader.getLocalName();
212    }
213
214
215    public static void consumeStart(XMLStreamReader reader) throws XMLStreamException {
216        if (!reader.isStartElement()) {
217            throw new XMLStreamException("not at a start elment event",
218                    reader.getLocation());
219        }
220        reader.next();
221    }
222
223
224    public static void consumeWhitespace(XMLStreamReader reader) throws XMLStreamException {
225        while (reader.isWhiteSpace() && reader.hasNext()) {
226            reader.next();
227            continue;
228        }
229    }
230
231} // class XmlStreamReaderUtils
Note: See TracBrowser for help on using the repository browser.