DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world


  • submit to reddit

Recent Snippets

    <name>Robert Jones</name>	
    <street>Cedar Park Way</street>

<xsl:stylesheet xmlns:xsl=""

  <xsl:output method="text"/>

  <xsl:template match="addresses">
      <xsl:sort select="name"/>

  <xsl:template match="address">
	<!-- Name -->
	<p ><strong><xsl:value-of select="name"/></strong> </p>

	<xsl:variable name="street" select="street"/>
	<xsl:if test="string($street)">
		<p ><strong>Address<br/></strong>
		<xsl:value-of select="$street" /><br/>
		<xsl:value-of select="postcode"/> <xsl:value-of select="city"/>

                    Grep a bunch of jars for a certain package or class or expression:

find /path/to/directory -type f -name '*.jar' -print0 | xargs -n1 -0i sh -c 'jar tf "{}" | grep -q query && echo "{}"'


find ~/soft/java/maven-repository -type f -name '*.jar' -print0 | xargs -n1 -0i sh -c 'jar tf "{}" | grep -q org.apache && echo "{}"'
                    This BigDecimalFormatter tries to parse the text as BigDecimal for the current locale. It uses the BigDecimal(String) constructor to keep the number of decimals from the text.

public class BigDecimalFormatter extends NumberFormatter {
    public Object stringToValue(String text) throws ParseException {
        if("".equals(text.trim())) {
             return null;
         char ds = getDefaultLocaleDecimalSeparator();

         try {
             String val = text;
             if(ds != '.') {
                 val = val.replace(".", "").replace(ds, '.');
             return new BigDecimal(val);
         } catch(NumberFormatException e) {
             return null;

    public String valueToString(Object value) throws ParseException {
        return super.valueToString(value);

    private char getDefaultLocaleDecimalSeparator() {
          DecimalFormatSymbols symbols = new DecimalFormat("0").getDecimalFormatSymbols();
          char ds = symbols.getDecimalSeparator();
          return ds;


public class BigDecimalFormatterFactory extends JFormattedTextField.AbstractFormatterFactory {
    public JFormattedTextField.AbstractFormatter getFormatter(JFormattedTextField tf) {
        return new BigDecimalFormatter();

Use it like this:

bigdecimalfield.setFormatterFactory(new BigDecimalFormatterFactory());
                    To create a borderless JButton in swing use the following:

JButton taskButton = new JButton(action);
taskButton.setHorizontalAlignment(JButton.LEADING); // optional

This is useful for "hyperlink" style buttons or "task buttons".
                    To change all .htm files in a folder to .html files:

for f in *.htm; do mv $f `basename $f .htm`.html; done;
                    Go back to your previous directory:

cd -

Go to a new directory and push the current one onto a stack:

pushd /newdirectory

Pop a directory from the stack and go to it:

things = [] #[1,2,3]

if things.each do |thing|
    puts thing
end.empty? then
    puts "no things!"
                    Example of a Auto Attendant menu.

exten => s,1,SetVar(NUMINVALID=0)
exten => s,2,Setvar(NUMTIMEOUTS=0)
exten => s,3,Background(sales_press_one)
exten => s,4,Background(support_press_two))
exten => s,5,ResponseTimeout(5)
exten => s,6,DigitTimeout(2)

exten => t,1,SetVar(NUMTIMEOUTS=$[${NUMTIMEOUTS} + 1])
exten => t,2,Gotoif($[${NUMTRIES} < 2]?s,3)
exten => t,3,Background(vm-goodbye)
exten => t,4,Hangup()

exten => i,1,SetVar(NUMINVALID=$[${NUMINVALID} + 1])
exten => i,2,Gotoif($[${NUMINVALID} < 4]?:10)
exten => i,3,Background(invalid)
exten => i,4,Goto(s,2)
exten => i,10,Playback(vm-goodbye)
exten => i,11,Hangup()
sudo port install ruby
sudo port install rb-rubygems
sudo gem install rails --include-dependencies
sudo ln /opt/local/bin/ruby /usr/local/bin/ruby

Originally from (added --include-dependencies)                
cvs -d login
cvs -d update -P darwinports

or, if you didn't get the source from CVS:

sudo port -d selfupdate