Skip navigation
ANNOUNCEMENT: community.oracle.com is currently Read only due to planned upgrade until 29-Sep-2020 9:30 AM Pacific Time. Any changes made during Read only mode will be lost and will need to be re-entered when the application is back read/write.

I write my lecture slides in XHTML, using the marvelous HTML Slidy package. I just dump the images into the same directory as the HTML files, which isn't so smart because it makes it hard to copy a presentation from one directory to another. I could change my habit, but hey, what is technology for? A couple of years ago I decided to write a script that simply generates a list of all images in an HTML file, so I can run

cp `images 01-intro.html` somewhere

http://weblogs.java.net/blog/cayhorstmann/archive/piece-of-cake.jpg

Piece of cake, right? Just look for <img src="foo.jpg" .../>. Now I could just use a regular expression. But, asJamie Zawinski said, “Some people, when confronted with a problem, think ‘I know, I'll use regular expressions.’ Now they have two problems.”

Wise words indeed. I could spend a long time fussing with problems such as <img (newline) src=. Of course, the right thing to do is to use an XML parser, and the manly thing to do is to use XSLT. After more pain than seemed warranted, I came up with this XSLT script.

<xsl:stylesheet version = '1.0'
      xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
      xmlns:html="http://www.w3.org/1999/xhtml">
   <xsl:output method="text"/>
   <xsl:template match="html:img">
      <xsl:value-of select="@src"/>
      <xsl:text></xsl:text>
   </xsl:template>
   <xsl:template match="@* | node()">
      <xsl:apply-templates select="@* | node()"/>
   </xsl:template>
</xsl:stylesheet>

Do not ask me about it. I do not want the pain to recur.

It worked fine for a couple of years, but this morning it broke.

java.io.IOException: Server returned HTTP response code: 503 for URL: http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd

WTH? I pasted the URL into my browser, and it worked just fine. Next, I tried telnet.

$ telnet www.w3c.org 80
Trying 128.30.52.45...
Connected to dolph.w3.org.
Escape character is '^]'.
GET /TR/xhtml1/DTD/xhtml1-strict.dtd HTTP/1.0

HTTP/1.1 503 Service Unavailable due to Unknown abuse from requesting IP
...
<h1>Forbidden due to abuse</h1>

<p>We are most interested in finding the source of this particular
abuse.  Please <a href="mailto:web-human+unknown-abuse@w3.org">contact
us</a> if you have any details as to the client software running
(browser, web crawler, other), what it was requesting, who your
provider is or are willing for us to follow up with you and try to get
details.</p>
...
Connection closed by foreign host.

Apparently, the W3C decided to crack down on programs that just fetch a DTD from its server. If the user agent is Java, not Mozilla, you get an Error 503. Of course, I don't actually need the DTD. No problem, I just use one of these magic incantations for the parser, or maybe the parser factory, or the parser factory configuration, or the parser factory configuration assembly—as the designers of the SAX API demonstrate so vividly, any problem in computer science can be amplified with another level of indirection.

http://weblogs.java.net/blog/cayhorstmann/archive/evel-knievel.jpeg

Except I didn't write a program that used the SAX API—I am no Evel Knievel. I just invoked Xalan on the command line. And I do not have the intestinal fortitude for figuring out its command line options.

Then I remembered that Scala can process XML natively.

My first attempt failed miserably:

val x = scala.xml.XML.loadFile("01-intro.html")
java.io.IOException: Server returned HTTP response code: 503 for URL: http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd

Ugh, the Scala library uses Xerces, just like Xalan does.

But they have another parser, and that one worked fine.

val doc = ConstructingParser.fromFile(new File("01-intro.html"), true).document
doc \\ "img" \\ "@src" foreach println

Two lines of Scala made the medicine go down...

http://weblogs.java.net/blog/cayhorstmann/archive/a-spoonful-of-sugar.jpeg

So, what is the moral of all this?

  • The Scala interpreter is your friend. It took me a few minutes of futzing around to get this to work, and they were not painful minutes because I got positive feedback along the way, not just the familiar series of error messages that tell me “You suck”. Having an interpreter is great for experimenting with an unknown API.
  • XML is really bad for describing behavior. It should not take twelve lines of gobbledygook to express doc \\ "img" \\ "@src" foreach println.
  • Operator overloading is (gasp) not evil. I am vaguely familiar with XPath, and the Scala \\ looks like XPath// (which, for obvious reasons, they couldn't have taken verbatim :-)), so the learning curve was minimal.
  • Learning a new programming language is hard. You have to make yourself do it. I resolved to write all my little utility programs in Scala until my fingers know it as well as they know Java.

I am rewriting a Java book for beginners, and it seems to make so much sense to use

import static java.lang.System.out;

public class Greeting
{
   public static void main(String[] args)
   {
      out.println("Hello, World!");
   }
}

static.jpg

I would no longer have to dissect the awfulSystem.out.println("Hello, World!") expression. (Ok, boys and girls. System is a class.System.out is a static field of typePrintStream. That's another class. What is static, you ask? It's what a TV set makes when it doesn't get a signal. No, it's what you don't want in your dryer. Ok, whatever it is, you don't ever want to do it in your code, unless it is alsofinal. That means, you can't change it. Oh, you spotted System.setOut. Next question?)

Ditto with mathematical expressions. Doesn't

sin(angle * PI / 180)

look so much nicer than

Math.sin(angle * Math.PI / 180)

Ok, at least to that tiny fraction of the population who knows trigonometry...

The fact is, I don't recall ever seeing anyone using static import outside JUnit 4, where it is justifiably popular to importorg.junit.Assert.*.

I made a Google search, only to find a sensible blogwhose comments were filled with rants that static imports are the work of the antichrist, something on static imports in C#, a recommendation to use it rarely, and the usual certification garbage (hereand here).

I am trying to get the reaction of the average Java coder here. Have you switched from System.out to outwith a static import? Would you think it weird to look at other people's code that did that? Or would you welcome it?