|
Before we start coding the first program we'll need to add
webapp.jar and the xbean.jar file to
our classpath. Our code starts out like this:
Listing 2-1. The FirstTry class
package dk.hansen.playground;
import java.io.File;
import java.io.IOException;
import com.bea.xml.XmlException;
import noNamespace.WebAppDocument;
import noNamespace.WebAppDocument.WebApp;
public class FirstTry {
public static void main(String[] args)
throws XmlException, IOException {
File file = new File(args[0]);
WebAppDocument webappDoc =
WebAppDocument.Factory.parse(file);
WebApp webapp = webappDoc.getWebApp();
. . .
}
}
The program takes the name of the web.xml file as a
parameter, and from this name we create a File
instance. To parse the file we use the static parse
method in a class named after our root element,
WebApp. This'll give us our first object--a
"document" instance. The document contains our XML
data, and we get a handle to this by using the
getWebApp method. The WebApp class has
these getters:
| Method name |
XML element |
Servlet[] getServletArray() |
<servlet> |
ServletMapping[] getServletMappingArray() |
<servlet-mapping> |
Taglib[] getTaglibArray() |
<taglib> |
WelcomeFileList getWelcomeFileList() |
<welcome-file-list> |
The mapping from the XML element name to the method name is
obvious. If an element can be repeated the getter method name
has an "Array" appended to it.
It's now rather simple to continue with the coding:
Listing 2-2. The FirstTry class
. . .
System.out.println("Servlets:");
Servlet[] servlet = webapp.getServletArray();
for (int i = 0; i < servlet.length; i++) {
System.out.println(" Name/class: "
+ servlet[i].getServletName() + "/"
+ servlet[i].getServletClass());
InitParam[] init = servlet[i].getInitParamArray();
System.out.println(" Init parameters:");
for (int j = 0; j < init.length; j++) {
System.out.println(" Name/value: " +
init[j].getParamName() + "/"
+ init[j].getParamValue());
}
BigInteger load = servlet[i].getLoadOnStartup();
if (load != null) {
System.out.println(" Load on startup: " + load);
}
}
System.out.println("Servlet Mappings:");
ServletMapping[] mapping = webapp.getServletMappingArray();
for (int j = 0; j < mapping.length; j++) {
System.out.println(" Name/URL: "
+ mapping[j].getServletName() + "/"
+ mapping[j].getUrlPattern());
}
System.out.println("Welcome File List:");
WelcomeFileList fileList = webapp.getWelcomeFileList();
String[] s = fileList.getWelcomeFileArray();
for (int i = 0; i < s.length; i++) {
System.out.println(" Filename: " + s[i]);
}
System.out.println("Taglibs:");
Taglib[] tags = webapp.getTaglibArray();
for (int j = 0; j < tags.length; j++) {
System.out.println(" URI/location: "
+ tags[j].getTaglibUri() + "/"
+ tags[j].getTaglibLocation());
}
}
The complete program can viewed here and may
be downloaded from the resources section at the end of the
article.
If we run the program with web-struts-blank.xml as
argument we'll get this listed:
*** Data in web-struts-blank.xml
Servlets:
Name/class: action/org.apache.struts.action.ActionServlet
Init parameters:
Name/value: config//WEB-INF/struts-config.xml
Name/value: debug/2
Name/value: detail/2
Load on startup: 2
Servlet Mappings:
Name/URL: action/*.do
Welcome File List:
Filename: index.jsp
Taglibs:
URI/location: /tags/struts-bean//WEB-INF/struts-bean.tld
URI/location: /tags/struts-html//WEB-INF/struts-html.tld
URI/location: /tags/struts-logic//WEB-INF/struts-logic.tld
URI/location: /tags/struts-nested//WEB-INF/struts-nested.tld
URI/location: /tags/struts-tiles//WEB-INF/struts-tiles.tld
If you compare this output with the output shown in the
article about Castor
you'll see that they're identical, which they should be!
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|