Notes by Dr. Xi
Displaying notes 41 - 50
Created by Dr. Xi on June 22, 2011 13:23:19
Last update: June 22, 2011 13:23:19
This is a utility to convert a byte array to hex string, and to convert a hex string back to the original.
To convert a String to hex, you must call String.getBytes() first. The utility does not pretend to know your string encoding.
public class StringHexUtil {
public static ...
Created by Dr. Xi on June 22, 2011 12:14:15
Last update: June 22, 2011 12:15:12
Parsing a CVS file seemed to be a sore spot for software development. It's not simple enough that you can roll your own code in a couple of hours, and yet not deemed big enough for a full-fledged project. As a result, there are multiple libraries, each one with its own quirks. This is a summary of some simple tests I've done with five CSV parsers. Apache commons CSV parser : Does not escape backslash. The backslash is treated as literal if not proceeding a double quote, but then there's no way to have a backslash as the last character before the ending quote (even though it's a rare scenario). IOException thrown for unmatched quotes SuperCSV parser Can't handle escapes inside quotes Throws Exception for...
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 August 11, 2007 14:36:11
Last update: June 21, 2011 15:19:41
When transforming XML into HTML with XSLT, doesn't seem to work, but   does the trick.
Frequently used entities:
Name Character Entity Reference Numeric Reference
quot " " $#34;
amp & & $#38;
apos ' ' $#39;
lt < < $#60;
gt > > $#62;
nbsp non-breaking space $#160;
copy © © $#169;
reg ® ® $#174;
The full list is available here:
http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references
Created by Dr. Xi on June 20, 2011 15:18:06
Last update: June 20, 2011 15:18:06
Perl does not have built-in functions to get the base name or dir name from a file path. But on the Unix platforms we can rely on the executables basename and dirname .
#!/usr/bin/perl
$f = '/A path with/space "/file...
The quotes, escapes, and chomp are necessary:
# This fails when there's a space in $f
$base =...
Created by Dr. Xi on June 16, 2011 14:23:44
Last update: June 16, 2011 14:25:04
This example shows how a service implementation can be loaded with a URLClassLoader .
The files.
HelloService.java :
public interface HelloService {
public void...
HelloServiceImpl.java :
public class HelloServiceImpl implements HelloServ...
ServiceFactory.java uses URLClassLoader to load the service implementation:
import java.io.*;
import java.net.*;
pub...
Test.java :
import java.io.*;
import java.net.*;
pub...
Create a runnable client jar:
C:\>jar -cfe client.jar Test Test.class ServiceFac...
Create a service jar:
jar -cf service.jar HelloServiceImpl.class
Test:
C:\>java -jar client.jar
Class Loader: java.net...
Extra Note: : JDK7 added a method URLClassLoader.close() , so that a URLClassLoader can be discarded, and a new one created to load a new implementation.