Java

Velocimacro – Generate a HTML Select box and its Options

A quick and simple Velocimacro to generate HTML Select boxes and its Options; automatically selects the correct option based on the value given. You can place this #macro anywhere in your Velocity templates but I used my global VM_global_library.vm file. Code [code] #macro( generateSelectBox $name $options $value ) #foreach ( $option in $options ) #set( …

Velocimacro – Generate a HTML Select box and its Options Read More »

Java Document to String

This is a very simple utility function to produce an indented XML document as a String given a Document. It throws a TransformerException if anything drastic goes wrong. [java] import java.io.*; import org.w3c.dom.Document; import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; … public static String documentToString( Document document ) throws TransformerException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = …

Java Document to String Read More »

Java Object Digester

I was working on a web-based application recently and wanted a lazy way to get data from a simple ‘value object’ out to the front end without having any hard-coded field names; I came up with this. Basically, this method will take any Object, iterate through its methods looking for any of the format “get<something>” …

Java Object Digester Read More »

Java Snippet – Using SecureRandom

A better random number generator [java] import java.security.SecureRandom; … public static SecureRandom random = null; static { try { random = SecureRandom.getInstance(“SHA1PRNG”); random.setSeed( random.generateSeed(256) ); } catch( Exception e ) { e.printStackTrace(); } } [/java]

Java Snippet – Fast File Copy

When using JDK 1.4 and above… [java] public static void copyFile(File in, File out) { try { FileChannel sourceChannel = new FileInputStream(in).getChannel(); FileChannel destinationChannel = new FileOutputStream(out).getChannel(); sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel); // or, you can also copy it this way // destinationChannel.transferFrom(sourceChannel, 0, sourceChannel.size()); sourceChannel.close(); destinationChannel.close(); } catch ( Exception e ) { e.printStackTrace(); } } …

Java Snippet – Fast File Copy Read More »

Java Snippet – Recursive Directory Deletion

Returns true on success, false on failure: [java] public static boolean deleteDir(File dir) { if (dir.isDirectory()) { String[] children = dir.list(); for (int i=0; i<children.length; i++) { boolean success = deleteDir(new File(dir, children[i])); if (!success) { return false; } } } return dir.delete(); } [/java]

Scroll to Top