<commit_before>/*

Copyright (c) 2010, <NAME> All rights reserved. 

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met: 

- Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer. 

- Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution. 

- Neither the name of jactiveresource.org nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

 */

package org.jactiveresource;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.StringTokenizer;

/**
 * Assemble valid URL's from component parts, encoding as necessary. There are
 * methods to add path components and query strings.
 * 
 * <h3>Anatomy of a URL</h3>
 * <p>
 * A picture is worth a thousand words, especially if it's ASCII Art. This
 * stunning image is reproduced from <a
 * href="http://tools.ietf.org/html/rfc3986">RFC 3986</a>
 * 
 * <code>
 * <pre>
 *        foo://example.com:8042/over/there?name=ferret#nose
 *        \_/   \______________/\_________/ \_________/ \__/
 *         |           |            |            |        |
 *      scheme     authority       path        query   fragment
 * </pre>
 * </code>
 * 
 * The URL class in Java is too stringent to accept our use cases here, namely
 * you can not create a URL class that excludes the scheme and authorith
 * portions of a URL. However, Java does include a URI class which is suitable
 * for our needs.
 * 
 * <h3>Usage Overview</h3>
 * <p>
 * The URLBuilder class doesn't help you build the scheme or authority parts of
 * a URL. The URI class in java.net handles that part perfectly well. This class
 * helps you with the path, query, and fragment parts of a URL.
 * <p>
 * Since this class relies on the URI class to handle the scheme and authority
 * parts of a URI, we frequently refer to them together. We've chosen the term
 * <code>base</code>. It's short and descriptive.
 * <p>
 * Here's a few examples of using URLBuilder to construct URL's. Here's an easy
 * one:
 * 
 * <code>
 * <pre>
 * URI base = new URI("http://www.example.com");
 * URLBuilder urlb = new URLBuilder(base);
 * urlb.add("yellow").add("brick");
 * urlb.add("road.html");
 * assertEquals("http://www.example.com/yellow/brick/road.html",urlb.toString());
 * </pre>
 * </code>
 * 
 * Here's one with a query string:
 * 
 * <code>
 * <pre>
 * URI base = new URI("http://www.example.com");<commit_msg>Fix typo in javadocs<commit_after>/*

Copyright (c) 2010, <NAME> All rights reserved. 

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met: 

- Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer. 

- Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution. 

- Neither the name of jactiveresource.org nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

 */

package org.jactiveresource;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.StringTokenizer;

/**
 * Assemble valid URL's from component parts, encoding as necessary. There are
 * methods to add path components and query strings.
 * 
 * <h3>Anatomy of a URL</h3>
 * <p>
 * A picture is worth a thousand words, especially if it's ASCII Art. This
 * stunning image is reproduced from <a
 * href="http://tools.ietf.org/html/rfc3986">RFC 3986</a>
 * 
 * <code>
 * <pre>
 *        foo://example.com:8042/over/there?name=ferret#nose
 *        \_/   \______________/\_________/ \_________/ \__/
 *         |           |            |            |        |
 *      scheme     authority       path        query   fragment
 * </pre>
 * </code>
 * 
 * The URL class in Java is too stringent to accept our use cases here, namely
 * you can not create a URL class that excludes the scheme and authority
 * portions of a URL. However, Java does include a URI class which is suitable
 * for our needs.
 * 
 * <h3>Usage Overview</h3>
 * <p>
 * The URLBuilder class doesn't help you build the scheme or authority parts of
 * a URL. The URI class in java.net handles that part perfectly well. This class
 * helps you with the path, query, and fragment parts of a URL.
 * <p>
 * Since this class relies on the URI class to handle the scheme and authority
 * parts of a URI, we frequently refer to them together. We've chosen the term
 * <code>base</code>. It's short and descriptive.
 * <p>
 * Here's a few examples of using URLBuilder to construct URL's. Here's an easy
 * one:
 * 
 * <code>
 * <pre>
 * URI base = new URI("http://www.example.com");
 * URLBuilder urlb = new URLBuilder(base);
 * urlb.add("yellow").add("brick");
 * urlb.add("road.html");
 * assertEquals("http://www.example.com/yellow/brick/road.html",urlb.toString());
 * </pre>
 * </code>
 * 
 * Here's one with a query string:
 * 
 * <code>
 * <pre>
 * URI base = new URI("http://www.example.com");
{'fraction_non_alphanumeric': 0.08528496006497902, 'fraction_numerical': 0.005414918099363747, 'mean_word_length': 3.6582597730138713, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3827208', 'n_tokens_mistral': 2174, 'n_tokens_neox': 1977, 'n_words': 916}