1 31 32 package org.opencms.frontend.templateone; 33 34 import org.opencms.file.CmsPropertyDefinition; 35 import org.opencms.util.CmsStringUtil; 36 37 import java.util.Map ; 38 import java.util.regex.Matcher ; 39 import java.util.regex.Pattern ; 40 41 import javax.servlet.http.HttpServletRequest ; 42 import javax.servlet.http.HttpServletResponse ; 43 import javax.servlet.jsp.PageContext ; 44 45 import org.apache.commons.httpclient.util.URIUtil; 46 47 56 public abstract class CmsTemplateForm extends CmsTemplateBean { 57 58 59 public static final String MESSAGE_BUNDLE_FORM = "templateone_form"; 60 61 62 public static final String PARAM_ACTION = "action"; 63 64 65 private Map m_errors; 66 67 68 private String m_formUri; 69 70 71 private String m_pageUri; 72 73 74 private String m_pageUrl; 75 76 77 private String m_textsUri; 78 79 82 public CmsTemplateForm() { 83 84 super(); 85 } 86 87 96 public CmsTemplateForm(PageContext context, HttpServletRequest req, HttpServletResponse res) { 97 98 super(); 99 init(context, req, res); 100 } 101 102 111 public static String getFormattedString(String inputString, char sepChar, int lineLength, String separator) { 112 113 if (inputString.length() > lineLength) { 114 int inputLength = inputString.length(); 116 StringBuffer result = new StringBuffer (inputLength + 4); 117 while (inputLength > lineLength) { 118 int count = lineLength; 119 while (count < inputLength && inputString.charAt(count) != sepChar) { 120 count++; 122 } 123 if (count < inputLength) { 124 result.append(inputString.substring(0, count + 1)); 126 result.append(separator); 127 inputString = inputString.substring(count + 1); 128 inputLength = inputString.length(); 129 } else { 130 break; 132 } 133 } 134 result.append(inputString); 136 inputString = result.toString(); 137 } 138 return inputString; 139 } 140 141 147 public static boolean isValidEmailAddress(String theAddress) { 148 149 if (theAddress == null) { 150 return false; 151 } 152 return (theAddress.lastIndexOf(".") < (theAddress.length() - 2) && theAddress.lastIndexOf(".") > theAddress.indexOf("@")); 153 } 154 155 163 public String getConfigPath() { 164 165 String uri = getRequestContext().getUri(); 167 getRequestContext().setUri(getPageUri(true)); 169 String path = property(PROPERTY_CONFIGPATH, "search", ""); 170 getRequestContext().setUri(uri); 172 return path; 173 } 174 175 181 public String getError(String key) { 182 183 if (m_errors != null) { 184 String message = (String )m_errors.get(key); 185 if (message == null) { 186 return ""; 187 } else { 188 return message; 189 } 190 } else { 191 return ""; 192 } 193 } 194 195 200 public Map getErrors() { 201 202 return m_errors; 203 } 204 205 212 public String getFormText(String element, boolean stripHtml) { 213 214 String content = getContent(getTextsUri(), element, getRequestContext().getLocale()); 215 if (stripHtml) { 216 content = removeHtmlTags(content); 218 } 219 return content; 220 } 221 222 229 public String getFormUri() { 230 231 return link(m_formUri); 232 } 233 234 242 public String getPageTitle() { 243 244 String title = property(CmsPropertyDefinition.PROPERTY_TITLE, getPageUri(true), ""); 245 if ("".equals(title)) { 246 title = getPageUri(true); 247 } 248 return title; 249 } 250 251 258 public String getPageUri() { 259 260 return getPageUri(false); 261 } 262 263 271 public String getPageUri(boolean removeParams) { 272 273 if (removeParams) { 274 String uri = m_pageUri; 276 if (CmsStringUtil.isNotEmpty(uri)) { 277 uri = URIUtil.getPath(uri); 278 } 279 return uri; 280 } else { 281 return m_pageUri; 282 } 283 } 284 285 290 public String getPageUrl() { 291 292 if (CmsStringUtil.isEmpty(m_pageUrl)) { 293 StringBuffer result = new StringBuffer (64); 294 HttpServletRequest request = getRequest(); 295 result.append(request.getScheme()); 296 result.append("://"); 297 result.append(request.getServerName()); 298 int port = request.getServerPort(); 299 String portString = ""; 300 if (port != 80 && port != 443) { 301 portString = ":" + port; 303 } 304 result.append(portString); 305 result.append(link(m_pageUri)); 306 m_pageUrl = result.toString(); 307 } 308 return m_pageUrl; 309 } 310 311 316 public String getTextsUri() { 317 318 if (m_textsUri == null) { 319 m_textsUri = checkTextsUri(); 320 } 321 return m_textsUri; 322 } 323 324 329 public boolean hasValidationErrors() { 330 331 if (isSubmitted()) { 332 if (getErrors() != null && getErrors().size() > 0) { 333 return true; 334 } 335 } 336 return false; 337 } 338 339 349 public void init(PageContext context, HttpServletRequest req, HttpServletResponse res) { 350 351 m_pageUri = req.getParameter(CmsTemplateBean.PARAM_URI); 353 super.init(context, req, res); 355 String siteRoot = req.getParameter(CmsTemplateBean.PARAM_SITE); 357 if (CmsStringUtil.isNotEmpty(siteRoot)) { 358 getRequestContext().setSiteRoot(siteRoot); 359 } 360 m_formUri = getRequestContext().getUri(); 362 if (m_pageUri == null) { 363 m_pageUri = m_formUri; 364 } 365 messages(MESSAGE_BUNDLE_FORM); 366 } 367 368 374 public String isChecked(String fieldValue) { 375 376 if (Boolean.valueOf(fieldValue).booleanValue()) { 377 return " checked=\"checked\""; 378 } else { 379 return ""; 380 } 381 } 382 383 392 public boolean isSelected(String fieldValue, String requestValue) { 393 394 if (fieldValue.equals(requestValue)) { 395 return true; 396 } 397 return false; 398 } 399 400 407 public boolean isSubmitted() { 408 409 return getRequest().getParameter(PARAM_ACTION) != null; 410 } 411 412 417 public void setErrors(Map errors) { 418 419 m_errors = errors; 420 } 421 422 427 public abstract boolean validate(); 428 429 436 protected abstract String checkTextsUri(); 437 438 444 protected String removeHtmlTags(String content) { 445 446 if (content != null && content.indexOf("<") != -1) { 447 Matcher matcher = Pattern.compile("<(.|\\n)+?>").matcher(content); 448 content = matcher.replaceAll(""); 449 } 450 return content; 451 } 452 453 } | Popular Tags |