<commit_before>/*
 * Copyright 2010 Google 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.
 */

package com.google.appengine.tools.mapreduce;

import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

/**
 * This object preprocesses a Configuration template, filling in template values
 * (those for which the {@code value} XML tag has a {@code template}
 *  XML attribute) using a provided map of names to values. 
 * 
 * <p>Additionally, when given the template XML, the object can generate a map
 * between map name and user visible name for ease of presentation.
 * 
 * <p>As a concrete example, suppose we have the the following XML string:
 * 
 * <pre>
 * <property>
 *   <name human="Username to analyze">username</name>
 *   <value template="required" />
 * </property>
 * <property>
 *   <name>basedir</name>
 *   <value>/user/${username}</value>
 * </property>
 * </pre>
 * 
 * <p>If we have that XML string stored in xmlString, then calling
 * 
 * <pre>
 * Map<String, String> parameters = new Map<String, String>();
 * parameters.put("username", "Bob");
 * new ConfigurationTemplatePreprocessor(xmlString).preprocess(parameters);
 * </pre>
 * 
 * <p>returns the string:
 * 
 * <pre>
 * <property>
 *   <name>username</name>
 *   <value>Bob</value>
 * </property>
 * <property>
 *   <name>basedir</name>
 *   <value>/user/${username}</value>
 * </property>
 * </pre>
 * 
 * <p>Hadoop's {@link org.apache.hadoop.conf.Configuration} class will then
 * populate the {@code basedir} property with the value {@code /user/Bob} as
 * usual.
 * 
 * <p>You can also enumerate the map from name to user visible name. For 
 * instance, given the same starting XML document
 * 
 * <pre>
 * new ConfigurationTemplatePreprocessor(xmlString).getHumanNameMap();
 * </pre>
 * 
 * <p> returns the {@code Map} corresponding to:
 * 
 * <pre>
 * {"username" => "Username to analyze"}
 * </pre>
 * 
 * <p>You can also specify that the child text of a templated value should be
 * used as the default if no value is given by the template="optional"
 * attribute. For instance, if no parameters are given, then:
 * 
 * <pre>
 * <property>
 *   <name human="Username to analyze">username</name>
 *   <value template="optional">Bob</value>
 * </property>
 * </pre>
 * 
 * <p>evaluates to simply:
 * 
 * <pre>
 * <property>
 *   <name>username</name>
 *   <value>Bob</value>
 * </property>
 * </pre>
 * 
 * @author <EMAIL> (<NAME>)
 */
public class ConfigurationTemplatePreprocessor {
  private Document doc;
  
  /**
   * DDO for the information needed to render the form element for a  single 
   * template property.
   */
  public static class TemplateEntryMetadata {
    private String name;
    private String humanName;
    private String defaultValue;
    
    /**
     * 
     * @param humanName Human readable name or the 
     * @param defaultValue
     */
    public TemplateEntryMetadata(String name, String humanName, String defaultValue) {
      this.name = name;
      this.humanName = humanName;
      this.defaultValue = defaultValue;
    }
    <commit_msg>HTML escapify XML in ConfigurationTemplatePreprocessor.java<commit_after>/*
 * Copyright 2010 Google 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.
 */

package com.google.appengine.tools.mapreduce;

import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

/**
 * This object preprocesses a Configuration template, filling in template values
 * (those for which the {@code value} XML tag has a {@code template}
 *  XML attribute) using a provided map of names to values. 
 * 
 * <p>Additionally, when given the template XML, the object can generate a map
 * between map name and user visible name for ease of presentation.
 * 
 * <p>As a concrete example, suppose we have the the following XML string:
 * 
 * <pre>
 * <property>
 *   <name human="Username to analyze">username</name>
 *   <value template="required" />
 * </property>
 * <property>
 *   <name>basedir</name>
 *   <value>/user/${username}</value>
 * </property>
 * </pre>
 * 
 * <p>If we have that XML string stored in xmlString, then calling
 * 
 * <pre>
 * Map<String, String> parameters = new Map<String, String>();
 * parameters.put("username", "Bob");
 * new ConfigurationTemplatePreprocessor(xmlString).preprocess(parameters);
 * </pre>
 * 
 * <p>returns the string:
 * 
 * <pre>
 * &lt;property&gt;
 *   &lt;name&gt;username&lt;/name&gt;
 *   &lt;value&gt;Bob&lt;/value&gt;
 * &lt;/property&gt;
 * &lt;property&gt;
 *   &lt;name&gt;basedir&lt;/name&gt;
 *   &lt;value&gt;/user/${username}&lt;/value&gt;
 * &lt;/property&gt;
 * </pre>
 * 
 * <p>Hadoop's {@link org.apache.hadoop.conf.Configuration} class will then
 * populate the {@code basedir} property with the value {@code /user/Bob} as
 * usual.
 * 
 * <p>You can also enumerate the map from name to user visible name. For 
 * instance, given the same starting XML document
 * 
 * <pre>
 * new ConfigurationTemplatePreprocessor(xmlString).getHumanNameMap();
 * </pre>
 * 
 * <p> returns the {@code Map} corresponding to:
 * 
 * <pre>
 * {"username" =&gt; "Username to analyze"}
 * </pre>
 * 
 * <p>You can also specify that the child text of a templated value should be
 * used as the default if no value is given by the template="optional"
 * attribute. For instance, if no parameters are given, then:
 * 
 * <pre>
 * &lt;property&gt;
 *   &lt;name human="Username to analyze"&gt;username&lt;/name&gt;
 *   &lt;value template="optional"&gt;Bob&lt;/value&gt;
 * &lt;/property&gt;
 * </pre>
 * 
 * <p>evaluates to simply:
 * 
 * <pre>
 * &lt;property&gt;
 *   &lt;name&gt;username&lt;/name&gt;
 *   &lt;value&gt;Bob&lt;/value&gt;
 * &lt;/property&gt;
 * </pre>
 * 
 * @author <EMAIL> (<NAME>)
 */
public class ConfigurationTemplatePreprocessor {
  private Document doc;
  
  /**
   * DDO for the information needed to render the form element for a  single 
   * template property.
   */
  public static class TemplateEntryMetadata {
    private String name;
    private String humanName;
    private String defaultValue;
    
    /**
     * 
     * @param humanName Human readable name or the 
     * @param defaultValue
     */
    public TemplateEntryMetadata(String name, String humanName, String defaultValue) {
      this.name = name;
      this.humanName = humanName;
      this.defaultValue = defaultValue;
    }
    
{'fraction_non_alphanumeric': 0.12022336581627066, 'fraction_numerical': 0.002846819226979087, 'mean_word_length': 3.974945533769063, 'pattern_counts': {'":': 0, '<': 103, '<?xml version=': 0, '>': 104, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 28}, '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': '5399835', 'n_tokens_mistral': 2753, 'n_tokens_neox': 2676, 'n_words': 845}