Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Extending Eclipse with Helpful Views : Page 3

Knowing how to extend your IDE is a useful tool to have in your belt, and thanks to a great plugin architecture, it's easy in Eclipse. In this article you'll learn how to create two Views that extend the Eclipse IDE: one that evaluates a regular expression and another that does the same for XPath.


advertisement
The XPath View
With the RegEx view complete, I can now move on to the XPath view. The UI for the XPath view is exactly the same as the RegEx view, so really the only thing that needs to change is the widgetSelected() method. As such, the proper thing to do would be to create an abstract base class that both the RegEx and XPath views would extend. Whether you decide to do that or simply copy the RegEx view, the new widgetSelected() method for the XPath view is below:

public void widgetSelected(SelectionEvent e) { try { DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance(); DocumentBuilder parser = fact.newDocumentBuilder(); Document doc = parser.parse(new StringBufferInputStream(search.getText())); doc.normalize(); XObject obj = XPathAPI.eval(doc, exp.getText()); if(obj.getType() == XObject.CLASS_NODESET) { NodeList nodes = obj.nodelist(); StringBuffer buf = new StringBuffer(); for(int itr = 0; itr < nodes.getLength(); itr++) buf.append("[").append(nodes.item(itr).toString()).append("]"); result.setText("Result: " + buf.toString()); } else result.setText("Result: " + obj.str()); result.pack(); } catch(Exception ex) { ex.printStackTrace(); } }

As you can see from the code, evaluating an XPath is a little more complicated. First, the XML contained in the search text widget must be parsed into a Document instance. With that done, the eval() method of XPathAPI is called with the parsed Document and the expression contained in the expression text widget. The result of this can vary depending on the XPath. The code above looks for two types of results. If the result is a NodeSet then I loop over the nodes and place them inside brackets. Otherwise, the assumption is that the result can be handled as a string.

Before being able to test the newly created XPath view, you will need to declare it in the plugin manifest file. Below is the XML snippet I used:

<view class="expressionViews.XPathView" category="article.views" name="XPath View" id="article.views.xpath"/>

I hope this article made evident just how easy it is to extend Eclipse with new views. While useful, these views are far from complete. Simple, expected functionality such as copy and paste aren't implemented nor is the ability to evaluate expressions against open files. The Eclipse API is rich and has solutions for these problems and a host of others.



Matt Liotta started his development career at the age of 12 by building C applications for faculty at Emory University. Since then he worked with an early ASP, Cignify, to build their transaction processing system for payroll time data.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date