Home » commons-chain-1.2-src » org.apache.commons » chain » config » [javadoc | source]
    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   package org.apache.commons.chain.config;
   18   
   19   
   20   import org.apache.commons.chain.Catalog;
   21   import org.apache.commons.chain.CatalogFactory;
   22   import org.apache.commons.digester.Rule;
   23   import org.xml.sax.Attributes;
   24   
   25   
   26   /**
   27    * <p>Digester rule that will cause the top-most element on the Digester
   28    * stack (if it is a {@link Catalog} to be registered with the
   29    * {@link CatalogFactory} instance for our application.  If the attribute
   30    * specified to our constructor has a value, that will be used as the name
   31    * under which to register this {@link Catalog}.  Otherwise, this will
   32    * become the default {@link Catalog} for this application.</p>
   33    *
   34    * @author Craig R. McClanahan
   35    * @version $Revision: 480477 $ $Date: 2006-11-29 08:34:52 +0000 (Wed, 29 Nov 2006) $
   36    */
   37   class ConfigCatalogRule extends Rule {
   38   
   39   
   40       // ----------------------------------------------------------- Constructors
   41   
   42   
   43       /**
   44        * <p>Construct a new instance of this rule that looks for an attribute
   45        * with the specified name.</p>
   46        *
   47        * @param nameAttribute Name of the attribute containing the name under
   48        *  which this command should be registered
   49        * @param catalogClass Name of the implementation class for newly
   50        *  created {@link Catalog} instances
   51        */
   52       public ConfigCatalogRule(String nameAttribute, String catalogClass) {
   53           super();
   54           this.nameAttribute = nameAttribute;
   55           this.catalogClass = catalogClass;
   56       }
   57   
   58   
   59       // ----------------------------------------------------- Instance Variables
   60   
   61   
   62       /**
   63        * <p>The fully qualified class name of a {@link Catalog} class to use for
   64        * instantiating new instances.</p>
   65        */
   66       private String catalogClass = null;
   67   
   68   
   69       /**
   70        * <p>The name of the attribute under which we can retrieve the name
   71        * this catalog should be registered with (if any).</p>
   72        */
   73       private String nameAttribute = null;
   74   
   75   
   76       // --------------------------------------------------------- Public Methods
   77   
   78   
   79       /**
   80        * <p>Retrieve or create a {@link Catalog} with the name specified by
   81        * the <code>nameAttribute</code> attribute, or the default {@link Catalog}
   82        * if there is no such attribute defined.  Push it onto the top of the
   83        * stack.</p>
   84        *
   85        * @param namespace the namespace URI of the matching element, or an
   86        *   empty string if the parser is not namespace aware or the element has
   87        *   no namespace
   88        * @param name the local name if the parser is namespace aware, or just
   89        *   the element name otherwise
   90        * @param attributes The attribute list of this element
   91        */
   92       public void begin(String namespace, String name, Attributes attributes)
   93           throws Exception {
   94   
   95           // Retrieve any current Catalog with the specified name
   96           Catalog catalog = null;
   97           CatalogFactory factory = CatalogFactory.getInstance();
   98           String nameValue = attributes.getValue(nameAttribute);
   99           if (nameValue == null) {
  100               catalog = factory.getCatalog();
  101           } else {
  102               catalog = factory.getCatalog(nameValue);
  103           }
  104   
  105           // Create and register a new Catalog instance if necessary
  106           if (catalog == null) {
  107               Class clazz = digester.getClassLoader().loadClass(catalogClass);
  108               catalog = (Catalog) clazz.newInstance();
  109               if (nameValue == null) {
  110                   factory.setCatalog(catalog);
  111               } else {
  112                   factory.addCatalog(nameValue, catalog);
  113               }
  114           }
  115   
  116           // Push this Catalog onto the top of the stack
  117           digester.push(catalog);
  118   
  119       }
  120   
  121   
  122   }

Save This Page
Home » commons-chain-1.2-src » org.apache.commons » chain » config » [javadoc | source]