Print output while Processing HTML/XML data in Jsoup Project

Currently, I encountered one problem while retrieving XML data from one website. In my case, assume that the original XML document is like

<result>
<device />
<name>Allen's device</name>
</result>

If I use Jsoup.parse(File, “UTF-8”); without additional options, the returned document object will be like:

<result>
<device>
<name>Allen's device</name>
</result>

The weired result is <device> is just an open tag but without close tag here. Howevver, if you process <size /> tag in this example, the program will produce the same tag <size />.

That’s because Jsoup adds some initial tags which can be viewed as an open tag but don’t attach a close tag. In this condition, <device /> can be first derived as an open tag <device> and jsoup will create an empty tag at line 204 from org.jsoup.parser.HtmlTreeBuilder.insertEmpty(Token.StartTag) method. Since <device /> is an self-closing tag, this procedure will go through line 205 ~ 210 of insertEmpty.

The program will go into line 205. The reason is that <device /> is one of known tags. The definitions of known tags are initialized since line 257 (the all known tags’ definition can be found in line 221 ~ line 253) of org.jsoup.parser.Tag and will be called by line 29 of org.jsoup.nodes.Document. Due to this insertEmpty procedure run into line 206, it called a boolean operation here then return element <device>. Notice that this insertEmpty methodis called by org.jsoup.parser.HtmlTreeBuilderState.process (Token, HtmlTreeBuilder).

After that, while reading close tag </device>, the procedure will call anyOtherEndTag(t, tb) in line 746 of HtmlBuilderState. Then, that procedure will call HtmlTreeBuilder.generateImpliedEndTags(String) in line 765. Significantly, the procedure uses popToStack() to pop out </device> element from a stack here.

However, it doesn’t perform true closing tag operation. That means, HtmlTreeBuilder leaves an open tag- <device> in its’ HTML Tree after executing line 206 of insertEmpty method, but doesn’t handle </device> tag to complete closing tag operation in anyOtherEndTag.

Posted in Java, Programming, XML, 程式設計, 網路 java | Leave a comment

How to fast calculate ( I mod N)?

Given integer I and an integer N which is power of 2, how does it work faster to calculate “I mod N”?
OpenJDK’s java.util.HashMap.indexFor method gives us a best solution for it.
It simply calculates ” I (bitwise AND) (N-1) “.

Reference

1. “HashMap.” Available: [Online] http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7u40-b43/java/util/HashMap.java#HashMap.indexFor%28int%2Cint%29

2. “HashMap implementation in Java. How does the bucket index calculation work?” Stackoverflow. Available: [Online] http://stackoverflow.com/questions/10879302/hashmap-implementation-in-java-how-does-the-bucket-index-calculation-work

Posted in Java, Programming, 程式設計 | Leave a comment

Install JDK in Debian

1. Donload jdk package from Oracle:

wget --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u40-b26/jdk-8u40-linux-x64.tar.gz
2. Setop JDK Environment
sudo sumkdir /opt/jdk
tar -zxf jdk-8u40-linux-x64.tar.gz -C /opt/jdk
update-alternatives --install /usr/bin/java java /opt/jdk/jdk1.8.0_40/bin/java 100
update-alternatives --install /usr/bin/javac javac /opt/jdk/jdk1.8.0_40/bin/javac 100
3. Check Java Environment. To check the java environment, you just use this command:
java –version

The console will prompt message like this:
java version “1.8.0_40”
Java(TM) SE Runtime Environment (build 1.8.0_40-b26)
Java HotSpot(TM) 64-Bit Server VM (build 25.40-b25, mixed mode)

If you get the following message while using java commend:
bash: /usr/bin/java: No such file or directory
This problem is due to lack of libc6-i386 package. Use the following command to install it and then java can be run.
sudo apt-get install libc6-i386


Reference
1. Digital Ocean.com , "How To Manually Install Oracle Java on a Debian or Ubuntu VPS", Digital Ocean Community. Available: [Online] https://www.digitalocean.com/community/tutorials/how-to-manually-install-oracle-java-on-a-debian-or-ubuntu-vps

Posted in Java, Linux | Leave a comment

The Problem of Ternary Operators in PHP

How do you think of the ternary operators in PHP? Recently, I’’ve used the code written as below:

echo (1?"Y":0?"N":"");

The code returns “N” but not “Y”. How does it work?

It shoud be like:

echo ((1?"Y":0)?"N":"");

Finally, please follow the advice from PHP docs:

It is recommended that you avoid “stacking” ternary expressions. PHP’s behaviour when using more than one ternary operator within a single statement is non-obvious.

Then you will make better code in PHP.

Reference

1. Mac Taylor, “Stacking Multiple Ternary Operators in PHP, “ stackoverflow [Online]. Available: http://stackoverflow.com/questions/5235632/stacking-multiple-ternary-operators-in-php

Posted in PHP, Programming, 程式設計 | Leave a comment

Google’s form notification for Add-on version is ready. (新版的Google表單信件通知外掛)

中文:
感謝網友的不斷提醒,敝人最近才發現我之前寫的Google 表單寄信scirpt已經失效了。由於Google把所有script改成 Add-On,逼得小弟不得不寫另一個Add-On出來。所有的source code都在https://github.com/allen501pc/Advanced-Form-Notificaiton
用法很簡單,把上面所有除了README.md的檔案下載下來。按照以下步驟:
1. 建立表單,輸入你要的欄位
2. 進入 工具指令編輯器
3. 工具列上方的新增→檔案→專案→對象為Form(表單),將專案命名為:Advanced Form Notification
4. 在你的程式碼.gs,刪除裡面內容,貼上下載下來的Main.gs的內容
5. 新增檔案→HTML檔案→名稱:About (不要有.html),貼上下載的About.html的內容。
6. 如上步驟依序新增AuthorizationEmail.html, CreatorNotification.html, RespondentNotification.html 以及 Slidebar.html等檔案,並貼上相對應檔的內容。
7. 儲存專案
8. 到原表單編輯器中,點選外掛程式→Advanced Form Notification→Configure…
9. 假設你有表單欄位如下:
欄位 1: name
欄位 2: email

今天若想要通知使用者如下內容(假設使用者填寫的email跟name分別是abc@example.com, Allen)

Thanks for filling our questionnaire survey. Your content shows below: 
Your e-mail: abc@example.com
Your name: Allen
那麼只要在右方把Notify respondents打勾,然後在Notification email body寫著如下:
Thanks for filling our questionnaire survey. Your content shows below: 
Your e-mail: __##email##__
Your name: __##name##__

指定好要收件人的email欄位跟信件標題就可以自動寄信囉。

In English:

Google has combined its’ script editor with Google Add-On. The past post I wrote is not available. I’ve created a new project named “Advanced Form Notification” on GitHub ( Project’s URL: https://github.com/allen501pc/Advanced-Form-Notificaiton ).

After downing all of the source codes except README.md of the project. All of you just do the following steps:

1. Create Google form and type any fields you want, including at least one e-mail field.

2. Navigate the toolbar and click script editor

3. Create a new project whose target is “Form”  and entitle it as “Advanced Form Notification “.

4. Remove the content of your current code and paste the content of downloaded Main.gs

5. Go to filenew→HTML file and click it,  adding a new HTML file named “About” (attention, don’t append .html extension file name) and then paste the content of downloaded “About.html”.

6. Similar to the above step 5, add AuthorizationEmail.html, CreatorNotification.html, RespondentNotification.html and Slidebar.html with correspondent file content.

7. Save the project.

8. In the form editor, go to Advanced Form NotificationConfigure….

9. Assume that you have a form like this:

field 1’s name: name
field 2’s name: email

and you want to customize your notification content for each respondent’s email like this ( in this case, the respondent’s e-mail is abc@example.com and his name is Allen:

Thanks for filling our questionnaire survey. Your content shows below: 
Your e-mail: abc@example.com
Your name: Allen

The easy way for you is just adding Notification email body of Notify respondents below:

Thanks for filling our questionnaire survey. Your content shows below: 
Your e-mail: __##email##__
Your name: __##name##__

Then indicate the respondent email field and email title. Your form emailer will be worked. Enjoy it!

Posted in 未分類 | 8 Comments

Superfish: A Malware or Adware in Lenovo Computers?

There was one security issue about Lenovo’s computers.
Part of Lenovo’s products, be sold during Sep. 2014 and Jan. 2015, are pre-installed one software called “superfish” on Windows platform. Some users found that it is spying the users’ search activities and inserted some ads in the Google search results, no matter what browsers they used.
That means this software is collecting your search behavior, storing the data into its’ remote server. Moreover, it might monitor your online transactions because this malware ran in operating system layer, binding into the browsers (including Internet Explorer and Chrome) , not as a browser extensions. 
The production models may be affected are listed below:
G series: G410, G510, G710, G40-70, G50-70, G40-30, G50-30, G40-45, G50-45
U series: U330P, U430P, U330Touch, U430Touch, U530Touch
Y series: Y430P, Y40-70, Y50-70
Z series: Z40-75, Z50-75, Z40-70, Z50-70
S series: S310, S410, S40-70, S415, S415 Touch, S20-30, S20-30 Touch
Flex series: Flex2 14D, Flex2 15D, Flex2 14, Flex2 15, Flex2 14(BTM), Flex2 15(BTM), Flex 10
Miix series: MIIX2-8, MIIX2-10, MIIX2-11
Yoga series: Yoga 2 Pro-13, Yoga 2-13, Yoga 2-11BTM, Yoga 2-11HSW
E series: E10-30.

You can visit the following link to know how to remove the malware.
https://www.eff.org/deeplinks/2015/02/how-remove-superfish-adware-your-lenovo-computer

Reference
1. Mike Shaver. (2015, Feb. 15)  Lenovo caught installing adware on new computers [Online]. Available: http://thenextweb.com/insider/2015/02/19/lenovo-caught-installing-adware-new-computers/
2. Dave Neal. (2015, Feb. 20)  Lenovo: Superfish is gone and was never on ThinkPads or business desktops [Online]. Available: http://www.theinquirer.net/inquirer/news/2396033/lenovo-caught-shipping-pcs-with-pre-installed-superfish-malware

Posted in 未分類 | Leave a comment

[電影] 我唾棄妳的墳墓

劇情

這部翻拍自1978年的電影「我唾棄妳的墳墓」,於2010年正式上映。

劇情描述一位身材標緻的女作家,跑到深山小屋獨自寫作。在加油站加油時遇見三位痞子Johnny, Stanley和Andy。以及在修馬桶時認識的輕度智能障礙男子Matthew 。在她以為當地人非常友善的時候,沒想到Johnny, Stanley與Andy三人卻在打她的壞主意。

Johnny三人帶著Matthew在月黑風高的夜晚強行闖入她家,拿著槍與酒瓶侮辱她。她趁四人不注意的情況下,跑出屋外,找到警長求救。

隔天,警長帶著她回到小屋尋找蛛絲馬跡,只是她萬萬沒想到警長跟這幾個流氓也是一夥的。之後的情節就是女主角受盡各種屈辱,喝泥巴水、肛交甚至被錄影拍下來。心有不甘的她選擇跳湖逃離這個悲慘現狀。

犯人一夥在四處找不到她的屍首情況下,趕緊掩埋一切證據,以免東窗事發。

幸運的是,女主角存活下來,並化身成復仇的惡魔,展開絕地大反攻。

首先對付的對象是第一個(被迫)上她的Matthew。其實Matthew每天活在內疚與自責當中。當他見到女主角時,對她表達他的深深懺悔。但惡魔終究是惡魔,以勒昏他再說。

後來女主角對付愛攝影的Stanley時,用他自己的錄影機拍下自身遭遇,再用吊鉤把他眼皮拉開,並在他臉上塗滿魚內臟。筆者原本想說這樣他怎麼會死?後來才發現這是「鳥刑」:因為內臟引來一群烏鴉,把他眼睛、臉跟腳(被捕獸夾夾傷)給啄死。

其次對付Andy時,以其人之道還治其人之身。他讓她喝泥巴水,這次讓他泡鹽酸水,毀容到死。

而後女主角不知道哪來的天生神力,一棒把主嫌Johnny敲昏。再逐一拔掉他的牙,最後剪掉他的種馬雞雞,流血過多致死。

警長死法最拍案叫絕!因為他喜歡走後門,女主角先用長槍插爆他肛門,並拉引線到昏迷的馬修身上。等到馬修醒來,不慎拉引線觸動板機,一槍貫穿警長屁股,順便把不知情的馬修給打死了。

心得

運鏡與演技:導演在前段的運鏡上,選擇側面拍攝,描繪那種風雨欲來的情境。在拍攝強暴戲的時候,不斷地拍攝女主角驚恐的表情,並順勢帶入女主角第一人稱的視角,來讓觀眾體會女主角當時受盡屈辱的情景,十分到位。接著後面描述犯人受刑罰凌虐時,再從旁就近側拍讓大家感受那種苦痛,真的很虐待觀眾啊。

不得不說,女主角真的很會演,被強暴的那種淒厲叫聲、恐懼以及最後發現警長也是同夥的那種絕望神情,讓身為男士的我,都能夠深刻體會那種絕望,感到不捨。還有最後那復仇工作完成之後的一抹淺淺微笑,實在讓人不寒而慄!

復仇方法:強鹽酸水、烏鴉啄眼睛、長槍插肛門的刑罰都還可以接受。但是拔牙跟剪雞雞的場面,差點讓我吐出來。

Bug:

  1. 女主角在拉起Andy屍首時,雙手浸入鹽酸水,怎麼沒受傷?

  1. 主角天生神力,一棒就敲暈兇手。

  2. 行刑過程中,怎麼不見其他人(可能非犯人)來援助?
Posted in Movie | Leave a comment

[旅遊] 大坑遊記

昨天早上10:40,拿了相機後由學校出發,騎了約莫25分鐘抵達大坑10號步道。

或許是體力真的不行了,爬到上面的時候就已經氣喘如牛。好在天氣沒有很炎熱,一路上的風景還算不錯。

據山友說,9號步道現在多開了一條捷徑,但是坡度有點抖,體力有點差的情況下,宣告放棄。因此改走往另一條比較長的路途。一路上也有一些農友在摘荔枝,看那荔枝真的很飽滿,顏色又火紅,要不是手上的$$不夠,早就買兩串回家。

下了山之後,中午11:40分左右,騎車前往「豬頭三的店」。點了一份蚵嗲以及竹筍湯,來得正是時候,170元吃得很撐。

完食後,前往「東東芋圓」,點一份芋頭仙草綜合芋圓,讓吃完熱食我,馬上解渴。

DSCF0431

在享用美食完畢後,接著騎車前往「郭叔叔獼猴園」,來去看新任猴王跟他的後宮佳麗以及他的徒子徒孫。根據郭叔叔描述,現在4~6月是猴子群的生產期,所以小猴子特別多。10月則是進入發情期。這任的猴王,才登基幾個月,威嚴度還沒有那麼高。不過看他的表情,真的沒什麼威嚴,散散的。

這次的入園券稍微漲價一些,一次要100元,不過可以憑券換飲料,加上郭叔叔的生動解說,還蠻物超所值的。

以上的半天簡易遊記,希望各位看官喜歡。

Posted in 未分類 | 1 Comment

Apache 2.4升級筆記

根據http://jasonpowell42.wordpress.com/2013/04/05/install-apache-2-4-4-on-centos-6-4/ ,但是安裝時要注意configure

cd /usr/src/
 
wget http://ftp.tc.edu.tw/pub/Apache//httpd/httpd-2.4.9.tar.gz
 
tar xzvf httpd-2.4.9.tar.gz
 
cd httpd-2.4.9
 
./configure --enable-so --enable-ssl --with-mpm=prefork --with-included-apr --mandir=/usr/share/man --sysconfdir=/etc/httpd
 
make; make install
 
cd /etc/httpd
 
rm httpd.conf
 
ln -s conf/httpd.conf httpd.conf
 
yum -y install libxml2 libxml2-devel 
 
cd /usr/src/
 
wget http://tw2.php.net/get/php-5.3.28.tar.gz/from/this/mirror
 
tar xzvf php-5.5.13.tar.gz
 
cd /usr/src/php-5.5.13
 
./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql --with-config-file-path=/etc
 
make
 
make install
 
libtool --finish /usr/src/php-5.5.13/libs
 
ln -s /usr/local/apache2/modules /etc/httpd/modules

修改/etc/init.d/httpd,將內容改為以下

apachectl=/usr/local/apache2/bin/apachectl
httpd=${HTTPD-usr/local/apache2/bin/httpd}

存檔離開。

接著 service httpd start就可以了。

請記住,若有設定過httpd.conf的話,請注意這一篇的提醒事項

 

參考資料

  1. [Linux] 重新編譯 PHP 時出現錯誤的解決方式
  2. Upgrading to 2.4 from 2.2
  3. How to Install Apache2.4 PHP5 and MySQL from Source on Debian Linux
  4. Apache 2.4 Configure
  5. Install Apache 2.4.4 on CentOS 6.4
  6. Linux Commands For Shared Library Management & Debugging Problem
  7. How install Apache 2.4 PHP 5.4 and MySQL 5.5.21 on Windows 7
  8. [Apache] Apache 從 2.2 換至 2.4 httpd.conf 的調整筆記 (windows 環境)
  9. Apache Module mod_slotmem_shm
  10. Apache Module mod_access_compat
  11. Apache Module mod_unixd
  12. Linux Tutorial: How to Build Apache, PHP5 and MySQL From Source ( the Hard Way)
  13. Linux: Set OR Change The Library Path
  14. Installing Apache 2.4 and PHP 5.4 from source
Posted in apache, Linux | Leave a comment

An easy way to fix SQL injection and XSS

In my current case, I find some PHP codes have flaws in SQL injection and  XSS.

After analyzing the codes, I find that the URL pameters are usual in numeric type.

So, I just add the codes in the PHP file below:

/* Jyun-Yao Huang modified on 2014/06/12: 
* Check the values of $_GET are in numeric type or not.
* If some values are not in numeric type, then set them as null.
* If some values are in numeric, then strip them.
*/
foreach ($_GET as $key => $value) {
if (! is_numeric($value)) {
$_GET[$key] = null;
} else {
$_GET[$key] = mysql_real_escape_string($value);
}
}

Posted in PHP, Web Design | Leave a comment