I just finished my first cup of coffee and realized I didn't address having the external def in the conf directory. You probably do not want to rely on each user having the same directory structure, so you can't rely on a hard coded absolute or relative path :)

First, let me say I usually put a .properties file in some system defined directory and configure at app start up. Sometime I need a hierarchy and use an XML file in some system defined dir and keep that stored in some DOMish structure (e.g. XOM, dom4j, etc) rather than converting it into some (brittle) object with something like JAXB.

Anyway, back to XInclude, there are good use cases especially within an XML heavy app. Relating to getting the external def into the conf dir, you would want to use XML Catalogs. They let you assign a local file to some reference in the XML. This can be used for many different types referenced file resolution. For example below, your local catalog would define:

<system
    systemId="someParam.txt"
    uri="/home/me/myapp/someParam.txt"/>
....

And of course, catalogs can reference other catalogs, which can reference other catalogs...

best,
-Rob


On Apr 22, 2009, at 5:41 AM, Robert Koberg wrote:


On Apr 22, 2009, at 4:25 AM, André Warnier wrote:


Allright, but I'm afraid this is still somewhat flying over my head, what we me not being /either/ a Java expert, /nor/ a Tomcat expert, /nor/ an XML expert. (What am I then doing on this list, one might ask).

So, since everyone but me seems to know pretty well how to do it, sometimes even in several ways, and since from previous threads I believe there is more interest for this, would it be possible for someone to give an effective simple example (or maybe two or three) based for example on this :

<?xml version="1.0"?>
<!DOCTYPE web-app
  PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  "http://java.sun.com/dtd/web-app_2_3.dtd";>

<web-app>
<display-name>MyApp</display-name>
<description>
  My simple webapp.
</description>

<servlet>
  <servlet-name>MyServlet</servlet-name>
  <servlet-class>my.servlet</servlet-class>
  <init-param>
        <param-name>someParam</param-name>
        <param-value>someValue</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
...
</web-app>

of how one could make it so that the <param-value> of the above <param-name> "someParam" is a reference to some value defined elsewhere, for example in a file in the Tomcat "conf" directory ?


OK, here you cannot use entities in an external file because you have already defined a DTD (one of the problems with DTDs).

As for XInclude, the problem is slightly different: The XInclude support has to be turned on for the parser. From Xerces:

http://xerces.apache.org/xerces2-j/faq-xinclude.html

"Applications using JAXP 1.3 can enable XInclude processing by setting XInclude awareness on the parser factory. The following demonstrates how to accomplish this with SAX:
                

import javax.xml.parsers.SAXParserFactory;

SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
spf.setXIncludeAware(true);
...

        
You can also enable XInclude processing by turning on the XInclude feature."

BTW, here is an article that has some examples:

http://www.xml.com/pub/a/2002/07/31/xinclude.html

Your example might look like:

<?xml version="1.0"?>
<!DOCTYPE web-app
  PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  "http://java.sun.com/dtd/web-app_2_3.dtd";>

<web-app xmlns:xi="http://www.w3.org/2001/XInclude";>
 <display-name>MyApp</display-name>
 <description>
   My simple webapp.
 </description>

 <servlet>
   <servlet-name>MyServlet</servlet-name>
   <servlet-class>my.servlet</servlet-class>
   <init-param>
     <param-name>someParam</param-name>
     <param-value>
       <!-- someParam.txt just contains the text 'someValue' -->
       <xi:include href="someParam.txt" parse="text">
         <xi:fallback>defaultValue</xi:fallback>
       </xi:include>
     </param-value>
   </init-param>

   <load-on-startup>1</load-on-startup>
 </servlet>

</web-app>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to