Tuesday, March 13, 2007

ClassLoader and the order of classpath entries

JBossAOP provides runtime aspectization feature. For this it has a classloader instrumentor which modifies the java.lang.ClassLoader which will intercept the call and invoke the interceptor chain.

Based on their docs
java -Xbootclasspath/p:<aop boot classpath as described> \
-Djboss.aop.path=<path to jboss-aop.xml> \
-classpath <path to your classes> com.blah.MyMainClass
The above command is used to execute a java class with the instrumented classloader. When i used this command, i had the bootclasspath as
(through eclipse) JRE Classpath;{generated ClassLoader};{JBossAOP jars};
It dint work as expected, that is the interceptors weren't invoked.

Then i remembered of ClassLoaders and the way it loads the classes. That is it searches the given paths, one by one and loads the class. Then i changed the order of the entries as {generated ClassLoader};{JBossAOP jars};JRE Classpath;. Now everything worked fine and the result was as expected.

The important point to remember is that, if you have duplicate classes like the one used above, java.lang.ClassLoader, the order of the desired class (instrumented ClassLoader) in the classpath should be at the top so that it will be loaded before other duplicate gets loaded.

Lesson learnt: order of the classpath entries is very important

Sunday, February 11, 2007

textEditorsInTheWorld++

I have been search, playing with and reviewing a number of text editors and now i have planned to roll out my own editor called MyMate soon.

This will be a project where i will learn advance C++ and I'm gonna enjoy using JUCE. The project is an inspiration from the wonderful editor TextMate.

This is an initial screen of MyMate(just started to work on it)


I'm not a very good C++ programmer, so i would like someone to guide me and help me in this project.
Anybody interested to help me, just add comment and we'll start a project with a repository in SourceForge.

Saturday, January 27, 2007

My dark colour scheme for VIM.

This is my dark colour scheme for VIM, which is based on rdark colour scheme. I have done some colour changes to

  1. highlight current line,
  2. type,
  3. special words
  4. and keywords.

Download colour scheme

Happy Vimming!

Sunday, January 14, 2007

Slippery Snippets for VIM. HOT!

Slippery Snippets brings the HOT HOT feature of TextMate to VIM. Define you snippet in snippets file and use the snippets with a single <TAB> key. And it also allows you to navigate to the fill up areas in the snippet using the same <TAB> key.

Just download the VIMBALL file and load it in VIM. Then use the sourcing command to extract it.

:so %

The content will be extracted to appropriate locations in the VIM's directory. Now, open a C++ source file and type "do"; then hit the
<TAB> key.

{ Note: Snippet files can be found at "vim/vimfiles/after/ftplugin". Refer the snippets files for creating your own snippets.}



Enjoy the Snippets!

Saturday, January 13, 2007

CrimsonEditor's opensourced and EmeraldEditor born

CrimsonEditor is a very clean, simple and lightweight editor for windows. And for a very long time there was no development. But now a team has come up with an idea to build cross platform editor using wxWindows and they call it EmeraldEditor.

I don't think i would be as light as Crimson, because WxWidgets is a huge and heavy library and makes the application a bit bloated.

Lets wait and check it out.

Thursday, January 11, 2007

TextMate like editor for Windows


TextMate has created a big impression in the minds of programmers, especially ROR developers. It seems that TextMate provides a very elegant look plus a number of features that makes development faster and cleaner.

Intype is a new programmers editor coming up with similar features for windows. The team has released an Alpha version of the editor.

It looks good, but some of the basic functionalities are not working for e.g undo is not implemented.

Saturday, December 16, 2006

Sorting XML in Java

Recently I had a requirement to sort an XML document based on the tag names in the document.
You can sort it using XSLT, but this post tells you how to sort the XML nodes through Java.

Lets extend the com.sun.org.apache.xerces.internal.util.DOMUtil or org.apache.xerces.internal.util.DOMUtil class which has some basic utility methods. And I'm going to extend it by adding a method called sortChildNodes() .

This method sorts the children of the given node in descending or ascending order with the given Comparator. And it recurses upto the specified depth if available.



1 package com.googlepages.aanand.dom;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Comparator;
6 import java.util.Iterator;
7 import java.util.List;
8
9 import org.w3c.dom.Node;
10 import org.w3c.dom.NodeList;
11 import org.w3c.dom.Text;
12
13 import com.sun.org.apache.xerces.internal.util.DOMUtil;
14
15 public class DOMUtilExt extends DOMUtil {
16
17 /**
18 * Sorts the children of the given node upto the specified depth if
19 * available
20 *
21 * @param node -
22 * node whose children will be sorted
23 * @param descending -
24 * true for sorting in descending order
25 * @param depth -
26 * depth upto which to sort in DOM
27 * @param comparator -
28 * comparator used to sort, if null a default NodeName
29 * comparator is used.
30 */
31 public static void sortChildNodes(Node node, boolean descending,
32 int depth,Comparator comparator) {
33
34 List nodes = new ArrayList();
35 NodeList childNodeList = node.getChildNodes();
36 if (depth > 0 && childNodeList.getLength() > 0) {
37 for (int i = 0; i < childNodeList.getLength(); i++) {
38 Node tNode = childNodeList.item(i);
39 sortChildNodes(tNode, descending, depth - 1,
40 comparator);
// Remove empty text nodes
41 if ((!(tNode instanceof Text))
42 || (tNode instanceof Text && ((Text) tNode)
43 .getTextContent().trim().length() > 1))
44 {
nodes.add(tNode);
45 }
46 }
47 Comparator comp = (comparator != null) ? comparator
48 : new DefaultNodeNameComparator();
49 if (descending)
50 {
51 //if descending is true, get the reverse ordered comparator
52 Collections.sort(nodes, Collections.reverseOrder(comp));
53 } else {
54 Collections.sort(nodes, comp);
55 }
56
57 for (Iterator iter = nodes.iterator(); iter.hasNext();) {
58 Node element = (Node) iter.next();
59 node.appendChild(element);
60 }
61 }
62
63 }
64
65 }
66
67 class DefaultNodeNameComparator implements Comparator {
68
69 public int compare(Object arg0, Object arg1) {
70 return ((Node) arg0).getNodeName().compareTo(
71 ((Node) arg1).getNodeName());
72 }
73
74 }


And I'm also removing the empty text nodes. If descending is set true, then a reverse ordering comparator is obtained from the Collections utility class.

The utility uses a default NodeName comparator if a comparator is not specified. Its sorts based on the name of the nodes in the DOM.

Writing a Comparator implementation is very simple, for example you may want to sort a document based on an attribute in the XML document.



class MyComparator3 implements Comparator {

public int compare(Object arg0, Object arg1) {

if (arg0 instanceof Element && arg1 instanceof Element) {
return ((Element) arg0).getAttribute("id").compareTo(
((Element) arg1).getAttribute("id"));
} else {
return ((Node) arg0).getNodeName().compareTo(
((Node) arg1).getNodeName());
}

}

}



Its a very simple class to sort the nodes in any way you want. Please comment on it, if you point out a problem with the utility.