[jdom-interest] JDOMBean

Alex Chaffee guru at edamame.stinky.com
Wed Jun 21 02:43:53 PDT 2000

Here's a bean I just whipped up so you can easily use JDOM in a
Servlet or JSP.  It's a light wrapper that also keeps a cache of
already-parsed files, and checks to see if they've changed on disk,
and reloads if they have.  (I know there's some sort of swap-out or
weak-reference stuff either in JDOM or coming soon, so this may be a
redundant feature.)  

Usage in JSP (take your pick):

<jsp:useBean id="jdom" class="JDOMBean" scope="application">
 <% jdom.setFileRoot(application.getRealPath("")); %>


<jsp:useBean id="jdom" class="JDOMBean" scope="application">
 <jsp:setProperty name="jdom" property="fileRoot" value='<%=application.getRealPath("")%>' />


Element root = jdom.getRootElement("news.xml");


Document doc = jdom.getDocument("news.xml");

and you're playing with JDOM.

You can also set the parser in the constructor (maybe it should be a
property too).

So, does this qualify for the contrib directory?

 - Alex

Alex Chaffee                       mailto:alex at jguru.com
jGuru - Java News and FAQs         http://www.jguru.com/alex/
Creator of Gamelan                 http://www.gamelan.com/
Founder of Purple Technology       http://www.purpletech.com/
Curator of Stinky Art Collective   http://www.stinky.com/

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Iterator;
import java.util.*;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

public class JDOMBean {

    /** Default SAX Driver class to use */
    private static final String DEFAULT_SAX_DRIVER_CLASS =

    /** SAX Driver Class to use */
    private String saxDriverClass = DEFAULT_SAX_DRIVER_CLASS;

    /** <code>{@link SAXBuilder}</code> instance to use */
    private SAXBuilder builder;

    /** file cache **/
    private Map files = new HashMap();

    /** where to locate files **/
    private File fileRoot;

    public JDOMBean() {

    public JDOMBean(String saxDriverClass) {
	this.saxDriverClass = saxDriverClass;

     * <p>
     * This will create an instance of <code>{@link SAXBuilder}</code>
     *   for use in the rest of this program.
     * </p>
     * @param saxDriverClass <code>String</code> name of driver class to use.
    public void init() {
        builder = new SAXBuilder(saxDriverClass);

    // todo: pathname normalization (remove ./ and foo/../ and so forth)
    public Document getDocument(String filename) throws JDOMException {
	FileInfo info = (FileInfo) files.get(filename);
	File file = getFile(filename);
	if (info == null ||
	    info.modified < file.lastModified())
	    Document doc = builder.build(file);
	    info = new FileInfo(filename, file.lastModified(), doc);
	    files.put(filename, info);
	return info.document;

    public Element getRootElement(String file) throws JDOMException {
	Document doc = getDocument(file);
	if (doc != null) return doc.getRootElement();
	return null;

    public void setFileRoot(String root) {
	if (!root.endsWith("/")) {
	    root = root + "/";
	this.fileRoot = new File(root);

    public String getFileRoot() {
	if (fileRoot == null) return null;
	else return fileRoot.getAbsolutePath();

    private File getFile(String filename) {
	File file;
	if (fileRoot == null) {
	    return new File(filename);
	else {
	    return new File(fileRoot, filename);

    class FileInfo {
	String name;
	long modified;
	Document document;
	public FileInfo(String name, long modified, Document document) {
	    this.name = name;
	    this.modified = modified;
	    this.document = document;

    // test
    public static void main(String[] args) throws IOException, JDOMException {
	int i=0;
	JDOMBean bean;
	if (args[i].equals("-parser")) {
	    bean = new JDOMBean(args[i]);
	else {
	    bean = new JDOMBean();

	XMLOutputter out = new XMLOutputter();

	for (; i<args.length; ++i) {
	    Document doc = bean.getDocument(args[i]);
	    out.output(doc, System.out);


More information about the jdom-interest mailing list