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 * <filetranslations enabled="true"> 064 * <translation>s#[\s]+#_#g</translation> 065 * <translation>s#\\#/#g</translation> 066 * <translation>s#ä#ae#g</translation> 067 * <translation>s#Ä#Ae#g</translation> 068 * <translation>s#ö#oe#g</translation> 069 * <translation>s#Ö#Oe#g</translation> 070 * <translation>s#ü#ue#g</translation> 071 * <translation>s#Ü#Ue#g</translation> 072 * <translation>s#ß#ss#g</translation> 073 * <translation>s#[^0-9a-zA-Z_$~\.\-\/]#!#g</translation> 074 * <translation>s#!+#x#g</translation> 075 * </filetranslations> 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 * <foldertranslations enabled="true"> 082 * <translation>s#/content/bodys/(.*)#/system/bodies/$1#</translation> 083 * <translation>s#/pics/system/(.*)#/system/workplace/resources/$1#</translation> 084 * <translation>s#/pics/(.*)#/system/galleries/pics/$1#</translation> 085 * <translation>s#/download/(.*)#/system/galleries/download/$1#</translation> 086 * <translation>s#/externallinks/(.*)#/system/galleries/externallinks/$1#</translation> 087 * <translation>s#/htmlgalleries/(.*)#/system/galleries/htmlgalleries/$1#</translation> 088 * <translation>s#/content/(.*)#/system/$1#</translation> 089 * </foldertranslations> 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}