View Javadoc

1   /*
2    * Jour - java profiler and monitoring library
3    *
4    * Copyright (C) 2004 Jour team
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Library General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Library General Public License for more details.
15   *
16   * You should have received a copy of the GNU Library General Public
17   * License along with this library; if not, write to the
18   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   * Boston, MA  02111-1307, USA.
20   */
21  package net.sf.jour.rt.view;
22  
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Vector;
26  
27  import org.apache.log4j.Logger;
28  
29  import net.sf.jour.rt.view.config.TrimType;
30  
31  /***
32   * The list of Replacement expressions.
33   *
34   * Created on 16.12.2004
35   * Contributing Author(s):
36   *
37   *   Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
38   *   Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
39   *
40   * @author vlads
41   * @version $Revision: 1.1 $ ($Author: vlads $)  $Date: 2004/12/16 17:40:19 $
42   */
43  public class ReplacementList {
44  
45      protected static final Logger log = Logger.getLogger(ReplacementList.class);
46      
47      List expressions  = new Vector();
48      
49      public ReplacementList() {
50          
51      }
52      
53      /*
54       * If none of regular expression match, the original String is returned unchanged
55       */
56      public String subst(String substituteIn) {
57          for (Iterator i = expressions.iterator(); i.hasNext();) {
58              ReplacementExpression e = (ReplacementExpression) i.next();
59              substituteIn = e.subst(substituteIn);
60          }
61          return substituteIn;
62      }
63      
64      /*
65       * Use for unit tests.
66       */
67      void buildList(String[] trims) {
68          for(int i = 0; i < trims.length; i +=2) {
69              this.expressions.add(new ReplacementExpression(trims[i], trims[i + 1])); 
70          }
71      }
72      
73      public void debug() {
74          log.debug("expressions:" + expressions.size());
75          for (Iterator i = expressions.iterator(); i.hasNext();) {
76              ReplacementExpression e = (ReplacementExpression) i.next();
77              e.debug();
78          }
79      }
80      
81  	public void readConfig(List list) {
82          if (list == null) {
83              return;
84          }
85          for (Iterator i = list.iterator(); i.hasNext();) {
86              Object o = i.next();
87              if (o instanceof TrimType) {
88                  TrimType t = (TrimType) o;
89                  this.expressions.add(new ReplacementExpression(t.getName(), t.getValue()));
90              }
91          }
92      }
93      
94  }