Wednesday, July 13, 2005

Bézier curves

อ่านเจอเรื่องนี้ Bézier curves
It is attributed and named after a French engineer, Pierre Bézier, who used them for the body design of the Renault car in the 1970's. They have since obtained dominance in the typesetting industry and in particular with the Adobe Postscript and font products.


ก็เลยลอง implement ด้วย java ดู
/*
* DrawPanel.java
*
* Created on July 9, 2005, 9:12 AM
*
*/

package curve;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;

/**
*
* @author pphetra
*/
public class DrawPanel extends JPanel {
private static int LOOP_COUNT = 60;

protected void paintComponent(Graphics g) {
super.paintComponent(g);
ArrayList points = new ArrayList();
points.add(new Point2D.Double(0.0, 200.0));
points.add(new Point2D.Double(50.0, 50.0));
points.add(new Point2D.Double(100.0, 150.0));
points.add(new Point2D.Double(200.0, 0.0));

paintBazier(g, points, LOOP_COUNT);

paintControlPoints(g, points);

}

private void paintControlPoints(Graphics g, List points) {
g.setColor(Color.MAGENTA);
Point2D.Double oldp = null;
for (int i = 0; i < points.size(); i++) {
Point2D.Double p = (Point2D.Double) points.get(i);
if (oldp != null) {
g.drawLine((int) oldp.x, (int) oldp.y, (int) p.x, (int) p.y);
}
oldp = p;
}


}

private void paintBazier(Graphics g, List points, int loopCount) {
Point2D.Double oldp = null;

for (int i = 0; i < loopCount; i++) {
Point2D.Double p = bazier(points, points.size()-1, (double) i/ (doub
le) loopCount);
if (oldp == null) {
oldp = p;
}
g.drawLine((int) p.x, (int) p.y, (int) oldp.x, (int) oldp.y);
oldp = p;
}
Point2D.Double p = (Point2D.Double) points.get(points.size()-1);

g.drawLine((int) oldp.x, (int) oldp.y, (int) p.x, (int) p.y);
}

private Point2D.Double bazier(List points, int n, double mu) {

int k, kn, nn, nkn;
double blend, muk, munk;
Point2D.Double b = new Point2D.Double(0.0, 0.0);

muk = 1;
munk = java.lang.Math.pow(1-mu, n);

for (k = 0; k <= n; k++) {
nn = n;
kn = k;
nkn = n-k;
blend = muk * munk;
muk *= mu;
munk /= (1-mu);
while (nn >= 1) {
blend *= nn;
nn--;
if (kn > 1) {
blend /= (double) kn;
kn--;
}
if (nkn > 1) {
blend /= (double) nkn;
nkn--;
}

}
Point2D.Double p = (Point2D.Double) points.get(k);
b.x += (p.x * blend);
b.y += (p.y * blend);
}

return b;
}
}


ได้ผลลัพท์ดังนี้


ในโปรแกรม จะเห็นว่ามีค่า LOOP_COUNT อยู่ตัวหนึ่ง
ค่านี้ก็คือจำนวน line segment ที่ใช้ draw curve
ปัญหาก็คือ ควรใช้ค่าเท่าไรดี
น้อยไป ก็ไม่โค้ง
มากไป ก็เปลือง
ก็เลยมีบทความ Adaptive Subdivision of Bezier Curves

Related link from Roti

Monday, July 11, 2005

ทดลอง google map api ใน blogspot

วันนี้ผมทดลองสมัครเข้าใช้บริการ google map api
โดย google map api ยอมให้ใช้ api
เฉพาะใน web site ที่สมัครเข้าใช้งานเท่านั้น
อย่างของผม ก็สมัครใช้ api
กับ url http://pphetra.blogspot.com
ได้ key ยืดยาวมาชุดหนึ่ง

เริ่มแรก ก็คือเราต้อง include api ในส่วน header ของ web page

<script src="http://maps.google.com/maps?file=api&v=1&key=abcdefg" type="text/ja
vascript"></script>

จากนั้นก็กำหนดบริเวณ render map ด้วย div block

<div id="mymap" style="width: 400px; height: 400px"></div>


ทำการ new GMap Object ขึ้นมาด้วย javascript
if (GBrowserIsCompatible()) { 
var map = new GMap(document.getElementById("mymap"));
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
map.setMapType(_SATELLITE_TYPE);
map.centerAndZoom(new GPoint(99.003489, 18.788563), 1);
}

Note: ค่า map type _SATELLITE_TYPE นั้นไม่ตรง
กับ api spec ซึ่งมีค่าเป็น G_SATELLITE_TYPE
ลองใช้ G_SATELLITE_TYPE แล้วทำงานไม่ถูกต้อง

ตัวอย่าง GPoint ที่ทดลองให้ดูนี้เป็นค่า latitude, longitude ของเมืองเขียงใหม่
บริเวณตลาดวโรรส
ถ้าใช้ google map ดู ก็จะมี url แบบนี้
http://maps.google.com/maps?ll=18.788563,99.003489&spn=0.008898,0.005922&t=k&hl=en

Note: ค่า latitude, longitude ใน url กับใน constructor ของ GPoint เรียงลำดับต่างกัน

จะเห็นได้ว่าวิธีใช้ google map Api นั้นง่ายๆ
แต่ประเด็นปัญหาก็คือ
เราไม่สามารถใส่ javascript
ลงไปใน post ของเราได้
(editor ของ blogger จะไม่ยอมให้เราใส่ tag javascript)
ก็เลยเดือนร้อนต้องหาวิธีการ embed javascirpt
ลงไปตอน post ให้ได้

วิธีการที่ใช้ก็คือ ห่อ javascript ที่ต้องการ
ลงไปใน div tag (โดยกำหนด style เป็น display:none)
จากนั้น ณ จังหวะที่ onload ของ page
ก็จัดการ scan หา div tag นัั้นๆ
แล้วก็สั่ง eval เจ้า javascript เสีย
function embedjs() {
var xs = document.getElementsByTagName("div");
var k = xs.length;
var maps = new Array(12);
var j = 0;
for (var i = 0; i < k; i++) {
if (xs[i].getAttribute("id") == "pokmap") {
maps[j++] = xs[i].innerHTML;
}
}

for (var i = 0; i < j; i++) {
eval(maps[i]);
}
}

Note : ให้ระวังอย่ายุบรวม for loop
scan กับ for loop evel เข้าด้วยกัน
เพราะว่าเมื่อเราเรียกเจ้า google map api
จะเกิดการ insert div ใหม่เข้ามา ผลลัพท์ก็คือ
จะเกิดวน loop ไม่รู้จบ

แก้ไขส่วน template เพิ่ม onload event
<body onload="embedjs()">


จากนั้นเวลา post ก็สามารถใส่ tag
ได้ดังนี้
<div id="123map" style="width: 400px; height: 400px">
</div>
<div id="pokmap" style="display: none;">if (GBrowserIsCompatible()) { var map =
new GMap(document.getElementById("123map")); map.addControl(new GSmallMapContro
l()); map.addControl(new GMapTypeControl()); map.setMapType(_SATELLITE_TYPE); ma
p.centerAndZoom(new GPoint(99.003489, 18.788563), 1);}</div>

ผลลัพท์ที่ได้ก็เป็นเช่นนี้


Related link from Roti

google map mania

ตั้งแต่รู้จาก คุณ Hui ว่า google map สามารถแสดง
ภาพของกรุงเทพได้แล้ว ก็เลยเกิดความสนุกในการใช้งาน

วันนี้เจอ link นี้เข้า
Google Maps Mania
เป็น blog ที่รวมสารพัดเรื่องที่เกี่ยวข้องกับ google map

มีบางอันเห็นแล้วอมยิ้ม
เช่น Scavengeroogle
เปิดให้เล่นเกมส์ตามล่า สถานที่ที่ post ว่าอยู่ที่ไหน

Related link from Roti

บทความน่าสนใจ

เห็นใน blog ของคนอื่นลง link ไว้ (เจอ 2 blog แล้วที่พูดถึงเรื่องนี้)
ก็เลยตามไปอ่านดู ค่อนข้างยาว
แต่พอจะอ่านข้ามได้
เพราะหลักการค่อนข้างเป็นที่รู้กันอยู่

Unskilled and Unaware of It: How Difficulties in Recognizing One's Own Incompetence Lead to Inflated Self-Assessments

quote มาให้ดูบางส่วนแล้วกัน
(เป็นหนูทดลองให้ bact เขาหน่อย
ว่า การ quote ถือเป็นการตัวแทนของ
ใจความสำคัญของเนื้อหาได้หรือไม่)
People tend to hold overly favorable views of their abilities in many social and intellectual domains.
Across 4 studies, the authors found that participants scoring in the bottom quartile on tests of humor, grammar, and logic grossly overestimated their test performance and ability. Although their test scores put them in the 12th percentile, they estimated themselves to be in the 62nd.


"above-average effect,"
the tendency of the average person to believe he or she is above average.
For example, high school students tend to see themselves as having more ability in leadership, getting along with others, and written expression than their peers
, business managers view themselves as more able than the typical manager
, and football players see themselves as more savvy in "football sense" than their teammates


"ignorance more frequently begets confidence than does knowledge"
Charles Darwin


ปล. เรื่องนี้สามารถ apply เข้ากับบรรดาท่านผู้ใหญ่ในบ้านเมืองนี้
ได้หลายท่านเลย

Related link from Roti

Friday, July 08, 2005

Code Snippets by tag

วันนี้เปิดเจอ site หนึ่งน่าสนใจดี
เป็นที่รวม code snippet
คล้ายๆกับ del.icio.us ที่ assign tag กับ url
อันนี้เปลี่ยนเป็น assign tag กับ code snippet แทน

ตัว application เขียนด้วย ruby on rails
แล้วก็เปิดให้ download ได้ด้วย

Link

Related link from Roti

Custom Schema Mapping in ActiveRecord (Rails)

ActiveRecord คือ module ที่ทำหน้าที่
Object/relation mapping ใน RubyOnRails Framework
วิธีการที่เขาใช้ จะใช้ reflection เป็นหลัก
และก็มี assumption บางอย่างที่ใช้ในการ
map column name, table name เข้ากับ Object

ตัวอย่างแรกสุดเลย
สมมติเราจะเขียน blog ด้วย Rails
ในแง่ของตัว relational database
เราก็ต้องมี table สำหรับเก็บ blog post
assumption ของ active record ก็คือ
  • ต้องมี column "id" เป็น primary key
  • ถ้ามี column created_on กับ updated_on
    เจ้า ActiveRecord จะดูแลการ set ค่าพวกนี้ให้โดยอัตโนมัติ
  • ชื่อ table ต้องอยู่ในรูปพหูพจน์


ดังนั้นเจ้า table blog post ของเราก็เลยออกมา
หน้าตาประมาณนี้
create table blog_posts (
id serial,
post text,
created_on timestamp,
primary key (id)
);


ส่วนเจ้า class BlogPost
ก็หน้าตาประมาณนี้
class BlogPost < ActiveRecord::Base
end

จะเห็นได้ว่าไม่ต้องระบุอะไรเลย
เดี๋ยว Reflection จัดให้เอง ตอน runtime

ส่วนการใช้งาน ก็เช่น
blogpost = BlogPost.new
blogpost.post = "hello world"
blogpost.save


ในกรณีที่เราเขียน app โดยออกแบบใหม่หมด
ตั้งแต่ table เลย ก็คงไม่มีปัญหาอะไร
แต่ถ้าเป็น Web Application ที่ต้องใช้
legacy table จากระบบเก่า
ก็จะเกิดปัญหาเรื่อง naming หรือ mapping ขึ้น
(แค่ชื่อ table ก็เป็นปัญหาแล้ว
บางคนเขานิยมตั้งชื่อ table เป็นรหัส)

ดังนั้นใน post วันนี้ เราจะลองมาดูว่า
เราสามารถ override assumption ต่างๆของ ActiveRecord
ได้อย่างไรบ้าง

เริ่มด้วยชื่อ table
ค้นเจอ 2 แบบ
class MyClassName < ActiveRecord::Base
def self.table_name() "my_table_name" end
end


class Mouse < ActiveRecord::Base
set_table_name "mice"
end

คืออันแรกใช้วิธี override method table_name()
ส่วนอันที่ 2 ใช้วิธี call method set_table_name("name")

กรณี primary key ของเราไม่ได้ชื่อ "id"
class MyClass < ActiveRecord::Base
set_primary_key "my_pk_name"
end


ส่วนกรณีที่เราต้องการชื่อ attribute
ที่ไม่ตรงกับชื่อ column
เท่าที่ค้นดู ยังไม่มีพูดถึงประเด็นนี้
แต่ถ้าทำด้วยวิธี object style ก็คงใช้วิธีเขียน method set, get ด้วยชื่อใหม่
แล้วข้างไนก็ค่อยไปเรียก set, get ด้วยชื่อจริง
class Song < ActiveRecord::Base

def length=(minutes)
write_attribute(:len, minutes)
end

def length
read_attribute(:len)
end
end

Related link from Roti

Thursday, July 07, 2005

Syntax Highligh Java source in Apache Forrest

เป้าหมายที่ต้องการทำก็คือ
ต้องการให้ Apache Forrest Application สามารถ
transform xdoc ในส่วนของ java source
ให้อยู่ในรูปสีสรรสดสวย
(เดิมใน xdoc มี tag ที่ชื่อ source
อยู่แล้ว แต่ไม่ได้ทำ highlight ให้)

วิธีการที่ทำได้มีอยู่ 2 ทางหลักๆ
  • Customize Project 's Pipeline
  • Customize Forrest 's Pipeline ที่ forrest installation directory โดยตรง


วิธีแรกดูดีสุด
โดยในคู่มือ forrest บอกไว้ว่า เราสามารถ
customize pipeline processing เฉพาะใน Document Project ของเรา
ผ่านทาง /src/documentation/sitemap.xmap ได้
โดยทำได้ 2 แบบคือ
  • render เองหมดเลย
  • render เฉพาะ body ที่เหลือปล่อยให้ forrest apply skin ให้

  • พยายามจะทำแบบที่ 2 แต่ไม่ได้สักที
    ก็เลยเปลี่ยนใจ ย้ายไป customize ที่ตัว
    forrest configuration โดยตรงเลย

    เริ่มจาก file /$FORREST_HOME/main/webapp/sitemap.xmap
    มันจะมีการ mapping url ไว้ดังนี้
    <map:match pattern="*.html">
    <map:aggregate element="site">
    <map:part src="cocoon:/skinconf.xml"/>
    <map:part src="cocoon:/build-info"/>
    <map:part src="cocoon:/tab-{0}"/>
    <map:part src="cocoon:/menu-{0}"/>
    <map:part src="cocoon:/body-{0}"/>
    </map:aggregate>

    <map:call resource="skinit">
    <map:parameter name="type" value="site2xhtml"/>
    <map:parameter name="path" value="{0}"/>
    </map:call>
    </map:match>


    นั่นคือหลังจากได้ url ที่ลงท้ายด้วย .html เข้ามา
    เจ้า forrest ก็จะแตกออกเป็น 5 dispatch ย่อย ที่จะ
    แจกไปให้แต่ละส่วนทำงาน เช่น ส่วน tab, ส่วน menu
    จากนั้นค่อย aggregate รวมกันแล้วส่งไป render อีกที

    กรณีของเรา ก็คือต้องการ customize ส่วน body
    จากของเดิมที่เป็นดังนี้
    <map:match pattern="**body-*.html">
    <map:generate src="cocoon:/{1}{2}.xml"/>
    <map:transform type="idgen"/>
    <map:transform type="xinclude"/>
    <map:transform type="linkrewriter" src="cocoon:/{1}linkmap-{2}.html"/>
    <map:transform src="resources/stylesheets/declare-broken-site-links.xsl" />
    <map:call resource="skinit">
    <map:parameter name="type" value="document2html"/>
    <map:parameter name="path" value="{1}{2}.html"/>
    <map:parameter name="notoc" value="false"/>
    </map:call>
    </map:match>


    โดยสิ่งที่เราจะเพิ่มเข้าไปก็คือ หลังจากที่ generator ได้ file xdoc
    มาแล้ว เราจะเข้าไป intercept sax event
    เพื่อที่ว่าจะได้ทำการ parse java source ให้อยู่ในรูป
    xml เสียก่อน จากนั้นก็จะส่งผ่านไปให้ xslt
    ทำการ render ให้อยู่ในรูปแบบที่ต้องการ

    <map:match pattern="**body-*.html">
    <map:generate src="cocoon:/{1}{2}.xml"/>
    <!-- parse java source to xml form -->
    <map:transform type="java2html" />
    <map:transform type="idgen"/>
    <map:transform type="xinclude"/>
    <map:transform type="linkrewriter" src="cocoon:/{1}linkmap-{2}.html"/>
    <!-- render xml syntax tree to html tag -->
    <map:transform src="resources/stylesheets/javahighlight.xsl"/>
    <map:transform src="resources/stylesheets/declare-broken-site-links.xsl" />
    <map:call resource="skinit">
    <map:parameter name="type" value="document2html"/>
    <map:parameter name="path" value="{1}{2}.html"/>
    <map:parameter name="notoc" value="false"/>
    </map:call>
    </map:match>


    ตัว transform type "java2html" จะทำหน้าที่แปลง
    javasource ให้อยู่ในรูป xml โดยมี tag แปะตาม
    ประเภทของ token
    เช่น
    package pok.test;
    import java.util.ArrayList;

    ก็จะถูกแปลงเป็น
    <javakeyword>package</javakeyword pok.test;
    <javakeyword>import</javakeyword> java.util.<java_type>ArrayList</java_type>;


    ส่วน transform อีกตัวจะทำหน้าที่ apply style sheet "javahighlight.xsl"
    เพื่อ render ให้อยู่ในรูปแบบ html ที่ต้องการ
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
    <xsl:apply-templates/>
    </xsl:template>

    <!-- the obligatory copy-everything -->
    <xsl:template match="node() | @*">
    <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:apply-templates/>
    </xsl:copy>
    </xsl:template>

    <xsl:template match="jshl">
    <pre class="hl">
    <xsl:apply-templates mode="javasource"/>
    </pre>
    </xsl:template>

    <xsl:template match="java_plain" mode="javasource">
    <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="java_keyword" mode="javasource">
    <span class="S_KEYWORD1"><xsl:apply-templates/></span>
    </xsl:template>
    ...


    วิธีการ implement transformer
    เขียน class Java2HtmlTransformer โดย extends AbstractSaxTransformer
    package pok.cocoon.transformer;



    import java.io.StringBufferInputStream;

    import org.apache.cocoon.transformation.AbstractSAXTransformer;
    import org.apache.cocoon.xml.AttributesImpl;
    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;



    public class Java2HtmlTransformer extends AbstractSAXTransformer {

    private static final String TAG_NAME = "javasource";

    public void endElement(String uri, String name, String raw)
    throws SAXException {
    if (name.equals(TAG_NAME)) {



    String srcStr = this.endTextRecording();
    JavaXmlRenderer render = new JavaXmlRenderer();
    sendStartElementEvent("jshl");
    try {

    render.highlight(new StringBufferInputStream(srcStr), this);

    } catch (Throwable e) {
    e.printStackTrace();
    }
    sendEndElementEvent("jshl");

    } else {
    super.endElement(uri, name, raw);
    }

    }

    public void startElement(String uri, String name, String raw,
    Attributes attr) throws SAXException {

    if (name.equals(TAG_NAME)) {
    this.startTextRecording();
    } else {

    super.startElement(uri, name, raw, attr);
    }
    }

    }

    หลังจากเขียน class แล้ว เราต้องบอกให้ Forrest
    รู้จัก transformer นี้โดยการเพิ่ม config เข้าไปใน sitemap.xmap
    <map:transformers>
    <map:transformer name="java2html"
    src="pok.cocoon.transformer.Java2HtmlTransformer"
    logger="pok"
    />
    </map:transformers>


    ลักษณะการทำงานของ Java2HtmlTransformer ก็คือเราจะสนใจ sax event
    เฉพาะส่วน startElement กับ endElement
    กรณีที่พบ tag <javasource> เราก็จะทำการ
    record text ที่เกิดขึ้นระหว่าง tag เปิด กับ tag ปิด
    และทำการส่งไปให้ JavaXmlRenderer ทำการ parse text ในส่วนนี้

    ส่วน Class JavaXmlRenderer ใช้ source code ตัวอย่างจาก
    project JHighlight
    package pok.cocoon.transformer;

    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.Reader;
    import java.io.StringReader;

    import org.apache.cocoon.transformation.AbstractSAXTransformer;

    import com.uwyn.jhighlight.highlighter.JavaHighlighter;
    import com.uwyn.jhighlight.tools.StringUtils;

    public class JavaXmlRenderer {



    public void highlight(InputStream in, AbstractSAXTransformer sax) throws Exc
    eption {

    JavaHighlighter highlighter = new JavaHighlighter();
    highlighter.ASSERT_IS_KEYWORD = true;

    Reader isr;
    isr = new InputStreamReader(in);


    BufferedReader r = new BufferedReader(isr);


    String line;
    String token;
    int length;
    int style;
    String css_class;
    String previous_class = null;
    int previous_style = 0;
    while ((line = r.readLine()) != null)
    {
    line += "\n";
    line = StringUtils.convertTabsToSpaces(line, 4);

    // should be optimized by reusing a custom LineReader class
    Reader lineReader = new StringReader(line);
    highlighter.setReader(lineReader);
    int index = 0;
    while (index < line.length())
    {
    style = highlighter.getNextToken();
    length = highlighter.getTokenLength();
    token = line.substring(index, index + length);

    if (style != previous_style)
    {
    css_class = getCssClass(style);

    if (css_class != null)
    {
    if (previous_class != null)
    {
    sax.sendEndElementEvent(previous_class);
    }
    sax.sendStartElementEvent(css_class);

    previous_class = css_class;
    previous_style = style;
    }
    }
    sax.sendTextEvent(token);

    index += length;
    }

    }
    }


    protected String getCssClass(int style)
    {
    switch (style)
    {
    case JavaHighlighter.PLAIN_STYLE:
    return "java_plain";
    case JavaHighlighter.KEYWORD_STYLE:
    return "java_keyword";
    case JavaHighlighter.TYPE_STYLE:
    return "java_type";
    case JavaHighlighter.OPERATOR_STYLE:
    return "java_operator";
    case JavaHighlighter.SEPARATOR_STYLE:
    return "java_separator";
    case JavaHighlighter.LITERAL_STYLE:
    return "java_literal";
    case JavaHighlighter.JAVA_COMMENT_STYLE:
    return "java_comment";
    case JavaHighlighter.JAVADOC_COMMENT_STYLE:
    return "java_javadoc_comment";
    case JavaHighlighter.JAVADOC_TAG_STYLE:
    return "java_javadoc_tag";
    }
    return null;
    }

    }


    โดยหลักการทำงาน ก็คือ JavaXmlRenderer จะเรียกใช้ scanner ที่ชื่อ
    JavaHighlighter (implement ด้วย Flex)
    ทำการ scan ทีละบรรทัด
    ผลลัพท์ที่ได้ จะได้ style กับ token ออกมา
    จากนั้น ก็จะทำการส่ง sax event ออกไป
    โดยมี tag name ตามรูปแบบ style ที่ได้มาจาก scanner

    ตัวอย่างการนำไปใช้
    <javasource>
    package pok.test;
    import java.util.ArrayList;

    /**
    * this is <code>test</code>
    * @param hi
    */
    public class Main implements java.io.Serializable {
    public static void main(String[] args) {
    System.out.println("helloWorld");
    if (1 != 2) {
    if (1 > 3) {
    // pok test
    }
    }
    }
    }
    </javasource>

    ผลลัพท์ที่ได้


    สรุป ปัญหาของการ customize forrest หรือ cocoon framework ก็คือ
    เกิดจากเจ้าตัว cocoon framework ที่ค่อนข้างจะซับซ้อนพอสมควร
    แถมมันยังใช้ IOC container ที่ชื่อ Avalon Excalibur
    ยิ่งทำให้การทำความเข้าใจ source code ต่างๆ ต้องใช้เวลามาก
    นอกจากนี้ ยังมีประเด็นความเข้าใจในส่วนของการ parse xml Document
    ที่มักจะมีปัญหาเรื่อง namespace เสมอ

    Related link from Roti

    Tuesday, July 05, 2005

    window1.01, 2.0 screenshots

    พึ่งเคยเห็น screen shot ของ window1.01 กับ 2.0
    ดู classic ดีจัง

    Link

    Related link from Roti

    Sunday, July 03, 2005

    ยืนด้วยครับ

    เมื่อวานออกไปทำธุระนอกบ้้าน
    เดินไปถึงหน้าปากซอย
    พบตำรวจกำลังปิดถนน
    รอขบวนเสด็จ
    ก็เลยขี้นิ้วไปที่สะพานลอย
    (ทำนองว่า ข้ามได้ไหม)
    ตำรวจก็บอก เดี๋ยวก่อน
    ก็เลยไปนั่งรอข้างๆสะพานลอย
    (เปิดอิฐก่อสูงประมาณ ฟุตครึ่ง)
    ตำรวจมอง แล้วก็บอกว่า "ช่วยยืนด้วยครับ"

    รู้สึกจี๊ดขึ้นมาที่หัวเลย
    แต่ก็ยังสงบอาการ
    บอกตำรวจไปว่า
    "เดี๋ยวมาแล้วค่อยยืน"

    พอขบวนมาถึง
    ตำรวจทำท่ายืน ระเบียบพัก
    แล้วก็พูดอีก "ยืนด้วยครับ"
    คราวนี้ ก็เลยส่ายหัว แล้วก็บอกว่า "ไม่ยืน"
    (นึกในใจ ทำไมกูต้องยืนด้วยวะ)

    จบ
    ปล. เจอเหตุการณ์นี้แล้วนึกถึง
    ตอนไปเที่ยวพระที่นั่งวิมาณเมฆ
    สวยดีนะ เสียอย่างเดียว พอ guide (พวกราชการ)
    นำชมมาถึงห้องสุดท้าย
    guide ก็บอกว่า ทุกคนก้มลงกราบพระรูปพร้อมกันด้วยค่ะ
    แถมมีบทพูดถวายบังคม ที่ต้องพูดพร้อมกันด้วยนะ

    ปล. ในเชิงวัฒนธรรมแล้ว
    ถือได้ว่าเป็นเรื่องเดียวกับ
    เรื่องรุ่นน้องต้องเคารพรุ่นพี่
    (หรือรุ่นพี่สั่งยืนแถว)
    ในตอนรับน้องไหมเนี่ย

    Related link from Roti

    Friday, July 01, 2005

    Flash Demo: Netbeans + J2EE1.5 + EJB3.0 + GlassFish

    Demo
    ดูแล้วน่าสนใจยิ่ง

    ยังไม่ได้อ่าน spec ของ EJB 3.0 เลย
    แต่คุ้นๆว่ามีการนำ annotation มาใช้เยอะมาก

    ใน flash demo มีตัวอย่าง
    ของ servlet เรียกใช้ session bean
    ชอบตรง annotation ที่ทำหน้าที่
    คล้ายๆ Injection
    (container จะรู้เองว่าควรเอา
    object หรืออะไร มา set ให้)

    Note:
    GlassFish คือ Opensource
    Application Server ของ Sun
    (เดิมคือ Sun Applicatoin Server 9
    ซึ่งให้ใช้ฟรีใน version PE )

    Related link from Roti

    Eclipse Bug Report in verse

    Eclipse Bug number 100918

    Shade Of Grey

    Lyrics: mvm
    Music: sung to the tune of the hit by "The Grateful Dead"

    I guess it's just a shade of grey
    When my editor dropdown opens I might realize
    Just might realize
    But those grey stripes
    That easy slice
    Gonna stay with me a while

    But out in Motif
    Well it's like a tractor factory
    Out in the streets
    They just don't use yellow

    Bout seven list items high
    All their dreams have been realized
    Are being verticalized
    And those grey strips
    Just light up and shine
    When I hit ctrl+e or touch that mouse of mine

    etc...

    Related link from Roti

    Wednesday, June 29, 2005

    Jdk1.5 & "Revenge of the Sith"

    จาก Kirill Grouchnikov's Blog
    The real story about how Anakin Skywalker became Darth Vader

    ใครลองทำ "บ้านทรายทอง" บ้างสิ

    Related link from Roti

    Art of Science Competition

    at princeton university
    Link

    "dynamic asset allocation problem in railroads"

    "Transgenic Neural Stem Cells"

    Related link from Roti

    Monday, June 27, 2005

    Forrest (Publishing framework)

    ช่วงนี้นั่งทำเอกสาร tutorial อยู่
    ก็เลยมองหา tool มาช่วยเขียน
    จำได้ว่าพวก Apache ใช้มาตรฐาน xdoc
    ในการทำเอกสาร ก็เลยตามเข้าไปดู

    ไปเจอโปรเจค Forrest เข้า
    Apache Forrest is a publishing framework that
    transforms input from various sources
    into a unified presentation
    in one or more output formats

    ก็เลย load มาทดลองใช้ดู

    การใช้งาน ก็ง่ายดี
    เริ่มต้นด้วยการสร้าง project ใหม่
    ด้วยคำสั่ง forrest seed
    โดยเจ้า forrest จะสร้าง directory, template file
    ต่างๆให้



    จากนั้นเราก็เข้าไป edit เอกสาร
    ซึ่งใช้ format xdoc
    และถ้าต้องการ generate web site ก็เพียงแต่สั่ง
    forrest run เจ้า forrest ก็จะจัดการ
    generate ให้เรียบร้อย

    นอกจากนี้ยังสามารถ start เป็น web application ได้ด้วย
    โดยสั่ง forrest run
    เจ้า forrest ก็จะทำการ start jetty ให้
    บน port 8888 ทำให้เราสามารถแก้ไขเอกสาร
    พร้อมกับดูผลลัพท์ได้ทันที

    หลังจากลองใช้สักพัก ก็เริ่มโลภมาก
    อยากได้ feature source highlight
    โดยกะว่าจะเพิ่ม tag พิเศษที่ชื่อ <javacode>
    จะได้เอาไว้ render source code ให้มีสีสรรสวยงาม
    ก็เลยค้นเอกสาร forrest
    จึงรู้ว่า เจ้า forrest ใช้ cocoon เป็นฐานราก
    Cocoon implements these concepts around
    the notion of 'component pipelines',
    each component on the pipeline
    specializing on a particular operation.
    This makes it possible to use a Lego(tm)-like approach
    in building web solutions,
    hooking together components into pipelines
    without any required programming.

    ใน pipeline ของ cocoon จะมี component
    อยู่ 3 แบบก็คือ
    • generator ทำหน้าที่สร้าง xml sax event
      ขึ้นจาก source ต่างๆ ไม่ว่าจะเป็น file system
      ,jdbc, xml database, etc..
    • transformer ทำหน้าที่ตามชื่อ
      โดยรับ input เป็น sax event
      และให้ output เป็น sax event
    • Serializer ทำหน้าที่แปลง
      sax event ออกเป็น final format
      ซึ่งอาจจะเป็น html, pdf, svg, office document, etc..

    forrests เปิดโอกาสให้เรา customize pipeline ได้
    ก็เลยกะว่าจะ implements เจ้า Java2HtmlTransformer
    ขึ้นมาเอง และก็ add เข้าไปใน pipeline ของ forrest เสีย
    โดยเอา library java2html มาใช้

    Related link from Roti

    del.icio.us direc.tor

    feature ใหม่ ของ del.icio.us
    ชื่อ direc.tor
    เป็นพวก AJAX อีกแล้ว
    ที่น่าสนใจก็คือ เขาเขียน "How does it work?"
    อธิบายการทำงานไว้ด้วย

    Related link from Roti

    The NetBeans Collaboration Project

    Sun ปล่อย opensource ออกมาอีกตัวแล้ว
    คราวนี้เป็น NetBeans Collaboration Project
    ซึ่งเดิมเป็น feature ใน Sun Java Studio Enterprise 7
    ซึ่งลักษณะการทำงาน ก็เป็นพวก IM
    ที่เปิดโอกาสให้ developer chat กันได้
    โดยผ่าน collaboration service ที่ java.net
    ให้บริการอยู่

    ได้ดู flash demo แล้ว
    ชอบ feature ในส่วน share code
    ที่เปิดโอกาสให้ร่วมกันแก้ไข source code กันได้พร้อมๆกัน
    โดยระหว่างแก้ source code ของอีกฝั่ง
    จะเปลี่ยนเป็น guard box ไว้ (lock เพื่อป้องกัน conflict)
    จนกว่าจะแก้เสร็จ (หยุดพิมพ์)

    Related link from Roti

    Saturday, June 25, 2005

    จดหมายผิดซอง

    วันนี้ได้รับจดหมายเชิญ

    Dear polawat phetra,

    You are confirmed to attend:

    Topic: Mobile & Entertainment Executive Summit Dinner
    Date: Tuesday, June 28, 2005
    Time: 7pm - 11pm (Reception 7pm, Dinner 8pm)
    Location: San Francisco Museum of Modern Art
    151 Third Street (between Mission and Howard Streets) (view map)
    Dress: Business/Cocktail

    Best Regards,
    Alan Brenner
    Vice President Engineering, Consumer & Mobile Systems Group


    หึ มาจากไหนวะ
    เลี้ยงใน Museum ด้วย งานน่าจะหรู

    จากนั้นอีกวันต่อมา
    Dear polawat phetra,

    Please accept our apologies as you were recently sent the below message in error. Sun Microsystems deeply regrets any inconvenience this may have caused. We are taking action to update our processes to ensure that this does not happen again.

    Thank you,
    Sun Microsystems

    Related link from Roti

    Tuesday, June 21, 2005

    Pan รูปขนาดใหญ่ด้วย javascript

    ดูได้ที่ Link นี้

    Related link from Roti

    คนเหล็ก

    ใน signal vs. noise วันนี้มี post
    ที่ชี้ไปยัง Lance Armstrong 's physical advantage

    Armstrong can ride up the mountains in France generating about 500 watts of power for 20 minutes, something a typical 25-year-old could do for only 30 seconds. A professional hockey player might last three minutes - and then throw up.


    มีหนังสือแปลชีวประวัติของเขา
    เป็นภาษาไทยด้วยนะ ออกมานานแล้ว

    Related link from Roti

    Monday, June 20, 2005

    Long Request Filter

    วันนี้เจอ Servlet Filter ที่น่าสนใจที่เดียว
    ชื่อว่า LongRequest ใช้เป็น solution
    สำหรับห่อ Web Application Request
    ที่ใช้เวลา processing นานๆ
    โดย filter ตัวนี้จะ intercept request
    จากนั้นก็ส่ง page เล็กๆ ที่ทำหน้าที่แสดง
    progress bar กลับไปก่อน
    และเจ้า progress page ก็จะทำการ poll
    ไปยัง filter เป็นระยะๆ
    เมื่องานจริงๆ เสร็จก็จะทำการแสดงผล

    ชอบ solution นี้ตรงที่มันไม่ต้องไปยุ่งกับ code
    ที่เราเขียนเลย แค่ดัก intercept ตรงข้างหน้าเท่านั้นเอง

    Related link from Roti

    ruby ติดดิน

    วันนี้เห็น blog Spring Live เขาอ้างถึง
    post The Rise of Open Source Java
    มี graph ยอดหนังสือของ O'REILLY ที่น่าสนใจ



    ดูเจ้า ruby สิ ติดดินเชียว

    Related link from Roti

    Sunday, June 19, 2005

    SWT Snippets

    SWT Snippets

    Related link from Roti

    ใช้ jEdit ช่วยทำ Source Highlighting #2

    วิธีการเขียน jEdit Plugin ก็คือ
    เริ่มต้นที่ Plugin Core Class โดยเขียนง่ายๆดังนี้

    package highlight;

    public class HighlightPlugin extends EditPlugin {
    public void start() {
    }

    public void stop() {
    }

    }

    ในส่วน start กับ stop ใช้ในการเตรียม resources
    ต่างๆที่ plugin จะใช้
    ในกรณีของเราไม่ต้องการทำอะไรก็เลยเว้นไว้

    จากนั้นก็ทำการเขียน plugin properties file
    โดยมีชื่อเดียวกับ plugin class แต่ตัด word "Plugin"
    ที่ต่อท้ายออก ในกรณีนี้ใช้ชื่อว่า Highlight.props

    plugin.highlight.HighlightPlugin.activate=defer
    plugin.highlight.HighlightPlugin.name=HighLight
    plugin.highlight.HighlightPlugin.author=pphetra
    plugin.highlight.HighlightPlugin.version=0.1
    plugin.highlight.HighlightPlugin.depend.0=jedit 04.00.03.00

    # Menu
    plugin.highlight.HighlightPlugin.menu=highlight.convert

    # Menu action labels
    highlight.convert.label=convert to clipboard


    ในตัว props จะประกอบด้วย 2 ส่วนคือ
    ส่วน metadata กับส่วน menu
    "highlight.convert" ก็คือ menuItem
    ที่จะ add เข้าไปใน Plugin Menu

    โดย ค่าของ plugin.highlight.HighLightPlugin.menu
    มีความหมายว่า
    • ให้ add menu ที่มี label เป็นชื่อเดียวกับ Plugin Name เข้าไปใต้
      Plugins Menu
    • add menuItem ที่ชื่อ highlight.convert
      เข้าเป็น menuItem ของ menu ข้างบน


    ส่วนกรณีที่มี submenu หลายๆอัน ก็แค่ใส่เรียงกันไป
    เหมือนตามตัวอย่างข้างล่างนี้

    plugin.highlight.HighLightPlugin.menu=highlight.current-buffer highlight.current
    -selection highlight.dump

    # Menu action labels
    highlight.current-buffer.label=Current buffer
    highlight.current-selection.label=Current selection
    highlight.dump.label=dump


    จากนั้นก็เขียน action mapping file
    ที่ชื่อ actions.xml

    <?xml version="1.0"?>
    <!DOCTYPE ACTIONS SYSTEM "actions.dtd">
    <ACTIONS>
    <ACTION NAME="highlight.convert">
    <CODE>
    plugin = org.gjt.sp.jedit.jEdit.getPlugin("highlight.HighLightPlugin"
    , true);
    plugin.convert(textArea, view);
    </CODE>
    </ACTION>
    </ACTIONS>

    หลักการของ file นี้ก็คือ เราจะกำหนดว่า
    เมื่อ action ถูก trig ให้ทำงาน
    จะต้องทำงานอะไร
    ซึ่งการสั่งว่าต้องทำงานอะไรนั้น จะใช้ BeanShell script
    สำหรับในตัวอย่างนี้ กรณีที่ action highlight.convert
    ถูกเรียกใช้ ก็จะทำการเรียกใช้
    method convert ใน HighlightPlugin ของเรา

    เพิ่ม method convert(..) เข้าไปใน HighlightPlugin ของเราดังนี้

    public void dump(JEditTextArea textArea, View view) {
    Selection[] selection = textArea.getSelection();
    StringWriter sw = new StringWriter();
    BufferedWriter out = new BufferedWriter(sw);

    int last = 0;
    for (int i = 0; i < selection.length; i++) {
    if (selection[i].getEndLine() > last) {
    last = selection[i].getEndLine();
    }
    }

    // Sort selections by their start lines
    MiscUtilities.quicksort(selection, new SelectionStartLineCompare());
    int lastLine = -1;
    for (int i = 0; i < selection.length; i++) {
    int physicalFirst = selection[i].getStartLine();
    int physicalLast = selection[i].getEndLine();

    if (physicalLast <= lastLine) {
    continue;
    }

    formatText(out, textArea, Math.max(physicalFirst, lastLine + 1),
    physicalLast);

    lastLine = physicalLast;
    }
    try {
    out.flush();
    out.close();
    } catch (IOException io) {

    }

    Registers.setRegister('$', sw.toString());
    }

    จะเห็นว่าใน source code มีการทำ sorting ด้วย
    เนื่องจาก jEdit สามารถทำ multiple selections ได้
    จึงต้องจัดเรียงลำดับ block ให้ถูกต้องก่อน
    จากนั้นก็ใล่ loop เพื่อทำการ format
    แต่ละ block ให้เป็น html

    ในการ convert เป็น html นั้น เราจะใช้
    feature ของ jedit ในส่วนของ syntax highlighting เอง
    โดยเราสามารถ access SyntaxStyle จาก JEditTextArea ได้
    ซึ่ง jedit กำหนดไว้ว่า แต่ละภาษา
    สามารถ config Token type ได้สูงสุด 18 แบบ
    โดยวิธีกำหนด style ของแต่ละ syntax สามารถดูได้จาก
    link นี้

    source code ในส่วนที่แปลงเป็น html
    ไม่ได้ลงไว้ให้ดูครับ เนื่องจากมันค่อนข้างยาว
    ผู้ที่สนใจสามารถ download Code2HTML plugin
    มาดูเป็นตัวอย่างได้

    Related link from Roti

    ใช้ jEdit ช่วยทำ Source Highlighting #1

    อยากให้ blog ของตัวเอง
    สามารถแสดง syntax highlighting ในส่วน source code ได้มานานแล้ว
    แต่ยังไม่มีโอกาสสักที วันนี้ก็เลยมองหา solution ดู

    กรณีที่เป็น unix command line
    ก็จะมี tool ที่เรียกว่า source-highlight อยู่แล้ว
    สามารถเรียกใช้ด้วยคำสั่ง
    source-highlight -s java -f html -cstylename Main.java

    แต่เนื่องจากวิธีนี้ไม่ค่อยสะดวกสบายนัก
    เพราะขณะที่เราเขียน blog ด้วย editor
    การ switch ไปใช้ command line ก็ไม่ใช่ทางเลือกทีี่ดีเท่าไร
    (แถม output ที่ได้ ยังเป็น html ตัวเต็มๆ
    ซึ่งเราต้องการแค่บางส่วนเท่านั้น )

    ทางเลือกที่ 2 ที่เจอก็คือ
    xmltools
    แนวทางนี้ก็คือเขียน blog เป็น xml file เลย
    จากนั้นก็ทำการ transform xml file นั้น
    ทางเลือกนี้ก็น่าสนใจดี
    เหมาะอย่างยิ่งสำหรับการเขียนเอกสาร ในลักษณะของ xml document
    แต่ก็ยังไม่ถูกใจอยู่ดี

    ทางเลือกที่ 3 ก็คือเขียนเป็น editor plugin หรือ script
    ให้สามารถเลือกส่วนที่ต้องการ convert
    จาก editor โดยตรง จากนั้นก็สั่ง
    transform เพื่อให้เป็น html ที่ต้องการ

    ตกลงใจเลือกทางเลือกที่ 3
    จากการค้นดูใน net พบว่า
    เจ้า jEdit มี plugin ที่ชื่อ Code2HTML อยู่
    ก็เลยลอง load มาใช้
    ผลลัพท์ที่ได้ก็ยังไม่น่าพอใจนัก เพราะ code ที่ gen
    ออกมานั้นยังทำส่วน CSS ไม่ดีนัก

    คำว่าไม่ดี ก็คือมันมี css element
    ที่มีชื่อเป็น syntax1, syntax2, syntax3 ... ออกมาเต็มไปหมด
    นอกจากนี้ การ gen ก็เป็นการ gen file html ทั้ง file
    คือมี tag html, head, body ออกมาด้วย

    สุดท้ายก็เลยทำการเขียน jEdit Plugin ขึ้นมาเอง
    โดยใช้ตัวอย่าง code จาก code2HTML plugin
    ซึ่งก็ได้ความมันส์พอสมควร เพราะมีบางอย่างที่คู่มือไม่ได้อธิบายไว้
    ต้องไล่จากตัว source code ของ jedit เอง

    Related link from Roti

    Saturday, June 18, 2005

    ปัญหา Common Logging + Log4j ใน Web Application

    สงสัยมานานแล้ว ว่ามันทะแม่งๆ
    วันดีคืนดีก็เจอปัญหาแปลกๆ LogConfigurationException บ้าง
    log หายบ้าง
    วันนี้เจอ article เรื่อง
    Taxonomy of class loader problems encountered when using Jakarta Commons Logging

    เนื้อหายาวมาก แถมละเอียด ชี้แจงประเด็นเป็นขั้นเป็นตอน
    แต่ผมอ่านไม่ครบหรอกนะ ไว้มีเวลาจะค่อยๆทำตามที่เขาว่าดู

    เอาเป็นว่าเขาสรุปทางแก้ไขไว้ดังนี้

    เลิกใช้ jakarta common logging
    ในกรณีที่ต้องการ abstract logging (พวกที่ switch ได้ว่า
    จะใช้ log4j หรือ jdk1.4 log หรืออื่นๆ..ได้)
    ให้เปลี่ยนไปใช้ UGLI แทน
    แต่ถ้าไม่สน ก็ใช้ log4j ไปตรงๆเลย

    สำหรับ tomcat 5.0.27 or later
    • เปลี่ยนไปใช้ jakarta commons logging ที่ version >= 1.0.4
    • อย่าไปยุ่งกับ file TOMCAT_HOME/bin/commons-logging-api.jar
    • ให้นำ file commons-logging.jar กับ log4j
      ไปวางไว้ที่ TOMCAT_HOME/commons/lib
    • อย่า include log4j, commons-logging.jar ใน WEB-INF/lib
    • ห้าม set system properties พวกนี้
      org.apaceh.commons.logging.LogFactory
      org.apache.commons.logging.Log


    Note: พวกที่ใช้ Resin 2.0.x, Jetty ตามอ่านเอกสารเอาเองนะ
    เพราะผมไม่ได้ใช้ เลยไม่ได้ลอกมาให้ดู

    อ้างอิง
    สำหรับคนที่ชอยอ่านประเด็นมากกว่านี้
    ให้ดูที่ Link นี้

    Related link from Roti