001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.util;
029
030import org.opencms.main.CmsLog;
031
032import org.apache.commons.logging.Log;
033import org.apache.oro.text.PatternCache;
034import org.apache.oro.text.PatternCacheFIFO;
035import org.apache.oro.text.perl.MalformedPerl5PatternException;
036import org.apache.oro.text.perl.Perl5Util;
037import org.apache.oro.text.regex.MalformedPatternException;
038
039/**
040 * Provides a resource name / path translation facility.<p>
041 *
042 * This facility is used for translating new file names that contain
043 * illegal chars to legal names. This feature is most useful (and currently
044 * only used) for uploaded files. It is also applied to uploded ZIP directories
045 * that are extracted after upload.
046 * The rules that are used for resource name translation are available from
047 * {@link org.opencms.file.CmsRequestContext#getFileTranslator()}.<p>
048 *
049 * Optionally, resource name translation is also applied to all files read
050 * from the VFS, so it can be used for accessing files out of teir usual context.
051 * This feature is called directoy translation, and the configured directory
052 * translations are available from {@link org.opencms.file.CmsRequestContext#getDirectoryTranslator()}.<p>
053 *
054 * Directory translation was originally required for backward compatibility
055 * to the directory layout before OpenCms 5.0 beta 2. In a modern installation,
056 * directory translation is usually disabled.<p>
057 *
058 * The translations can be configured in <code>opencms-vfs.xml</code>
059 * in the <code>opencms\vfs\resources\translations</code> node.<p>
060 *
061 * The default file name translation setting is:<br>
062 * <pre>
063 * &lt;filetranslations enabled="true"&gt;
064 *    &lt;translation&gt;s#[\s]+#_#g&lt;/translation&gt;
065 *    &lt;translation&gt;s#\\#/#g&lt;/translation&gt;
066 *    &lt;translation&gt;s#&auml;#ae#g&lt;/translation&gt;
067 *    &lt;translation&gt;s#&Auml;#Ae#g&lt;/translation&gt;
068 *    &lt;translation&gt;s#&ouml;#oe#g&lt;/translation&gt;
069 *    &lt;translation&gt;s#&Ouml;#Oe#g&lt;/translation&gt;
070 *    &lt;translation&gt;s#&uuml;#ue#g&lt;/translation&gt;
071 *    &lt;translation&gt;s#&Uuml;#Ue#g&lt;/translation&gt;
072 *    &lt;translation&gt;s#&szlig;#ss#g&lt;/translation&gt;
073 *    &lt;translation&gt;s#[^0-9a-zA-Z_$~\.\-\/]#!#g&lt;/translation&gt;
074 *    &lt;translation&gt;s#!+#x#g&lt;/translation&gt;
075 * &lt;/filetranslations&gt;
076 * </pre><p>
077 *
078 * Directory translation is now usually not required and since disabled by default.
079 * The directory translation setting to convert an OpenCms 5.0 to 6.0 VFS is:<br>
080 * <pre>
081 * &lt;foldertranslations enabled="true"&gt;
082 *    &lt;translation&gt;s#/content/bodys/(.*)#/system/bodies/$1#&lt;/translation&gt;
083 *    &lt;translation&gt;s#/pics/system/(.*)#/system/workplace/resources/$1#&lt;/translation&gt;
084 *    &lt;translation&gt;s#/pics/(.*)#/system/galleries/pics/$1#&lt;/translation&gt;
085 *    &lt;translation&gt;s#/download/(.*)#/system/galleries/download/$1#&lt;/translation&gt;
086 *    &lt;translation&gt;s#/externallinks/(.*)#/system/galleries/externallinks/$1#&lt;/translation&gt;
087 *    &lt;translation&gt;s#/htmlgalleries/(.*)#/system/galleries/htmlgalleries/$1#&lt;/translation&gt;
088 *    &lt;translation&gt;s#/content/(.*)#/system/$1#&lt;/translation&gt;
089 * &lt;/foldertranslations&gt;
090 * </pre><p>
091 *
092 * @since 6.0.0
093 */
094public class CmsResourceTranslator {
095
096    /** The log object for this class. */
097    private static final Log LOG = CmsLog.getLog(CmsResourceTranslator.class);
098
099    /** Flag to indicate if one or more matchings should be tried. */
100    private boolean m_continueMatching;
101
102    /** Perl5 patter cache to avoid unecessary re-parsing of properties. */
103    private PatternCache m_perlPatternCache;
104
105    /** Perl5 utility class. */
106    private Perl5Util m_perlUtil;
107
108    /** Internal array containing the translations from opencms.properties. */
109    private String[] m_translations;
110
111    /**
112     * Constructor for the CmsResourceTranslator.
113     *
114     * @param translations The array of translations read from the
115     *      opencms,properties
116     * @param continueMatching if <code>true</code>, matching will continue after
117     *      the first match was found
118     */
119    public CmsResourceTranslator(String[] translations, boolean continueMatching) {
120
121        super();
122        m_translations = translations;
123        m_continueMatching = continueMatching;
124        // pre-cache the patterns
125        m_perlPatternCache = new PatternCacheFIFO(m_translations.length + 1);
126        for (int i = 0; i < m_translations.length; i++) {
127            try {
128                m_perlPatternCache.addPattern(m_translations[i]);
129            } catch (MalformedPatternException e) {
130                LOG.error(
131                    Messages.get().getBundle().key(Messages.LOG_MALFORMED_TRANSLATION_RULE_1, m_translations[i]),
132                    e);
133            }
134        }
135        // initialize the Perl5Util
136        m_perlUtil = new Perl5Util(m_perlPatternCache);
137        if (LOG.isInfoEnabled()) {
138            LOG.info(
139                Messages.get().getBundle().key(
140                    Messages.LOG_NUM_TRANSLATION_RULES_INITIALIZED_1,
141                    new Integer(translations.length)));
142        }
143    }
144
145    /**
146     * Returns a copy of the initialized translation rules.<p>
147     *
148     * @return String[] a copy of the initialized translation rules
149     */
150    public String[] getTranslations() {
151
152        String[] copy = new String[m_translations.length];
153        System.arraycopy(m_translations, 0, copy, 0, m_translations.length);
154        return copy;
155    }
156
157    /**
158     * Translate a resource name according to the expressions set in
159     * <code>opencms-vfs.xml</code>. If no match is found,
160     * the resource name is returned unchanged.<p>
161     *
162     * @param resourceName The resource name to translate
163     * @return The translated name of the resource
164     */
165    public String translateResource(String resourceName) {
166
167        if (m_translations.length == 0) {
168            // no translations defined
169            return resourceName;
170        }
171        if (resourceName == null) {
172            return null;
173        }
174
175        StringBuffer result;
176        String current = resourceName;
177        int size = current.length() * 2;
178
179        for (int i = 0; i < m_translations.length; i++) {
180            result = new StringBuffer(size);
181            try {
182                if (m_perlUtil.substitute(result, m_translations[i], current) != 0) {
183
184                    if (m_continueMatching) {
185                        // continue matching
186                        current = result.toString();
187                    } else {
188                        // first pattern matched, return the result
189                        if (LOG.isDebugEnabled()) {
190                            LOG.debug(
191                                Messages.get().getBundle().key(
192                                    Messages.LOG_TRANSLATION_MATCH_3,
193                                    new Integer(i),
194                                    resourceName,
195                                    result));
196                        }
197                        // Return first match result
198                        return result.toString();
199                    }
200                }
201            } catch (MalformedPerl5PatternException e) {
202                LOG.error(
203                    Messages.get().getBundle().key(Messages.LOG_MALFORMED_TRANSLATION_RULE_1, m_translations[i]),
204                    e);
205            }
206        }
207
208        // the pattern matched, return the result
209        if (LOG.isDebugEnabled()) {
210            LOG.debug(Messages.get().getBundle().key(Messages.LOG_TRANSLATION_MATCH_2, resourceName, current));
211        }
212        // return last translation (or original if no matching translation found)
213        return current;
214    }
215}