Nov
29
2005
The first thing a programmer learns about exceptions is how to ignore them. Then a programmer learns that it is always best to throw them up the stack to the caller. And if you are the caller you can always print the stack trace to get a glimpse of what line of code is possibly at fault. The Throwable class is the base class for all exceptions, and errors for that matter. If you ever had to do anything interesting with Java you must had caught or thrown an exception.
The two methods from the Throwable class that most developers are familiar with are the printStackTrace and getMessage. The information that is printed out when you call printStackTrace is also available to you with the getStrackTrace method. The getStrackTrace method returns an array of StackTraceElement objects. The StackTraceElement class has getter methods for the class name, method name, file name, and line number to access the information of the call stack trace.
With the information made available to you by the Throwable class you see the caller hierarchy. You can trace the callers all the way back to the main method of the class that launched an application. You have access to this information by just creating a Throwable object, no need to throw it.
Throwable t = new Throwable();
StackTraceElement[] stack = t.getStackTrace();
System.out.println(stack[0].getLineNumber());
With this information you can allow or disallow certain call hierarchy or certain classes from calling a method a specific method. You can print the line number and class name in a debugging statement, just what Log4j does. The Throwable class provides access to metadata of your class environment at runtime. A note of warning, creating a Throwable seems like a very expensive call so make sure you know what you are doing.
no comments | posted in Java, TechKnow
Nov
25
2005
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.
no comments | posted in DotNET, TechKnow, Visual Basic
Nov
25
2005
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.
no comments | posted in HTML/XML, Rant, TechKnow
Nov
24
2005
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 <. With the disable-output-escaping attribute set to yes, the contents from the CDATA will not be escaped and treated as is.
2 comments | posted in HTML/XML, TechKnow
Nov
23
2005
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.
no comments | posted in IDE, Java, TechKnow
Nov
21
2005
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>
2 comments | posted in HTML/XML, Java, TechKnow