Archive for the 'Programming' Tag

SLF4J and making JUL shut up

I’ve decided to switch to the Simple Logging Facade for Java (SLF4J) plus Logback to bridge java.util.logging (JUL), Log4J, and Apache Commons Logging all into one log output.

Problem is, JUL won’t shut up. Frameworks that log to JUL output the log to the console, and then SLF4J repeats it right after. However, putting this code in before running SLF4BridgeHandler.install() seems to fix it:

java.util.logging.Logger root_logger = java.util.logging.LogManager.getLogManager().getLogger("");

java.util.logging.Handler[] root_handlers = root_logger.getHandlers();

rootLogger.removeHandler(root_handlers[0]);

Now I get one single log output alone.

Ivy and Sun’s Java.net Maven repo

I want to use Sun’s Java.net Maven repo with Ivy, and this is not documented well anywhere.

In ivysettings.xml (Ivy will automatically use it) put:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ivysettings>
<ivysettings>
  <settings defaultResolver="chained" />
  <property name="java.net.maven.pattern"
    value="[organisation]/jars/[module]-[revision].[ext]" />
  <resolvers>
    <chain name="chained" returnFirst="true">
      <ibiblio name="ibiblio" m2compatible="true" />
      <ibiblio name="java-net-maven2"
        root="http://download.java.net/maven/2/"
        m2compatible="true" />
      <ibiblio name="java-net-maven1"
        root="http://download.java.net/maven/1/"
        pattern="${java.net.maven.pattern}"
        m2compatible="false" />
    </chain>
  </resolvers>
</ivysettings>

Now you can make an ivy.xml with dependencies like <dependency org="com.sun.grizzly" name="grizzly-http" rev="2.0.0-SNAPSHOT"/> and have it work right.

Feeding marshalled JAXB data to Jersey

Although Jersey supports eating JAXB’ed classes fine, sometimes you want to manually alter the data, such as including a processing instruction for XSL stylesheets. There probably should be a less verbose way to do this.

The object should be an @XMLRootElement annotated object.

@GET
@Produces("application/xml")
public static StreamingOutput outputXMLwithXSL() {
  return new StreamingOutput() {
    public void write(OutputStream output) throws IOException,
    WebApplicationException {
      Object object = yourJAXBObject();

      JAXBContext jc = null;
      try { jc = JAXBContext.newInstance(object.getClass()); } 
      catch (JAXBException e) { e.printStackTrace(); }

      Marshaller m = null;
      try { m = jc.createMarshaller(); }
      catch (JAXBException e) { e.printStackTrace(); }

      PrintStream ps = new PrintStream(output);
      ps.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
      ps.println("<?xml-stylesheet type=\"text/xsl\" href=\"your.xsl\"?>"); 

      try { m.setProperty(Marshaller.JAXB_FRAGMENT, true); }
      catch (PropertyException e) { e.printStackTrace(); }

      try { m.marshal(object, output); }
      catch (JAXBException e) { e.printStackTrace(); }
    }
  };
}

Evil solution to the XSLT empty xmlns probem

I’m currently using XSLT, and I’ve come across the dreaded empty xmlns problem. My XML contains elements that do not have rules in my XSL stylesheet and most XSLT engines append the attribute xmlns="" when it gets confused about what namespace the element belongs to… I get bitten by this because I do not have an input DTD as the document is not meant to be used as anything but fodder to create XHTML.

Many people even have this problem when using XSLT to transform XHTML into XHTML… the input and output namespaces are the same, and they’re using properly formated and validated XHTML documents (complete with the doctype statement and the xmlns attribute on the html element).

Many people just want to force the transforming engine to blindly copy the elements as is over to the new document, and ignore the namespace issue. The below XSL should do this, use wisely.

<xsl:template match="*|@*">
    <xsl:element name="{ local-name( . ) }">
        <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
</xsl:template>

So, now I can go use XHTML in my input and have it spit back out unmolested.

Random Perl: How to check if something is a number

Perl has no built in function or sub to test if a variable is a number or not. Scalar::Util makes it easy, and is a core module as well.


use Scalar::Util qw(looks_like_number);

my $number = 192;
my $string = '123foobarbazquux123';

if(looks_like_number($number)) {
  print "$number is a number!\n";
}

if(!looks_like_number($string)) {
  print "$string is not a number!\n";
}

Tada!