Recent Notes

Displaying keyword search results 1 - 12
Created by Fang on January 28, 2012 13:24:09    Last update: January 28, 2012 13:31:22
This is a simple JSP custom tags library with tag body. Just like the JSF counterpart , it splits a string and repeats the body for each word, i.e., with this markup: <%@ taglib uri="http://custom.tag.com/demo" prefix... output: <html> <body> <p>Hello Tigger!</p> <p>H... With Maven, this is the directory structure: ./src ./src/main ./src/main/resources ./s... There are three files to write: pom.xml : <project xmlns="http://maven.apache.org/POM/4.0.0"... src/main/java/tagdemo/IterateTag.java : package tagdemo; import java.io.IOException... src/main/resources/META-INF/demotag.tld : <?xml version="1.0" encoding="UTF-8"?> <!DO... Build with: mvn clean install To use it as a dependency in other Maven projects: <dependency> <groupId>tag-demo</groupId> ...
Created by Fang on November 08, 2011 20:55:00    Last update: November 21, 2011 18:19:44
In the simple taglib example , I used a tag handler class to implement a taglib. This is an example to implement a taglib with a UI component. The purpose is to use a custom tag to split a string and print each part in a separate paragraph, i.e., print <p>john</p> <p>steve</p> <p>mike</p> with custom tag <my:foreach> : <my:foreach var="who" value="john steve mike"> ... These are the files: pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0"... src/main/java/com/example/UIForeash.java : package com.example; import java.io.IOExcep... src/main/resources/META-INF/faces-config.xml : <?xml version="1.0" encoding="UTF-8"?> <faces-c... src/main/resources/META-INF/foreach.taglib.xml : <?xml version="1.0" encoding="UTF-8"?> <facelet... How to use: Put the JAR file generated by the above project in the WEB-INF/lib folder of the web app. If the web app is a Maven project, just add the taglib project as a dependency:...
Created by Dr. Xi on June 22, 2011 07:33:45    Last update: June 22, 2011 11:57:54
Demo code for CSV parsing with OpenCSV . Java code: import java.io.*; import au.com.bytecode.opencs... Test with a simple CSV file: psmith01,CLASS2B,Peter Smith 1,YEAR2,1,N,ADVANCED,... The parser worked correctly: Line 1 has 11 values: |psmith01| |CLASS2B|... Test with a more complicated CSV file: "psmith01 abc", "CLASS2B " , " Peter... Result: Line 1 has 4 values: |psmith01 abc| |CLASS... The parser: Escaped quote and backslash correctly Ignored spaces before the quotation mark - sometimes (see below) Counted spaces after the right quotation mark till the comma as content, including the right quotation mark (bug). Ignored improperly quoted item - silently (third line) Indeed, the OpenCSV parser has a problem with spaces: Input: "Smith, Jack", "210-345-8888" "Smith, J... Result: Line 1 has 2 values: | Smith, Jack| |210-3......
Created by Dr. Xi on June 22, 2011 09:42:35    Last update: June 22, 2011 11:39:40
Demo code for CSV parsing with Skife CSV . Java code: import java.io.*; import java.util.*; import... Test with a simple CSV file: psmith01,CLASS2B,Peter Smith 1,YEAR2,1,N,ADVANCED,... The parser worked correctly: Line 1 has 11 values: |psmith01| |CLASS2B|... Test with a more complicated CSV file: "psmith01 abc", "CLASS2B " , " Peter... Result: Line 1 has 4 values: |psmith01 abc| | CLAS... The parser worked correctly. But notice that it counts the spaces outside of the quotes significant, and doing so consistently. One more test for spaces: "Smith, Jack" , "210-345-8888" "Smith, Jack"... Result: Line 1 has 2 values: | Smith, Jack | | 210... Add a new line in item two: "One", "Two ", "Three" Result: Line 1 has 2 values: |One| | Two| L...
Created by Dr. Xi on June 21, 2011 15:41:51    Last update: June 22, 2011 11:33:36
Demo code for CSV parsing with Apache Commons CSV parser . Java code: import java.io.*; import org.apache.commons.csv... Test with a simple CSV file: psmith01,CLASS2B,Peter Smith 1,YEAR2,1,N,ADVANCED,... Result: Line 1 has 11 values: |psmith01| |CLASS2B|... The parser worked correctly. Test with a more complicated CSV file: "psmith01 abc", "CLASS2B " , " Peter... Result: Line 1 has 4 values: |psmith01 abc| |CLASS... The third line is invalid input, but throwing a Java IOException is a bit grave. Also, the parser is not able to escape a backslash. Add a new line in item two: "One", "Two ", "Three" Result: Line 2 has 3 values: |One| |Two | |...
Created by Dr. Xi on June 21, 2011 15:54:00    Last update: June 22, 2011 11:33:09
Demo code for CSV parsing with SuperCSV parser . Java code: import java.io.*; import java.util.List; imp... Test with a simple CSV file: psmith01,CLASS2B,Peter Smith 1,YEAR2,1,N,ADVANCED,... The parser worked correctly: Line 1 has 11 values: |psmith01| |CLASS2B|... Test with a more complicated CSV file: "psmith01 abc", "CLASS2B " , " Peter... The parser messed up on all three lines: Line 1 has 5 values: |psmith01 abc| |CLASS... Using two lines input: "psmith01 abc", "CLASS2B " , " Peter... It generates an exception: Line 1 has 5 values: |psmith01 abc| |CLASS... Add a new line in item two: "One", "Two ", "Three" Result: Line 2 has 3 values: |One| |Two | |...
Created by Dr. Xi on June 22, 2011 07:12:02    Last update: June 22, 2011 11:32:02
Demo code for CSV parsing with OstermillerUtils . Java code: import java.io.*; import java.util.List; imp... Test with a simple CSV file: psmith01,CLASS2B,Peter Smith 1,YEAR2,1,N,ADVANCED,... The parser worked correctly: Line 1 has 11 values: |psmith01| |CLASS2B|... Test with a more complicated CSV file: "psmith01 abc", "CLASS2B " , " Peter... The parser messed up on all three lines: Line 1 has 6 values: |psmith01 abc| | "CLA... Putting a space before the first quote: "Smith, Jack","210-345-8888" It dismissed the quotes: Line 1 has 3 values: | "Smith| | Jack"| ... Add a new line in item two: "One", "Two ", "Three" Result: Line 1 has 2 values: |One| | "Two| ...
Created by Dr. Xi on January 14, 2010 00:28:27    Last update: March 30, 2011 15:37:44
A task that a Java developer does so frequently is to find out where a certain class can be found - to resolve compilation errors, classpath issues, or version conflicts of the same class introduced by multiple class loaders. A long while back I wrote a simple Perl script to perform the task. Later I was informed that there are Swing based Jar Browser and Jars Browser . Then, there are a couple of shell one-liners: # one liner 1 find -name "*.jar" -print0 | xarg... But all of them share the same problem: if a class is in a jar nested in another jar, it cannot be found. Such is the case for a class inside a jar under the WEB-INF/lib directory of a...
Created by Dr. Xi on October 16, 2008 20:45:40    Last update: March 28, 2011 20:23:22
Java's built-in classes are way too complex/flexible for a simple protocol like HTTP. This is a wrapper to simplify HTTP GET and POST. import java.io.*; import java.net.*; imp... A simple test: import java.io.*; import java.util.*; ...
Created by Dr. Xi on June 20, 2010 14:35:17    Last update: June 20, 2010 14:35:17
This XML signature validator comes from the Apache XML Security project. It validates the signature according to the core validation processing rules . It does not verify that the key used to generate the signature is a trusted key. You can override the KeySelector class to make sure that the signing key is from a trusted store. import javax.xml.crypto.*; import javax.xml.cry...
Created by Dr. Xi on February 01, 2010 23:56:25    Last update: February 01, 2010 23:56:25
This simple class demonstrates the use of java.text.SimpleDateFormat . The strict option can be used to validate a date string. The lenient option takes all kinds of junk and is not suitable for validating date strings. import java.util.*; import java.text.*; ...
Created by Dr. Xi on November 19, 2008 00:22:27    Last update: January 07, 2010 23:00:36
There is a open source project named [ini4j] for processing Windows .ini configuration files. However, I found it an overkill for my purposes. So here is my simple implementation of a .ini parser. It mimics the standard java.util.Properties class with enhancements to get and set properties by section name. There are only a few simple rules: Leading and trailing spaces are trimmed from section names, property names and property values. Section names are enclosed between [ and ] . Properties following a section header belong to that section Properties defined before the appearance of any section headers are considered global properties and should be set and get with no section names. You can use either equal sign ( = ) or colon ( : )...