1 31 32 package org.opencms.frontend.layoutpage; 33 34 import com.alkacon.simapi.Simapi; 35 36 import org.opencms.file.CmsFile; 37 import org.opencms.jsp.CmsJspActionElement; 38 import org.opencms.loader.CmsImageScaler; 39 import org.opencms.main.CmsException; 40 import org.opencms.main.CmsLog; 41 import org.opencms.util.CmsMacroResolver; 42 import org.opencms.util.CmsStringUtil; 43 import org.opencms.workplace.CmsWorkplace; 44 import org.opencms.xml.content.CmsXmlContent; 45 import org.opencms.xml.content.CmsXmlContentFactory; 46 import org.opencms.xml.types.I_CmsXmlContentValue; 47 48 import java.util.HashMap ; 49 import java.util.Iterator ; 50 import java.util.Locale ; 51 import java.util.Map ; 52 53 import javax.servlet.http.HttpServletRequest ; 54 import javax.servlet.http.HttpServletResponse ; 55 import javax.servlet.jsp.PageContext ; 56 57 import org.apache.commons.logging.Log; 58 59 70 public class CmsLayoutPageBean extends CmsJspActionElement { 71 72 73 public static final String ALIGN_TYPE_BOTTOM = "imagebottom"; 74 75 76 public static final String ALIGN_TYPE_IMAGE_LEFT = "imageleft"; 77 78 79 public static final String ALIGN_TYPE_IMAGE_LEFT_TEXT_RIGHT = "imageleft_textright"; 80 81 82 public static final String ALIGN_TYPE_IMAGE_RIGHT = "imageright"; 83 84 85 public static final String ALIGN_TYPE_IMAGE_RIGHT_TEXT_LEFT = "imageright_textleft"; 86 87 88 public static final String ALIGN_TYPE_TEXT_ONLY = "textonly"; 89 90 91 public static final String ALIGN_TYPE_TOP = "imagetop"; 92 93 94 public static final int IMG_WIDTH_LARGE = 737; 95 96 97 public static final int IMG_WIDTH_MEDIUM = 200; 98 99 100 public static final int IMG_WIDTH_SMALL = 100; 101 102 103 public static final String MACRO_DESCRIPTION = "layout.description"; 104 105 106 public static final String MACRO_HEADLINE = "layout.headline"; 107 108 109 public static final String MACRO_IMAGE = "layout.image"; 110 111 112 public static final String MACRO_TEXT = "layout.text"; 113 114 115 public static final String MODULE_NAME = "org.opencms.frontend.layoutpage"; 116 117 118 public static final String NODE_ALIGN = "Align"; 119 120 121 public static final String NODE_DESCRIPTION = "Description"; 122 123 124 public static final String NODE_HEADLINE = "Headline"; 125 126 127 public static final String NODE_IMAGE = "Image"; 128 129 130 public static final String NODE_PARAGRAPH = "Paragraph"; 131 132 133 public static final String NODE_TEXT = "Text"; 134 135 136 public static final String VFS_PATH_LAYOUTELEMENTS = CmsWorkplace.VFS_PATH_MODULES + MODULE_NAME + "/layouts/"; 137 138 139 private static final Log LOG = CmsLog.getLog(CmsLayoutPageBean.class); 140 141 142 private CmsXmlContent m_content; 143 144 145 private Map m_layoutPatterns; 146 147 148 private String m_pathLayoutElements; 149 150 153 public CmsLayoutPageBean() { 154 155 super(); 156 } 157 158 167 public CmsLayoutPageBean(PageContext context, HttpServletRequest req, HttpServletResponse res) { 168 169 super(); 170 init(context, req, res); 171 } 172 173 179 public void addLayoutPattern(String patternName, int imgWidth) { 180 181 m_layoutPatterns.put(patternName, new Integer (imgWidth)); 182 } 183 184 189 public String buildHtmlParagraphs() { 190 191 StringBuffer result = new StringBuffer (16384); 192 Locale locale = getRequestContext().getLocale(); 193 Iterator i = m_content.getValues(NODE_PARAGRAPH, locale).iterator(); 194 while (i.hasNext()) { 195 I_CmsXmlContentValue value = (I_CmsXmlContentValue)i.next(); 197 String xPath = value.getPath() + "/"; 198 199 String headline = ""; 201 if (m_content.hasValue(xPath + NODE_HEADLINE, locale)) { 202 headline = m_content.getStringValue(getCmsObject(), xPath + NODE_HEADLINE, locale); 203 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(headline)) { 204 headline = "<h2 class=\"lp_headline\">" + headline + "</h2>\n"; 205 } 206 } 207 208 String textValue = m_content.getStringValue(getCmsObject(), xPath + NODE_TEXT, locale); 210 211 xPath += NODE_IMAGE + "/"; 213 String imgDesc = ""; 214 String imgAlign = ""; 215 String imgTag = ""; 216 if (m_content.hasValue(xPath, locale)) { 217 String imgUri = m_content.getStringValue(getCmsObject(), xPath + NODE_IMAGE, locale); 219 if (getCmsObject().existsResource(imgUri)) { 220 if (m_content.hasValue(xPath + NODE_DESCRIPTION, locale)) { 222 imgDesc = m_content.getStringValue(getCmsObject(), xPath + NODE_DESCRIPTION, locale); 224 } 225 imgAlign = m_content.getStringValue(getCmsObject(), xPath + NODE_ALIGN, locale); 226 CmsImageScaler scaler = getImageScaler(imgAlign); 227 Map attrs = new HashMap (5); 229 attrs.put("alt", imgDesc); 230 attrs.put("title", imgDesc); 231 imgTag = img(imgUri, scaler, attrs); 232 } 233 } 234 235 if (CmsStringUtil.isEmpty(imgAlign)) { 236 imgAlign = ALIGN_TYPE_TEXT_ONLY; 238 } 239 240 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(imgDesc)) { 241 imgDesc = "<p class=\"lp_imgdesc\">" + imgDesc + "</p>"; 243 } 244 245 String elementContent = getContent(m_pathLayoutElements + imgAlign); 247 CmsMacroResolver resolver = CmsMacroResolver.newInstance(); 248 resolver.addMacro(MACRO_HEADLINE, headline); 250 resolver.addMacro(MACRO_TEXT, textValue); 251 resolver.addMacro(MACRO_IMAGE, imgTag); 252 resolver.addMacro(MACRO_DESCRIPTION, imgDesc); 253 254 result.append(resolver.resolveMacros(elementContent)); 256 } 257 258 return result.toString(); 259 } 260 261 264 public void createDefaultLayoutPatterns() { 265 266 m_layoutPatterns.put(ALIGN_TYPE_BOTTOM, new Integer (IMG_WIDTH_LARGE)); 267 m_layoutPatterns.put(ALIGN_TYPE_TOP, new Integer (IMG_WIDTH_LARGE)); 268 m_layoutPatterns.put(ALIGN_TYPE_IMAGE_LEFT, new Integer (IMG_WIDTH_MEDIUM)); 269 m_layoutPatterns.put(ALIGN_TYPE_IMAGE_RIGHT, new Integer (IMG_WIDTH_MEDIUM)); 270 m_layoutPatterns.put(ALIGN_TYPE_IMAGE_LEFT_TEXT_RIGHT, new Integer (IMG_WIDTH_SMALL)); 271 m_layoutPatterns.put(ALIGN_TYPE_IMAGE_RIGHT_TEXT_LEFT, new Integer (IMG_WIDTH_SMALL)); 272 } 273 274 279 public Map getLayoutPatterns() { 280 281 return m_layoutPatterns; 282 } 283 284 289 public String getPathLayoutElements() { 290 291 return m_pathLayoutElements; 292 } 293 294 304 public void init(PageContext context, HttpServletRequest req, HttpServletResponse res) { 305 306 super.init(context, req, res); 307 m_pathLayoutElements = VFS_PATH_LAYOUTELEMENTS; 309 try { 311 CmsFile file = getCmsObject().readFile(getRequestContext().getUri()); 312 m_content = CmsXmlContentFactory.unmarshal(getCmsObject(), file); 313 } catch (CmsException e) { 314 if (LOG.isErrorEnabled()) { 316 LOG.error(Messages.get().getBundle().key(Messages.LOG_ERR_VFS_RESOURCE_1, getRequestContext().getUri())); 317 } 318 } 319 m_layoutPatterns = new HashMap (10); 321 } 322 323 328 public void setLayoutPatterns(Map layoutPatterns) { 329 330 m_layoutPatterns = layoutPatterns; 331 } 332 333 338 public void setPathLayoutElements(String pathLayoutElements) { 339 340 m_pathLayoutElements = pathLayoutElements; 341 } 342 343 349 protected CmsImageScaler getImageScaler(String imgAlign) { 350 351 CmsImageScaler scaler = new CmsImageScaler(); 353 scaler.setType(1); 354 scaler.setPosition(Simapi.POS_DOWN_LEFT); 355 356 Integer imgWidth = (Integer )getLayoutPatterns().get(imgAlign); 358 if (imgWidth != null) { 359 scaler.setWidth(imgWidth.intValue()); 361 } else { 362 scaler.setWidth(IMG_WIDTH_MEDIUM); 364 } 365 366 return scaler; 367 } 368 369 } 370 | Popular Tags |