KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > util > CmsResourceTranslator


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/util/CmsResourceTranslator.java,v $
3  * Date : $Date: 2006/03/27 14:52:41 $
4  * Version: $Revision: 1.23 $
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.util;
33
34 import org.opencms.main.CmsLog;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.oro.text.PatternCache;
38 import org.apache.oro.text.PatternCacheFIFO;
39 import org.apache.oro.text.perl.MalformedPerl5PatternException;
40 import org.apache.oro.text.perl.Perl5Util;
41 import org.apache.oro.text.regex.MalformedPatternException;
42
43 /**
44  * This class provides a resource name translation facility.<p>
45  *
46  * Resource name translation is required for backward compatibility
47  * to the old OpenCms (pre 5.0 beta 2) directory layout.
48  * It can also be used to translate special characters or
49  * names automatically.<p>
50  *
51  * It is also used for translating new resource names that contain
52  * illegal chars to legal names. This feature is most useful (and currently
53  * only used) for uploaded files. It is also applied to uploded ZIP directories
54  * that are extracted after upload.<p>
55  *
56  * The translations can be configured in the opencms.properties.<p>
57  *
58  * The default directory translation setting is:<br>
59  * <pre>
60  * directory.translation.rules=s#/default/vfs/content/bodys/(.*)#/default/vfs/system/bodies/$1#, \
61  * s#/default/vfs/pics/system/(.*)#/default/vfs/system/workplace/resources/$1#, \
62  * s#/default/vfs/pics/(.*)#/default/vfs/system/galleries/pics/$1#, \
63  * s#/default/vfs/download/(.*)#/default/vfs/system/galleries/download/$1#, \
64  * s#/default/vfs/externallinks/(.*)#/default/vfs/system/galleries/externallinks/$1#, \
65  * s#/default/vfs/htmlgalleries/(.*)#/default/vfs/system/galleries/htmlgalleries/$1#, \
66  * s#/default/vfs/content/(.*)#/default/vfs/system/modules/default/$1#, \
67  * s#/default/vfs/moduledemos/(.*)#/default/vfs/system/moduledemos/$1#, \
68  * s#/default/vfs/system/workplace/config/language/(.*)#/default/vfs/system/workplace/locales/$1#, \
69  * s#/default/vfs/system/workplace/css/(.*)#/default/vfs/system/workplace/resources/$1#, \
70  * s#/default/vfs/system/workplace/templates/js/(.*)#/default/vfs/system/workplace/scripts/$1#
71  * </pre><p>
72  *
73  * The default file name translation setting is:<br>
74  * <pre>
75  * filename.translation.rules=s#[\s]+#_#g, \
76  * s#\\#/#g, \
77  * s#[^0-9a-zA-Z_\.\-\/]#!#g, \
78  * s#!+#x#g
79  * </pre><p>
80  *
81  * @author Alexander Kandzior
82  *
83  * @version $Revision: 1.23 $
84  *
85  * @since 6.0.0
86  */

87 public class CmsResourceTranslator {
88
89     /** The log object for this class. */
90     private static final Log LOG = CmsLog.getLog(CmsResourceTranslator.class);
91
92     /** Flag to indicate if one or more matchings should be tried. */
93     private boolean m_continueMatching;
94
95     /** Perl5 patter cache to avoid unecessary re-parsing of properties. */
96     private PatternCache m_perlPatternCache;
97
98     /** Perl5 utility class. */
99     private Perl5Util m_perlUtil;
100
101     /** Internal array containing the translations from opencms.properties. */
102     private String JavaDoc[] m_translations;
103
104     /**
105      * Constructor for the CmsResourceTranslator.
106      *
107      * @param translations The array of translations read from the
108      * opencms,properties
109      * @param continueMatching if <code>true</code>, matching will continue after
110      * the first match was found
111      */

112     public CmsResourceTranslator(String JavaDoc[] translations, boolean continueMatching) {
113
114         super();
115         m_translations = translations;
116         m_continueMatching = continueMatching;
117         // pre-cache the patterns
118
m_perlPatternCache = new PatternCacheFIFO(m_translations.length + 1);
119         for (int i = 0; i < m_translations.length; i++) {
120             try {
121                 m_perlPatternCache.addPattern(m_translations[i]);
122             } catch (MalformedPatternException e) {
123                 LOG.error(
124                     Messages.get().getBundle().key(Messages.LOG_MALFORMED_TRANSLATION_RULE_1, m_translations[i]),
125                     e);
126             }
127         }
128         // initialize the Perl5Util
129
m_perlUtil = new Perl5Util(m_perlPatternCache);
130         if (LOG.isInfoEnabled()) {
131             LOG.info(Messages.get().getBundle().key(
132                 Messages.LOG_NUM_TRANSLATION_RULES_INITIALIZED_1,
133                 new Integer JavaDoc(translations.length)));
134         }
135     }
136
137     /**
138      * Returns a copy of the initialized translation rules.<p>
139      *
140      * @return String[] a copy of the initialized translation rules
141      */

142     public String JavaDoc[] getTranslations() {
143
144         String JavaDoc[] copy = new String JavaDoc[m_translations.length];
145         System.arraycopy(m_translations, 0, copy, 0, m_translations.length);
146         return copy;
147     }
148
149     /**
150      * Translate a resource name according to the expressions set in
151      * the opencms.properties. If no match is found,
152      * the resource name is returned unchanged.<p>
153      *
154      * @param resourceName The resource name to translate
155      * @return The translated name of the resource
156      */

157     public String JavaDoc translateResource(String JavaDoc resourceName) {
158
159         if (m_translations.length == 0) {
160             // no translations defined
161
return resourceName;
162         }
163         if (resourceName == null) {
164             return null;
165         }
166
167         StringBuffer JavaDoc result;
168         String JavaDoc current = resourceName;
169         int size = current.length() * 2;
170
171         for (int i = 0; i < m_translations.length; i++) {
172             result = new StringBuffer JavaDoc(size);
173             try {
174                 if (m_perlUtil.substitute(result, m_translations[i], current) != 0) {
175
176                     if (m_continueMatching) {
177                         // continue matching
178
current = result.toString();
179                     } else {
180                         // first pattern matched, return the result
181
if (LOG.isDebugEnabled()) {
182                             LOG.debug(Messages.get().getBundle().key(
183                                 Messages.LOG_TRANSLATION_MATCH_3,
184                                 new Integer JavaDoc(i),
185                                 resourceName,
186                                 result));
187                         }
188                         // Return first match result
189
return result.toString();
190                     }
191                 }
192             } catch (MalformedPerl5PatternException e) {
193                 LOG.error(
194                     Messages.get().getBundle().key(Messages.LOG_MALFORMED_TRANSLATION_RULE_1, m_translations[i]),
195                     e);
196             }
197         }
198
199         // the pattern matched, return the result
200
if (LOG.isDebugEnabled()) {
201             LOG.debug(Messages.get().getBundle().key(Messages.LOG_TRANSLATION_MATCH_2, resourceName, current));
202         }
203         // return last translation (or original if no matching translation found)
204
return current;
205     }
206 }
Popular Tags