KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > service > core > impl > UtilManagerImpl


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.service.core.impl;
17
18 import com.blandware.atleap.persistence.core.ContentFieldValueDAO;
19 import com.blandware.atleap.persistence.core.MenuDAO;
20 import com.blandware.atleap.common.util.RegExUtil;
21 import com.blandware.atleap.common.util.ConvertUtil;
22 import com.blandware.atleap.common.Constants;
23 import com.blandware.atleap.model.core.ContentFieldValue;
24 import com.blandware.atleap.model.core.ContentField;
25 import com.blandware.atleap.model.core.MenuItem;
26 import com.blandware.atleap.service.core.UtilManager;
27
28 import java.util.List JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 import org.apache.commons.validator.GenericValidator;
32 import org.apache.oro.text.regex.Perl5Compiler;
33 import org.apache.oro.text.regex.MalformedPatternException;
34
35 /**
36  * Implementation of {@link UtilManager}.
37  *
38  * <p><a HREF="UtilManagerImpl.java.htm"><i>View source</i></a></p>
39  *
40  * @author Roman Puchkovskiy <a HREF="mailto:roman.puchkovskiy@blandware.com">
41  * &lt;roman.puchkovskiy@blandware.com&gt;</a>
42  * @version $Revision: 1.4 $ $Date: 2005/08/27 11:17:09 $
43  */

44 public class UtilManagerImpl extends BaseManagerImpl implements UtilManager {
45
46     /**
47      * Application context path -- needed for correct replacement of URIs
48      */

49     protected String JavaDoc contextPath = null;
50
51     /**
52      * Application host -- needed for correct replacement of URIs
53      */

54     protected String JavaDoc host = null;
55
56     /**
57      * Application HTTP port -- needed for correct replacement of URIs
58      */

59     protected Integer JavaDoc httpPort = null;
60
61     /**
62      * Application HTTPS port -- needed for correct replacement of URIs
63      */

64     protected Integer JavaDoc httpsPort = null;
65
66     /**
67      * DAO to use with content field values
68      */

69     protected ContentFieldValueDAO contentFieldValueDAO;
70
71     /**
72      * DAO to use with menu items
73      */

74     protected MenuDAO menuDAO;
75
76     /**
77      * Sets context path used for correct URI replacing
78      *
79      * @param contextPath path to set
80      */

81     public void setContextPath(String JavaDoc contextPath) {
82         this.contextPath = contextPath;
83     }
84
85     /**
86      * Sets host name used for correct URI replacing
87      *
88      * @param host host name to set
89      */

90     public void setHost(String JavaDoc host) {
91         this.host = host;
92     }
93
94     /**
95      * Sets application HTTP port used for correct URI replacing
96      *
97      * @param httpPort HTTP port to set
98      */

99     public void setHttpPort(Integer JavaDoc httpPort) {
100         this.httpPort = httpPort;
101     }
102
103     /**
104      * Sets application HTTPS port used for correct URI replacing
105      *
106      * @param httpsPort HTTPS port to set
107      */

108     public void setHttpsPort(Integer JavaDoc httpsPort) {
109         this.httpsPort = httpsPort;
110     }
111
112     /**
113      * Sets DAO to use with content field values
114      *
115      * @param contentFieldValueDAO DAO to set
116      */

117     public void setContentFieldValueDAO(ContentFieldValueDAO contentFieldValueDAO) {
118         this.contentFieldValueDAO = contentFieldValueDAO;
119     }
120
121     /**
122      * Sets DAO to use with content field values
123      *
124      * @param menuDAO DAO to set
125      */

126     public void setMenuDAO(MenuDAO menuDAO) {
127         this.menuDAO = menuDAO;
128     }
129
130     /**
131      * Builds a regular expression for URI
132      *
133      * @param uriWithoutSlash prepared URI (currently preparation is just
134      * removing leading slash)
135      * @param postfix postfix of URI; this can be, for instance, a dot
136      * followed with extension, or just an empty string
137      * @param noExtension if <code>true</code>, then no locale suffix
138      * will be allowed
139      * @return regular expression for given URI
140      */

141     protected String JavaDoc buildUriRegex(String JavaDoc uriWithoutSlash, String JavaDoc postfix, boolean noExtension) {
142         return RegExUtil.escapeMetasymbols(uriWithoutSlash)
143                + buildLocaleSuffixRegex(noExtension)
144                + RegExUtil.escapeMetasymbols(postfix);
145     }
146
147     /**
148      * Prepares URI for building regular expression. Currently just removes a
149      * leading slash.
150      *
151      * @param uri URI to be prepared
152      * @return prepared URI
153      */

154     protected String JavaDoc prepareUri(String JavaDoc uri) {
155         String JavaDoc oldUriWithoutSlash;
156         oldUriWithoutSlash = uri;
157         if (oldUriWithoutSlash.startsWith("/")) {
158             oldUriWithoutSlash = oldUriWithoutSlash.substring("/".length());
159         }
160         return oldUriWithoutSlash;
161     }
162
163     /**
164      * Builds a regular expression for locale suffix.
165      *
166      * @param noExtension if <code>true</code>, no locale suffix will be allowed
167      * @return locale suffix regular expression
168      */

169     protected String JavaDoc buildLocaleSuffixRegex(boolean noExtension) {
170         return noExtension ? "()" : "(\\.[a-zA-Z]{2})?";
171     }
172
173     /**
174      * Computes a postfix for URI. If <code>noExtension</code> is true, result
175      * will be empty string, else result will be a dot followed with a common
176      * extension determined from constants.
177      *
178      * @param noExtension whether no extension need to be added
179      * @return URI postfix
180      */

181     protected String JavaDoc computePostfix(boolean noExtension) {
182         String JavaDoc mappingPostfix = Constants.ACTION_MAPPING;
183         if (mappingPostfix.startsWith("*.")) {
184             mappingPostfix = mappingPostfix.substring("*.".length());
185         }
186         String JavaDoc postfix = (noExtension || GenericValidator.isBlankOrNull(mappingPostfix)) ? "" : "." + mappingPostfix;
187         return postfix;
188     }
189
190     /**
191      * Builds a regular expression for RW prefix (which may be inserted between
192      * context path and URI).
193      *
194      * @param rwPrefix RW prefix to use
195      * @return RW prefix regular expression
196      */

197     protected String JavaDoc buildRwPrefixRegex(String JavaDoc rwPrefix) {
198         if (GenericValidator.isBlankOrNull(rwPrefix)) {
199             rwPrefix = "";
200         }
201         if (!"/".equals(rwPrefix) && !"".equals(rwPrefix)) {
202             rwPrefix += "/";
203         } else {
204             rwPrefix = "";
205         }
206         if (rwPrefix.startsWith("/")) {
207             rwPrefix = rwPrefix.substring(1);
208         }
209         String JavaDoc rwPrefixRegex = "(" + RegExUtil.escapeMetasymbols(rwPrefix) + ")?";
210         return rwPrefixRegex;
211     }
212
213     /**
214      * Builds a regular expression for context path.
215      *
216      * @return context path regular expression
217      */

218     protected String JavaDoc buildContextPathRegex() {
219         boolean isContextPathPresent = contextPath != null;
220         String JavaDoc context = contextPath;
221
222         String JavaDoc contextPathRegex = "()";
223         if (isContextPathPresent) {
224             if ("".equals(context)) {
225                 context = "/";
226             }
227             if (!"/".equals(context)) {
228                 context += "/";
229             }
230             contextPathRegex = "(" + RegExUtil.escapeMetasymbols(context) + ")?";
231         }
232         return contextPathRegex;
233     }
234
235     /**
236      * Builds a regular expression for our host. 'Our host' means host name
237      * of server on which we are running with one of ports, that are assigned to
238      * our application (or no port if our port is standard one).
239      *
240      * @return our host regular expression
241      */

242     protected String JavaDoc buildHostRegex() {
243         // TODO: may be it's better to pass port numbers in some other way?
244
int httpPortNumber = httpPort == null ? 80 : httpPort.intValue();
245         int httpsPortNumber = httpsPort == null ? 443 : httpsPort.intValue();
246
247         String JavaDoc httpPortRegex = httpPortNumber == 80 ? "(\\:80)?" : "(\\:" + RegExUtil.escapeMetasymbols(String.valueOf(httpPortNumber)) + ")";
248         String JavaDoc httpsPortRegex = httpsPortNumber == 443 ? "(\\:443)?" : "(\\:" + RegExUtil.escapeMetasymbols(String.valueOf(httpsPortNumber)) + ")";
249         String JavaDoc portRegex = "(" + httpPortRegex + "|" + httpsPortRegex + ")";
250
251         String JavaDoc hostRegex = "((https?\\:\\/\\/" + RegExUtil.escapeMetasymbols(host) + portRegex + ")?)";
252         return hostRegex;
253     }
254
255     /**
256      * Builds a regular expression for query string inside a href attribute,
257      * for example (with leading ? sign).
258      *
259      * @return query string regular expression
260      */

261     protected String JavaDoc buildQueryStringRegex() {
262         return "(\\?[^\"\\s]*)?";
263     }
264
265     /**
266      * @see com.blandware.atleap.service.core.UtilManager#replaceLinkableObjectUriInLinkedObjects(String, String, java.util.List, java.util.List, String, boolean)
267      */

268     public void replaceLinkableObjectUriInLinkedObjects(String JavaDoc oldUri, String JavaDoc newUri,
269                                                         List JavaDoc linkedContentFieldValues,
270                                                         List JavaDoc linkedMenuItems, String JavaDoc rwPrefix,
271                                                         boolean noExtension) {
272
273         String JavaDoc hostRegex = buildHostRegex();
274         String JavaDoc contextPathRegex = buildContextPathRegex();
275         String JavaDoc rwPrefixRegex = buildRwPrefixRegex(rwPrefix);
276
277         // remove leading slashes
278
String JavaDoc oldUriWithoutSlash = prepareUri(oldUri);
279         String JavaDoc newUriWithoutSlash = prepareUri(newUri);
280
281         // compute postfix
282
String JavaDoc postfix = computePostfix(noExtension);
283
284         // regex for URI
285
String JavaDoc uriRegex = buildUriRegex(oldUriWithoutSlash, postfix, noExtension);
286         // regex for whole tag
287
String JavaDoc regex = "(<([\\w]+)\\b[^>]*\\b(href|src|background)=(\\\")?" + hostRegex + contextPathRegex + rwPrefixRegex + ")" + uriRegex + "(" + buildQueryStringRegex() + "(\\\")?[^>]*>)";
288         // replacement string for whole <a> tag
289
String JavaDoc replace = "$1" + newUriWithoutSlash + "$12" + postfix + "$13";
290
291         // process content field values
292
for (Iterator JavaDoc i = linkedContentFieldValues.iterator(); i.hasNext();) {
293             ContentFieldValue value = (ContentFieldValue) i.next();
294             ContentField field = value.getContentField();
295             byte type = field.getType();
296
297             // get content
298
String JavaDoc content = null;
299             if (type == ContentField.LINE_TYPE) {
300                 content = value.getSimpleValue();
301             } else {
302                 content = ConvertUtil.convertToString(value.getValue());
303             }
304             // do replace
305
try {
306                 content = RegExUtil.replaceAll(content, regex, replace, Perl5Compiler.CASE_INSENSITIVE_MASK);
307             } catch (MalformedPatternException e) {
308                 log.error("Bad regexp", e);
309             }
310
311             // set changed content
312
if (type == ContentField.LINE_TYPE) {
313                 value.setSimpleValue(content);
314             } else {
315                 value.setValue(ConvertUtil.convertToByteArray(content));
316             }
317
318             // save content field value
319
contentFieldValueDAO.updateContentFieldValue(value, field, value.getContentLocale());
320         }
321
322         // process menu items
323
for (Iterator JavaDoc i = linkedMenuItems.iterator(); i.hasNext();) {
324             MenuItem item = (MenuItem) i.next();
325             // skip menu items defined in configuration files
326
if (item.isDynamic()) {
327                 String JavaDoc location = item.getLocation();
328                 String JavaDoc image = item.getImage();
329                 String JavaDoc altImage = item.getAltImage();
330                 if (!GenericValidator.isBlankOrNull(location)) {
331                     try {
332                         if (location != null) {
333                             location = RegExUtil.replaceAll(location, "(" + hostRegex + contextPathRegex + rwPrefixRegex + ")" + uriRegex, "$1" + newUriWithoutSlash + "$9" + postfix, Perl5Compiler.CASE_INSENSITIVE_MASK);
334                         }
335                         String JavaDoc uriRegexWithoutExt = RegExUtil.escapeMetasymbols(oldUriWithoutSlash);
336                         if (image != null) {
337                             image = RegExUtil.replaceAll(image, "(" + hostRegex + contextPathRegex + rwPrefixRegex + ")" + uriRegexWithoutExt, "$1" + newUriWithoutSlash, Perl5Compiler.CASE_INSENSITIVE_MASK);
338                         }
339                         if (altImage != null) {
340                             altImage = RegExUtil.replaceAll(altImage, "(" + hostRegex + contextPathRegex + rwPrefixRegex + ")" + uriRegexWithoutExt, "$1" + newUriWithoutSlash, Perl5Compiler.CASE_INSENSITIVE_MASK);
341                         }
342                     } catch (MalformedPatternException e) {
343                         log.error("Bad regexp", e);
344                     }
345                     item.setLocation(location);
346                     item.setImage(image);
347                     item.setAltImage(altImage);
348                     menuDAO.updateMenuItem(item, item.getParentItem(), item.getOwner());
349                 }
350             }
351         }
352     }
353
354     /**
355      * @see com.blandware.atleap.service.core.UtilManager#replaceImageSizesInLinkedObjects(String,java.util.List,int,int,int,int,String,boolean)
356      */

357     public void replaceImageSizesInLinkedObjects(String JavaDoc uri, List JavaDoc linkedContentFieldValues,
358                                                  int oldWidth, int oldHeight,
359                                                  int newWidth, int newHeight,
360                                                  String JavaDoc rwPrefix, boolean noExtension) {
361         String JavaDoc hostRegex = buildHostRegex();
362         String JavaDoc contextPathRegex = buildContextPathRegex();
363         String JavaDoc rwPrefixRegex = buildRwPrefixRegex(rwPrefix);
364
365         // remove leading slash
366
String JavaDoc uriWithoutSlash = prepareUri(uri);
367
368         // compute postfix
369
String JavaDoc postfix = computePostfix(noExtension);
370
371         // regex for URI
372
String JavaDoc uriRegex = buildUriRegex(uriWithoutSlash, postfix, noExtension);
373         // regex for whole URL (without query string!)
374
String JavaDoc urlRegex = hostRegex + contextPathRegex + rwPrefixRegex + uriRegex;
375         // regex for part of tag that is before some attribute
376
String JavaDoc betweenAttrsRegex = "[^>]*\\b";
377         // regex for query string
378
String JavaDoc queryStringRegex = buildQueryStringRegex();
379         // regex for whole <img> tag
380
String JavaDoc widthRegexL = "(<img\\b" + betweenAttrsRegex + ")width=\\\"?" + oldWidth + "\\\"?(" + betweenAttrsRegex + "src=\\\"?" + urlRegex + queryStringRegex + "\\\"?[^>]*>)";
381         String JavaDoc widthReplaceL = "$1width=\"" + newWidth + "\"$2";
382         String JavaDoc widthRegexR = "(<img\\b" + betweenAttrsRegex + "src=\\\"?" + urlRegex + queryStringRegex + "\\\"?" + betweenAttrsRegex + ")width=\\\"?" + oldWidth + "\\\"?(" + "[^>]*>)";
383         String JavaDoc widthReplaceR = "$1width=\"" + newWidth + "\"$11";
384         String JavaDoc heightRegexL = "(<img\\b" + betweenAttrsRegex + ")height=\\\"?" + oldHeight + "\\\"?(" + betweenAttrsRegex + "src=\\\"?" + urlRegex + queryStringRegex + "\\\"?[^>]*>)";
385         String JavaDoc heightReplaceL = "$1height=\"" + newHeight + "\"$2";
386         String JavaDoc heightRegexR = "(<img\\b" + betweenAttrsRegex + "src=\\\"?" + urlRegex + queryStringRegex + "\\\"?" + betweenAttrsRegex + ")height=\\\"?" + oldHeight + "\\\"?(" + "[^>]*>)";
387         String JavaDoc heightReplaceR = "$1height=\"" + newHeight + "\"$11";
388
389         // replace sizes for each linked CFV
390
for (Iterator JavaDoc i = linkedContentFieldValues.iterator(); i.hasNext();) {
391             ContentFieldValue contentFieldValue = (ContentFieldValue) i.next();
392             ContentField contentField = contentFieldValue.getContentField();
393             byte type = contentField.getType();
394             String JavaDoc content = null;
395             if (type == ContentField.LINE_TYPE) {
396                 content = contentFieldValue.getSimpleValue();
397             } else {
398                 content = ConvertUtil.convertToString(contentFieldValue.getValue());
399             }
400             try {
401                 content = RegExUtil.replaceAll(content, widthRegexL, widthReplaceL, Perl5Compiler.CASE_INSENSITIVE_MASK);
402                 content = RegExUtil.replaceAll(content, widthRegexR, widthReplaceR, Perl5Compiler.CASE_INSENSITIVE_MASK);
403                 content = RegExUtil.replaceAll(content, heightRegexL, heightReplaceL, Perl5Compiler.CASE_INSENSITIVE_MASK);
404                 content = RegExUtil.replaceAll(content, heightRegexR, heightReplaceR, Perl5Compiler.CASE_INSENSITIVE_MASK);
405             } catch (MalformedPatternException e) {
406                 log.error("Bad regexp", e);
407             }
408             if (type == ContentField.LINE_TYPE) {
409                 contentFieldValue.setSimpleValue(content);
410             } else {
411                 contentFieldValue.setValue(ConvertUtil.convertToByteArray(content));
412             }
413             contentFieldValueDAO.updateContentFieldValue(contentFieldValue, contentField, contentFieldValue.getContentLocale());
414         }
415     }
416 }
417
Popular Tags