Red Squirrel Reflections
Dave Hoover explores the psychology of software development

Dave Hoover
dave.hoover@gmail.com

Categories
All [Atom]
Craftsmanship [Atom]
Dynamic [Atom]
Intersection [Atom]
Learning [Atom]
Links [Atom]
Polyglot [Atom]
Projects [Atom]
XP [Atom]
Old Blog

Obtivian Blogs

Andy Maleh
Colin Harris
Fred Polgardy
Jim Breen
Kevin Taylor
Todd Webb
Turner King
Tyler Jennings

Archives

March 2009 (1)
January 2009 (1)
December 2008 (1)
October 2008 (3)
September 2008 (1)
June 2008 (4)
April 2008 (3)
March 2008 (1)
February 2008 (1)
August 2007 (1)
July 2007 (1)
June 2007 (1)
May 2007 (4)
April 2007 (3)
March 2007 (5)
February 2007 (6)
January 2007 (6)
December 2006 (10)
November 2006 (5)
October 2006 (8)
September 2006 (8)
August 2006 (5)
July 2006 (12)
June 2006 (7)
May 2006 (5)
April 2006 (5)
March 2006 (4)
February 2006 (2)
January 2006 (5)
December 2005 (5)
November 2005 (3)
October 2005 (3)
September 2005 (6)
August 2005 (4)
July 2005 (7)
June 2005 (14)
May 2005 (6)
April 2005 (8)
March 2005 (9)
February 2005 (11)
January 2005 (16)
Old Archives

 

Fri, 14 Jan 2005

Quotes Manager Project -- Day 1

An ongoing pet project blog...

OK, that wasn't very fun. I grabbed the XSLT specfication and wrestled my style sheet into shape. I've got it looking exactly the way my quotes page does now. Hoo-boy. Here's the stylesheet:

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:variable name="book_url" select="'http://amazon.com/ISBN'"/>

<xsl:template match="/">
	<html>
	<body>
		<xsl:for-each select="quotes/quote">
			<p>
			<xsl:value-of select="text"/>

			<xsl:text>--</xsl:text>

			<xsl:if test="quoter">
				<a href="{quoter/@url}"><xsl:value-of select="quoter"/></a> in
			</xsl:if>

			<xsl:choose>
				<xsl:when test="source/@isbn">
					<a href="{$book_url}/{source/@isbn}">
					<i><xsl:value-of select="source"/></i>
					</a>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="source"/>
				</xsl:otherwise>
			</xsl:choose>

			<xsl:text>, </xsl:text>

			<xsl:if test="source/@page">
				p. <xsl:value-of select="source/@page"/>,
			</xsl:if>

			<xsl:for-each select="author">
				<xsl:choose>
					<xsl:when test="@url">
						<a href="{@url}"><xsl:apply-templates/></a>
					</xsl:when>
					<xsl:otherwise>
						<xsl:apply-templates/>
					</xsl:otherwise>
				</xsl:choose>
				<xsl:choose>
					<xsl:when test="position()=last()-1"> and </xsl:when>
					<xsl:when test="not(position()=last())">, </xsl:when>
				</xsl:choose>
			</xsl:for-each>
			</p>
		</xsl:for-each>
	</body>
	</html>
</xsl:template>

</xsl:stylesheet>
Here is the data I'm using. It has most of the quote variations that I can remember:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="sample.xsl"?>

<quotes>
	<quote>
		<text>
			We in the software industry are working with a more or less invisible product,
			yet this very invisibility only heightens our need for feedback.
		</text>
		<quoter url="http://www.geraldmweinberg.com/">Gerald Weinberg</quoter>
		<source isbn="0932633447" page="xiii">Project Retrospectives</source>
		<author url="http://www.retrospectives.com/">Norm Kerth</author>
	</quote>
	<quote>
		<text>
			By building tests <i>before</i> you implement the code,
			you get to try out the interface before you commit to it.
		</text>
		<source isbn="020161622X" page="192">The Pragmatic Programmer</source>
		<author url="http://www.pragmaticprogrammer.com/">Dave Thomas</author>
		<author url="http://www.pragmaticprogrammer.com/">Andy Hunt</author>
	</quote>
	<quote>
		<text>
			For everyone who exalts himself will be humbled,
			and he who humbles himself will be exalted.
		</text>
		<source>Luke 18:14b</source>
	</quote>
	<quote>
		<text>
			Programming is the Great Game.
			It consumes you, body and soul.
			When you're caught up in it, nothing else matters.
		</text>
		<source url="http://www.apocalypse.org/pub/u/kjc/cool/Card.on.Software.html">How Software Companies Die</source>
		<author url="http://www.hatrack.com/osc/about.shtml">Orson Scott Card</author>
	</quote>
	<quote>
		<text>
			It's never the size of the step that a person takes that counts,
			but its direction.
		</text>
		<source isbn="0393700984">Narrative Means to Therapeutic Ends</source>
		<author>Michael White</author>
		<author>David Epston</author>
	</quote>
	<quote>
		<text>
			Having a plan isn't everything, but planning is.
		</text>
		<source isbn="0201708426">Extreme Programming Installed</source>
		<author url="http://www.xprogramming.com/">Ron Jeffries</author>
		<author url="http://c2.com/cgi/wiki?AnnAnderson">Ann Anderson</author>
		<author url="http://c2.com/cgi/wiki?ChetHendrickson">Chet Hendrickson</author>
	</quote>
</quotes>
Time for a check in. OK, so what's next? Let's take a look at the original plan...
  • I want to change the quotes page to be sorted by quoter. There should be a list of all the quoters at the top. Their names should link down to their quotes on the page below.
  • I want to use a simple Swing application to manage the quotes.
  • The Swing app will store the quotes in XML. The app will have the ability to transform this XML into HTML via XSLT.
I could start setting up the Swing app, which would be a nice change of pace. Or I could learn how to do XSL tranformations in Java. Or I could add sorting and a quoter list to the style sheet. Hmm, if I were a customer, which would I think was more important? For now, I think I'm satisfied with seeing what I already have on the quotes page. But I'm uncomfortable with having Firefox do the transformations. I want to see the HTML generated by Java. Looks like we're pulling into the station...

Reading about JAXP Transformation Packages... I learned that Java 5.0 ships with JAXP 1.3, so I don't need to download anything.

OK, that was a straightforward spike.

import org.w3c.dom.Document;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.File;


public class TransformSample {
    public static void main(String[] args) throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse("sample.xml");

        StreamSource stylesource = new StreamSource(new File("sample.xsl"));
        Transformer transformer = TransformerFactory.newInstance().newTransformer(stylesource);

        Source source = new DOMSource(document);
        Result result = new StreamResult(System.out);
        transformer.transform(source, result);
    }
}

[/projects/quotes] permanent link

Quote Manager Project -- Day O

The beginning of a pet project blog...

I love to read. As I read, I am constantly underlining, writing in the margin and transcribing quotes onto scratch paper. When I finish a book, I read through the quotes I've collected. I choose my favorites and transcribe them into my quotes page. I've been doing this since I started learning how to develop software. Actually, I started the habit before I was a programmer, but the web page thing didn't start until after I learned HTML.

My quotes page has grown pretty hefty, and I would like to develop some software to help me manage it.

Last year I read Ron Jeffries' Extreme Programming Adventures in C#. I liked Ron's idea of keeping a public journal as he developed software. I am going to do something similar.

"...the best way to create is by imitation, refinement and combination." --Object-Oriented Software Construction, Second Edition, Bertrand Meyer
Ron's "adventures" were learning a new language (C#) while developing an XML editor. I'm already learning a new language right now (C++), so I think I'll stick with one that I'm familiar with (Java). But I will try out a technology (XSL) that's new to me, though it's not new to many.

Here's the plan:

  • I want to change the quotes page to be sorted by quoter. There should be a list of all the quoters at the top. Their names should link down to their quotes on the page below.
  • I want to use a simple Swing application to manage the quotes.
  • The Swing app will store the quotes in XML. The app will have the ability to transform this XML into HTML via XSLT.
The only time I will work on this project is on the train, going to and from Chicago on my way to work. It's about a 40 minute ride each way.

So where to start, where to start... What aspect of the project do I know the least about? XSL. I'll start there. I'll start with creating a sample XML file. No, wait. Source control first. I just imported what I've written so far into Subversion. OK, now I'm ready to create my XML sample.

<quotes>
	<quote>
		<text>
			We in the software industry are working with a more or less invisible product,
			yet this very invisibility only heightens our need for feedback.
		</text>
		<quoter url="http://www.geraldmweinberg.com/">Gerald Weinberg</quoter>
		<source isbn="0932633447" page="xiii">Project Retrospectives</source>
		<author url="http://www.retrospectives.com/">Norm Kerth</author>
	</quote>
</quotes>
OK, checked that in.

What's next? XSL-ify the XML into something resembling the way I want my quotes page to look. I printed out an XSL tutorial from W3Schools, which should be helpful. Looks like the first thing I need to do is define an XSL style sheet...

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
	<html>
	<body>
		<xsl:for-each select="quotes/quote">
			<p><xsl:value-of select="text"/></p>
		</xsl:for-each>
	</body>
	</html>
</xsl:template>

</xsl:stylesheet>
I'll start with that. I need to tell my XML document that it should be rendered with my style sheet. Looks like adding these lines to the top of my XML file should do it...
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="sample.xsl"?>
Let's see what that looks like in Firefox. Looks pretty boring, but just what I expected. Jerry's quote.

I'm going to add another quote to make sure that <xsl:for-each> is working right. Yep, looks good. Time for a check in.

I'm going to keep building my style sheet until it looks close enough for now.

Stumbling forward, wishing I had web access right now. Looks like my battery is going to run out. I guess this stuff will need to wait until tomorrow...

[/projects/quotes] permanent link


powered by blosxom