KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > tools > CmsToolMacroResolver


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/tools/CmsToolMacroResolver.java,v $
3  * Date : $Date: 2005/06/27 23:22:07 $
4  * Version: $Revision: 1.2 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.workplace.tools;
33
34 import org.opencms.main.OpenCms;
35 import org.opencms.util.CmsMacroResolver;
36 import org.opencms.util.CmsUUID;
37 import org.opencms.util.I_CmsMacroResolver;
38 import org.opencms.workplace.CmsWorkplace;
39
40 import java.util.Arrays JavaDoc;
41 import java.util.Collections JavaDoc;
42 import java.util.Iterator JavaDoc;
43 import java.util.List JavaDoc;
44
45 /**
46  * Resolves special macros for the admin view.<p>
47  *
48  * Supported macros are:<p>
49  * <ul>
50  * <li>admin.userName|id</li>
51  * <li>admin.groupName|id</li>
52  * <li>admin.jobName|id</li>
53  * <li>admin.projectName|id</li>
54  * </ul><p>
55  *
56  * @author Michael Moossen
57  *
58  * @version $Revision: 1.2 $
59  * @since 6.0.0
60  */

61 public class CmsToolMacroResolver implements I_CmsMacroResolver {
62
63     /** Identifier for admin macros prefix. */
64     public static final String JavaDoc PREFIX_ADMIN = "admin.";
65
66     /** Identifier for admin parameter names. */
67     public static final String JavaDoc KEY_USERNAME = "userName.";
68
69     /** Identifier for admin parameter names. */
70     public static final String JavaDoc KEY_GROUPNAME = "groupName.";
71
72     /** Identifier for admin parameter names. */
73     public static final String JavaDoc KEY_JOBNAME = "jobName.";
74
75     /** Identifier for admin parameter names. */
76     public static final String JavaDoc KEY_PROJECTNAME = "projectName.";
77
78     /** Identified for admin parameter commands. */
79     public static final String JavaDoc[] VALUE_NAME_ARRAY = {KEY_USERNAME, KEY_GROUPNAME, KEY_JOBNAME, KEY_PROJECTNAME};
80
81     /** The admin commands wrapped in a List. */
82     public static final List JavaDoc VALUE_NAMES = Collections.unmodifiableList(Arrays.asList(VALUE_NAME_ARRAY));
83
84     /** The workplace class for falling back, and use the cms context. */
85     private CmsWorkplace m_wp;
86
87     /**
88      * Default private constructor.<p>
89      *
90      * @param wp the workplace instance
91      */

92     public CmsToolMacroResolver(CmsWorkplace wp) {
93
94         m_wp = wp;
95     }
96
97     /**
98      * Resolves the macros in the given input using the provided parameters.<p>
99      *
100      * A macro in the form <code>${key}</code> in the content is replaced with it's assigned value
101      * returned by the <code>{@link I_CmsMacroResolver#getMacroValue(String)}</code> method of the given
102      * <code>{@link I_CmsMacroResolver}</code> instance.<p>
103      *
104      * If a macro is found that can not be mapped to a value by the given macro resolver,
105      * it is left untouched in the input.<p>
106      *
107      * @param input the input in which to resolve the macros
108      * @param wp the workplace class for falling back
109      *
110      * @return the input with the macros resolved
111      */

112     public static String JavaDoc resolveMacros(String JavaDoc input, CmsWorkplace wp) {
113
114         return new CmsToolMacroResolver(wp).resolveMacros(input);
115     }
116
117     /**
118      * @see org.opencms.util.I_CmsMacroResolver#getMacroValue(java.lang.String)
119      */

120     public String JavaDoc getMacroValue(String JavaDoc macro) {
121
122         if (!macro.startsWith(CmsToolMacroResolver.PREFIX_ADMIN)) {
123             // the key is not an admin macro, fallback
124
return m_wp.getMacroResolver().getMacroValue(macro);
125         }
126         macro = macro.substring(CmsToolMacroResolver.PREFIX_ADMIN.length());
127         String JavaDoc id = null;
128         // validate macro command
129
Iterator JavaDoc it = VALUE_NAMES.iterator();
130         while (it.hasNext()) {
131             String JavaDoc cmd = it.next().toString();
132             if (macro.startsWith(cmd)) {
133                 id = macro.substring(cmd.length());
134                 macro = cmd;
135             }
136         }
137         if (id == null) {
138             // macro command not found
139
return null;
140         }
141         try {
142             if (macro == CmsToolMacroResolver.KEY_USERNAME) {
143                 return m_wp.getCms().readUser(new CmsUUID(id)).getName();
144             }
145             if (macro == CmsToolMacroResolver.KEY_GROUPNAME) {
146                 return m_wp.getCms().readGroup(new CmsUUID(id)).getName();
147             }
148             if (macro == CmsToolMacroResolver.KEY_PROJECTNAME) {
149                 return m_wp.getCms().readProject(new Integer JavaDoc(id).intValue()).getName();
150             }
151             if (macro == CmsToolMacroResolver.KEY_JOBNAME) {
152                 return OpenCms.getScheduleManager().getJob(id).getJobName();
153             }
154         } catch (Exception JavaDoc e) {
155             // ignore
156
}
157         return null;
158     }
159
160     /**
161      * Resolves the macros in the given input.<p>
162      *
163      * Calls <code>{@link #resolveMacros(String)}</code> until no more macros can
164      * be resolved in the input. This way "nested" macros in the input are resolved as well.<p>
165      *
166      * @see org.opencms.util.I_CmsMacroResolver#resolveMacros(java.lang.String)
167      */

168     public String JavaDoc resolveMacros(String JavaDoc input) {
169
170         String JavaDoc result = input;
171
172         if (input != null) {
173             String JavaDoc lastResult;
174             do {
175                 // save result for next comparison
176
lastResult = result;
177                 // resolve the macros
178
result = CmsMacroResolver.resolveMacros(result, this);
179                 // if nothing changes then the final result is found
180
} while (!result.equals(lastResult));
181         }
182         // return the result
183
return result;
184     }
185
186     /**
187      * @see org.opencms.util.I_CmsMacroResolver#isKeepEmptyMacros()
188      */

189     public boolean isKeepEmptyMacros() {
190
191         return true;
192     }
193 }
Popular Tags