content: /*******************************************************************************
   * Software Name : RCS IMS Stack
   *
   * Copyright (C) 2010-2016 Orange.
   * Copyright (C) 2015 Sony Mobile Communications Inc.
   *
   * 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.
   *
   * NOTE: This file has been modified by Sony Mobile Communications Inc.
   * Modifications are licensed under the License.
   ******************************************************************************/
  
  package com.gsma.rcs.core.ims.service.presence.xdm;
  
  import com.gsma.rcs.utils.logger.Logger;
  
  import org.xml.sax.Attributes;
  import org.xml.sax.InputSource;
  import org.xml.sax.SAXException;
  import org.xml.sax.SAXParseException;
  import org.xml.sax.helpers.DefaultHandler;
  
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.List;
  
  import javax.xml.parsers.ParserConfigurationException;
  import javax.xml.parsers.SAXParser;
  import javax.xml.parsers.SAXParserFactory;
  
  /**
   * XCAP response parser
   * 
   * @author jexa7410
   */
  public class XcapResponseParser extends DefaultHandler {
  
      private StringBuffer accumulator;
  
      private List<String> uriList = new ArrayList<String>();
  
      /**
       * The logger
       */
      private Logger logger = Logger.getLogger(this.getClass().getName());
  
      /**
       * Constructor
       * 
       * @param inputSource Input source
       * @throws SAXException
       * @throws ParserConfigurationException
       * @throws IOException
       */
      public XcapResponseParser(InputSource inputSource) throws ParserConfigurationException,
              SAXException, IOException {
          SAXParserFactory factory = SAXParserFactory.newInstance();
          SAXParser parser = factory.newSAXParser();
          parser.parse(inputSource, this);
      }
  
      public void startDocument() {
          if (logger.isActivated()) {
              logger.debug("Start document");
          }
          accumulator = new StringBuffer();
      }
  
      public void characters(char buffer[], int start, int length) {
          accumulator.append(buffer, start, length);
      }
  
      public void startElement(String namespaceURL, String localName, String qname, Attributes attr) {
          accumulator.setLength(0);
  
          if (localName.equals("entry")) {
              String uri = attr.getValue("uri").trim();
              uriList.add(uri);
          }
      }
  
      public void endElement(String namespaceURL, String localName, String qname) {
      }
  
      public void endDocument() {
          if (logger.isActivated()) {
              logger.debug("End document");
          }
      }
  
      public void warning(SAXParseException exception) {
          if (logger.isActivated()) {
              logger.error("Warning: line " + exception.getLineNumber() + ": "
                      + exception.getMessage());
          }
      }
  
      public void error(SAXParseException exception) {
          if (logger.isActivated()) {
              logger.error("Error: line " + exception.getLineNumber() + ": " + exception.getMessage());
          }
      }
  
      public void fatalError(SAXParseException exception) throws SAXException {
          if (logger.isActivated()) {
              logger.error("Fatal: line " + exception.getLineNumber() + ": " + exception.getMessage());
          }
          throw exception;
      }
  
      public List<String> getUris() {
          return uriList;
      }
  }
  