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, 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.jsp.util; 029 030import org.opencms.file.CmsObject; 031import org.opencms.file.CmsResource; 032import org.opencms.main.CmsException; 033import org.opencms.util.CmsCollectionsGenericWrapper; 034 035import java.util.HashMap; 036import java.util.Locale; 037import java.util.Map; 038 039import org.apache.commons.collections.Transformer; 040import org.apache.commons.lang3.LocaleUtils; 041 042/** Utility class for providing {@link Transformer} implementations for various purposes. */ 043public final class CmsJspValueTransformers { 044 045 /** Transformer that yields locale specific properties for a resource. */ 046 public static final class CmsLocalePropertyLoaderTransformer implements Transformer { 047 048 /** The {@link CmsObject} used for reading properties. */ 049 private CmsObject m_cms; 050 /** The resource where the properties are read from. */ 051 private CmsResource m_res; 052 053 /** The map from locale to properties. */ 054 private Map<Locale, Map<String, String>> m_localeProperties; 055 056 /** Search for the property or not. */ 057 private boolean m_search; 058 059 /** 060 * Default constructor. 061 * @param cms the {@link CmsObject} used to read properties. 062 * @param resource the resource for which properties should be read. 063 * @param search flag, indicating if property should be searched or not. 064 */ 065 public CmsLocalePropertyLoaderTransformer(CmsObject cms, CmsResource resource, boolean search) { 066 067 m_cms = cms; 068 m_res = resource; 069 m_search = search; 070 m_localeProperties = new HashMap<Locale, Map<String, String>>(); 071 } 072 073 /** 074 * @see org.apache.commons.collections.Transformer#transform(java.lang.Object) 075 */ 076 public Object transform(Object inputLocale) { 077 078 Locale locale = null; 079 if (null != inputLocale) { 080 if (inputLocale instanceof Locale) { 081 locale = (Locale)inputLocale; 082 } else if (inputLocale instanceof String) { 083 try { 084 locale = LocaleUtils.toLocale((String)inputLocale); 085 } catch (@SuppressWarnings("unused") IllegalArgumentException | NullPointerException e) { 086 // do nothing, just go on without locale 087 } 088 } 089 } 090 if (m_localeProperties.get(locale) == null) { 091 Map<String, String> lazyMap = CmsCollectionsGenericWrapper.createLazyMap( 092 new CmsJspValueTransformers.CmsPropertyLoaderTransformer(m_cms, m_res, m_search, locale)); 093 m_localeProperties.put(locale, lazyMap); 094 } 095 return m_localeProperties.get(locale); 096 } 097 098 } 099 100 /** 101 * Transformer that reads a resource property, 102 * the input is used as String for the property name to read.<p> 103 */ 104 public static final class CmsPropertyLoaderTransformer implements Transformer { 105 106 /** The {@link CmsObject} used for reading properties. */ 107 private CmsObject m_cms; 108 /** The resource where the properties are read from. */ 109 private CmsResource m_res; 110 /** The locale for which properties should be read. */ 111 private Locale m_locale; 112 /** Search for the property or not. */ 113 private boolean m_search; 114 115 /** 116 * Creates a new property loading Transformer.<p> 117 * 118 * @param resource the resource where the properties are read from 119 * @param cms the {@link CmsObject} used for reading properties. 120 * @param search flag, indicating if property should be searched or not. 121 */ 122 public CmsPropertyLoaderTransformer(CmsObject cms, CmsResource resource, boolean search) { 123 124 m_cms = cms; 125 m_res = resource; 126 m_search = search; 127 128 } 129 130 /** 131 * Creates a new property loading Transformer.<p> 132 * 133 * @param resource the resource where the properties are read from 134 * @param locale the locale for which properties should be accessed 135 * @param cms the {@link CmsObject} used for reading properties. 136 * @param search flag, indicating if property should be searched or not. 137 */ 138 public CmsPropertyLoaderTransformer(CmsObject cms, CmsResource resource, boolean search, Locale locale) { 139 140 m_cms = cms; 141 m_res = resource; 142 m_locale = locale; 143 m_search = search; 144 145 } 146 147 /** 148 * @see org.apache.commons.collections.Transformer#transform(java.lang.Object) 149 */ 150 public Object transform(Object input) { 151 152 String result; 153 try { 154 // read the requested property 155 result = m_cms.readPropertyObject(m_res, String.valueOf(input), m_search, m_locale).getValue(); 156 } catch (@SuppressWarnings("unused") CmsException e) { 157 // unable to read property, return null 158 result = null; 159 } 160 return result; 161 } 162 } 163 164 /** Hide the default constructor. */ 165 private CmsJspValueTransformers() { 166 // just hide the default constructor 167 } 168 169}