1/*
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.xml.internal.ws.message;
27
28import com.sun.xml.internal.ws.encoding.soap.SOAP12Constants;
29import com.sun.xml.internal.ws.encoding.soap.SOAPConstants;
30import org.xml.sax.Attributes;
31import org.xml.sax.SAXException;
32import org.xml.sax.helpers.DefaultHandler;
33
34import javax.xml.namespace.QName;
35
36/**
37 * Parses the SOAP message in order to get {@link QName} of a payload element.
38 * It parses message until it
39 *
40 * @author Miroslav Kos (miroslav.kos at oracle.com)
41 */
42public class PayloadElementSniffer extends DefaultHandler {
43
44    // flag if the last element was SOAP body
45    private boolean bodyStarted;
46
47    // payloadQName used as a return value
48    private QName payloadQName;
49
50    @Override
51    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
52
53        if (bodyStarted) {
54            payloadQName = new QName(uri, localName);
55            // we have what we wanted - let's skip rest of parsing ...
56            throw new SAXException("Payload element found, interrupting the parsing process.");
57        }
58
59        // check for both SOAP 1.1/1.2
60        if (equalsQName(uri, localName, SOAPConstants.QNAME_SOAP_BODY) ||
61                equalsQName(uri, localName, SOAP12Constants.QNAME_SOAP_BODY)) {
62            bodyStarted = true;
63        }
64
65    }
66
67    private boolean equalsQName(String uri, String localName, QName qname) {
68        return qname.getLocalPart().equals(localName) &&
69                qname.getNamespaceURI().equals(uri);
70    }
71
72    public QName getPayloadQName() {
73        return payloadQName;
74    }
75}
76