<commit_before>/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2001-04 The eXist Project
 *  http://exist-db.org
 *  
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *  
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *  
 *  $Id$
 */
package org.exist.xquery.functions;

import org.exist.dom.QName;
import org.exist.util.UTF8;
import org.exist.xquery.BasicFunction;
import org.exist.xquery.Cardinality;
import org.exist.xquery.FunctionSignature;
import org.exist.xquery.Module;
import org.exist.xquery.XPathException;
import org.exist.xquery.XQueryContext;
import org.exist.xquery.value.Sequence;
import org.exist.xquery.value.SequenceType;
import org.exist.xquery.value.StringValue;
import org.exist.xquery.value.Type;

/**
 * @author wolf
 *
 */
public class FunEscapeURI extends BasicFunction {

    public final static FunctionSignature signature =
        new FunctionSignature(
            new QName("escape-uri", Module.BUILTIN_FUNCTION_NS),
            "This function applies the URI escaping rules defined in section 2 " +
            "of [RFC 2396] as amended by [RFC 2732], with one exception, to " +
            "the string supplied as $uri-part, which typically represents all or part " +
            "of a URI. The effect of the function is to escape a set of identified " +
            "characters in the string. Each such character is replaced in the string " +
            "by an escape sequence, which is formed by encoding the character " +
            "as a sequence of octets in UTF-8, and then representing each of these " +
            "octets in the form %HH, where HH is the hexadecimal representation " +
            "of the octet.",
            new SequenceType[] {
                 new SequenceType(Type.STRING, Cardinality.ZERO_OR_ONE),
                 new SequenceType(Type.BOOLEAN, Cardinality.EXACTLY_ONE)
            },
            new SequenceType(Type.STRING, Cardinality.EXACTLY_ONE));
    
    public FunEscapeURI(XQueryContext context) {
        super(context, signature);
    }
    
    public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
        if (args[0].getLength() == 0)
            return StringValue.EMPTY_STRING;
        String uri = args[0].getStringValue();
        boolean escapeReserved = args[1].effectiveBooleanValue();
        return new StringValue(escape(uri, escapeReserved));
    }
    
    /**
     * Does the actual escaping. This method is copied from <NAME>ay's
     * saxon (see http://saxon.sf.net).
     * 
     * @param s
     * @param escapeReserved
     * @return
     */
    public static String escape(CharSequence s, boolean escapeReserved) {
        StringBuffer sb = new StringBuffer(s.length());<commit_msg>Made function documentation a bit clearer<commit_after>/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2001-04 The eXist Project
 *  http://exist-db.org
 *  
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *  
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *  
 *  $Id$
 */
package org.exist.xquery.functions;

import org.exist.dom.QName;
import org.exist.util.UTF8;
import org.exist.xquery.BasicFunction;
import org.exist.xquery.Cardinality;
import org.exist.xquery.FunctionSignature;
import org.exist.xquery.Module;
import org.exist.xquery.XPathException;
import org.exist.xquery.XQueryContext;
import org.exist.xquery.value.Sequence;
import org.exist.xquery.value.SequenceType;
import org.exist.xquery.value.StringValue;
import org.exist.xquery.value.Type;

/**
 * @author wolf
 *
 */
public class FunEscapeURI extends BasicFunction {

    public final static FunctionSignature signature =
        new FunctionSignature(
            new QName("escape-uri", Module.BUILTIN_FUNCTION_NS),
            "This function applies the URI escaping rules defined in section 2 " +
            "of [RFC 2396] as amended by [RFC 2732], with one exception, to " +
            "the string supplied as $a, which typically represents all or part " +
            "of a URI. The effect of the function is to escape a set of identified " +
            "characters in the string. Each such character is replaced in the string " +
            "by an escape sequence, which is formed by encoding the character " +
            "as a sequence of octets in UTF-8, and then representing each of these " +
            "octets in the form %HH, where HH is the hexadecimal representation " +
            "of the octet. $b indicates whether to escape reserved characters.",
            new SequenceType[] {
                 new SequenceType(Type.STRING, Cardinality.ZERO_OR_ONE),
                 new SequenceType(Type.BOOLEAN, Cardinality.EXACTLY_ONE)
            },
            new SequenceType(Type.STRING, Cardinality.EXACTLY_ONE));
    
    public FunEscapeURI(XQueryContext context) {
        super(context, signature);
    }
    
    public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
        if (args[0].getLength() == 0)
            return StringValue.EMPTY_STRING;
        String uri = args[0].getStringValue();
        boolean escapeReserved = args[1].effectiveBooleanValue();
        return new StringValue(escape(uri, escapeReserved));
    }
    
    /**
     * Does the actual escaping. This method is copied from <NAME>'s
     * saxon (see http://saxon.sf.net).
     * 
     * @param s
     * @param escapeReserved
     * @return
     */
    public static String escape(CharSequence s, boolean escapeReserved) {
        StringBuffer sb = new StringBuffer(s.length());
{'fraction_non_alphanumeric': 0.08268468203847848, 'fraction_numerical': 0.008811866647084741, 'mean_word_length': 2.8847689674843124, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '1783168', 'n_tokens_mistral': 1907, 'n_tokens_neox': 1835, 'n_words': 689}