1 /*
2 * $Id$
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
27 import jakarta.servlet.ServletContext;
28 import jakarta.servlet.ServletRequest;
29
30 import org.apache.struts.tiles.ComponentDefinition;
31 import org.apache.struts.tiles.DefinitionsFactoryException;
32 import org.apache.struts.tiles.NoSuchDefinitionException;
33
34 /**
35 * A factory for definitions.
36 * This factory allows to retrieve definitions by their keys.
37 */
38 public class DefinitionsFactory implements Serializable
39 {
40 private static final long serialVersionUID = -1606414944612676291L;
41
42 /** Underlying map containing all definitions.*/
43 protected HashMap<String, ComponentDefinition> 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 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 for( XmlDefinition xmlDefinition : xmlDefinitions.getDefinitions().values() )
87 {
88 putDefinition( new ComponentDefinition( xmlDefinition) );
89 } // end loop
90 }
91 /**
92 * Return String representation.
93 * @return String representation.
94 */
95 public String toString()
96 {
97 return definitions.toString();
98 }
99 }