/** * Copyright (C) 2008-2010, Squale Project - http://www.squale.org * * This file is part of Squale. * * Squale 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 3 of the * License, or any later version. * * Squale 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 Lesser General Public License * along with Squale. If not, see <http://www.gnu.org/licenses/>. */ /* * Cr�� le 13 avr. 04 * * Pour changer le mod�le de ce fichier g�n�r�, allez � : * Fen�tre>Pr�f�rences>Java>G�n�ration de code>Code et commentaires */ package org.squale.welcom.outils; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * @author M327837 Pour changer le mod�le de ce commentaire de type g�n�r�, allez � : * Fen�tre>Pr�f�rences>Java>G�n�ration de code>Code et commentaires */ public class TrimStringBuffer { /** logger */ private static Log log = LogFactory.getLog( TrimStringBuffer.class ); /** Par default on effectue le trim */ private static boolean trim = true; /** Surcharg� ? */ private static boolean override = false; /** Buffer */ private StringBuffer sb; /** Optimize */ private boolean optimize = TrimStringBuffer.trim; /** * Constructeur */ public TrimStringBuffer() { this( getDefaultOptimization() ); } /** * Constructeur * * @param pOptimize true si on est en optimise */ public TrimStringBuffer( final boolean pOptimize ) { this( new StringBuffer(), pOptimize ); } /** * Constructeur * * @param pSb StringBuffer * @param pOptimize : optimise */ public TrimStringBuffer( final StringBuffer pSb, final boolean pOptimize ) { this.sb = pSb; this.optimize = pOptimize; } /** * @return L'optimization dans les Welcomresources */ public static boolean getDefaultOptimization() { if ( !override ) { final String s = WelcomConfigurator.getMessage( "optiflux.cleanspaces" ); return Util.isTrue( s ); } return TrimStringBuffer.trim; } /** * Trim la chaine * * @param b : boolean */ public static void setTrim( final boolean b ) { override = true; trim = b; } /** * Ajoute un valeur * * @param value : Valeur */ public void append( final int value ) { sb.append( value ); } /** * Ajoute un valeur * * @param value : Valeur */ public void append( final boolean value ) { sb.append( value ); } /** * Ajoute un valeur * * @param value : Valeur */ public void append( final float value ) { sb.append( value ); } /** * Ajoute un valeur * * @param value : Valeur */ public void append( final String value ) { sb.append( value ); } /** * Ajoute un valeur * * @param value : Valeur */ public void append( final Object value ) { sb.append( value ); } /** * @return si on aptimize ou pas */ public boolean optimize() { return optimize; } /** * Renvoi le string buffer optmi� si necessaire * * @return le string buffer optmi� si necessaire */ public String toString() { if ( optimize ) { final String value = sb.toString(); try { final Pattern reg = Pattern.compile( "<\\s*(textarea|pre)[^>]*>([^<]*)<\\/\\s*\\1\\s*>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE ); final StringBuffer sbtmp = new StringBuffer(); final Matcher match = reg.matcher( value ); int start = 0; int end = 0; while ( match.find() ) { final String chaineANePasRempacer = match.group(); end = match.start(); final String chaineARemplacer = value.substring( start, end ); final String chainePurge = removeSpaces( chaineARemplacer ); sbtmp.append( chainePurge ); sbtmp.append( chaineANePasRempacer ); start = match.end(); } sbtmp.append( removeSpaces( value.substring( start, value.length() ) ) ); return sbtmp.toString(); } catch ( final Exception ex ) { log.error( ex, ex ); return sb.toString(); } } else { return sb.toString(); } } /** * Suppressiondes espaces * * @param value : valeurs d'origine * @return chaine sans espaces */ private String removeSpaces( String value ) { try { value = value.replaceAll( "[\n\r\t\f]", "" ); // value = regexp.subst(value,"",RE.REPLACE_ALL); // Suppression des espaces entre les balises // regexp = new RE("\\s*>\\s*"); // value = regexp.subst(value,">",RE.REPLACE_ALL); value = value.replaceAll( "\\s*>\\s*", ">" ); // regexp = new RE("\\s*<\\s*"); // value = regexp.subst(value,"<",RE.REPLACE_ALL); value = value.replaceAll( "\\s*<\\s*", "<" ); // Suppression des espaces multiples // regexp = new RE("\\s+\\s"); // value = regexp.subst(value," ",RE.REPLACE_ALL); value = value.replaceAll( "\\s+\\s", " " ); } catch ( final Exception ex ) { log.error( ex, ex ); } return value; } /** * Retourne la chaine su stringbuffer * * @param optimize : si on optimize * @param s : chaine a optimiser * @return : chaine finale */ public static String toString( final boolean optimize, final String s ) { final TrimStringBuffer sb = new TrimStringBuffer( optimize ); sb.append( s ); return sb.toString(); } /** * Retourne la chaine su stringbuffer * * @param s : chaine a optimiser * @return chaine optimis�e */ public static String toString( final String s ) { return TrimStringBuffer.toString( TrimStringBuffer.trim, s ); } }