001: /*
002: * $Id: DefinitionsFactory.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: package org.apache.struts.tiles.xmlDefinition;
023:
024: import java.io.Serializable;
025: import java.util.HashMap;
026: import java.util.Iterator;
027: import java.util.Map;
028:
029: import javax.servlet.ServletContext;
030: import javax.servlet.ServletRequest;
031:
032: import org.apache.struts.tiles.ComponentDefinition;
033: import org.apache.struts.tiles.DefinitionsFactoryException;
034: import org.apache.struts.tiles.NoSuchDefinitionException;
035:
036: /**
037: * A factory for definitions.
038: * This factory allows to retrieve definitions by their keys.
039: */
040: public class DefinitionsFactory implements Serializable {
041: /** Underlying map containing all definitions.*/
042: protected Map definitions;
043:
044: /**
045: * Get a definition by its name.
046: * @param name Name of the definition.
047: * @param request Servlet request.
048: * @param servletContext Servlet context.
049: * @throws DefinitionsFactoryException An error occur while getting
050: * definition.
051: * @throws NoSuchDefinitionException No definition found for specified name
052: * Implementation can throw more accurate exception as a subclass of this
053: * exception.
054: */
055: public ComponentDefinition getDefinition(String name,
056: ServletRequest request, ServletContext servletContext)
057: throws NoSuchDefinitionException,
058: DefinitionsFactoryException {
059: return (ComponentDefinition) definitions.get(name);
060: }
061:
062: /**
063: * Put definition in set.
064: * @param definition Definition to put.
065: */
066: public void putDefinition(ComponentDefinition definition) {
067: definitions.put(definition.getName(), definition);
068: }
069:
070: /**
071: * Constructor.
072: * Create a factory initialized with definitions from {@link XmlDefinitionsSet}.
073: * @param xmlDefinitions Resolved definition from XmlDefinitionSet.
074: * @throws NoSuchDefinitionException If an error occurs while resolving inheritance
075: */
076: public DefinitionsFactory(XmlDefinitionsSet xmlDefinitions)
077: throws NoSuchDefinitionException {
078: definitions = new HashMap();
079:
080: // First, resolve inheritance
081: xmlDefinitions.resolveInheritances();
082:
083: // Walk thru xml set and copy each definitions.
084: Iterator i = xmlDefinitions.getDefinitions().values()
085: .iterator();
086: while (i.hasNext()) {
087: XmlDefinition xmlDefinition = (XmlDefinition) i.next();
088: putDefinition(new ComponentDefinition(xmlDefinition));
089: } // end loop
090: }
091:
092: /**
093: * Return String representation.
094: * @return String representation.
095: */
096: public String toString() {
097: return definitions.toString();
098: }
099:
100: }
|