dev@javaserverfaces.java.net

[REVIEW] Changes to enable Schema support with Digester

From: Ryan Lubke <Ryan.Lubke_at_Sun.COM>
Date: Thu, 28 Oct 2004 16:10:05 -0400

These changes enable schema and dtd validation support within the
Digester (necessary for JSF 1.2).
One small caveat with this change bundle is that in order for validation
to work with *both* DTD and
Schema, Xerces will need to be present. If Xerces isn't available, then
we'll log a warning and disable
validation.


SECTION: Modified Files
------------------------------------------
Modified Files
------------------
M jsf-ri/build.xml
    - include schemas with RI JAR
    - include DigesterFactory when copying tools sources
    - Strip JSF namespace information from merged config file
      (necessary due to namespace change between 1.x and 1.2)

M jsf-ri/src/com/sun/faces/config/ConfigureListener.java
    - use new DigesterFactory to obtain a configured Digester instance
    - removed unneeded stream.close()/stream = null calls as they are
       done in the finally block

M jsf-tools/build.xml
    - include schemas with jsf-tools JAR
    - removed --dtd command line arguments

M jsf-tools/src/com/sun/faces/generate/AbstractGenerator.java
   - use new DigesterFactory to obtain configured Digester instance

M jsf-tools/src/com/sun/faces/generate/HtmlComponentGenerator.java
M jsf-tools/src/com/sun/faces/generate/HtmlTaglibGenerator.java
M jsf-tools/src/com/sun/faces/generate/RenderKitSpecificationGenerator.java
    - removed --dtd command line processing
 
New Files
------------------
A jsf-tools/src/com/sun/faces/config/DigesterFactory.java
    - Simple factory to encapsulate the creation and basic configuration
      of a Digester instance.


SECTION: Diffs
------------------------------------
JSF-TOOLS
================================
Index: build.xml
===================================================================
RCS file: /cvs/javaserverfaces-sources/jsf-tools/build.xml,v
retrieving revision 1.35
diff -u -r1.35 build.xml
--- build.xml 5 Oct 2004 21:36:45 -0000 1.35
+++ build.xml 28 Oct 2004 19:44:56 -0000
@@ -114,8 +114,9 @@
     <copy todir="${build.temp}">
       <fileset dir="${jsf-api.dir}/doc" includes="*.xml"/>
     </copy>
- <copy todir="${build.classes}/META-INF">
+ <copy todir="${build.classes}/com/sun/faces">
       <fileset dir="${jsf-api.dir}/doc" includes="*.dtd"/>
+ <fileset dir="${jsf-api.dir}/doc" includes="*.xsd"/>
     </copy>
     <copy todir="${build.temp}"
               file="${standard-html-renderkit-impl}"/>
@@ -200,8 +201,6 @@
       <arg value="${build.temp}/standard-html-renderkit.xml"/>
       <arg value="--dir"/>
       <arg value="${build.generate}"/>
- <arg value="--dtd"/>
- <arg value="-//Sun Microsystems, Inc.//DTD JavaServer
Faces Config 1.0//EN|${jsf-api.dir}/doc/web-facesconfig_1_0.dtd|-//Sun
Microsystems, Inc.//DTD JavaServer Faces Config
1.1//EN|${jsf-api.dir}/doc/web-facesconfig_1_1.dtd"/>
       <arg value="--copyright"/>
       <arg value="${basedir}/COPYRIGHT"/>
 
@@ -402,9 +401,7 @@
       <arg value="--config"/>
       <arg value="${build.temp}/standard-html-renderkit.xml"/>
       <arg value="--dir"/>
- <arg value="${build.generate.facesdoc}"/>
- <arg value="--dtd"/>
- <arg value="-//Sun Microsystems, Inc.//DTD JavaServer
Faces Config 1.0//EN|${jsf-api.dir}/doc/web-facesconfig_1_0.dtd|-//Sun
Microsystems, Inc.//DTD JavaServer Faces Config
1.1//EN|${jsf-api.dir}/doc/web-facesconfig_1_1.dtd"/>
+ <arg value="${build.generate.facesdoc}"/>
       <arg value="--copyright"/>
       <arg value="${basedir}/COPYRIGHT"/>
 
Index: src/com/sun/faces/generate/AbstractGenerator.java
===================================================================
RCS file:
/cvs/javaserverfaces-sources/jsf-tools/src/com/sun/faces/generate/AbstractGenerator.java,v
retrieving revision 1.6
diff -u -r1.6 AbstractGenerator.java
--- src/com/sun/faces/generate/AbstractGenerator.java 24 Aug 2004
17:07:13 -0000 1.6
+++ src/com/sun/faces/generate/AbstractGenerator.java 28 Oct 2004
19:44:56 -0000
@@ -10,22 +10,23 @@
 package com.sun.faces.generate;
 
 
+import com.sun.faces.config.DigesterFactory;
+import com.sun.faces.config.beans.FacesConfigBean;
+import com.sun.faces.config.rules.FacesConfigRuleSet;
+import org.apache.commons.digester.Digester;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
 import java.io.BufferedInputStream;
 import java.io.File;
 import java.io.FileInputStream;
-import java.io.InputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.Writer;
-import java.net.MalformedURLException;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
-import com.sun.faces.config.beans.FacesConfigBean;
-import com.sun.faces.config.rules.FacesConfigRuleSet;
-import org.apache.commons.digester.Digester;
-import org.xml.sax.InputSource;
-import org.xml.sax.SAXException;
 
 
 /**
@@ -75,6 +76,7 @@
         keywords.add("do");
         keywords.add("double");
         keywords.add("else");
+ keywords.add("enum");
         keywords.add("extends");
         keywords.add("final");
         keywords.add("finally");
@@ -104,6 +106,7 @@
         keywords.add("return");
         keywords.add("short");
         keywords.add("static");
+ keywords.add("strictfp");
         keywords.add("super");
         keywords.add("switch");
         keywords.add("synchronized");
@@ -202,42 +205,22 @@
      * <p>Configure and return a <code>Digester</code> instance
suitable for
      * use in the environment specified by our parameter flags.</p>
      *
- * @param dtd[] array of toString()'d URLs to DTDs to be registered
- * (if any) and their corresponding public identifiers
      * @param design Include rules suitable for design time use in a tool
      * @param generate Include rules suitable for generating component,
      * renderer, and tag classes
      * @param runtime Include rules suitable for runtime execution
- *
- * @exception MalformedURLException if a URL cannot be formed correctly
      */
- protected static Digester digester(String dtd[], boolean design,
- boolean generate, boolean runtime)
- throws MalformedURLException {
-
- Digester digester = new Digester();
-
- // Configure basic properties
- digester.setNamespaceAware(false);
- digester.setUseContextClassLoader(true);
- digester.setValidating(true);
+ protected static Digester digester(boolean design,
+ boolean generate, boolean runtime) {
+
+ Digester digester =
DigesterFactory.newInstance(true).createDigester();
 
         // Configure parsing rules
         digester.addRuleSet(new FacesConfigRuleSet(design, generate,
runtime));
 
         // Configure preregistered entities
- int i = 0;
- while (dtd.length > 0) {
- if (dtd[i] != null && dtd[i+1] != null) {
- digester.register(dtd[i], dtd[i+1]);
- }
- i += 2;
- if (i >= dtd.length) {
- break;
- }
- }
- return (digester);
 
+ return (digester);
     }
 
 
@@ -250,7 +233,7 @@
     protected static String mangle(String name) {
 
         if (keywords.contains(name)) {
- return ("_" + name);
+ return ('_' + name);
         } else {
             return (name);
         }
@@ -273,10 +256,10 @@
         while (i < args.length) {
             if (!args[i].startsWith("-")) {
                 throw new IllegalArgumentException
- ("Invalid option name '" + args[i] + "'");
+ ("Invalid option name '" + args[i] + '\'');
             } else if ((i + 1) >= args.length) {
                 throw new IllegalArgumentException
- ("Missing value for option '" + args[i] + "'");
+ ("Missing value for option '" + args[i] + '\'');
             }
             options.put(args[i], args[i+1]);
             i += 2;
@@ -347,7 +330,7 @@
      */
     protected static String shortName(String className) {
 
- int index = className.lastIndexOf(".");
+ int index = className.lastIndexOf('.');
         if (index >= 0) {
             return (className.substring(index + 1));
         } else {
Index: src/com/sun/faces/generate/HtmlComponentGenerator.java
===================================================================
RCS file:
/cvs/javaserverfaces-sources/jsf-tools/src/com/sun/faces/generate/HtmlComponentGenerator.java,v
retrieving revision 1.8
diff -u -r1.8 HtmlComponentGenerator.java
--- src/com/sun/faces/generate/HtmlComponentGenerator.java 24 Aug
2004 17:07:13 -0000 1.8
+++ src/com/sun/faces/generate/HtmlComponentGenerator.java 28 Oct
2004 19:44:57 -0000
@@ -10,6 +10,14 @@
 package com.sun.faces.generate;
 
 
+import com.sun.faces.config.beans.ComponentBean;
+import com.sun.faces.config.beans.DescriptionBean;
+import com.sun.faces.config.beans.FacesConfigBean;
+import com.sun.faces.config.beans.PropertyBean;
+import org.apache.commons.digester.Digester;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.File;
@@ -19,14 +27,6 @@
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
-import java.util.StringTokenizer;
-import org.apache.commons.digester.Digester;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import com.sun.faces.config.beans.ComponentBean;
-import com.sun.faces.config.beans.DescriptionBean;
-import com.sun.faces.config.beans.FacesConfigBean;
-import com.sun.faces.config.beans.PropertyBean;
 
 
 /**
@@ -521,35 +521,10 @@
                 log.debug("Processing command line options");
             }
             Map options = options(args);
- String dtd = (String) options.get("--dtd");
- if (log.isDebugEnabled()) {
- log.debug("Configuring digester instance with public
identifiers and DTD '" +
- dtd + "'");
- }
- StringTokenizer st = new StringTokenizer(dtd, "|");
- int arrayLen = st.countTokens();
- if (arrayLen == 0) {
- // PENDING I18n
- throw new Exception("No DTDs specified");
- }
- String[] dtds = new String[arrayLen];
- int i=0;
- while (st.hasMoreTokens()) {
- // even numbered elements are left alone
- if (0 == (i % 2)) {
- dtds[i] = st.nextToken();
- }
- else {
- // odd numbered elements are treated as absolute
- // filenames
- dtds[i] =(new File(st.nextToken())).toURL().toString();
- }
- i++;
- }
 
             copyright((String) options.get("--copyright"));
             directories((String) options.get("--dir"));
- Digester digester = digester(dtds, false, true, false);
+ Digester digester = digester(false, true, false);
             String config = (String) options.get("--config");
             if (log.isDebugEnabled()) {
                 log.debug("Parsing configuration file '" + config + "'");
@@ -562,7 +537,7 @@
 
             // Generate concrete HTML component classes
             ComponentBean cbs[] = fcb.getComponents();
- for (i = 0; i < cbs.length; i++) {
+ for (int i = 0; i < cbs.length; i++) {
                 String componentClass = cbs[i].getComponentClass();
                 if
(componentClass.startsWith("javax.faces.component.html.")) {
                     cb = cbs[i];
Index: src/com/sun/faces/generate/HtmlTaglibGenerator.java
===================================================================
RCS file:
/cvs/javaserverfaces-sources/jsf-tools/src/com/sun/faces/generate/HtmlTaglibGenerator.java,v
retrieving revision 1.28
diff -u -r1.28 HtmlTaglibGenerator.java
--- src/com/sun/faces/generate/HtmlTaglibGenerator.java 3 Sep 2004
17:20:54 -0000 1.28
+++ src/com/sun/faces/generate/HtmlTaglibGenerator.java 28 Oct 2004
19:44:58 -0000
@@ -10,8 +10,11 @@
 import com.sun.faces.config.beans.DescriptionBean;
 import com.sun.faces.config.beans.FacesConfigBean;
 import com.sun.faces.config.beans.PropertyBean;
-import com.sun.faces.config.beans.RendererBean;
 import com.sun.faces.config.beans.RenderKitBean;
+import com.sun.faces.config.beans.RendererBean;
+import org.apache.commons.digester.Digester;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
@@ -25,15 +28,8 @@
 import java.util.List;
 import java.util.Map;
 import java.util.SortedMap;
-import java.util.StringTokenizer;
 import java.util.TreeMap;
 
-import java.net.URL;
-
-import org.apache.commons.digester.Digester;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
 /**
  * This class generates tag handler class code that is special to the
"html_basic"
  * package.
@@ -1192,51 +1188,10 @@
                 log.debug("Processing command line options");
             }
             Map options = options(args);
- String dtd = (String) options.get("--dtd");
- if (log.isDebugEnabled()) {
- log.debug("Configuring digester instance with public
identifiers and DTD '" +
- dtd + "'");
- }
-
- String[] dtds = null;
-
- // this if-else block populates the dtds array according to
- // the expectations of the digester() method.
- if (null == dtd) {
- ClassLoader cl =Thread.currentThread().getContextClassLoader();
- dtds = new String[4];
- dtds[0] = "-//Sun Microsystems, Inc.//DTD JavaServer Faces
Config 1.0//EN";
- dtds[1] =
((URL)cl.getResource("META-INF/web-facesconfig_1_0.dtd")).toString();
- dtds[2] = "-//Sun Microsystems, Inc.//DTD JavaServer Faces
Config 1.1//EN";
- dtds[3] = ((URL)
cl.getResource("META-INF/web-facesconfig_1_1.dtd")).toString();
-
- }
- else {
- StringTokenizer st = new StringTokenizer(dtd, "|");
- int arrayLen = st.countTokens();
- if (arrayLen == 0) {
- // PENDING I18n
- throw new Exception("No DTDs specified");
- }
- dtds = new String[arrayLen];
- int i=0;
- while (st.hasMoreTokens()) {
- // even numbered elements are left alone
- if (0 == (i % 2)) {
- dtds[i] = st.nextToken();
- }
- else {
- // odd numbered elements are treated as absolute
- // filenames
- dtds[i] =(new File(st.nextToken())).toURL().toString();
- }
- i++;
- }
- }
 
             copyright((String) options.get("--copyright"));
             directories((String) options.get("--tlddir"), false);
- Digester digester = digester(dtds, false, true, false);
+ Digester digester = digester(false, true, false);
             String config = (String) options.get("--config");
         loadOptionalTags((String) options.get("--tagdef"));
             if (log.isDebugEnabled()) {
Index: src/com/sun/faces/generate/RenderKitSpecificationGenerator.java
===================================================================
RCS file:
/cvs/javaserverfaces-sources/jsf-tools/src/com/sun/faces/generate/RenderKitSpecificationGenerator.java,v
retrieving revision 1.4
diff -u -r1.4 RenderKitSpecificationGenerator.java
--- src/com/sun/faces/generate/RenderKitSpecificationGenerator.java
12 May 2004 03:08:50 -0000 1.4
+++ src/com/sun/faces/generate/RenderKitSpecificationGenerator.java
28 Oct 2004 19:44:59 -0000
@@ -10,37 +10,32 @@
 package com.sun.faces.generate;
 
 
-import java.io.BufferedReader;
+import com.sun.faces.config.beans.AttributeBean;
+import com.sun.faces.config.beans.ComponentBean;
+import com.sun.faces.config.beans.DescriptionBean;
+import com.sun.faces.config.beans.FacesConfigBean;
+import com.sun.faces.config.beans.PropertyBean;
+import com.sun.faces.config.beans.RenderKitBean;
+import com.sun.faces.config.beans.RendererBean;
+import org.apache.commons.digester.Digester;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import java.io.BufferedInputStream;
 import java.io.BufferedWriter;
 import java.io.File;
-import java.io.FileReader;
-import java.io.FileWriter;
 import java.io.FileOutputStream;
-import java.io.Writer;
-import java.io.InputStream;
+import java.io.FileWriter;
 import java.io.InputStreamReader;
-import java.io.BufferedInputStream;
+import java.io.Writer;
+import java.net.URL;
+import java.net.URLConnection;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
-import java.util.Locale;
 import java.util.Map;
 import java.util.SortedMap;
-import java.util.StringTokenizer;
 import java.util.TreeMap;
-import java.net.URL;
-import java.net.URLConnection;
-
-import org.apache.commons.digester.Digester;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import com.sun.faces.config.beans.ComponentBean;
-import com.sun.faces.config.beans.DescriptionBean;
-import com.sun.faces.config.beans.FacesConfigBean;
-import com.sun.faces.config.beans.PropertyBean;
-import com.sun.faces.config.beans.RenderKitBean;
-import com.sun.faces.config.beans.RendererBean;
-import com.sun.faces.config.beans.AttributeBean;
 
 
 
@@ -867,25 +862,9 @@
                 log.debug("Processing command line options");
             }
             Map options = options(args);
- String dtd = (String) options.get("--dtd");
- if (log.isDebugEnabled()) {
- log.debug("Configuring digester instance with public
identifiers and DTD '" +
- dtd + "'");
- }
- StringTokenizer st = new StringTokenizer(dtd, "|");
- int arrayLen = st.countTokens();
- if (arrayLen == 0) {
- // PENDING I18n
- throw new Exception("No DTDs specified");
- }
- String[] dtds = new String[arrayLen];
- int i=0;
- while (st.hasMoreTokens()) {
- dtds[i] = st.nextToken();
- i++;
- }
+
             directories((String) options.get("--dir"));
- Digester digester = digester(dtds, false, true, false);
+ Digester digester = digester(false, true, false);
             String config = (String) options.get("--config");
             if (log.isDebugEnabled()) {
                 log.debug("Parsing configuration file '" + config + "'");


JSF-RI
===============================
Index: build.xml
===================================================================
RCS file: /cvs/javaserverfaces-sources/jsf-ri/build.xml,v
retrieving revision 1.168
diff -u -r1.168 build.xml
--- build.xml 18 Oct 2004 22:46:03 -0000 1.168
+++ build.xml 28 Oct 2004 19:43:53 -0000
@@ -244,6 +244,7 @@
 
       <!-- Copy the configuration processing classes -->
       <fileset dir="${jsf-tools.dir}/src">
+ <include name="com/sun/faces/config/DigesterFactory.java"/>
         <include name="com/sun/faces/config/beans/*.java"/>
         <include name="com/sun/faces/config/rules/*.java"/>
       </fileset>
@@ -389,6 +390,7 @@
         <fileset dir="${jsf-api.home}/doc">
           <include name="*.dtd"/>
           <include name="*.xml"/>
+ <include name="*.xsd"/>
         </fileset>
     </copy>
 
@@ -670,7 +672,10 @@
                   
location="${build.home}/classes/com/sun/faces/web-facesconfig_1_0.dtd"/>
           </xmlcatalog>
       </xslt>
- <delete file="${basedir}/merged.xml"/>
+ <replace
file="${build.home}/classes/com/sun/faces/jsf-ri-runtime.xml"
token="xmlns=&quot;http://java.sun.com/JSF/Configuration&quot;" />
+ <replace
file="${build.home}/classes/com/sun/faces/jsf-ri-runtime.xml"
token="xmlns:jsf=&quot;http://java.sun.com/JSF/Configuration&quot;" />
+ <replace
file="${build.home}/classes/com/sun/faces/jsf-ri-runtime.xml"
token="&lt;faces-config &gt;" value="&lt;faces-config&gt;"/>
+ <delete file="${basedir}/merged.xml"/>
   </target>
 
   <!--
Index: src/com/sun/faces/config/ConfigureListener.java
===================================================================
RCS file:
/cvs/javaserverfaces-sources/jsf-ri/src/com/sun/faces/config/ConfigureListener.java,v
retrieving revision 1.25
diff -u -r1.25 ConfigureListener.java
--- src/com/sun/faces/config/ConfigureListener.java 17 Aug 2004
17:05:21 -0000 1.25
+++ src/com/sun/faces/config/ConfigureListener.java 28 Oct 2004
19:43:55 -0000
@@ -130,16 +130,6 @@
     protected static final String ENABLE_HTML_TLV =
         RIConstants.FACES_PREFIX + "enableHtmlTagLibValidator";
 
- /*
- * The first element is the path, the second is the public ID.
- */
- private static String[][] DTD_INFO = {
- { "/com/sun/faces/web-facesconfig_1_0.dtd",
- "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config
1.0//EN" },
- { "/com/sun/faces/web-facesconfig_1_1.dtd",
- "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config
1.1//EN" }
- };
-
     /**
      * <p>All known factory names.</p>
      */
@@ -961,29 +951,14 @@
      * @param validateXml if true, validation is turned on during parsing.
      */
     protected Digester digester(boolean validateXml) {
- Digester digester = new Digester();
 
- // Configure basic properties
- digester.setNamespaceAware(false);
- digester.setUseContextClassLoader(true);
- digester.setValidating(validateXml);
+ Digester digester =
+ DigesterFactory.newInstance(validateXml).createDigester();
 
         // Configure parsing rules
         // PENDING - Read from file?
         digester.addRuleSet(new FacesConfigRuleSet(false, false, true));
 
- // Register known entities
- for (int i = 0; i < DTD_INFO.length; i++) {
- URL url = this.getClass().getResource(DTD_INFO[i][0]);
- if (url != null) {
- digester.register(DTD_INFO[i][1], url.toString());
- } else {
- throw new FacesException(
-
Util.getExceptionMessageString(Util.NO_DTD_FOUND_ERROR_ID,
- new Object[]{ DTD_INFO[i][1], DTD_INFO[i][0] }));
- }
- }
-
         // Push an initial FacesConfigBean onto the stack
         digester.push(new FacesConfigBean());
 
@@ -1186,8 +1161,6 @@
             digester.clear();
             digester.push(fcb);
             digester.parse(source);
- stream.close();
- stream = null;
         } catch (Exception e) {
             String message = null;
             try {


SECTION: New Files
-------------------------------------------
SEE ATTACHMENT


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe_at_javaserverfaces.dev.java.net
For additional commands, e-mail: dev-help_at_javaserverfaces.dev.java.net