/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 arq;

import java.io.IOException;

import org.apache.jena.atlas.io.IO;
import org.apache.jena.atlas.lib.StrUtils ;

public class wwwenc
{
    /* http://en.wikipedia.org/wiki/Percent-encoding
     * Reserved characters after percent-encoding 
     *   !    *   "   '   (   )   ;   :   @   &   =   +   $   ,   /   ?   %   #   [   ]
     *   %21  %2A %22 %27 %28 %29 %3B %3A %40 %26 %3D %2B %24 %2C %2F %3F %25 %23 %5B %5D
     * These loose any reserved meaning if encoded.
     * 
     * Other common, but unreserved, characters after percent-encoding 
     *   <   >   ~   .   {   }   |   \   -   `   _   ^
     *   %3C %3E %7E %2E %7B %7D %7C %5C %2D %60 %5F %5E
     * 
     * Unreserved characters treated equivalent to their un-encoded form.  
     */
    public static void main(String...args) throws IOException
    {
//        RFC3986:
//        unreserved    = ALPHA / DIGIT / "-" / "." / "_" / "~"
//        reserved      = gen-delims / sub-delims
//        gen-delims    = ":" / "/" / "?" / "#" / "[" / "]" / "@"
//        sub-delims    = "!" / "$" / "&" / "'" / "(" / ")"
//                      / "*" / "+" / "," / ";" / "="
        char encode[] =
            {' ', '\n','\t',
             // gen-delims
             ':' , '/' , '?' , '#' , '[' , ']' , '@',
             // sub-delims
             '!' , '$' , '&' , '\'' , '(' , ')',
             '*' , '+' , ',' , ';' , '=',
             // general trouble
             '<', '>', '{', '}', '|', '\\', '`', '^'
            } ;
        
        if ( args.length == 0 ) {
            String x = IO.readWholeFileAsUTF8(System.in);
            String y = StrUtils.encodeHex(x, '%', encode) ;
            System.out.println(y) ;
            return;
        }       
        for ( String x : args) {
            // Not URLEncoder which does www-form-encoding.
            String y = StrUtils.encodeHex(x, '%', encode) ;
            System.out.println(y) ;
//            String s2 = URLEncoder.encode(s, "utf-8") ;
//            System.out.println(s2) ;

        }
    }
}
