Sunday, April 22, 2007

Vista broke my website...

... so it appears. One of our websites at work doesn't appear to work with this new fangled Windows Vista. Lately we've been getting complains of the site not working properly, customers would log in, click through a page or two and then boom, back to the login page. From where I sit, the site is acting line the Session is being invalidated. It appears that everyone experiencing the problem is running Vista and IE7. I've tested it on IE6, IE7 and Firefox on XP and the site works just fine, so last variable appears to be Vista. At the moment, we do not have a Vista box (we've also been told not to install IE7 yet), but there is one on the way. Hopefully there's a setting somewhere in their new security "features" that will get the site working again.

Sunday, April 8, 2007

That Tinkering Feeling

A friend of mine recently bought a new Mac desktop machine, his first such venture into the arena. I got to play around with it over the weekend and came away impressed. The neatest thing I got to see was the Parallels Desktop, a virtualization solution in which he setup Windows XP and was working on getting Ubuntu up and going.

While I don't see myself buying a Mac anytime soon, it did get me thinking about my little home network. I am strongly thinking about creating a new domain/file server based on Ubuntu and Samba, but first I'm going to need more hard drive space. And then, of course, I'll have to follow through. Updates to come... hopefully...

Wednesday, April 4, 2007

XSL Transformers: More Than Meets The Eye

I don't know how your workplace functions but mine has one glaring flaw, a lack of centralization. Take a look around and you can see a few places in which common tasks are being preformed across multiple teams. With the hope of starting a trend, work presented us with a fun little problem to solve. How can we centralize our outgoing e-mail messages? We have a handful of websites, some in .NET others in Java, each responsible for composing and sending their own e-mails (mostly confirmations and reminders).

To meet the centralization requirement we decided on setting up a web service that was then exposed to all development teams. Ok, one problem solved, but we still had to figure out how compose our data-centric e-mails and then send them out the door. Enter XSLT, in a nutshell what XSLT allows us to do is transform an XML document into some other format, in our case HTML and/or text. This will allow our web service to take an incoming XML document and transform it into an HTML page. In order to do these transformations programatically we will use the Xalan XSLT processor. Before we get started with the Java code, we will also need some XML and an XSLT template.

First the XML:


emailMessage.xml
----------------
<EmailMessage>
<Customer>
<customerFirstName>Joe</customerFirstName>
<customerLastName>Customer</customerLastName>
<customerEmail>joe.customer@somewhere.com</customerEmail>
</Customer>
<Service>
<confirmationNumber>12345-67</confirmationNumber>
<price>59.99</price>
<date>2007-04-04</date>
</Service>
</EmailMessage>


Now the XSLT:

emailMessage_html.xslt
----------------------
<?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="/EmailMessage">
<html>
<head>
<title>Email Message Sample</title>
</head>
<body>
<div>
<p>Thank you for your order. The information we have on file is below:</p>
<p>
<b>Customer Information</b><br />
First Name: <xsl:value-of select="Customer/customerFirstName" /><br />
Last Name: <xsl:value-of select="Customer/customerLastName" /><br />
E-mail: <xsl:value-of select="Customer/customerEmail" /><br />
</p>

<p>
<b>Service Information</b><br />
Confirmation Number: <xsl:value-of select="Service/confirmationNumber" /><br />
Price: <xsl:value-of select="Service/price" /><br />
Date: <xsl:value-of select="Service/date" /><br />
</p>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

And finally the Xalan transformer class:

TemplateTransformer.java
------------------------
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;

public class TemplateTransformer {
// constructors
public TemplateTransformer() {}

// methods
public String transform(String xml, String xsltStyleSheetPath) {
String text = "";

TransformerFactory tFactory = TransformerFactory.newInstance();

// Define the source XSL
StreamSource source = new StreamSource(new File(xsltStyleSheetPath));
// Define the source XML
StreamSource sourceXML = new StreamSource(new StringReader(xml));
// Define the result destination
StreamResult result = new StreamResult(new StringWriter());

try {
// Tranform the XML using the XSL
Transformer transformer = tFactory.newTransformer(source);
transformer.transform(sourceXML, result);

StringWriter writer = (StringWriter)result.getWriter();
text = writer.toString();
}
catch (TransformerException te) {
return ""; // yeah, I'm being lazy
}

return text;
}
}

And you're done, it's essentially four lines of code. Specify the source XSL, the source XML and the destination. Then call the transform method, simple as that.

Now some testing code. The following is a simple JSP page that can be used to see the results of the transformation, for simplicity sake I'm assuming the XSLT and XML files are on the local hard drive.

<%@ page language = "java"
import = "java.text.*, java.util.*, java.io.*"
%>

<%
String xmlPath = "c:\\temp\\xslt\\emailMessage.xml";
String xslPath = "c:\\temp\\xslt\\emailMessage_html.xslt";

TemplateTransformer t = new TemplateTransformer();
String result = t.transform(xmlPath, xslPath);
%>

<html>
<head>
<title>XSLT/Xalan Test Page</title>
</head>
<body>
<b>XSL Path:</b> <%= xslPath %><br />
<b>XML Path:</b> <%= xmlPath %><br />
<br />

<b>Result:</b><br />
<pre>
<%= result %>
</pre>
</body>
</html>

Well, hopefully all of that worked and made a little bit of sense.

Tuesday, April 3, 2007

Beginnings

So... I suppose I should say a few words. This is my first go with this type of thing so bear with me through the rough bits. I haven't totally decided on the overall direction of this blog, but I think to start I am going to use it to post topics related to software development. Chances are there will be some other random things thrown in there from time to time, so hopefully I won't bore everybody to tears. Worst case, this will turn into an archive of code snippets at which point I will bore you all.

Now, can I keep this place from getting stagnating? We shall see.