Recent Notes

Displaying keyword search results 1 - 10
Created by Fang on March 06, 2012 12:24:53    Last update: March 06, 2012 12:24:53
Validation groups can be used to control which rules validation rules to run. A validation group can be identified by any Java interface (not class!). Multiple validation groups may be specified when validating. In this example, I added a validation group named MyValidationGroup ( src/main/java/com/example/MyValidationGroup.java in Maven project): package com.example; public interface MyVal... and added a @Size rule for a person's name, because my database can only store up to 15 characters for a person's name: package com.example; import javax.validatio... Now validate Person with a JUnit test ( src/test/java/com/example/TestPersonWithGroup.java in Maven project): package com.example; import java.util.Set; ... Test with " mvn clean test ". The rules where groups is not specified, which belong to the javax.validation.groups.Default group, are not executed with these tests.
Created by Dr. Xi on June 22, 2011 15:15:15    Last update: June 22, 2011 15:15:15
There's no such thing as an immutable byte array in Java. What we are trying to achieve here is to protect the byte array inside the object from which it is being returned. We want to caller to have the contents of the byte array, but not be able to change the contents inside the object. The trick is to return a copy instead of the internal reference: public class ReturnByteArray { public stati...
Created by alfa on June 02, 2011 15:49:26    Last update: June 02, 2011 15:51:08
Facts: Dynamic proxy classes are generated by the Java runtime, from a list of interfaces given by the user. The generated proxy class implements all interfaces given by the user. The dynamic proxy class is not synthetic . The dynamic proxy class is useless without a user supplied InvocationHandler class, since there's only one constructor for the proxy class and it takes a InvocationHandler as parameter. Example code: import java.lang.reflect.Constructor; import ja... Output: Class: $Proxy0 isSynthetic: false Constructo...
Created by alfa on June 02, 2011 15:26:37    Last update: June 02, 2011 15:26:37
While doing some Java reflection code, I noticed the method Class.isSynthetic() , which the JavaDoc says returns " true if and only if this class is a synthetic class as defined by the Java Language Specification". However, there's no definition of "synthetic class" in the JLS ! The only thing that I can find that remotely resembles a definition is in the JVM spec , where it defines the synthetic attribute : "The Synthetic attribute is a fixed-length attribute in the attributes table of ClassFile (§4.1), field_info (§4.5), and method_info (§4.6) structures. A class member that does not appear in the source code must be marked using a Synthetic attribute." By this definition, a default constructor, which does not appear in the source code, should...
Created by James on July 19, 2009 20:51:23    Last update: January 11, 2011 20:14:18
If CSS3 border-image is properly supported, making a rounded corner box is very easy. You just need a round corner image like this: The following markup: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" ... would render like this (try it in Firefox 3.5 and Google Chrome): However, IE as of version 8.0 does not support border-image . So until border-image is reliably supported in all major browsers, we still have to rely on tried and true tricks to make it work. In general, I found three general categories of tricks to make rounded corners: Good old tables. This trick creates a table of 9 cells and uses the 8 cells on the perimeter to render the borders and rounded corners. The central cell is used for...
Created by Dr. Xi on October 26, 2010 16:07:40    Last update: October 26, 2010 16:07:40
This is a more generic version, which can be expanded to accommodate additional file signatures. import java.io.*; import java.util.*; pu...
Created by Dr. Xi on August 03, 2010 16:23:55    Last update: October 22, 2010 15:35:20
According to Java documentation: The Java compiler and other Java tools can only process files which contain Latin-1 and/or Unicode-encoded (\udddd notation) characters. This utility converts a utf-8 encoded file to ascii with unicode escape strings for non-ascii characters. import java.io.*; /** * Reads file in U... It is equivalent to: native2ascii -encoding utf-8 using the standard Java native2ascii utility.
Created by Fang on August 23, 2010 22:55:58    Last update: August 24, 2010 15:45:04
The tags XML flow control tags are exactly the same as their Core flow control equivalents, except that the test condition with a boolean EL expression is replaced by the select condition with an XPath expression. In the case of the forEach tag, the items attribute is replaced with the select attribute. In a test condition, the XPath expression is evaluated to a boolean value by the rules of the XPath boolean() function, which converts its argument to a boolean as follows: a number is true if and only if it is neither positive or negative zero nor NaN. a node-set is true if and only if it is non-empty. a string is true if and only if its length is non-zero. an object of...
Created by Dr. Xi on July 19, 2010 21:58:34    Last update: July 23, 2010 21:37:23
Parsing XML in Java is really simple: import java.io.*; import javax.xml.parsers.Docu... The parser implementation details are hidden behind the JAXP API. In case you want to know which parser implementation is used, this is what the JavaDoc for DocumentBuilderFactory.newInstance says: Use the javax.xml.parsers.DocumentBuilderFactory system property. Use the properties file " lib/jaxp.properties " in the JRE directory. This configuration file is in standard java.util.Properties format and contains the fully qualified name of the implementation class with the key being the system property defined above. The jaxp.properties file is read only once by the JAXP implementation and it's values are then cached for future use. If the file does not exist when the first attempt is made to read from it, no further attempts are made to...
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...
Previous  1 2 Next