<commit_before>/*
 * Copyright 2004-2005 The Apache Software Foundation or its licensors,
 *                     as applicable.
 *
 * 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 org.apache.jackrabbit.core.config;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

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

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * Configuration parser. This class is used to parse the repository and
 * workspace configuration files. Each configuration parser instance
 * contains a set of parser variables that are used for variable replacement
 * in the configuration file.
 * <p>
 * The following code sample outlines the usage of this class:
 * <pre>
 *     Properties variables = ...; // parser variables
 *     ConfigurationParser parser = new ConfigurationParser(variables);
 *     RepositoryConfig rc = parser.parseRepositoryConfig(...);
 *     WorkspaceConfig wc = parser.parseWorkspaceConfig(...);
 * </pre>
 * <p>
 * Note that the configuration objects returned by this parser are not
 * initialized. The caller needs to initialize the configuration objects
 * before using them.
 */
public class ConfigurationParser {

    /** Name of the repository home directory parser variable. */
    public static final String REPOSITORY_HOME_VARIABLE = "rep.home";

    /** Name of the workspace home directory parser variable. */
    public static final String WORKSPACE_HOME_VARIABLE = "wsp.home";

    /** Name of the repository name parser variable. */
    private static final String WORKSPACE_NAME_VARIABLE = "wsp.name";

    /** Name of the security configuration element. */
    private static final String SECURITY_ELEMENT = "Security";

    /** Name of the access manager configuration element. */
    private static final String ACCESS_MANAGER_ELEMENT = "AccessManager";

    /** Name of the login module configuration element. */
    private static final String LOGIN_MODULE_ELEMENT = "LoginModule";

    /** Name of the general workspace configuration element. */
    private static final String WORKSPACES_ELEMENT = "Workspaces";

    /** Name of the workspace configuration element. */
    private static final String WORKSPACE_ELEMENT = "Workspace";

    /** Name of the versioning configuration element. */
    private static final String VERSIONING_ELEMENT = "Versioning";

    /** Name of the file system configuration element. */
    private static final String FILE_SYSTEM_ELEMENT = "FileSystem";

    /** Name of the persistence manager configuration element. */
    private static final String PERSISTENCE_MANAGER_ELEMENT =
        "PersistenceManager";

    /** Name of the search index configuration element. */
    private static final String SEARCH_INDEX_ELEMENT = "SearchIndex";

    /** Name of the bean parameter configuration element. */
    private static final String PARAM_ELEMENT = "param";

    /** Name of the application name configuration attribute. */
    private static final String APP_NAME_ATTRIBUTE = "appName";

    /** Name of the root path configuration attribute. */
    private static final String ROOT_PATH_ATTRIBUTE = "rootPath";

    /** Name of the default workspace configuration attribute. */
    private static final String DEFAULT_WORKSPACE_ATTRIBUTE =
        "defaultWorkspace";

    /** Name of the bean implementation class configuration attribute. */
    private static final String CLASS_ATTRIBUTE = "class";

    /** Name of the bean parameter name configuration attribute. */
    private static final String NAME_ATTRIBUTE = "name";

    /** Name of the bean parameter value configuration attribute. */
    private static final String VALUE_ATTRIBUTE = "value";

    /** Name of the default search index implementation class. */
    private static final String DEFAULT_QUERY_HANDLER =
            "org.apache.jackrabbit.core.search.lucene.SearchIndex";

    /**
     * The configuration parser variables. These name-value pairs
     * are used to substitute <code>${...}</code> variable references
     * with context-dependent values in the configuration.
     *
     * @see #replaceVariables(String)
     */
    private final Properties variables;

    /**
     * Creates a new configuration parser with the given parser variables.
     *
     * @param variables parser variables
     */
    public ConfigurationParser(Properties variables) {
        this.variables = variables;
    }

    /**
     * Parses repository configuration. Repository configuration uses the
     * following format:
     * <pre>
     *   &lt;Repository&gt;
     *     &lt;FileSystem ...&gt;
     *     &lt;Security appName="..."&gt;
     *       &lt;AccessManager ...&gt;<commit_msg>Default query handler class still uses old package name.<commit_after>/*
 * Copyright 2004-2005 The Apache Software Foundation or its licensors,
 *                     as applicable.
 *
 * 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 org.apache.jackrabbit.core.config;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

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

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * Configuration parser. This class is used to parse the repository and
 * workspace configuration files. Each configuration parser instance
 * contains a set of parser variables that are used for variable replacement
 * in the configuration file.
 * <p>
 * The following code sample outlines the usage of this class:
 * <pre>
 *     Properties variables = ...; // parser variables
 *     ConfigurationParser parser = new ConfigurationParser(variables);
 *     RepositoryConfig rc = parser.parseRepositoryConfig(...);
 *     WorkspaceConfig wc = parser.parseWorkspaceConfig(...);
 * </pre>
 * <p>
 * Note that the configuration objects returned by this parser are not
 * initialized. The caller needs to initialize the configuration objects
 * before using them.
 */
public class ConfigurationParser {

    /** Name of the repository home directory parser variable. */
    public static final String REPOSITORY_HOME_VARIABLE = "rep.home";

    /** Name of the workspace home directory parser variable. */
    public static final String WORKSPACE_HOME_VARIABLE = "wsp.home";

    /** Name of the repository name parser variable. */
    private static final String WORKSPACE_NAME_VARIABLE = "wsp.name";

    /** Name of the security configuration element. */
    private static final String SECURITY_ELEMENT = "Security";

    /** Name of the access manager configuration element. */
    private static final String ACCESS_MANAGER_ELEMENT = "AccessManager";

    /** Name of the login module configuration element. */
    private static final String LOGIN_MODULE_ELEMENT = "LoginModule";

    /** Name of the general workspace configuration element. */
    private static final String WORKSPACES_ELEMENT = "Workspaces";

    /** Name of the workspace configuration element. */
    private static final String WORKSPACE_ELEMENT = "Workspace";

    /** Name of the versioning configuration element. */
    private static final String VERSIONING_ELEMENT = "Versioning";

    /** Name of the file system configuration element. */
    private static final String FILE_SYSTEM_ELEMENT = "FileSystem";

    /** Name of the persistence manager configuration element. */
    private static final String PERSISTENCE_MANAGER_ELEMENT =
        "PersistenceManager";

    /** Name of the search index configuration element. */
    private static final String SEARCH_INDEX_ELEMENT = "SearchIndex";

    /** Name of the bean parameter configuration element. */
    private static final String PARAM_ELEMENT = "param";

    /** Name of the application name configuration attribute. */
    private static final String APP_NAME_ATTRIBUTE = "appName";

    /** Name of the root path configuration attribute. */
    private static final String ROOT_PATH_ATTRIBUTE = "rootPath";

    /** Name of the default workspace configuration attribute. */
    private static final String DEFAULT_WORKSPACE_ATTRIBUTE =
        "defaultWorkspace";

    /** Name of the bean implementation class configuration attribute. */
    private static final String CLASS_ATTRIBUTE = "class";

    /** Name of the bean parameter name configuration attribute. */
    private static final String NAME_ATTRIBUTE = "name";

    /** Name of the bean parameter value configuration attribute. */
    private static final String VALUE_ATTRIBUTE = "value";

    /** Name of the default search index implementation class. */
    private static final String DEFAULT_QUERY_HANDLER =
            "org.apache.jackrabbit.core.query.lucene.SearchIndex";

    /**
     * The configuration parser variables. These name-value pairs
     * are used to substitute <code>${...}</code> variable references
     * with context-dependent values in the configuration.
     *
     * @see #replaceVariables(String)
     */
    private final Properties variables;

    /**
     * Creates a new configuration parser with the given parser variables.
     *
     * @param variables parser variables
     */
    public ConfigurationParser(Properties variables) {
        this.variables = variables;
    }

    /**
     * Parses repository configuration. Repository configuration uses the
     * following format:
     * <pre>
     *   &lt;Repository&gt;
     *     &lt;FileSystem ...&gt;
     *     &lt;Security appName="..."&gt;
     *       &lt;AccessManager ...&gt;
{'fraction_non_alphanumeric': 0.08462379304062671, 'fraction_numerical': 0.003097103297504099, 'mean_word_length': 3.3916, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 10}, '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': '139146', 'n_tokens_mistral': 2749, 'n_tokens_neox': 2683, 'n_words': 1069}