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.search.config.parser.simplesearch.preconfiguredrestrictions; 029 030import org.opencms.jsp.search.config.parser.simplesearch.CmsConfigurationBean.CombinationMode; 031import org.opencms.jsp.search.config.parser.simplesearch.Messages; 032import org.opencms.jsp.search.config.parser.simplesearch.preconfiguredrestrictions.CmsRestrictionRule.MatchType; 033import org.opencms.main.CmsException; 034import org.opencms.main.CmsLog; 035 036import java.util.Arrays; 037 038import org.apache.commons.logging.Log; 039 040/** 041 * Parser for restriction rules. 042 */ 043public final class CmsRestrictionRuleParser { 044 045 /** The logger for this class. */ 046 private static final Log LOG = CmsLog.getLog(CmsRestrictionRuleParser.class.getName()); 047 048 /** Rule prefix for the resource type name. */ 049 private static String PREFIX_TYPE = "type="; 050 /** Rule prefix for the field name. */ 051 private static String PREFIX_FIELD = "field="; 052 /** Rule prefix for the match mode. */ 053 private static String PREFIX_MATCH = "match="; 054 /** Rule prefix for the combination mode. */ 055 private static String PREFIX_COMBINE = "combine="; 056 057 /** 058 * Hide the default constructor. 059 */ 060 private CmsRestrictionRuleParser() { 061 062 // Hide default constructor 063 } 064 065 /** 066 * Parses the provided restriction rule. 067 * @param rule the rule to parse. 068 * @return the parsed rule. 069 * @throws CmsException thrown if the rule cannot be parsed. 070 */ 071 public static CmsRestrictionRule parseRule(String rule) throws CmsException { 072 073 if ((rule != null) && (rule.length() > 0)) { 074 String[] ruleParts = rule.split(","); 075 if (ruleParts.length == 1) { 076 if (!rule.contains("=")) { 077 return new CmsRestrictionRule(rule); 078 } else { 079 if (rule.startsWith(PREFIX_FIELD)) { 080 return new CmsRestrictionRule(rule.substring(PREFIX_FIELD.length())); 081 } 082 throw new CmsException(Messages.get().container(Messages.ERR_WRONG_CONFIGURATION_SYNTAX_1, rule)); 083 } 084 } else { 085 String field = null; 086 String type = null; 087 MatchType match = null; 088 CombinationMode modeBetweenFields = null; 089 CombinationMode modeInField = null; 090 for (String rulePart : Arrays.asList(ruleParts)) { 091 if (rulePart.startsWith(PREFIX_FIELD)) { 092 field = rulePart.substring(PREFIX_FIELD.length()); 093 } else if (rulePart.startsWith(PREFIX_COMBINE)) { 094 String modeString = rulePart.substring(PREFIX_COMBINE.length()); 095 try { 096 if (modeString.contains("-")) { 097 String[] modes = modeString.split("-"); 098 modeBetweenFields = CombinationMode.valueOf(modes[0].toUpperCase()); 099 modeInField = CombinationMode.valueOf(modes[1].toUpperCase()); 100 } 101 modeBetweenFields = CombinationMode.valueOf(modeString.toUpperCase()); 102 } catch (Throwable t) { 103 LOG.info("Invalid combination mode '" + modeString + "' is ignored"); 104 } 105 } else if (rulePart.startsWith(PREFIX_MATCH)) { 106 String matchString = rulePart.substring(PREFIX_MATCH.length()); 107 try { 108 match = MatchType.valueOf(matchString.toUpperCase()); 109 } catch (Throwable t) { 110 LOG.info("Invalid match type \"" + matchString + "\" is ignored."); 111 match = MatchType.DEFAULT; 112 } 113 } else if (rulePart.startsWith(PREFIX_TYPE)) { 114 type = rulePart.substring(PREFIX_TYPE.length()); 115 } else { 116 LOG.info("Invalid rule part '" + rulePart + "' is ignored."); 117 } 118 } 119 if (field != null) { 120 return new CmsRestrictionRule(field, type, match, modeBetweenFields, modeInField); 121 } else { 122 throw new CmsException(Messages.get().container(Messages.ERR_WRONG_CONFIGURATION_SYNTAX_1, rule)); 123 } 124 } 125 } else { 126 throw new CmsException(Messages.get().container(Messages.ERR_WRONG_CONFIGURATION_SYNTAX_1, rule)); 127 } 128 } 129 130}