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

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