KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > applicationResource > ListApplicationResourcesAction


1 /*
2  * Copyright 2004 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.action.core.applicationResource;
17
18 import com.blandware.atleap.common.util.Folder;
19 import com.blandware.atleap.common.util.FolderList;
20 import com.blandware.atleap.common.util.RegExUtil;
21 import com.blandware.atleap.webapp.action.core.grid.BaseGridAction;
22 import com.blandware.atleap.webapp.util.core.ApplicationResources;
23 import com.blandware.atleap.webapp.util.core.VirtualFolder;
24 import com.blandware.atleap.webapp.util.core.WebappConstants;
25 import org.apache.oro.text.regex.MatchResult;
26 import org.apache.oro.text.regex.Pattern;
27 import org.apache.oro.text.regex.PatternCompiler;
28 import org.apache.oro.text.regex.PatternMatcher;
29 import org.apache.oro.text.regex.Perl5Compiler;
30 import org.apache.oro.text.regex.Perl5Matcher;
31 import org.apache.struts.action.ActionForm;
32 import org.apache.struts.action.ActionForward;
33 import org.apache.struts.action.ActionMapping;
34
35 import javax.servlet.http.HttpServletRequest JavaDoc;
36 import javax.servlet.http.HttpServletResponse JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.LinkedList JavaDoc;
40 import java.util.List JavaDoc;
41 import java.util.SortedSet JavaDoc;
42 import java.util.TreeSet JavaDoc;
43
44 /**
45  * <p>Forward to the page with list of action pages
46  * </p>
47  * <p><a HREF="ListApplicationResourcesAction.java.htm"><i>View Source</i></a></p>
48  * <p/>
49  *
50  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
51  * @version $Revision: 1.6 $ $Date: 2006/03/10 17:10:15 $
52  * @struts.action path="/core/applicationResource/list"
53  * name="applicationResourceForm"
54  * validate="false"
55  * roles="core-applicationResource-list"
56  * @struts.action-forward name="listApplicationResources"
57  * path=".core.applicationResource.list"
58  */

59 public final class ListApplicationResourcesAction extends BaseGridAction {
60     /**
61      * @param mapping The ActionMapping used to select this instance
62      * @param form The optional ActionForm bean for this request (if any)
63      * @param request The HTTP request we are proceeding
64      * @param response The HTTP response we are creating
65      * @return an ActionForward instance describing where and how
66      * control should be forwarded, or null if response
67      * has already been completed
68      */

69     public ActionForward execute(ActionMapping mapping, ActionForm form,
70                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
71
72         String JavaDoc currentFolder = "";
73         if ( request.getParameter("currentFolder") != null ) {
74             currentFolder = request.getParameter("currentFolder");
75             request.getSession().setAttribute(WebappConstants.APPLICATION_RESOURCES_CURRENT_PATH_KEY, currentFolder);
76         } else if ( request.getSession().getAttribute(WebappConstants.APPLICATION_RESOURCES_CURRENT_PATH_KEY) != null ) {
77             currentFolder = (String JavaDoc) request.getSession().getAttribute(WebappConstants.APPLICATION_RESOURCES_CURRENT_PATH_KEY);
78         }
79
80         if ( currentFolder == null ) {
81             currentFolder = "";
82         }
83
84         ApplicationResources applicationResources = ApplicationResources.getInstance(servlet.getServletContext());
85
86         // algorithm used to extract folders from set of keys:
87
// 1. if key contains more than one dot after entry of current folder, sequence of symbols between first
88
// and second dots will be recognized as folder name
89
// 2. if key contains only one dot after entry of current folder, it will be recognized as key in current folder
90
// 3. if key does not start with current folder, it will be skipped
91

92         // ensure, that current folder does not end with dot
93
if ( currentFolder.length() > 0 && currentFolder.endsWith(".") ) {
94             currentFolder = currentFolder.substring(0, currentFolder.length() - 1);
95         }
96
97         // pattern to determine whether or not we have an additional folder
98
// if current folder is not an empty string, key must start from it and have at least two dots
99
// otherwise one dot is enough
100
String JavaDoc folderPatternString = "";
101         if ( currentFolder.length() > 0 ) {
102             folderPatternString = "(" + RegExUtil.escapeMetasymbols(currentFolder) + ")\\.";
103         }
104         folderPatternString += "([^\\.]+)(\\.[^\\.]+)+";
105
106         // patter to determine, whether or not we have key
107
// if current folder is not an empty string, key must start from it and have at least one dot
108
// otherwise, no dot is required
109
String JavaDoc keyPatternString = "";
110         if ( currentFolder.length() > 0 ) {
111             keyPatternString = "(" + RegExUtil.escapeMetasymbols(currentFolder) + ")\\.";
112         }
113         keyPatternString += "([^\\.]+)";
114
115         // pattern compiler instance to use
116
PatternCompiler compiler = new Perl5Compiler();
117
118         // compiled patterns
119
Pattern folderPattern = compiler.compile(folderPatternString);
120         Pattern keyPattern = compiler.compile(keyPatternString);
121
122         // pattern matcher instance to use
123
PatternMatcher matcher = new Perl5Matcher();
124
125         // list of folders
126
SortedSet JavaDoc folderSet = new TreeSet JavaDoc();
127
128         // list of keys
129
List keys = new ArrayList JavaDoc(applicationResources.getKeys());
130
131         for ( Iterator JavaDoc i = keys.iterator(); i.hasNext(); ) {
132             String JavaDoc key = (String JavaDoc) i.next();
133
134             // check folder name
135
if ( matcher.matches(key, folderPattern) ) {
136                 MatchResult result = matcher.getMatch();
137
138                 // according to pattern, if current folder is not an empty string, folder name is in second group
139
// otherwise it is in the first group;
140
String JavaDoc folderName = currentFolder.length() > 0 ? result.group(2) : result.group(1);
141                 Folder folder = new VirtualFolder(folderName, currentFolder, '.');
142                 folderSet.add(folder);
143
144                 // remove this key from list
145
i.remove();
146             } else if ( !matcher.matches(key, keyPattern) ) {
147                 // remove key, because it is placed outside of current folder
148
i.remove();
149             }
150         }
151
152         // create parent folder if current folder is not the root folder
153
Folder parentFolder = null;
154         if ( currentFolder.length() > 0 ) {
155             int k = currentFolder.lastIndexOf(".");
156             if ( k < 0 ) {
157                 k = 0;
158             }
159             String JavaDoc parentFolderPath = currentFolder.substring(0, k);
160             parentFolder = new VirtualFolder("..", parentFolderPath, '.');
161         }
162
163         LinkedList JavaDoc folders = new LinkedList JavaDoc(folderSet);
164         if ( parentFolder != null ) {
165             folders.addFirst(parentFolder);
166         }
167
168         request.setAttribute(WebappConstants.APPLICATION_RESOURCES_COLLECTION_KEY, keys);
169         request.setAttribute(WebappConstants.FOLDERS_COLLECTION_KEY, folders);
170         request.setAttribute(WebappConstants.CURRENT_PATH_KEY, new FolderList(VirtualFolder.splitPath(currentFolder, '.', false), '.'));
171
172         // save transaction token in request
173
saveToken(request);
174         return mapping.findForward("listApplicationResources");
175     }
176 }
Popular Tags