Author: tchemit Date: 2009-08-26 21:08:21 +0200 (Wed, 26 Aug 2009) New Revision: 261 Modified: trunk/nuiton-processor/src/main/java/org/nuiton/processor/filters/DefaultFilter.java Log: refactor DefaultFilter class Modified: trunk/nuiton-processor/src/main/java/org/nuiton/processor/filters/DefaultFilter.java =================================================================== --- trunk/nuiton-processor/src/main/java/org/nuiton/processor/filters/DefaultFilter.java 2009-08-26 19:07:41 UTC (rev 260) +++ trunk/nuiton-processor/src/main/java/org/nuiton/processor/filters/DefaultFilter.java 2009-08-26 19:08:21 UTC (rev 261) @@ -16,35 +16,80 @@ * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* */ /******************************************************************************* * DefaultFilter.java - * + * * Created: Wed Sep 4 2002 - * + * * @author <poussin@codelutin.com>Copyright Code Lutin - * + * * @version $Revision$ - * + * * Mise a jour: $Date$ par : $Author$ */ package org.nuiton.processor.filters; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * A common astract filter. + * + * Implements the methods : + * <ul> + * <li>{@link #getHeader()}</li> + * <li>{@link #getFooter()}</li> + * <li>{@link #performInFilter(String)}</li> + * <li>{@link #performOutFilter(String)}</li> + * </ul> + * + * @author poussin + */ public abstract class DefaultFilter implements Filter { // DefaultFilter - public static int NOT_FOUND = -1; + public final static int NOT_FOUND = -1; + /** to use log facility, just put in your code: log.info(\"...\"); */ + static private final Log log = LogFactory.getLog(DefaultFilter.class); + /** + * le buffer interne pour conserve ce qui n'a pas encore été écrit + */ protected StringBuffer cachedContent = new StringBuffer(); - public DefaultFilter() { - } - /** Type of states */ - protected static class State { + protected enum State { /** Looking-for-header state */ - public final static State SEARCH_HEADER = new State(); + SEARCH_HEADER, /** Looking-for-footer state */ - public final static State SEARCH_FOOTER = new State(); + SEARCH_FOOTER; } - protected State currentState = State.SEARCH_HEADER; + /** + * l'état interne du filtre + */ + protected State state = State.SEARCH_HEADER; + /** + * methode appele lorsqu'on a la chaine entiere entre le header et le + * footer. + * + * @param ch la chaine trouve + * @return ce qu'il faut ecrire dans le fichier de sortie + */ + abstract protected String performInFilter(String ch); + + /** + * methode appele lorsqu'on a la chaine entiere a l'exterieur du + * header/footer + * + * @param ch la chaine trouve + * @return ce qu'il faut ecrire dans le fichier de sortie + */ + abstract protected String performOutFilter(String ch); + + /** @return la chaine du header */ + abstract protected String getHeader(); + + /** @return la chaine du footer */ + abstract protected String getFooter(); + @Override public String parse(String input) { int matchingIndex; @@ -53,34 +98,38 @@ cachedContent.append(input); String content = cachedContent.toString(); - if (currentState.equals(State.SEARCH_HEADER)) { + if (state.equals(State.SEARCH_HEADER)) { // Looking for a header matchingIndex = getMatchIndexFor(content, getHeader()); if (matchingIndex != NOT_FOUND) { // Header found, return "out" part of it + recursive process of remaining chars - currentState = State.SEARCH_FOOTER; - cachedContent.setLength(0); - return performOutFilter(content.substring(0, matchingIndex)) + parse(content.substring(matchingIndex)); + changeState(State.SEARCH_FOOTER); + StringBuilder buffer = new StringBuilder(); + buffer.append(performOutFilter(content.substring(0, matchingIndex))); + buffer.append(parse(content.substring(matchingIndex))); + return buffer.toString(); } // No header found ! return EMPTY_STRING; } - if (currentState.equals(State.SEARCH_FOOTER)) { + if (state.equals(State.SEARCH_FOOTER)) { // Looking for a footer matchingIndex = getMatchIndexFor(content, getFooter()); if (matchingIndex != NOT_FOUND) { // Footer found, return "in" part of it + recursive process of remaining chars - currentState = State.SEARCH_HEADER; - cachedContent.setLength(0); + changeState(State.SEARCH_HEADER); int matchingEndIndex = matchingIndex + getMatchLengthFor(getFooter()); - return performInFilter(performHeaderFooterFilter(content.substring(0, matchingEndIndex))) + parse(content.substring(matchingEndIndex)); + StringBuilder buffer = new StringBuilder(); + buffer.append(performInFilter(performHeaderFooterFilter(content.substring(0, matchingEndIndex)))); + buffer.append(parse(content.substring(matchingEndIndex))); + return buffer.toString(); +// return performInFilter(performHeaderFooterFilter(content.substring(0, matchingEndIndex))) + parse(content.substring(matchingEndIndex)); } // No footer found ! return EMPTY_STRING; } - - return "INVALID STATE in DefaultFilter !"; + throw new IllegalStateException("state " + state + " is unkwon..."); } public String performHeaderFooterFilter(String ch) { @@ -106,35 +155,20 @@ // Empty the cache, cachedContent.setLength(0); // and returns its content - if (currentState.equals(State.SEARCH_HEADER)) { + if (state.equals(State.SEARCH_HEADER)) { return performOutFilter(line); } return performInFilter(line); } - /** - * methode appele lorsqu'on a la chaine entiere entre le header et le - * footer. - * - * @param ch - * la chaine trouve - * @return ce qu'il faut ecrire dans le fichier de sortie - */ - abstract protected String performInFilter(String ch); + protected void changeState(State newState) { + if (log.isDebugEnabled()) { + log.debug("change state : <old:" + this.state + ", new:" + newState + ">"); + } - /** - * methode appele lorsqu'on a la chaine entiere a l'exterieur du - * header/footer - * - * @param ch - * la chaine trouve - * @return ce qu'il faut ecrire dans le fichier de sortie - */ - abstract protected String performOutFilter(String ch); + this.state = newState; + // le changement d'état réinitialise toujours le buffer interne + cachedContent.setLength(0); + } +} // DefaultFilter - /** @return la chaine du header */ - abstract protected String getHeader(); - - /** @return la chaine du footer */ - abstract protected String getFooter(); -} // DefaultFilter