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.search.solr.updateprocessors; 029 030import java.io.IOException; 031import java.util.regex.Pattern; 032 033import javax.annotation.Nonnull; 034 035import org.apache.solr.common.SolrInputDocument; 036import org.apache.solr.update.AddUpdateCommand; 037import org.apache.solr.update.processor.UpdateRequestProcessor; 038 039/** 040 * Implementation of an {@link UpdateRequestProcessor} that 041 * <ul> 042 * <li>reads a field's (source) value,</li> 043 * <li>applies a regex replacement,</li> 044 * <li>adds a field (target) with the modified value.</li> 045 * </ul> 046 * 047 * @see CmsSolrCopyModifiedUpateProcessorFactory 048 */ 049public class CmsSolrCopyModifiedUpateProcessor extends UpdateRequestProcessor { 050 051 /** The source field where the original value is read from. */ 052 private final String m_source; 053 /** The target field where the modified value is written to. */ 054 private final String m_target; 055 /** The regular expression that the original value is matched to. */ 056 private final Pattern m_regex; 057 /** The replacement for the matches of the regex on the original value. */ 058 private final String m_replacement; 059 060 /** 061 * Default constructor. 062 * @param source the name of the source field (where the original value is read from). 063 * @param target the name of the target field (where the modified value is written to). 064 * @param regex the regex applied to the original value. 065 * @param replacement the replacement for the matched parts in the original value. 066 * @param nextProcessor the {@link UpdateRequestProcessor} to process next. 067 */ 068 public CmsSolrCopyModifiedUpateProcessor( 069 @Nonnull String source, 070 @Nonnull String target, 071 @Nonnull Pattern regex, 072 @Nonnull String replacement, 073 UpdateRequestProcessor nextProcessor) { 074 super(nextProcessor); 075 m_source = source; 076 m_target = target; 077 m_regex = regex; 078 m_replacement = replacement; 079 } 080 081 /** 082 * @see org.apache.solr.update.processor.UpdateRequestProcessor#processAdd(org.apache.solr.update.AddUpdateCommand) 083 */ 084 @Override 085 public void processAdd(AddUpdateCommand cmd) throws IOException { 086 087 SolrInputDocument doc = cmd.getSolrInputDocument(); 088 089 String v = (String)doc.getFieldValue(m_source); 090 if (v != null) { 091 String v1 = m_regex.matcher(v).replaceAll(m_replacement); 092 doc.addField(m_target, v1); 093 } 094 095 // pass it up the chain 096 super.processAdd(cmd); 097 } 098}