/*
 * Copyright (c) 2017 XMLmind Software. All rights reserved. 
 *
 * Author: Hussein Shafie
 *
 * This file is part of the XMLmind W2X project.
 * For conditions of distribution and use, see the accompanying legal.txt file.
 */
package com.xmlmind.w2x_servlet;

import java.io.UnsupportedEncodingException;
import com.xmlmind.util.StringUtil;

/*package*/ final class FileNameUtil {
    private FileNameUtil() {}

    // -----------------------------------------------------------------------

    public static final String escapeQuotes(String text) {
        if (text.indexOf('"') >= 0) {
            text = StringUtil.replaceAll(text, "\"", "\\\"");
        }
        return text;
    }

    // -----------------------------------------------------------------------

    public static boolean isASCII(String s) {
        final int charCount = s.length();
        for (int i = 0; i < charCount; ++i) {
            if (s.charAt(i) > 127) {
                return false;
            }
        }

        return true;
    }

    private static final String ASCII =
    "AaEeIiOoUu" +    // grave
    "AaEeIiOoUuYy" +  // acute
    "AaEeIiOoUuYy" +  // circumflex
    "AaOoNn" +        // tilde
    "AaEeIiOoUuYy" +  // umlaut
    "Aa" +            // ring
    "Oo" +            // slash
    "Cc" +            // cedilla
    "AaOo" +          // latin ligatures
    "sE" +            // eszett, euro
    "OoUu";           // double acute
    
    private static final String ASCII2 =
    "          " +    // grave
    "            " +  // acute
    "            " +  // circumflex
    "      " +        // tilde
    "            " +  // umlaut
    "  " +            // ring
    "  " +            // slash
    "  " +            // cedilla
    "EeEe" +          // latin ligatures, eszett
    "sU" +            // eszett, euro
    "    ";           // double acute
    
    private static final String UNICODE =
    "\u00C0\u00E0\u00C8\u00E8\u00CC\u00EC\u00D2\u00F2\u00D9\u00F9" +
    "\u00C1\u00E1\u00C9\u00E9\u00CD\u00ED\u00D3\u00F3\u00DA\u00FA\u00DD\u00FD" +
    "\u00C2\u00E2\u00CA\u00EA\u00CE\u00EE\u00D4\u00F4\u00DB\u00FB\u0176\u0177" +
    "\u00C3\u00E3\u00D5\u00F5\u00D1\u00F1" +
    "\u00C4\u00E4\u00CB\u00EB\u00CF\u00EF\u00D6\u00F6\u00DC\u00FC\u0178\u00FF" +
    "\u00C5\u00E5" +
    "\u00D8\u00F8" +                       // slash
    "\u00C7\u00E7" +
    "\u00C6\u00E6\u0152\u0153" +           // latin ligatures
    "\u00DF\u20AC" +                       // eszett, euro
    "\u0150\u0151\u0170\u0171";

    public static String toASCII(String s) {
        StringBuilder buffer = new StringBuilder();

        final int charCount = s.length();
        for (int i = 0; i < charCount; ++i) {
            char c = s.charAt(i);

            int pos = UNICODE.indexOf(c);
            if (pos >= 0) {
                buffer.append(ASCII.charAt(pos));

                char ascii2 = ASCII2.charAt(pos);
                if (ascii2 != ' ') {
                    buffer.append(ascii2);
                }
            } else {
                buffer.append(c);
            }
        }

        return buffer.toString();
    }

    /*
    public static void main(String[] args) throws IOException {
        if (args.length != 1) {
            System.err.println(
              "usage: java com.xmlmind.w2x_servlet.FileNameUtil out_xml_file");
            System.exit(1);
        }

        StringBuilder buffer = 
            new StringBuilder("<?xml version='1.0'?>\n<test>\n<unico>");
        buffer.append(UNICODE);
        buffer.append("</unico>\n<ascii>");
        buffer.append(toASCII(UNICODE));
        buffer.append("</ascii>\n</test>\n");

        FileUtil.saveString(buffer.toString(), new File(args[0]), "UTF-8");
    }
    */

    // -----------------------------------------------------------------------

    public static String encode(String s, String charset) {
        StringBuilder buffer = new StringBuilder();

        final int length = s.length();
        for (int i = 0; i < length; ++i) {
            char c = s.charAt(i);

            if ((c >= 'a' && c <= 'z') ||
                (c >= 'A' && c <= 'Z') ||
                (c >= '0' && c <= '9') ||
                c == '.' || c == '-' || c == '_') {
                buffer.append(c);
            } else {
                final byte[] bytes;
                try {
                    bytes = (Character.toString(c)).getBytes(charset);
                } catch (UnsupportedEncodingException shouldNothappen) {
                    shouldNothappen.printStackTrace();
                    return null;
                }

                final int byteCount = bytes.length;
                for (int j = 0; j < byteCount; ++j) {
                    String hex = Integer.toHexString(bytes[j] & 0xFF);

                    buffer.append('%');
                    if (hex.length() == 1) {
                        buffer.append('0');
                    }
                    buffer.append(hex.toUpperCase());
                }
            }
        }

        return buffer.toString();
    }
}
