01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19: package org.apache.axis2.databinding.utils;
20:
21: import org.apache.axiom.om.OMElement;
22: import org.apache.axiom.om.impl.builder.StAXOMBuilder;
23:
24: import javax.xml.namespace.QName;
25: import javax.xml.stream.XMLStreamConstants;
26: import javax.xml.stream.XMLStreamReader;
27:
28: public class NamedStaxOMBuilder {
29:
30: //wrap a StAXOMBuilder
31: private StAXOMBuilder builder;
32: private XMLStreamReader reader;
33: private QName nameToMatch;
34:
35: /**
36: * @param xmlStreamReader
37: * @param nameToMatch
38: */
39: public NamedStaxOMBuilder(XMLStreamReader xmlStreamReader,
40: QName nameToMatch) {
41: reader = xmlStreamReader;
42: builder = new StAXOMBuilder(xmlStreamReader);
43: this .nameToMatch = nameToMatch;
44: }
45:
46: /**
47: *
48: */
49: public OMElement getOMElement() {
50: //force to build within the given QName
51: boolean done = false;
52: int depth = 0;
53: while (!done) {
54: if (reader.getEventType() == XMLStreamConstants.END_ELEMENT) {
55: depth--;
56: } else if (reader.getEventType() == XMLStreamConstants.START_ELEMENT) {
57: depth++;
58: }
59:
60: if (depth == 0
61: && reader.getEventType() == XMLStreamConstants.END_ELEMENT
62: && nameToMatch.equals(reader.getName())) {
63: done = true;
64: } else {
65: builder.next();
66: }
67:
68: }
69:
70: return builder.getDocumentElement();
71: }
72:
73: }
|