aboutsummaryrefslogtreecommitdiff
path: root/src/com/kenai/jbosh/BodyParserSAX.java
blob: 54c6c017cd34f8c459ff5b373e716f5afc53da74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
 * Copyright 2009 Mike Cumings
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.kenai.jbosh;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * Implementation of the BodyParser interface which uses the SAX API
 * that is part of the JDK.  Due to the fact that we can cache and reuse
 * SAXPArser instances, this has proven to be significantly faster than the
 * use of the javax.xml.stream API introduced in Java 6 while simultaneously
 * providing an implementation accessible to Java 5 users.
 */
final class BodyParserSAX implements BodyParser {

    /**
     * Logger.
     */
    private static final Logger LOG =
            Logger.getLogger(BodyParserSAX.class.getName());

    /**
     * SAX parser factory.
     */
    private static final SAXParserFactory SAX_FACTORY;
    static {
        SAX_FACTORY = SAXParserFactory.newInstance();
        SAX_FACTORY.setNamespaceAware(true);
        SAX_FACTORY.setValidating(false);
    }

    /**
     * Thread local to contain a SAX parser instance for each thread that
     * attempts to use one.  This allows us to gain an order of magnitude of
     * performance as a result of not constructing parsers for each
     * invocation while retaining thread safety.
     */
    private static final ThreadLocal<SoftReference<SAXParser>> PARSER =
        new ThreadLocal<SoftReference<SAXParser>>() {
            @Override protected SoftReference<SAXParser> initialValue() {
                return new SoftReference<SAXParser>(null);
            }
        };

    /**
     * SAX event handler class.
     */
    private static final class Handler extends DefaultHandler {
        private final BodyParserResults result;
        private final SAXParser parser;
        private String defaultNS = null;

        private Handler(SAXParser theParser, BodyParserResults results) {
            parser = theParser;
            result = results;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public void startElement(
                final String uri,
                final String localName,
                final String qName,
                final Attributes attributes) {
            if (LOG.isLoggable(Level.FINEST)) {
                LOG.finest("Start element: " + qName);
                LOG.finest("    URI: " + uri);
                LOG.finest("    local: " + localName);
            }

            BodyQName bodyName = AbstractBody.getBodyQName();
            // Make sure the first element is correct
            if (!(bodyName.getNamespaceURI().equals(uri)
                    && bodyName.getLocalPart().equals(localName))) {
                throw(new IllegalStateException(
                        "Root element was not '" + bodyName.getLocalPart()
                        + "' in the '" + bodyName.getNamespaceURI()
                        + "' namespace.  (Was '" + localName + "' in '" + uri
                        + "')"));
            }

            // Read in the attributes, making sure to expand the namespaces
            // as needed.
            for (int idx=0; idx < attributes.getLength(); idx++) {
                String attrURI = attributes.getURI(idx);
                if (attrURI.length() == 0) {
                    attrURI = defaultNS;
                }
                String attrLN = attributes.getLocalName(idx);
                String attrVal = attributes.getValue(idx);
                if (LOG.isLoggable(Level.FINEST)) {
                    LOG.finest("    Attribute: {" + attrURI + "}"
                            + attrLN + " = '" + attrVal + "'");
                }

                BodyQName aqn = BodyQName.create(attrURI, attrLN);
                result.addBodyAttributeValue(aqn, attrVal);
            }
            
            parser.reset();
        }

        /**
         * {@inheritDoc}
         *
         * This implementation uses this event hook to keep track of the
         * default namespace on the body element.
         */
        @Override
        public void startPrefixMapping(
                final String prefix,
                final String uri) {
            if (prefix.length() == 0) {
                if (LOG.isLoggable(Level.FINEST)) {
                    LOG.finest("Prefix mapping: <DEFAULT> => " + uri);
                }
                defaultNS = uri;
            } else {
                if (LOG.isLoggable(Level.FINEST)) {
                    LOG.info("Prefix mapping: " + prefix + " => " + uri);
                }
            }
        }
    }

    ///////////////////////////////////////////////////////////////////////////
    // BodyParser interface methods:

    /**
     * {@inheritDoc}
     */
    public BodyParserResults parse(String xml) throws BOSHException {
        BodyParserResults result = new BodyParserResults();
        Exception thrown;
        try {
            InputStream inStream = new ByteArrayInputStream(xml.getBytes());
            SAXParser parser = getSAXParser();
            parser.parse(inStream, new Handler(parser, result));
            return result;
        } catch (SAXException saxx) {
            thrown = saxx;
        } catch (IOException iox) {
            thrown = iox;
        }
        throw(new BOSHException("Could not parse body:\n" + xml, thrown));
    }

    ///////////////////////////////////////////////////////////////////////////
    // Private methods:

    /**
     * Gets a SAXParser for use in parsing incoming messages.
     *
     * @return parser instance
     */
    private static SAXParser getSAXParser() {
        SoftReference<SAXParser> ref = PARSER.get();
        SAXParser result = ref.get();
        if (result == null) {
            Exception thrown;
            try {
                result = SAX_FACTORY.newSAXParser();
                ref = new SoftReference<SAXParser>(result);
                PARSER.set(ref);
                return result;
            } catch (ParserConfigurationException ex) {
                thrown = ex;
            } catch (SAXException ex) {
                thrown = ex;
            }
            throw(new IllegalStateException(
                    "Could not create SAX parser", thrown));
        } else {
            result.reset();
            return result;
        }
    }
    
}