Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Extending the NetBeans IDE Spring Support

DZone's Guide to

Extending the NetBeans IDE Spring Support

· Java Zone ·
Free Resource

Java-based (JDBC) data connectivity to SaaS, NoSQL, and Big Data. Download Now.

The cool thing about the new Spring support in NetBeans IDE 6.1 is the fact that you can extend it. A very simple entry point into the Spring support is its dedicated MIME type, x-springconfig+xml. Via this MIME type, you can add a new menu item, either to the editor or to the node in the explorer view, specifically for Spring configuration files. Using this technique, I added a menu item called "Generate Java from Beans":

What happens when the menu item is selected? You get some Java code generated at the bottom of the file, for accessing each bean, which you can then copy into your Java classes, wherever the code is needed. Maybe it would be nice if this were integrated into the Java classes, so that the beans would appear during code completion, but that's a couple of bridges too far for me at the moment, I'd need to do a bit of research for that. Currently I'm only supporting the util:list bean, but it would be simple to expand this to all other types. Here's the result for the above two beans, i.e., everything that is commented out below is generated when the menu item above is selected:

And here's the relevant performAction in my CookieAction. Take particular note of org.openide.xml.XMLUtil which, if you're not aware of it, is very useful indeed (even more so if you're aware of it):

protected void performAction(Node[] activatedNodes) {

//Figure out the name of the configuration file,
//plus its packages, plus "src",
//which is what the org.springframework.core.io.FileSystemResource class needs:
DataObject dobj = activatedNodes[0].getLookup().lookup(DataObject.class);
FileObject fo = dobj.getPrimaryFile();
setFileName(fo.getPath().substring(fo.getPath().indexOf("src")));

try {
//Figure out the document and parse it:
EditorCookie editorCookie = activatedNodes[0].getLookup().lookup(EditorCookie.class);
StyledDocument styledDoc = editorCookie.openDocument();
String allText = styledDoc.getText(0, styledDoc.getLength());
InputSource source = new InputSource(new StringReader(allText));
//No validation, not namespace aware, no entity resolver, no error handler:
Document doc = XMLUtil.parse(source, false, false, null, null);

//Figure out the list of elements:
NodeList list = doc.getElementsByTagName("*");
int docLength = list.getLength();
for (int i = 0; i < docLength; i++) {
org.w3c.dom.Node node = list.item(i);

//Figure out the list of attributes:
NamedNodeMap map = node.getAttributes();
int mapLength = map.getLength();
for (int j = 0; j < mapLength; j++) {
org.w3c.dom.Node attr = map.item(j);

//Insert the template, with values filled in,
//if the attribute is "id":
if (attr.getNodeName().equals("id")) {
setBeanId(attr.getNodeValue());
styledDoc.insertString(styledDoc.getLength(),
"\n<!-- How to access the \"" + attr.getNodeValue() +
"\" bean:\n" + getTemplate(), null);
}

}
}
} catch (SAXException ex) {
Exceptions.printStackTrace(ex);
} catch (BadLocationException ex) {
Exceptions.printStackTrace(ex);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}

}

And, FWIW, here's the template:

private String getTemplate() {
template =
"\nBeanFactory factory = new XmlBeanFactory(new FileSystemResource(\"" + getFileName() + "\"));" +
"\nArrayList list = (ArrayList) factory.getBean(\""+getBeanId()+"\");" +
"\nIterator it = list.iterator();" +
"\nint count = 0;" +
"\nwhile (it.hasNext()) {" +
"\n count = count + 1;" +
"\n System.out.println(\""+getBeanId()+" \" + count + \": \" + it.next().toString());" +
"\n}" +
"\n-->\n";
return template;
}

 

Connect any Java based application to your SaaS data.  Over 100+ Java-based data source connectors.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}