Popular Posts
Enable SSL connection for Jsoup import org.jsoup.Connection; import org.jsoup.Jsoup; import javax.net.ssl.*; import java.io.IOException; import java.security.KeyManagement... Get files name in batch Windows Batch : get filename using [%%~ni] Linux Shell Script : get filename using [awk, sed] windows sample : rename all *.htm file to ... Translating 2.0 <html> <head>     <title>Translation 2.0</title>     <meta http-equiv="content-type" content="text...
Stats
JSON Foramter
<html>
<head>
    <title>JSON Formater</title>
    <style type="text/css">
    body{
        margin: 0px;
        padding: 0px;
        overflow: hidden;
    }
    </style>
    <script type="text/javascript">
        var JSONFormater = {
            indentType: "\t",
            source: false,
            output: "",
            format: function() {
                if (document.getElementById("inputJSON").value.length == 0) {
                    alert("Empty source.");
                    return;
                }
                try {
                    eval("JSONFormater.source = " + document.getElementById("inputJSON").value + ";");
                } catch (e) {
                    alert("Invalid JSON data.");
                    return;
                }
                this.indentType = this.selectedIndent();
                this.iteratorChilds(this.source, 0, false);
                this.clearTails();
                document.getElementById("inputJSON").value = this.output;
                this.output = "";
            },
            hasMembers: function(obj) {
                for (var m in obj) return true;
                return false;
            },
            isArray: function(obj) {
                return obj.constructor == Array;
            },
            isString: function(obj) {
                return obj.constructor == String;
            },
            iteratorChilds: function(obj, indent, isMember) {

                if (this.isArray(obj)) {
                    this.output += "[\n";
                    for (var i = 0; i < obj.length; i++) {
                        this.iteratorChilds(obj[i], indent + 1, false);
                    }
                    this.clearTails();
                    this.output += this.currentIndent(indent);
                    this.output += "],\n";
                } else if (this.hasMembers(obj)) {
                    if (!isMember) this.output += this.currentIndent(indent);
                    this.output += "{\n";
                    for (var e in obj) {
                        this.output += this.currentIndent(indent + 1);
                        this.output += (this.isString(e) ? "\"" + e + "\"" : e) + ": ";
                        this.iteratorChilds(obj[e], indent + 1, true);
                    }
                    this.clearTails();
                    this.output += this.currentIndent(indent);
                    this.output += "},\n";
                } else {
                    if (!isMember) this.output += this.currentIndent(indent);
                    this.output += this.isString(obj) ? "\"" + obj + "\"" : obj;
                    this.output += ",\n";
                }
            },
            currentIndent: function(ident) {
                var it = "";
                for (var i = 0; i < ident; i++) {
                    it += this.indentType;
                }
                return it;
            },
            clearTails: function() {
                if (this.output.length > 2) this.output = this.output.substring(0, this.output.length - 2) + "\n";
            },
            selectedIndent: function() {
                switch (document.getElementById("indentType").value) {
                    case "1":
                        return "\t";
                    case "2":
                        return "  ";
                    case "3":
                        return "   ";
                    case "4":
                        return "    ";
                    case "8":
                        return "        ";
                }
            }
        };
        window.onresize = function(){
            document.getElementById("inputJSON").style.height = document.body.offsetHeight-30;
        };
    </script>

</head>
<body onload="document.getElementById('inputJSON').style.height = document.body.offsetHeight-30;">
    <textarea id="inputJSON" style="width: 100%;" rows="15" style="overflow: both;"></textarea>
    <div>
        <input type="button" value="format" onclick="JSONFormater.format();" />
        <select id="indentType">
            <option value="1" selected="selected">indent with a tab character</option>
            <option value="2">indent with 2 spaces</option>
            <option value="3">indent with 3 spaces</option>
            <option value="4">indent with 4 spaces</option>
            <option value="8">indent with 8 spaces</option>
        </select>
    </div>
</body>
</html>
Url rewrite using IHttpModule
UrlRewriter.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

/// <summary>
/// UrlRewriter 的摘要描述
/// </summary>
namespace idv.modules
{
    public class UrlRewriter : IHttpModule
    {

        #region IHttpModule 成員

        public void Dispose()
        {
            //throw new NotImplementedException();
        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }

        void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication application = sender as HttpApplication;
            HttpContext context = application.Context;

            if (context.Request.Path.EndsWith("TranslatedPage.html", StringComparison.CurrentCultureIgnoreCase))
            {
                string currentUrl = context.Request.Path;

                // rewrite url [http://www.domain.com/actionValue/TranslatedPage.html]
                // to [http://www.domain.com/TranslatedPage.aspx?action=actionValue]
                string action = Regex.Match(currentUrl, "/([^/]+)/TranslatedPage.html", RegexOptions.IgnoreCase).Groups[1].Value;
                context.RewritePath("~/TranslatedPage.aspx?action=" + action);
            }
        }

        #endregion
    }
}
Web.config
<httpModules>
    <add name="UrlRewriter" type="idv.modules.UrlRewriter"/>
</httpModules>
Bomb Sweeper
<html>
<head>
    <title>Bomb Sweeper</title>
    <style type="text/css">
        body
        {
            background-color: #95B7DF;
        }
        #tableContainer td
        {
            width: 20px;
            height: 20px;
            text-align: center;
            vertical-align: middle;
            -moz-border-radius: 5px;
            -khtml-border-radius: 5px;
            -webkit-border-radius: 5px;
        }
    </style>

    <script type="text/javascript" src="jquery.js"></script>

    <script type="text/javascript">
        var BombSweeper = {
            result: true,
            bombMap: false,
            colSize: 0,
            rowSize: 0,
            locateBomb: function(rowSize, colSize, bombCount) {
                var bombArr = new Array(colSize * rowSize);
                var count = 0;
                var index;
                while (count < bombCount) {
                    index = Math.floor(Math.random() * bombArr.length);
                    if (!bombArr[index]) {
                        count++;
                        bombArr[index] = true;
                    }
                }
                this.colSize = colSize;
                this.rowSize = rowSize;
                this.bombMap = bombArr;
            },
            getSurroundedIndex: function(x, y) {
                var indexArr = [];
                for (var i = x - 1; i <= x + 1; i++) {
                    if (i >= 0 && i < this.colSize) {
                        for (var j = y - 1; j <= y + 1; j++) {
                            if (j >= 0 && j < this.rowSize) {
                                indexArr.push(i * this.colSize + j);
                            }
                        }
                    }
                }
                return indexArr;
            },
            getSurroundedBombCount: function(arr) {
                var count = 0;
                for (var i in arr) {
                    if (this.bombMap[arr[i]]) count++;
                }
                return count;
            },
            getTips: function(x, y) {
                var _x = parseInt(x, 10);
                var _y = parseInt(y, 10);
                if (this.bombMap[_x * this.colSize + _y]) {
                    return -1;
                } else {
                    return this.getSurroundedBombCount(this.getSurroundedIndex(_x, _y));
                }
            },
            isBomb: function(x, y) {
                var _x = parseInt(x, 10);
                var _y = parseInt(y, 10);
                return this.bombMap[_x * this.colSize + _y] == true;
            }
        };
        $(document).ready(function() {
            $("#generateTable").bind("click", function() {
                var colSize = parseInt($("#colSize").val(), 10);
                var rowSize = parseInt($("#rowSize").val(), 10);
                var bombCount = parseInt($("#bombCount").val(), 10);

                if (isNaN(colSize) || isNaN(rowSize)) {
                    $("#tableContainer").text("Not availible size.");
                } else {
                    BombSweeper.locateBomb(colSize, rowSize, bombCount);

                    // rewrite value
                    $("#colSize").val(colSize);
                    $("#rowSize").val(rowSize);
                    $("#col").text(colSize);
                    $("#row").text(rowSize);
                    $("#bomb").text(0);
                    $("#bombEmbed").text(bombCount);


                    // create bomb table
                    $("#tableContainer").html("");
                    $("<table/>").attr({
                        border: 0,
                        align: "center"
                    }).appendTo("#tableContainer");

                    for (var i = 0; i < rowSize; i++) {
                        var currentRow = $("<tr/>").appendTo("#tableContainer table");

                        for (var j = 0; j < colSize; j++) {
                            var currentCell = $("<td/>").css({
                                //textAlign: "center",
                                //verticalAlign: "middle",
                                //fontSize: "12pt",
                                border: "2px outset black",
                                //width: "20px",
                                //height: "20px",
                                backgroundColor: "gray"
                            }).attr({ x: i, y: j })
                            .html("&nbsp;")
                            //.html(BombSweeper.bombMap[i * colSize + j] ? "X" : "&nbsp;")
                            .click(function(e) {
                                if (e.ctrlKey && $(this).attr("x") && $(this).attr("y")) {
                                    if ($(this).attr("marked") == "true") {
                                        $(this).removeAttr("marked")
                                        .css({
                                            color: "black",
                                            fontWeight: "normal"
                                        })
                                        .html("&nbsp;");
                                        $("#bomb").text(parseInt($("#bomb").text(), 10) - 1);
                                    } else {
                                        if (parseInt($("#bomb").text(), 10) < parseInt($("#bombEmbed").text(), 10)) {
                                            $(this).css({
                                                color: "blue",
                                                fontWeight: "bold"
                                            })
                                            .attr("marked", true)
                                            .text("P");
                                            $("#bomb").text(parseInt($("#bomb").text(), 10) + 1);
                                            if (parseInt($("#bomb").text(), 10) == parseInt($("#bombEmbed").text(), 10)) {
                                                var marked = $("#tableContainer td[marked]");
                                                for (var i = 0; i < marked.length; i++) {
                                                    if (!BombSweeper.isBomb($(marked.get(i)).attr("x"), $(marked.get(i)).attr("y"))) return;
                                                }

                                                // unbind all click event
                                                $("#tableContainer td").unbind("click");
                                                // finish game
                                                if (confirm("Congratulations!\nYou finish this game.\nWould you like to play again?")) {
                                                    $("#generateTable").click();
                                                }
                                            }
                                        }
                                    }
                                } else {
                                    if (!$(this).attr("marked") && $(this).attr("x") && $(this).attr("y")) {
                                        var x = parseInt($(this).attr("x"), 10);
                                        var y = parseInt($(this).attr("y"), 10);
                                        var tips = BombSweeper.getTips(x, y);

                                        if (tips == -1) {
                                            $(this).css({
                                                border: "2px inset black",
                                                backgroundColor: "transparent",
                                                color: "red"
                                            })
                                            .text("X")
                                            .removeAttr("x")
                                            .removeAttr("y");

                                            // unbind all click event
                                            $("#tableContainer td").unbind("click");
                                            if (confirm("Sorry, you lose this game.\nWould you like to play again?")) {
                                                $("#generateTable").click();
                                            }
                                        } else {
                                            $(this).css({
                                                border: "2px inset black",
                                                backgroundColor: "white"
                                            })
                                            .html(tips == 0 ? "&nbsp;" : tips)
                                            .removeAttr("x")
                                            .removeAttr("y");

                                            // extended action
                                            if (tips == 0) {
                                                $("#tableContainer td[x][y][marked!='true']")
                                                .filter(function(index) {
                                                    return ($(this).attr("x") == x - 1 || $(this).attr("x") == x || $(this).attr("x") == x + 1)
                                                    && ($(this).attr("y") == y - 1 || $(this).attr("y") == y || $(this).attr("y") == y + 1);
                                                })
                                                .each(function(index) {
                                                    $(this).click();
                                                });
                                            }
                                        }
                                    }
                                }
                            })
                            .appendTo(currentRow);
                        }
                    }
                }
            });
        });
    </script>

</head>
<body>
    <div style="text-align: center;">
        <input id="colSize" type="text" size="5" value="10" />
        x
        <input id="rowSize" type="text" size="5" value="10" />
        @
        <input id="bombCount" type="text" size="5" value="30" />
        <input id="generateTable" type="button" value="New Game" />
    </div>
    <div style="text-align: center;">
        Size of grid : <span id="col">0</span>/ <span id="row">0</span>
        <br />
        Marked bombs : <span id="bomb">0</span> / <span id="bombEmbed">0</span>
    </div>
    <div id="tableContainer">
    </div>
    <div style="text-align: center; font-size: 9pt;">
        (To mark bomb, press ctrl key and click.)</div>
</body>
</html>
Round corner
border-width: 1px;
border-style: solid;
-moz-border-radius: 11px;
-khtml-border-radius: 11px;
-webkit-border-radius: 11px;
border-radius: 11px;
reference : http://www.wowbox.com.tw/blog/article.asp?id=3148
Current appliction location
System.AppDomain.CurrentDomain.BaseDirectory
Date format memo
Available Picture Formatting Codes
Specifier Description Examples
y One-digit year. If the year cannot be specified in one digit then two digits are used automatically. "7"
"95"
yy Two-digit year with leading zeroes if required. "07"
yyyy Full four-digit year. "2007"
g or gg Indicator of Anno Domini (AD). "A.D."
M One-digit month number. If the month cannot be specified in one digit then two digits are used automatically. "1"
"12"
MM Two-digit month number with leading zeroes if required. "01"
MMM Three letter month abbreviation. "Jan"
MMMM Month name. "January"
d One-digit day number. If the day cannot be specified in one digit then two digits are used automatically. "3"
"31"
dd Two-digit day number with leading zeroes if required. "03"
ddd Three letter day name abbreviation. "Wed"
dddd Day name. "Wednesday"
h One-digit hour using the twelve hour clock. If the hour cannot be specified in one digit then two digits are used automatically. "9"
"12"
hh Two-digit hour using the twelve hour clock with leading zeroes if required. "09"
H One-digit hour using the twenty four hour clock. If the hour cannot be specified in one digit then two digits are used automatically. "1"
"21"
HH Two-digit hour using the twenty four hour clock with leading zeroes if required. "09"
t Single letter indicator of AM or PM, generally for use with twelve hour clock values. "A"
"P"
tt Two letter indicator of AM or PM, generally for use with twelve hour clock values. "AM"
"PM"
m One-digit minute. If the minute cannot be specified in one digit then two digits are used automatically. "1"
"15"
mm Two-digit minute with leading zeroes if required. "01"
s One-digit second. If the second cannot be specified in one digit then two digits are used automatically. "1"
"59"
ss Two-digit second with leading zeroes if required. "01"
f Fraction of a second. Up to seven f's can be included to determine the number of decimal places to display. "0"
"0000000"
z One-digit time zone offset indicating the difference in hours between local time and UTC time. If the offset cannot be specified in one digit then two digits are used automatically. "+6"
"-1"
zz Two-digit time zone offset indicating the difference in hours between local time and UTC time with leading zeroes if required. "+06"
Execute a STRING as a query
MySQL:
BEGIN
    SET @sql = CONCAT('SELECT * FROM ', table_name, ' WHERE id = ', vID);
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
END
BEGIN
    PREPARE stmt1 FROM 'SELECT SQRT(POW(?,2) + POW(?,2)) AS hypotenuse';
    SET @a = 3;
    SET @b = 4;
    EXECUTE stmt1 USING @a, @b;
    DEALLOCATE PREPARE stmt1;
END;
Create web service client cross SSL with eclipse
When creating web service cross SSL using eclipse, it occuss some error like below:

And it can't build the client source from this way.
To solve this problem, there are two ways to achieve it.
1. Connection wsdl using HTTP protocol.
and it seems to be a stupid way when this web service only provide HTTPS protocol.
2. Save the wsdl file from the url and use local file to create client.
a. Save the wsdl as a local file. (Such as myService.wsdl)
b. Copy the wsdl file to the project.
c. right click the wsdl file -> [ Web Services ] -> [ Generate Client ]
d. Congratulations. You got it.
Screen capture
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Case062 {

    /**
     * @param args
     * @throws AWTException
     * @throws IOException 
     */
    public static void main(String[] args) throws AWTException, IOException {
        // TODO Auto-generated method stub
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension screenSize = toolkit.getScreenSize();
        Rectangle screenRect = new Rectangle(screenSize);
        Robot robot = new Robot();
        BufferedImage image = robot.createScreenCapture(screenRect);
        ImageIO.write(image, "jpg", new File("capture.jpg"));
    }

}