Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/opencms/flex/util/CmsResourceTranslator.java


1   /*
2    * File   : $Source: /usr/local/cvs/opencms/src/com/opencms/flex/util/Attic/CmsResourceTranslator.java,v $
3    * Date   : $Date: 2003/03/25 17:04:49 $
4    * Version: $Revision: 1.8 $
5    *
6    * This library is part of OpenCms -
7    * the Open Source Content Mananagement System
8    *
9    * Copyright (C) 2002 - 2003 Alkacon Software (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, 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 com.opencms.flex.util;
33  
34  import com.opencms.boot.I_CmsLogChannels;
35  import com.opencms.core.A_OpenCms;
36  
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 (a.kandzior@alkacon.com)
82   * @version $Revision: 1.8 $
83   * @since 5.0 beta 2
84   */
85  public class CmsResourceTranslator implements I_CmsLogChannels {
86  
87      /** Internal array containing the translations from opencms.properties */
88      private String[] m_translations = null;
89      
90      /** Perl5 utility class */
91      private Perl5Util m_perlUtil = null;
92      
93      /** Perl5 patter cache to avoid unecessary re-parsing of properties */
94      private PatternCache m_perlPatternCache = null;   
95      
96      /** Flag to indicate if one or more matchings should be tried */
97      private boolean m_continueMatching = false;
98      
99      /** DEBUG flag */
100     private static final int DEBUG = 0;
101     
102     /**
103      * Constructor for the CmsResourceTranslator.
104      * 
105      * @param translations The array of translations read from the 
106      *    opencms,properties
107      */
108     public CmsResourceTranslator(String[] translations, boolean continueMatching) {
109         super();
110         m_translations = translations;
111         m_continueMatching = continueMatching;
112         // Pre-cache the patterns 
113         m_perlPatternCache = new PatternCacheFIFO(m_translations.length+1);
114         for(int i=0; i<m_translations.length; i++) {
115             try {
116                 m_perlPatternCache.addPattern(m_translations[i]);
117             } catch(MalformedPatternException e){
118                 if(C_LOGGING && A_OpenCms.isLogging(C_OPENCMS_CRITICAL) ) {
119                     A_OpenCms.log(C_OPENCMS_CRITICAL, "["+this.getClass().getName()+"] Malformed resource translation rule:\""+m_translations[i]+"\"");
120                 }
121             }
122         }        
123         // Initialize the Perl5Util
124         m_perlUtil = new Perl5Util(m_perlPatternCache);
125         if (DEBUG > 0) System.out.println("["+this.getClass().getName()+"] Resource translation: Iinitialized " + translations.length + " rules.");        
126         if(C_LOGGING && A_OpenCms.isLogging(C_OPENCMS_INFO) ) {
127             A_OpenCms.log(C_OPENCMS_INIT, ". Resource translation : " + translations.length + " rules initialized");
128         }          
129     }    
130     
131     /**
132      * Translate a resource name according to the expressions set in 
133      * the opencms.properties. If no match is found, 
134      * the resource name is returned unchanged.<p>
135      * 
136      * @param resourceName The resource name to translate
137      * @return The translated name of the resource
138      */
139     public String translateResource(String resourceName) {  
140         if (resourceName == null) return null;      
141         // Check all translations in the list
142         if (DEBUG > 1) System.out.println("["+this.getClass().getName()+"] Resource Translation: Checking: " + resourceName);
143         StringBuffer result;
144         for(int i=0; i<m_translations.length; i++) {
145             result = new StringBuffer();
146             try {
147                 if(m_perlUtil.substitute(result, m_translations[i], resourceName) != 0) {
148                     // The pattern matched, return the result
149                     if(C_LOGGING && A_OpenCms.isLogging(C_OPENCMS_INFO) ) {
150                         A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_INFO, "["+this.getClass().getName()+"] Resource translation: " + resourceName + " --> " + result);
151                     }                    
152                     if (DEBUG > 0) {
153                         System.out.println("Translation: " + resourceName + "\n        ---> " + result + "\n");
154                     }
155                     if (m_continueMatching) {
156                         // Continue matching
157                         resourceName = result.toString();
158                     } else {
159                         // Return first match result
160                         return result.toString();
161                     }
162                     
163                 }
164             } catch(MalformedPerl5PatternException e){
165                 if(C_LOGGING && A_OpenCms.isLogging(C_OPENCMS_CRITICAL) ) {
166                     A_OpenCms.log(C_OPENCMS_CRITICAL, "["+this.getClass().getName()+"] Malformed resource translation rule:\""+m_translations[i]+"\"");
167                 }
168             }
169         }
170         // Return last translation (or original if no matching translation found)
171         return resourceName;
172     }
173     
174     /**
175      * Returns a copy of the initialized translation rules.<p>
176      * 
177      * @return String[] a copy of the initialized translation rules
178      */
179     public String[] getTranslations() {
180         String[] copy = new String[m_translations.length];
181         System.arraycopy(m_translations, 0, copy, 0, m_translations.length);
182         return copy;
183     }
184 }