org.jdom.contrib.beans
Class JDOMBean

java.lang.Object
  extended by org.jdom.contrib.beans.JDOMBean

public class JDOMBean
extends java.lang.Object

A light wrapper on the JDOM library that you use to load in a file and turn it into a JDOM Document. It 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 from Java:

 JDOMBean jdom = new JDOMBean(); // or new JDOMBean("com.foo.saxparser")
 jdom.setFileRoot("/path/to/my/xml/documents/");
 Document doc = jdom.getDocument("foo.xml");
 Element root = jdom.getRootElement("foo.xml");
 

Usage from JSP:

  
 <jsp:useBean id="jdom" class="JDOMBean" scope="application">
  <% jdom.setFileRoot(application.getRealPath("")); %>
 </jsp:useBean>
  
 or
  
 <jsp:useBean id="jdom" class="JDOMBean" scope="application">
  <jsp:setProperty name="jdom" property="fileRoot"
 +value='<%=application.getRealPath("")%>' />
 </jsp:useBean>
                                           
 then 
  
 <%
 Element root = jdom.getRootElement("foo.xml");
 %>
 Bar: <%=root.getChild("bar").getContent()%>
 

Author:
Alex Chaffee [alex@jguru.com]

Constructor Summary
JDOMBean()
          default constructor, uses "org.apache.xerces.parsers.SAXParser"
JDOMBean(java.lang.String parser)
           
 
Method Summary
 org.jdom.Document getDocument(java.lang.String filename)
          Load a file, parse it with JDOM, return a org.jdom.Document.
 java.lang.String getFileRoot()
           
 java.lang.String getParser()
           
 org.jdom.Element getRootElement(java.lang.String file)
          Convenience method, calls getDocument(filename).getRootElement()
static void main(java.lang.String[] args)
           
 void setFileRoot(java.lang.String root)
          All files are fetched relative to this path
 void setParser(java.lang.String parser)
           This will create an instance of SAXBuilder for use in the rest of this program.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JDOMBean

public JDOMBean()
default constructor, uses "org.apache.xerces.parsers.SAXParser"


JDOMBean

public JDOMBean(java.lang.String parser)
Parameters:
parser - String name of driver class to use.
Method Detail

setParser

public void setParser(java.lang.String parser)

This will create an instance of SAXBuilder for use in the rest of this program.

Parameters:
parser - String name of SAX parser class to use.

getParser

public java.lang.String getParser()
Returns:
name of SAX parser class being used

setFileRoot

public void setFileRoot(java.lang.String root)
All files are fetched relative to this path

Parameters:
root - the path (absolute or relative) to the document root

getFileRoot

public java.lang.String getFileRoot()
Returns:
the path (absolute or relative) to the document root

getDocument

public org.jdom.Document getDocument(java.lang.String filename)
                              throws org.jdom.JDOMException,
                                     java.io.IOException
Load a file, parse it with JDOM, return a org.jdom.Document. If the file has already been parsed, return the previously cached object. If the file has changed, ignore the previously parsed version and reload.

Note that this never unloads a document, so is unsuitable for long-term server-side use for a constantly changing set of files. Todo: use weak references or cache timeouts.

Also does not do secure checking on file requested, so if there's no root, and the parameter starts with a "/", this could conceivably access files you don't want accessed. So be careful out there.

Parameters:
filename - the file to load, relative to file root
Returns:
a JDOM Document corresponding to the given filename
Throws:
org.jdom.JDOMException
java.io.IOException

getRootElement

public org.jdom.Element getRootElement(java.lang.String file)
                                throws org.jdom.JDOMException,
                                       java.io.IOException
Convenience method, calls getDocument(filename).getRootElement()

Throws:
org.jdom.JDOMException
java.io.IOException

main

public static void main(java.lang.String[] args)
                 throws java.io.IOException,
                        org.jdom.JDOMException
Throws:
java.io.IOException
org.jdom.JDOMException


Copyright © 2007 Jason Hunter, Brett McLaughlin. All Rights Reserved.