2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

Coverage Report - org.apache.shale.tiger.config.FacesConfigParser
 
Classes in this File Line Coverage Branch Coverage Complexity
FacesConfigParser
100%
40/40
N/A
2.125
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to you under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 
 18  
 package org.apache.shale.tiger.config;
 19  
 
 20  
 import java.io.IOException;
 21  
 import java.io.InputStream;
 22  
 import java.net.URL;
 23  
 import org.apache.commons.digester.Digester;
 24  
 import org.apache.commons.digester.RuleSet;
 25  
 import org.apache.shale.tiger.managed.rules.ManagedBeansRuleSet;
 26  
 import org.xml.sax.InputSource;
 27  
 import org.xml.sax.SAXException;
 28  
 
 29  
 /**
 30  
  * <p>Parser utility for processing <code>faces-config.xml</code> resources.
 31  
  * Information parsed by each call to the <code>parse()</code> method is
 32  
  * merged with any previous information stored in the specified
 33  
  * {@link FacesConfigConfig} bean.</p>
 34  
  *
 35  
  * <p>To use this utility, instantiate a new instance and set the
 36  
  * <code>facesConfig</code>, <code>resource</code>, and <code>validating</code>
 37  
  * properties.  Then, call the <code>parse()</code> method.  You can parse
 38  
  * more than one resource by resetting the <code>resource</code> property
 39  
  * and calling <code>parse()</code> again.</p>
 40  
  */
 41  22
 public class FacesConfigParser {
 42  
 
 43  
     // -------------------------------------------------------- Static Constants
 44  
 
 45  
 
 46  
     /**
 47  
      * <p>Registration information for the DTD used to validate the specified
 48  
      * configuration resources.</p>
 49  
      */
 50  1
     private static final String[] REGISTRATIONS =
 51  
     { "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN",
 52  
       "/org/apache/shale/tiger/resources/web-facesconfig_1_0.dtd",
 53  
       "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN",
 54  
       "/org/apache/shale/tiger/resources/web-facesconfig_1_1.dtd",
 55  
     };
 56  
 
 57  
 
 58  
     // -------------------------------------------------------------- Properties
 59  
 
 60  
 
 61  
     /**
 62  
      * <p>Configuration resource summarizing all of the configuration metadata
 63  
      * from resources that have been parsed so far.</p>
 64  
      */
 65  22
     private FacesConfigConfig facesConfig = null;
 66  
 
 67  
 
 68  
     /**
 69  
      * <p>Return the {@link FacesConfigConfig} bean summarizing all of the
 70  
      * configuration metadata from resources that have been parsed so far.</p>
 71  
      */
 72  
     public FacesConfigConfig getFacesConfig() {
 73  115
         return this.facesConfig;
 74  
     }
 75  
 
 76  
 
 77  
     /**
 78  
      * <p>Set the {@link FacesConfigConfig} bean that will summarize all of the
 79  
      * configuration metadata from parsed resources.</p>
 80  
      *
 81  
      * @param facesConfig New {@link FacesConfigConfig} instance to be populated
 82  
      */
 83  
     public void setFacesConfig(FacesConfigConfig facesConfig) {
 84  21
         this.facesConfig = facesConfig;
 85  21
     }
 86  
 
 87  
 
 88  
     /**
 89  
      * <p>The URL of the configuration resource to be parsed.</p>
 90  
      */
 91  22
     private URL resource = null;
 92  
 
 93  
 
 94  
     /**
 95  
      * <p>Return the URL of the configuration resource to be parsed.</p>
 96  
      */
 97  
     public URL getResource() {
 98  229
         return this.resource;
 99  
     }
 100  
 
 101  
 
 102  
     /**
 103  
      * <p>Set the URL of the configuration resource to be parsed.</p>
 104  
      *
 105  
      * @param resource The new configuration resource URL
 106  
      */
 107  
     public void setResource(URL resource) {
 108  111
         this.resource = resource;
 109  111
     }
 110  
 
 111  
 
 112  
     /**
 113  
      * <p>Flag indicating whether we should do a validating parse.</p>
 114  
      */
 115  22
     private boolean validating = true;
 116  
 
 117  
 
 118  
     /**
 119  
      * <p>Return the validating parse flag.</p>
 120  
      */
 121  
     public boolean isValidating() {
 122  25
         return this.validating;
 123  
     }
 124  
 
 125  
 
 126  
     /**
 127  
      * <p>Set the validating parse flag.</p>
 128  
      *
 129  
      * @param validating The new validating parse flag
 130  
      */
 131  
     public void setValidating(boolean validating) {
 132  21
         this.validating = validating;
 133  21
     }
 134  
 
 135  
 
 136  
     // ---------------------------------------------------------- Public Methods
 137  
 
 138  
 
 139  
     /**
 140  
      * <p>Parse the configuration resource specified by the <code>resource</code>
 141  
      * property, storing resulting information in the configuration bean
 142  
      * specified by the <code>facesConfig</code> property.</p>
 143  
      *
 144  
      * @exception IOException if an input/output error occurs
 145  
      * @exception SAXException if an XML parsing error occurs
 146  
      */
 147  
     public void parse() throws IOException, SAXException {
 148  
 
 149  111
         Digester digester = digester();
 150  111
         digester.clear();
 151  111
         digester.push(getFacesConfig());
 152  111
         InputSource source = new InputSource(getResource().toExternalForm());
 153  111
         InputStream stream = null;
 154  
         try {
 155  111
             stream = getResource().openStream();
 156  111
             source.setByteStream(stream);
 157  111
             digester.parse(source);
 158  
         } catch (IOException e) {
 159  
             throw e;
 160  
         } catch (SAXException e) {
 161  
             throw e;
 162  
         } finally {
 163  111
             if (stream != null) {
 164  
                 try {
 165  111
                     stream.close();
 166  
                 } catch (IOException e) {
 167  
                     ; // Fall through
 168  111
                 }
 169  
             }
 170  
         }
 171  
 
 172  111
     }
 173  
 
 174  
 
 175  
     // --------------------------------------------------------- Private Methods
 176  
 
 177  
 
 178  
     /**
 179  
      * <p>Configured <code>Digester</code> instance to be used (lazily
 180  
      * instantiated.</p>
 181  
      */
 182  22
     private Digester digester = null;
 183  
 
 184  
 
 185  
     /**
 186  
      * <p>Return a fully configured <code>Digester</code> instance.</p>
 187  
      */
 188  
     private Digester digester() {
 189  
 
 190  
         // Return any previously created instance
 191  111
         if (digester != null) {
 192  90
             return digester;
 193  
         }
 194  
 
 195  
         // Create and configure a new Digester instance
 196  21
         digester = new Digester();
 197  21
         digester.setNamespaceAware(false);
 198  21
         digester.setUseContextClassLoader(true);
 199  21
         digester.setValidating(isValidating());
 200  
 
 201  
         // Register our local copy of DTDs we recognize
 202  63
         for (int i = 0; i < REGISTRATIONS.length; i += 2) {
 203  42
             URL url = this.getClass().getResource(REGISTRATIONS[i + 1]);
 204  42
             digester.register(REGISTRATIONS[i], url.toString());
 205  
         }
 206  
 
 207  
         // Configure processing rules
 208  21
         RuleSet ruleSet = null;
 209  21
         ruleSet = new ManagedBeansRuleSet();
 210  21
         ruleSet.addRuleInstances(digester);
 211  
 
 212  
         // Return the configured instance
 213  21
         return digester;
 214  
 
 215  
     }
 216  
 
 217  
 
 218  
 }