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.jsonpart; 029 030import java.util.List; 031import java.util.regex.Matcher; 032import java.util.regex.Pattern; 033 034import com.google.common.collect.Lists; 035 036/** 037 * Helper class used to translate key/value pairs from and to the format which can be processed by the 038 * CmsJsonPartFilter.<p> 039 */ 040public class CmsJsonPart { 041 042 /** The terminator used for JSON parts. */ 043 public static final String END = "\u0007ENDJSONPART"; 044 045 /** Pattern used to detect the parts of the content which should be transformed to JSON. */ 046 private static Pattern FORMAT_PATTERN = Pattern.compile( 047 "BEGINJSONPART\u0007(.*?)\u0007(.*?)\u0007ENDJSONPART", 048 Pattern.DOTALL); 049 050 /** The key. */ 051 private String m_key; 052 053 /** The value. */ 054 private String m_value; 055 056 /** 057 * Creates a new instance.<p> 058 * 059 * @param key the key 060 * @param value the value 061 */ 062 public CmsJsonPart(String key, String value) { 063 super(); 064 m_key = key; 065 m_value = value; 066 } 067 068 /** 069 * Gets the header section for a named JSON part.<p> 070 * 071 * @param key the JSON key for the part 072 * @return the header section for the given key 073 */ 074 public static final String getHeader(String key) { 075 076 return "BEGINJSONPART\u0007" + key + "\u0007"; 077 } 078 079 /** 080 * Parses the encoded JSON parts from the given string and puts them in a list.<p> 081 * 082 * @param text the text containing the encoded JSON parts 083 * @return the decoded JSON parts 084 */ 085 public static List<CmsJsonPart> parseJsonParts(String text) { 086 087 List<CmsJsonPart> result = Lists.newArrayList(); 088 Matcher matcher = FORMAT_PATTERN.matcher(text); 089 while (matcher.find()) { 090 String key = matcher.group(1); 091 String value = matcher.group(2); 092 CmsJsonPart part = new CmsJsonPart(key, value); 093 result.add(part); 094 } 095 return result; 096 } 097 098 /** 099 * Returns the key.<p> 100 * 101 * @return the key 102 */ 103 public String getKey() { 104 105 return m_key; 106 } 107 108 /** 109 * Returns the value.<p> 110 * 111 * @return the value 112 */ 113 public String getValue() { 114 115 return m_value; 116 } 117 118}