In the WTP – XML Search [step1], I have introduced WTP/XML Search. I would like show you how it’s easy to manage completion, text hover, validation, hyperlink for your own XML files descriptor with WTP/XML Search. I decided to create a little tutorial which will explain you step by step how manage completion, text hover, validation, hyperlink for the JEE web.xml XML file descriptor with WTP/XML Search. web.xml is interesting because it uses 3 types of XML references :
- [1] reference to Java class : servlet-class, filter-class, listener-class Text node reference to Java class.
- [2] reference to XML node : servlet-mapping/servlet-name, filter-mapping/filter-name Text node reference to servlet/servlet-name, filter/filter-name Text node.
- [3] reference to resource file : welcome-file Text node reference to HTML, JSP…resources files.
Here a scheme which show you this 3 types of references with servlet, servlet-mapping and welcome-file XML nodes :
This article show you screenshot about completion, text hover, hyperlink and validation for web.xml. In next article [step3], I will explain how manage that with WTP/XML Search.
File examples
In this article I will use web.xml and MyServlet class.
web.xml
Here the content of the web.xml :
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>MyWebProject</display-name> <servlet> <description>It's my servlet.</description> <display-name>My Servlet</display-name> <servlet-name>MyServlet</servlet-name> <servlet-class>org.example.wtpxmlsearch.MyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/MyServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
MyServlet
Here the content of the org.example.wtpxmlsearch.MyServlet :
package org.example.wtpxmlsearch; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class MyServlet. This servlet manages : * * <ul> * <li>bla bla bla 1</li> * <li>bla bla bla 2</li> * <li>bla bla bla 3</li> * </ul> * * * @author Angelo ZERR * @version 1.0.0 */ public class MyServlet extends HttpServlet { private static final long serialVersionUID = 1L; public MyServlet() { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
We will see that Java doc from this class will be used for Java completion and Java Text Hover.
XML reference – Syntax Coloring
WTP/XML Search can manage Syntax Coloring for the XML reference declared for your XML file descriptor. When you will open web.xml you will notice, that servlet-class, servlet-mapping/servlet-name, welcome-file Text node… will appears with bold styles :
This syntax coloring can be customized with Preferences (Window Preferences -> XML/XMl Search->Editor->Syntax Coloring) :
XML Reference to Java class
Servlet, Filter, Listener can be declared in the web.xml with servlet-class, filter-class, listener-class text node which reference a Java class. In [step3] I will explain how manage that.
Completion for Java
Completion for Java class is managed : after typing a start of name of your Java class, you can do Ctrl+Space and completion opens :
Text hover for Java
Text hover for Java class is managed : when you hover with your mouse the declared Java class, a popup appears and display Java doc of the class :
Hyperlink for Java
Hyperlink for Java class is managed : you can do Ctrl+Click on the declared Java class to open the Java class Editor and select the Java class. When you do Ctrl on the declared Java class, the link becomes activated :
When you do Click, it opens the Java editor :
Validation for Java
Validation for Java class is managed: if you declare in the servlet-class, a NONE existing Java class, you will see error :
XML Reference to XML node
Servlet, Filter mapping, XML Text node reference to Servlet, Filter declaration XML Text node (ex : servlet-mapping/servlet-name/text() > servlet/servlet-name/text()). In [step3] I will explain how manage that.
Completion for XML
Completion for XML node is managed : after typing a start of name of your servlet you wish declare a mapping (servlet-mapping/servlet-name/text() ) , you can do Ctrl+Space and completion opens with the whole declared servlet ( servlet/servlet-name/text()) :
You can notice completion display custom icon and information about the declared servlet.
Text Hover for XML
Text hover for XML node is managed : when you hover with your mouse the referenced servlet in the servlet-mapping popup appears and display information about servlet declared:
Hyperlink for XML
Hyperlink for XML nodes is managed : you can do Ctrl+Click on the declared XML nodes to open the XML Editor and select the referenced servlet. When you do Ctrl on the referenced XML node, the link becomes activated :
When you do Click, it opens the XML editor and select the node :
Validation for XML
Validation for XML nodes is managed: if you declare in the servlet/mapping/servlet-name an NONE existing servlet, you will see error :
XML Reference to resource (file)
Welcome files (HTML, JSP….) can be declared in the web.xml with welcome-file Text node which reference HTML, JSP…file. In [step3] I will explain how manage that.
Completion for resource (file)
Completion for resource (file, folder…) is managed : after typing a start of name of your HTML, JSP…file, you can do Ctrl+Space and completion opens :
Text Hover for resource (file)
Text hover for resource (file, folder…) is managed : when you hover with your mouse the declared resource, a popup appears and display Java doc of the resource:
Hyperlink for resource (file)
Hyperlink for resource (file, folder…) is managed : you can do Ctrl+Click on the declared resource to open the Resource Editor. When you do Ctrl on the declared resource, the link becomes activated :
When you do Click, it opens the resource editor (Text, HTML….) :
Validation for resource (file)
Validation for resource (file, folder…) is managed: if you declare in the welcome-file an NONE existing file resource, you will see error :
Ctrl+Shift+G
When you manage XML reference to XML nodes with WTP/XML search you benefit with Ctrl+Shift+G command to retrieve inverse referenced nodes. For instance if you wish find servlet-mapping declared for a servlet, you can select the servlet/servlet-name text node and do Ctrl+Shift+G. XML search is launched and display servlet-mapping which reference the servlet :
This feature is not very interesting for web.xml because XML nodes are declared in the same XML files. But for Spring, Jetty descriptor this feature can be very interesting to retrieve in a XML file, the inversed referenced node which can be declared on another files.
Validate
WTP XML/Search provides a WTP Validator V2 XML References Validator which is enable to launch the validate for several files, folder… for XML references with WTP Validate menu :
After clicking on Validate menu, you will see that web.xml has red marker (if there are errors):
The Problems View display errors with type XML References Problem :
Conclusion
In this article I have shown you screen shot which show you completion, text hover, hyper link, validation to manage web.xml descriptor. In the next article I will explain you how develop this Plugin with WTP/XML Search. The basic idea is to declare with the extension point org.eclipse.wst.xml.search.editor.references provided by WTP/XML Search the referenced between from Node and to (Node, Java, Resource). To manage completion, validation, text hover, validation for servlet/servlet-class Text node, you MUST just declare this reference with org.eclipse.wst.xml.search.editor.references :
<!-- servlet/servlet-class => Java class --> <reference> <from path="/web-app/servlet/servlet-class" targetNodes="text()" /> <toJava /> </reference>
I will explain more that in the [step3] article.