<commit_before>/*
 * Copyright (c) 2005-2010 Grameen Foundation USA
 * All rights reserved.
 *
 * 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.
 *
 * See also http://www.apache.org/licenses/LICENSE-2.0.html for an
 * explanation of the license and how it is applied.
 */

package org.mifos.customers.ppi.helpers;

import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.mifos.config.GeneralConfig;
import org.mifos.core.ClasspathResource;
import org.mifos.customers.ppi.business.PPIChoice;
import org.mifos.customers.ppi.business.PPILikelihood;
import org.mifos.customers.ppi.business.PPISurvey;
import org.mifos.customers.surveys.business.Question;
import org.mifos.customers.surveys.business.QuestionChoice;
import org.mifos.customers.surveys.business.SurveyQuestion;
import org.mifos.customers.surveys.helpers.AnswerType;
import org.mifos.framework.exceptions.ValidationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XmlPPISurveyParser {

    /** TODO: Should be private */
    public PPISurvey parseInto(String uri, PPISurvey survey) throws URISyntaxException, IOException,
            ParserConfigurationException, SAXException {
        InputStream xml = ClasspathResource.getURI(uri).toURL().openStream();
        return parseInto(xml, survey);
    }

    private PPISurvey parseInto(InputStream stream, PPISurvey survey) throws ParserConfigurationException, IOException,
            SAXException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(stream);

        return parseInto(document, survey);
    }

    PPISurvey parseInto(Document document, PPISurvey survey) {
        Element docElement = document.getDocumentElement();
        parseSurveyName(survey, docElement);
        parseSurveyCountry(survey, docElement);
        parseSurveyQuestions(survey, docElement);
        parseSurveyLikelihoods(survey, docElement);<commit_msg>Deprecate an unused XML parser from the old surveys code<commit_after>/*
 * Copyright (c) 2005-2010 Grameen Foundation USA
 * All rights reserved.
 *
 * 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.
 *
 * See also http://www.apache.org/licenses/LICENSE-2.0.html for an
 * explanation of the license and how it is applied.
 */

package org.mifos.customers.ppi.helpers;

import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.mifos.config.GeneralConfig;
import org.mifos.core.ClasspathResource;
import org.mifos.customers.ppi.business.PPIChoice;
import org.mifos.customers.ppi.business.PPILikelihood;
import org.mifos.customers.ppi.business.PPISurvey;
import org.mifos.customers.surveys.business.Question;
import org.mifos.customers.surveys.business.QuestionChoice;
import org.mifos.customers.surveys.business.SurveyQuestion;
import org.mifos.customers.surveys.helpers.AnswerType;
import org.mifos.framework.exceptions.ValidationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * WARNING, this is old code, and will be removed.
 *
 * @deprecated As of Mifos release Leila E, replaced by
 *             {@link org.mifos.platform.questionnaire.parsers.QuestionGroupDefinitionParser}.
 */
public class XmlPPISurveyParser {

    /** TODO: Should be private */
    public PPISurvey parseInto(String uri, PPISurvey survey) throws URISyntaxException, IOException,
            ParserConfigurationException, SAXException {
        InputStream xml = ClasspathResource.getURI(uri).toURL().openStream();
        return parseInto(xml, survey);
    }

    private PPISurvey parseInto(InputStream stream, PPISurvey survey) throws ParserConfigurationException, IOException,
            SAXException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(stream);

        return parseInto(document, survey);
    }

    PPISurvey parseInto(Document document, PPISurvey survey) {
        Element docElement = document.getDocumentElement();
        parseSurveyName(survey, docElement);
        parseSurveyCountry(survey, docElement);
        parseSurveyQuestions(survey, docElement);
        parseSurveyLikelihoods(survey, docElement);
{'fraction_non_alphanumeric': 0.08811815466312645, 'fraction_numerical': 0.005974112180550946, 'mean_word_length': 4.362099644128114, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 12}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '422405', 'n_tokens_mistral': 1741, 'n_tokens_neox': 1712, 'n_words': 456}