001/*
002 * Copyright (c) 2011-2017 Nexmo Inc
003 *
004 * Permission is hereby granted, free of charge, to any person obtaining a copy
005 * of this software and associated documentation files (the "Software"), to deal
006 * in the Software without restriction, including without limitation the rights
007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008 * copies of the Software, and to permit persons to whom the Software is
009 * furnished to do so, subject to the following conditions:
010 *
011 * The above copyright notice and this permission notice shall be included in
012 * all copies or substantial portions of the Software.
013 *
014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
020 * THE SOFTWARE.
021 */
022package com.nexmo.client.legacyutils;
023
024import com.nexmo.client.NexmoResponseParseException;
025import org.w3c.dom.Document;
026
027import javax.xml.parsers.DocumentBuilder;
028import javax.xml.parsers.DocumentBuilderFactory;
029import javax.xml.parsers.ParserConfigurationException;
030import java.util.concurrent.locks.Lock;
031import java.util.concurrent.locks.ReentrantLock;
032
033public class XmlParser {
034    /**
035     * A lock associated with {@link #documentBuilder}.
036     */
037    private final Lock documentBuilderLock = new ReentrantLock();
038
039    /**
040     * Used for parsing XML data.
041     *
042     * Do not use this without locking on {@link #documentBuilderLock}
043     */
044    private DocumentBuilder documentBuilder;
045
046    /**
047     * Parse a provided XML String and return the generated DOM Document.
048     *
049     * @param xml A String containing XML.
050     * @return A Document generated from the parsed XML.
051     * @throws NexmoResponseParseException If there is a problem initializing the XML parser or parsing the XML.
052     */
053    public Document parseXml(String xml) throws NexmoResponseParseException {
054        // TODO: Maybe an Error subclass for XML initialization errors, as these are serious and unexpected.
055        Document doc;
056        this.documentBuilderLock.lock();
057        try {
058            if (this.documentBuilder == null) {
059                DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
060                this.documentBuilder = documentBuilderFactory.newDocumentBuilder();
061            }
062            doc = XmlUtil.parseXmlString(this.documentBuilder, xml);
063        } catch (ParserConfigurationException e) {
064            throw new NexmoResponseParseException("Exception initialing XML parser", e);
065        } finally {
066            this.documentBuilderLock.unlock();
067        }
068        return doc;
069    }
070}