001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.chain.config;
017:
018: import org.apache.commons.chain.Catalog;
019: import org.apache.commons.chain.CatalogFactory;
020: import org.apache.commons.digester.Rule;
021: import org.xml.sax.Attributes;
022:
023: /**
024: * <p>Digester rule that will cause the top-most element on the Digester
025: * stack (if it is a {@link Catalog} to be registered with the
026: * {@link CatalogFactory} instance for our application. If the attribute
027: * specified to our constructor has a value, that will be used as the name
028: * under which to register this {@link Catalog}. Otherwise, this will
029: * become the default {@link Catalog} for this application.</p>
030: *
031: * @author Craig R. McClanahan
032: * @version $Revision: 155403 $ $Date: 2005-02-26 12:52:46 +0000 (Sat, 26 Feb 2005) $
033: */
034: class ConfigCatalogRule extends Rule {
035:
036: // ----------------------------------------------------------- Constructors
037:
038: /**
039: * <p>Construct a new instance of this rule that looks for an attribute
040: * with the specified name.</p>
041: *
042: * @param nameAttribute Name of the attribute containing the name under
043: * which this command should be registered
044: * @param catalogClass Name of the implementation class for newly
045: * created {@link Catalog} instances
046: */
047: public ConfigCatalogRule(String nameAttribute, String catalogClass) {
048: super ();
049: this .nameAttribute = nameAttribute;
050: this .catalogClass = catalogClass;
051: }
052:
053: // ----------------------------------------------------- Instance Variables
054:
055: /**
056: * <p>The fully qualified class name of a {@link Catalog} class to use for
057: * instantiating new instances.</p>
058: */
059: private String catalogClass = null;
060:
061: /**
062: * <p>The name of the attribute under which we can retrieve the name
063: * this catalog should be registered with (if any).</p>
064: */
065: private String nameAttribute = null;
066:
067: // --------------------------------------------------------- Public Methods
068:
069: /**
070: * <p>Retrieve or create a {@link Catalog} with the name specified by
071: * the <code>nameAttribute</code> attribute, or the default {@link Catalog}
072: * if there is no such attribute defined. Push it onto the top of the
073: * stack.</p>
074: *
075: * @param namespace the namespace URI of the matching element, or an
076: * empty string if the parser is not namespace aware or the element has
077: * no namespace
078: * @param name the local name if the parser is namespace aware, or just
079: * the element name otherwise
080: * @param attributes The attribute list of this element
081: */
082: public void begin(String namespace, String name,
083: Attributes attributes) throws Exception {
084:
085: // Retrieve any current Catalog with the specified name
086: Catalog catalog = null;
087: CatalogFactory factory = CatalogFactory.getInstance();
088: String nameValue = attributes.getValue(nameAttribute);
089: if (nameValue == null) {
090: catalog = factory.getCatalog();
091: } else {
092: catalog = factory.getCatalog(nameValue);
093: }
094:
095: // Create and register a new Catalog instance if necessary
096: if (catalog == null) {
097: Class clazz = digester.getClassLoader().loadClass(
098: catalogClass);
099: catalog = (Catalog) clazz.newInstance();
100: if (nameValue == null) {
101: factory.setCatalog(catalog);
102: } else {
103: factory.addCatalog(nameValue, catalog);
104: }
105: }
106:
107: // Push this Catalog onto the top of the stack
108: digester.push(catalog);
109:
110: }
111:
112: }
|