/* This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package com.ecyrd.jspwiki; import org.apache.log4j.*; import java.io.InputStream; import java.io.IOException; import java.util.Properties; import java.util.Iterator; import java.util.Enumeration; import javax.servlet.ServletContext; /** * Verifies text * @author Dave Brondsema * @version 1.0 */ public class TextContentVerifier { private static final Category log = Category.getInstance(TextContentVerifier.class); private static Properties m_prohibited = new Properties(); private static boolean m_enabled = false; private static final String PROHIBITED_PATTERNS_FILE = "/WEB-INF/prohibited-patterns.properties"; public static void init(Properties props, ServletContext context) throws WikiException { // check and set enabled if (props.getProperty("jspwiki.TextContentVerifier.enabled", "false").equals("true")) { m_enabled = true; } else { m_enabled = false; } if (m_enabled) { InputStream inputStream = context.getResourceAsStream(PROHIBITED_PATTERNS_FILE); if( inputStream == null ) { throw new WikiException("File cannot be found: "+PROHIBITED_PATTERNS_FILE); } try { m_prohibited.load(inputStream); } catch (IOException e) { throw new WikiException("Could not load file: "+PROHIBITED_PATTERNS_FILE); } } log.info("loaded patterns file"); } public static String verify(String text) { if (m_enabled) { log.info("text==" + text); log.info("---------"); for (Enumeration e = m_prohibited.propertyNames(); e.hasMoreElements(); ) { String pattern_key = (String)e.nextElement(); // (?s) means . will match newlines String pattern = "(?s).*" + m_prohibited.getProperty(pattern_key) + ".*"; log.info("testing against: " + pattern); if (text.matches(pattern)) { log.info("prohibited!"); return pattern_key; } } } return null; } }