001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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
064        super();
065        m_key = key;
066        m_value = value;
067    }
068
069    /**
070     * Gets the header section for a named JSON part.<p>
071     *
072     * @param key the JSON key for the part
073     * @return the header section for the given key
074     */
075    public static final String getHeader(String key) {
076
077        return "BEGINJSONPART\u0007" + key + "\u0007";
078    }
079
080    /**
081     * Parses the encoded JSON parts from the given string and puts them in a list.<p>
082     *
083     * @param text the text containing the encoded JSON parts
084     * @return the decoded JSON parts
085     */
086    public static List<CmsJsonPart> parseJsonParts(String text) {
087
088        List<CmsJsonPart> result = Lists.newArrayList();
089        Matcher matcher = FORMAT_PATTERN.matcher(text);
090        while (matcher.find()) {
091            String key = matcher.group(1);
092            String value = matcher.group(2);
093            CmsJsonPart part = new CmsJsonPart(key, value);
094            result.add(part);
095        }
096        return result;
097    }
098
099    /**
100     * Returns the key.<p>
101     *
102     * @return the key
103     */
104    public String getKey() {
105
106        return m_key;
107    }
108
109    /**
110     * Returns the value.<p>
111     *
112     * @return the value
113     */
114    public String getValue() {
115
116        return m_value;
117    }
118
119}