KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > util > core > DefinitionUtil


1 /*
2  * Copyright 2005 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.webapp.util.core;
17
18 import com.blandware.atleap.webapp.struts.HeritableComponentDefinition;
19 import com.blandware.atleap.webapp.struts.HeritableI18nFactorySet;
20 import org.apache.struts.tiles.DefinitionsFactoryException;
21 import org.apache.struts.tiles.TilesUtil;
22 import org.apache.struts.tiles.definition.ComponentDefinitionsFactoryWrapper;
23
24 import javax.servlet.ServletContext JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import java.util.*;
27
28 /**
29  * <p>
30  * Contains utilities to work with definitions
31  * </p>
32  * <p><a HREF="DefinitionUtil.java.htm"><i>View source</i></a></p>
33  *
34  * @author Roman Puchkovskiy <a HREF="mailto:roman.puchkovskiy@blandware.com">
35  * &lt;roman.puchkovskiy@blandware.com&gt;</a>
36  * @version $Revision: 1.1 $ $Date: 2006/03/16 15:18:04 $
37  */

38 public final class DefinitionUtil {
39     /**
40      * Servlet context key under which instance of this class is saved
41      */

42     private static final String JavaDoc INSTANCE_KEY = "com.blandware.atleap.webapp.util.DefinitionUtil.INSTANCE";
43
44     /**
45      * Creates new instance of DefinitionUtil
46      */

47     private DefinitionUtil() {
48     }
49
50     /**
51      * Returns instance of DefinitionUtil corresponding to a given servlet context
52      *
53      * @param servletContext Servlet context
54      * @return Instance of DefinitionUtil
55      */

56     public static DefinitionUtil getInstance(ServletContext JavaDoc servletContext) {
57         DefinitionUtil definitionUtil = (DefinitionUtil) servletContext.getAttribute(INSTANCE_KEY);
58         if ( definitionUtil == null ) {
59             definitionUtil = new DefinitionUtil();
60             servletContext.setAttribute(INSTANCE_KEY, definitionUtil);
61         }
62         return definitionUtil;
63     }
64
65     /**
66      * Returns names of all definitions
67      *
68      * @param request request
69      * @return list of names
70      */

71     public List getDefinitionsNames(HttpServletRequest JavaDoc request) throws DefinitionsFactoryException {
72         Map definitionsMap = ((HeritableI18nFactorySet) ((ComponentDefinitionsFactoryWrapper) TilesUtil.getDefinitionsFactory(request, request.getSession().getServletContext())).getInternalFactory()).getDefinitions(request, request.getSession().getServletContext());
73         List names = new ArrayList(definitionsMap.keySet());
74         return names;
75     }
76
77     /**
78      * Returns all definitions
79      *
80      * @param request request
81      * @return list of definitions
82      */

83     public List getDefinitions(HttpServletRequest JavaDoc request) throws DefinitionsFactoryException {
84         Map definitionsMap = ((HeritableI18nFactorySet) ((ComponentDefinitionsFactoryWrapper) TilesUtil.getDefinitionsFactory(request, request.getSession().getServletContext())).getInternalFactory()).getDefinitions(request, request.getSession().getServletContext());
85         List definitions = new ArrayList(definitionsMap.values());
86         return definitions;
87     }
88
89     /**
90      * Returns names of all definitions that are descendants of a definition
91      * with a given name
92      *
93      * @param name name of definition for which to obtain descendants
94      * @param request request
95      * @param servletContext servlet context
96      * @return list of descendants names
97      */

98     public List getDescendantDefinitionsNames(String JavaDoc name, HttpServletRequest JavaDoc request, ServletContext JavaDoc servletContext) throws DefinitionsFactoryException {
99         if (name == null) {
100             return new ArrayList();
101         }
102
103         List allDefinitions = getDefinitionsNames(request);
104         // This set contains names of already found descendants
105
Set descendants = new TreeSet();
106         // This set contains names of definitions that are NOT descendants of
107
// given definition
108
Set notDescendants = new TreeSet();
109
110         for (int i = 0; i < allDefinitions.size(); i++) {
111             String JavaDoc definitionName = (String JavaDoc) allDefinitions.get(i);
112             if (!descendants.contains(definitionName) && !notDescendants.contains(definitionName)) {
113                 // Not already in one of sets: check if this is descendant
114
// This set contains definition and its ancestors that still
115
// can be descendants of parent definition
116
Set definitionAndAncestors = new TreeSet();
117                 definitionAndAncestors.add(definitionName);
118                 do {
119                     definitionName = ((HeritableComponentDefinition) TilesUtil.getDefinition(definitionName, request, servletContext)).getExtends();
120                     if (definitionName != null) {
121                         if (name.equals(definitionName) || descendants.contains(definitionName)) {
122                             // Reached parent (or its descendant), add all
123
// passed definitions as descendants
124
descendants.addAll(definitionAndAncestors);
125                             break;
126                         } else if (notDescendants.contains(definitionName)) {
127                             // Reached a definition that is not descendant, add
128
// all passed definitions as NOT descendents
129
notDescendants.addAll(definitionAndAncestors);
130                             break;
131                         } else {
132                             // Proceeding to parent
133
definitionAndAncestors.add(definitionName);
134                         }
135                     }
136                 } while (definitionName != null);
137                 // Reached root, parent wasn't met: this is NOT descendant
138
notDescendants.addAll(definitionAndAncestors);
139             }
140         }
141
142         return new ArrayList(descendants);
143     }
144 }
145
Popular Tags