KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tanesha > replacer > ReplacerEnvironment


1 /*
2  * libreplacer, Java library to support C-sprintf alike text formatting
3  * Copyright (C) 2002 Tanesha FTPD Project, www.tanesha.net
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 /*
20  * $Id: ReplacerEnvironment.java,v 1.6 2002/12/12 13:06:48 flower Exp $
21  */

22 package org.tanesha.replacer;
23
24 import java.util.Map JavaDoc;
25 import java.util.HashMap JavaDoc;
26
27 /**
28  * An environment for holding variables for replacement.
29  *
30  *
31  * @author Soren, www.tanesha.net
32  */

33 public class ReplacerEnvironment {
34
35     private Map JavaDoc _macros = new HashMap JavaDoc();
36     private ReplacerEnvironment _parent;
37
38     /**
39      * Create a new 'root' replacer environement.
40      *
41      */

42     public ReplacerEnvironment() {
43         this(null);
44     }
45
46     /**
47      * Create a new local replacer environment with a parent.
48      *
49      * @param parent the parent replacer environment
50      */

51     public ReplacerEnvironment(ReplacerEnvironment parent) {
52         _parent = parent;
53     }
54
55     protected Object JavaDoc resolve(String JavaDoc key) {
56         if (_macros.containsKey(key))
57             return _macros.get(key);
58         else if (_parent != null)
59             return _parent.resolve(key);
60
61         return null;
62     }
63
64     /**
65      * Adds a new object to the replacer environment
66      *
67      * @param key The name of the new object
68      * @param value the value of the new replacer item
69      * @return this for chaining add(.., ..)
70      */

71     public ReplacerEnvironment add(String JavaDoc key, Object JavaDoc value) {
72         _macros.put(key, value);
73
74         return this;
75     }
76
77     /**
78      * Create a new ReplacerEnvironment which has two parent environments.
79      *
80      * @param e1 The first replacer environment, this will be searched first.
81      * @param e2 The second replacer environment, this will be searched last.
82      */

83     public static ReplacerEnvironment chain(ReplacerEnvironment e1, ReplacerEnvironment e2) {
84         return new ChainedReplacerEnvironment(e1, e2);
85     }
86
87     private static class ChainedReplacerEnvironment extends ReplacerEnvironment {
88
89         ReplacerEnvironment _e2;
90
91         public ChainedReplacerEnvironment(ReplacerEnvironment e1, ReplacerEnvironment e2) {
92             super(e1);
93
94             _e2 = e2;
95         }
96
97         public Object JavaDoc resolve(String JavaDoc key) {
98             Object JavaDoc t = super.resolve(key);
99             if (t == null) {
100                 t = _e2.resolve(key);
101             }
102
103             return t;
104         }
105     }
106
107 }
108
Popular Tags