Nov 25 2005

Visual Kill -9

Here is some Visual Basic script code which allows you to terminate a process given a process id number.

' Kills a program given its process id.
Function ProgKill(strProcessId)
   ' Declare used variables
   Dim strWQL
   Dim objProcess
   Dim objResult
   Dim intReturnCode
   Dim wmi

   Set wmi = GetObject("winmgmts:")
   ' Get Process by WMI
   strWQL = _
      "select * from win32_process where ProcessId='" _
      & strProcessId & "'"
   Set objResult = wmi.ExecQuery(strWQL)

   ' Kill all found process
   For Each objProcess in objResult
      ' Try to kill the process
      intReturnCode = objProcess.Terminate(0)
   Next
End Function

You can use code like this to kill a process started in your script after a given event or set time.


Nov 25 2005

WordprocessingML

I remember who excited I was when I found out that I could save a word document to HTML. Now after two years using Word 2003, I learn that Word 2003 can save a Word document as XML via Microsoft’s WordprocesingML. What this implies, but I haven’t test, is that if you can write xml files, you can write word documents. The only issue I fear is that I will most likely have to write XML the Microsoft way, which is usually the non-standard way!

Having a Word document in XML format, you can transform it into another format using XSLT or XSL-FO. This opens a lot more avenues with Word. It is amassing how much XML Word will generate, a ‘hello world’ file will write out 4 kb.


Nov 24 2005

CDATA With XSLT

In an XML file you can use CDATA to tell the parser that anything inside the CDATA element is your text data and not XML formatting. In a sense, you can use CDATA to escape data that looks like XML. See Escape From XML. For example, you can escape the ampersand like this:

&

or by using CDATA as in:

<![CDATA[ & ]]>

I often work with XML and XSLT so when I want to treat a block of HTML as data I use the CDATA element in my XML file that will later be transformed with an XSL. For example, in my XML I might have something like this:

<![CDATA[
   <b>this is bold</b>
]]>

In my XSL file, I need to disable the data from being escaped by using the disable-output-escaping of the xsl:value-of tag as in:

<xsl:value-of
   select="."
   disable-output-escaping="yes"/>

If you forget the disable-output-escaping attribute the contents of the CDATA will be escaped, for example < will be transformed to &lt;. With the disable-output-escaping attribute set to yes, the contents from the CDATA will not be escaped and treated as is.


Nov 23 2005

Eclipse Tool Tip #5: Templates

Continuing with the Eclipse Tool Tip Series try this out. Inside a method type ‘ins’ and then hit the ctrl+space keys. You should see the instanceof template in the code assist popup box. Select it and you get the following bit of code generated for you:

if (name instanceof type)  {
    type new_name = (type) name;
}

The sample code above does not do the Eclipse template functionality any service because I can’t show you how if you update the type in the if condition it will update it in the cast and declaration for you. You have to try it out yourself or take me word for it.

By default you will have templates for try/catch, for/in, private static method, several while loops, etc. And of course you can add your custom templates. For example, I created a try catch template that automatically has our applications exception already filled. So when I type ‘try’ and hit the ctrl+space keys my try/catch template generates code like this:

try {
    // TODO: Fill in exception block
}catch(MyApplicationException ex) {
    ex.printStackTrace();
    logger.error(ex.getMessage());
    NotificationService.error(ex.getMessage());
}

If you want to add your own code template you need to open up the preferences (Window > Preferences) and click on Java > Editor > Templates to add a new template. Yes, I know, some where there is some ol’ timer muttering under his breath “Emacs had templates back in ’72.” Hey, I wasn’t there then so I wouldn’t know but templates are a common functionality in NetBeans, IntelliJ, and since Microsoft ‘tailgates’ other technologies I am sure Visual Studio has it as well.


Nov 21 2005

Using Dom4J: Writing An XML

In a previous post, see Using Dom4J: Reading An XML, I went over how to read in an XML document using Dom4J. Here I’ll show how to write an XML document using Dom4J. The first part to writing out an XML document is to have an in memory representation of it. Here is how you construct and XML document.

Document doc = DocumentFactory.getInstance()
	.createDocument();
Element root = doc.addElement("myroot");
root.addAttribute("myattr", "myval");
Element elem = root.addElement("myelem");
elem.addText("my text");

In this code you see how to create elements with child elements, how to add attributes and text data to elements. Once we have an in memory representation of the XML file, we can serialize it to the file system like this:

FileOutputStream fos = new FileOutputStream("simple.xml");
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(fos, format);
writer.write(doc);
writer.flush();

All this code will produce the following XML document:

<?xml version="1.0" encoding="UTF-8"?>

<myroot myattr="myval">
<myelem>my text</myelem>
</myroot>

Nov 18 2005

Two Write A File

I learned how to print text in Java using System.out, so it was natural for me to use the PrintStream to write text out to a file. Here is how I used to write text to a file:

FileOutputStream fos = new FileOutputStream(filePathStr);
PrintStream ps = new PrintStream(fos);
ps.println("Hello, World!");
ps.close();

To write some quick debugging message to a file I would sometimes use the FileWriter as in the following example:

FileWriter fw = new FileWriter(filePathStr);
fw.write("Hello, World!");
fw.close();

Now with Java 1.5 I am exclusively using the PrintWriter. Unlike FileWriter, Printwriter defines and overloads all my favorite println methods that I am so used to by from the years of using the System.out PrintStream.

PrintWriter pw = new PrintWriter(filePathStr);
pw.println("Hello, World!");
pw.close();