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.search.config;
029
030import org.opencms.main.CmsLog;
031
032import java.util.Collections;
033import java.util.Map;
034
035import org.apache.commons.logging.Log;
036
037/**
038 * Search configuration for highlighting options.
039 */
040public class CmsSearchConfigurationHighlighting implements I_CmsSearchConfigurationHighlighting {
041
042    /** Logger for the class. */
043    protected static final Log LOG = CmsLog.getLog(CmsSearchConfigurationHighlighting.class);
044
045    /** Additional configuration parameters */
046    private final Map<String, String> m_params;
047
048    /** The constructor setting all configuration values.
049     * @param params the highlighting parameters as given to solr, all without the "hl." prefix that is added automatically.
050     */
051    public CmsSearchConfigurationHighlighting(final Map<String, String> params) {
052
053        m_params = params == null ? Collections.emptyMap() : Collections.unmodifiableMap(params);
054
055    }
056
057    /**
058     * @see org.opencms.jsp.search.config.I_CmsSearchConfigurationHighlighting#getAlternateHighlightField()
059     */
060    @Deprecated
061    @Override
062    public String getAlternateHighlightField() {
063
064        return m_params.get("alternateField");
065    }
066
067    /**
068     * @see org.opencms.jsp.search.config.I_CmsSearchConfigurationHighlighting#getFormatter()
069     */
070    @Deprecated
071    @Override
072    public String getFormatter() {
073
074        return m_params.get("formatter");
075    }
076
077    /**
078     * @see org.opencms.jsp.search.config.I_CmsSearchConfigurationHighlighting#getFragmenter()
079     */
080    @Deprecated
081    @Override
082    public String getFragmenter() {
083
084        return m_params.get("fragmenter");
085    }
086
087    /**
088     * @see org.opencms.jsp.search.config.I_CmsSearchConfigurationHighlighting#getFragSize()
089     */
090    @Deprecated
091    @Override
092    public Integer getFragSize() {
093
094        String fragSize = m_params.get("fragsize");
095        if ((fragSize != null) && !fragSize.isBlank()) {
096            try {
097                return Integer.valueOf(fragSize);
098            } catch (NumberFormatException e) {
099                LOG.error(
100                    "Invalid fragsize value \""
101                        + fragSize
102                        + "\" will be used in the final query even if null is returned here.");
103            }
104        }
105        return null;
106    }
107
108    /**
109     * @see org.opencms.jsp.search.config.I_CmsSearchConfigurationHighlighting#getHightlightField()
110     */
111    @Deprecated
112    @Override
113    public String getHightlightField() {
114
115        return m_params.get("fl");
116    }
117
118    /**
119     * @see org.opencms.jsp.search.config.I_CmsSearchConfigurationHighlighting#getMaxAlternateHighlightFieldLength()
120     */
121    @Deprecated
122    @Override
123    public Integer getMaxAlternateHighlightFieldLength() {
124
125        String maxAlternateFieldLength = m_params.get("maxAlternateFieldLength");
126        if ((maxAlternateFieldLength != null) && !maxAlternateFieldLength.isBlank()) {
127            try {
128                return Integer.valueOf(maxAlternateFieldLength);
129            } catch (NumberFormatException e) {
130                LOG.error(
131                    "Invalid maxAlternateFieldLength value \""
132                        + maxAlternateFieldLength
133                        + "\" will be used in the final query even if null is returned here.");
134            }
135        }
136        return null;
137    }
138
139    /**
140     * @see org.opencms.jsp.search.config.I_CmsSearchConfigurationHighlighting#getParams()
141     */
142    @Override
143    public Map<String, String> getParams() {
144
145        return Collections.unmodifiableMap(m_params);
146    }
147
148    /**
149     * @see org.opencms.jsp.search.config.I_CmsSearchConfigurationHighlighting#getSimplePost()
150     */
151    @Deprecated
152    @Override
153    public String getSimplePost() {
154
155        return m_params.get("simple.post");
156    }
157
158    /**
159     * @see org.opencms.jsp.search.config.I_CmsSearchConfigurationHighlighting#getSimplePre()
160     */
161    @Deprecated
162    @Override
163    public String getSimplePre() {
164
165        return m_params.get("simple.pre");
166    }
167
168    /**
169     * @see org.opencms.jsp.search.config.I_CmsSearchConfigurationHighlighting#getSnippetsCount()
170     */
171    @Deprecated
172    @Override
173    public Integer getSnippetsCount() {
174
175        String snippets = m_params.get("snippets");
176        if ((snippets != null) && !snippets.isBlank()) {
177            try {
178                return Integer.valueOf(snippets);
179            } catch (NumberFormatException e) {
180                LOG.error(
181                    "Invalid snippets value \""
182                        + snippets
183                        + "\" will be used in the final query even if null is returned here.");
184            }
185        }
186        return null;
187    }
188
189    /**
190     * @see org.opencms.jsp.search.config.I_CmsSearchConfigurationHighlighting#getUseFastVectorHighlighting()
191     */
192    @Deprecated
193    @Override
194    public Boolean getUseFastVectorHighlighting() {
195
196        String method = m_params.get("method");
197        if (null == method) {
198            return null;
199        }
200        return Boolean.valueOf("fastVector".equals(method));
201    }
202}