1 /* 2 * $Id: DefinitionsFactory.java 471754 2006-11-06 14:55:09Z husted $ 3 * 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 package org.apache.struts.tiles.xmlDefinition; 23 24 import java.io.Serializable; 25 import java.util.HashMap; 26 import java.util.Iterator; 27 import java.util.Map; 28 29 import javax.servlet.ServletContext; 30 import javax.servlet.ServletRequest; 31 32 import org.apache.struts.tiles.ComponentDefinition; 33 import org.apache.struts.tiles.DefinitionsFactoryException; 34 import org.apache.struts.tiles.NoSuchDefinitionException; 35 36 /** 37 * A factory for definitions. 38 * This factory allows to retrieve definitions by their keys. 39 */ 40 public class DefinitionsFactory implements Serializable 41 { 42 /** Underlying map containing all definitions.*/ 43 protected Map definitions; 44 45 /** 46 * Get a definition by its name. 47 * @param name Name of the definition. 48 * @param request Servlet request. 49 * @param servletContext Servlet context. 50 * @throws DefinitionsFactoryException An error occur while getting 51 * definition. 52 * @throws NoSuchDefinitionException No definition found for specified name 53 * Implementation can throw more accurate exception as a subclass of this 54 * exception. 55 */ 56 public ComponentDefinition getDefinition(String name, ServletRequest request, ServletContext servletContext) 57 throws NoSuchDefinitionException, DefinitionsFactoryException 58 { 59 return (ComponentDefinition)definitions.get(name); 60 } 61 62 /** 63 * Put definition in set. 64 * @param definition Definition to put. 65 */ 66 public void putDefinition(ComponentDefinition definition) 67 { 68 definitions.put( definition.getName(), definition ); 69 } 70 71 /** 72 * Constructor. 73 * Create a factory initialized with definitions from {@link XmlDefinitionsSet}. 74 * @param xmlDefinitions Resolved definition from XmlDefinitionSet. 75 * @throws NoSuchDefinitionException If an error occurs while resolving inheritance 76 */ 77 public DefinitionsFactory(XmlDefinitionsSet xmlDefinitions) 78 throws NoSuchDefinitionException 79 { 80 definitions = new HashMap(); 81 82 // First, resolve inheritance 83 xmlDefinitions.resolveInheritances(); 84 85 // Walk thru xml set and copy each definitions. 86 Iterator i = xmlDefinitions.getDefinitions().values().iterator(); 87 while( i.hasNext() ) 88 { 89 XmlDefinition xmlDefinition = (XmlDefinition)i.next(); 90 putDefinition( new ComponentDefinition( xmlDefinition) ); 91 } // end loop 92 } 93 /** 94 * Return String representation. 95 * @return String representation. 96 */ 97 public String toString() 98 { 99 return definitions.toString(); 100 } 101 102 }