Index: lutinprocessor/src/java/org/codelutin/processor/filters/I18nFilter.java diff -u lutinprocessor/src/java/org/codelutin/processor/filters/I18nFilter.java:1.1 lutinprocessor/src/java/org/codelutin/processor/filters/I18nFilter.java:1.2 --- lutinprocessor/src/java/org/codelutin/processor/filters/I18nFilter.java:1.1 Mon Aug 16 20:47:12 2004 +++ lutinprocessor/src/java/org/codelutin/processor/filters/I18nFilter.java Tue Dec 7 17:19:09 2004 @@ -1,66 +1,139 @@ /* *##%% -* Copyright (C) 2002, 2003 Code Lutin -* -* This program is free software; you can redistribute it and/or -* modify it under the terms of the GNU General Public License -* as published by the Free Software Foundation; either version 2 -* 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 General Public License for more details. -* -* You should have received a copy of the GNU 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. -*##%%**/ - -/* * -* I18nFilter.java -* -* Created: Sun Sep 1 2002 -* -* @author -* Copyright Code Lutin -* @version $Revision: 1.1 $ -* -* Mise a jour: $Date: 2004/08/16 20:47:12 $ -* par : $Author: pineau $ -*/ + * Copyright (C) 2002, 2003 Code Lutin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * 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 General Public License for more details. + * + * You should have received a copy of the GNU 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. + *##%%**/ + +/******************************************************************************* + * I18nFilter.java + * + * Created: Sun Sep 1 2002 + * + * @author Copyright Code Lutin + * + * @version $Revision: 1.2 $ + * + * Mise a jour: $Date: 2004/12/07 17:19:09 $ par : $Author: pineau $ + */ package org.codelutin.processor.filters; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + + public class I18nFilter extends DefaultFilter { // I18nFilter - private String header = "_(\""; - private String footer = "\")"; + private String header = "I18n\\s*\\._\\(\\s*\""; + private String footer = "\"\\s*\\)"; + + private Pattern headerPattern = Pattern.compile(getHeader()); + private Pattern footerPattern = Pattern.compile(getFooter()); + + protected void setFooter(String footer) { + this.footer = footer; + } + + protected void setHeader(String header) { + this.header = header; + } - protected String getHeader(){ + protected String getHeader() { return header; } - protected String getFooter(){ + + protected String getFooter() { return footer; } + private Matcher matcher = null; + public int getMatchIndexFor(String input, String sequence) { + int index = NOT_FOUND; + + setMatcher(null); + if (sequence == getHeader()) { + setMatcher(getHeaderPattern().matcher(input)); + } else if (sequence == getFooter()){ + setMatcher(getFooterPattern().matcher(input)); + } + if (getMatcher() != null) { + try { + getMatcher().find(); + index = getMatcher().start(); + } catch (RuntimeException e) {} + } + + return index; + } + + public int getMatchLengthFor(String sequence) { + int length = NOT_FOUND; + + try { + length = getMatcher().end()-getMatcher().start(); + } catch (RuntimeException e) {} + + return length; + } + /** - * 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 - */ - protected String performInFilter(String ch){ - return ch+"="; + * 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 + */ + protected String performInFilter(String ch) { + return ch.substring(ch.indexOf('"')+1, ch.lastIndexOf('"')).replaceAll("\"\\s*\\+\\s*\"","") + "="; } /** - * 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 - */ - protected String performOutFilter(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 + */ + protected String performOutFilter(String ch) { return EMPTY_STRING; } + /** + * @return Returns the footerPattern. + */ + protected Pattern getFooterPattern() { + return footerPattern; + } + /** + * @return Returns the headerPattern. + */ + protected Pattern getHeaderPattern() { + return headerPattern; + } + /** + * @return Returns the matcher. + */ + protected Matcher getMatcher() { + return matcher; + } + /** + * @param matcher The matcher to set. + */ + protected void setMatcher(Matcher matcher) { + this.matcher = matcher; + } } // I18nFilter Index: lutinprocessor/src/java/org/codelutin/processor/filters/DefaultFilter.java diff -u lutinprocessor/src/java/org/codelutin/processor/filters/DefaultFilter.java:1.4 lutinprocessor/src/java/org/codelutin/processor/filters/DefaultFilter.java:1.5 --- lutinprocessor/src/java/org/codelutin/processor/filters/DefaultFilter.java:1.4 Mon Aug 16 20:47:12 2004 +++ lutinprocessor/src/java/org/codelutin/processor/filters/DefaultFilter.java Tue Dec 7 17:19:09 2004 @@ -23,16 +23,18 @@ * * @author Copyright Code Lutin * - * @version $Revision: 1.4 $ + * @version $Revision: 1.5 $ * - * Mise a jour: $Date: 2004/08/16 20:47:12 $ par : $Author: pineau $ + * Mise a jour: $Date: 2004/12/07 17:19:09 $ par : $Author: pineau $ */ package org.codelutin.processor.filters; public abstract class DefaultFilter implements Filter { // DefaultFilter - protected StringBuffer cache = new StringBuffer(); + public static int NOT_FOUND = -1; + + protected StringBuffer cachedContent = new StringBuffer(); public DefaultFilter() { @@ -50,54 +52,60 @@ protected State currentState = State.SEARCH_HEADER; public String parse(String input) { - int index; - if (currentState.equals(State.SEARCH_HEADER)) { + int matchingIndex; + + // add new input to current content + cachedContent.append(input); + String content = cachedContent.toString(); + + if (currentState.equals(State.SEARCH_HEADER)) { // Looking for a header - index = input.indexOf(getHeader()); - if (index != -1) { - // Header found, store last part of input, return first part of - // it. + matchingIndex = getMatchIndexFor(content, getHeader()); + if (matchingIndex != NOT_FOUND) { + // Header found, return "out" part of it + recursive process of remaining chars currentState = State.SEARCH_FOOTER; - cache.append(input.substring(0, index)); - String ch = cache.toString(); - cache.setLength(0); - return performOutFilter(ch) - + parse(input.substring(index + getHeader().length())); + cachedContent.setLength(0); + return performOutFilter(content.substring(0, matchingIndex)) + + parse(content.substring(matchingIndex)); } - // No header, cache the string - cache.append(input); + // No header found ! return EMPTY_STRING; } if (currentState.equals(State.SEARCH_FOOTER)) { // Looking for a footer - index = input.indexOf(getFooter()); - if (index != -1) { - // Footer found, add first part of input to the cache, - cache.append(input.substring(0, index)); - String ch = cache.toString(); - // then return parsing of last part of input. - currentState = State.SEARCH_HEADER; - cache.setLength(0); - return performInFilter(ch) - + parse(input.substring(index + getFooter().length())); - } - // No footer, add the input to cache - cache.append(input); + 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); + int matchingEndIndex = matchingIndex + getMatchLengthFor(getFooter()); + return performInFilter(content.substring(0, matchingEndIndex)) + + parse(content.substring(matchingEndIndex)); + } + // No footer found ! return EMPTY_STRING; } return "INVALID STATE in DefaultFilter !"; } - public boolean hasCachedData() { - return (cache.length() > 0); + public int getMatchIndexFor(String input, String sequence) { + return input.indexOf(sequence); + } + + public int getMatchLengthFor(String sequence) { + return sequence.length(); + } + + public boolean hasCachedData() { + return (cachedContent.length() > 0); } public String flush() { - String line = cache.toString(); + String line = cachedContent.toString(); // Empty the cache, - cache.setLength(0); + cachedContent.setLength(0); // and returns its content if (currentState.equals(State.SEARCH_HEADER)) return performOutFilter(line);