KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > ant > ConfigHolder


1 /*
2 /*
3  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
4  *
5  * http://www.izforge.com/izpack/
6  * http://developer.berlios.de/projects/izpack/
7  *
8  * Copyright 2002 Marcus Wolschon
9  * Copyright 2002 Jan Blok
10  * Copyright 2004 Klaus Bartz
11  *
12  * Licensed under the Apache License, Version 2.0 (the "License");
13  * you may not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  * http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  */

24 package com.izforge.izpack.ant;
25
26 import java.util.Enumeration JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import org.apache.tools.ant.BuildException;
30 import org.apache.tools.ant.Project;
31
32 /**
33  * A nested element holder for the installation configuration document content.
34  * The installation document must be passed in using a CDATA element.
35  *
36  * @author Scott Stark
37  * @version $Revision: 1708 $
38  */

39 public class ConfigHolder
40 {
41     /** The parent element project */
42     private Project project;
43
44     /** The config element body text with ${x} property references replaced */
45     private String JavaDoc installText;
46
47     /**
48      * Taken from the ant org.apache.tools.ant.PropertyHelper and '$' replaced
49      * with '@' to deal with @{x} style property references.
50      *
51      * Parses a string containing @{xxx} style property references
52      * into two lists. The first list is a collection of text fragments, while
53      * the other is a set of string property names. null entries in the
54      * first list indicate a property reference from the second list.
55      *
56      * It can be overridden with a more efficient or customized version.
57      *
58      * @param value Text to parse. Must not be null.
59      * @param fragments List to add text fragments to. Must not be null.
60      * @param propertyRefs List to add property names to. Must not be null.
61      *
62      * @exception BuildException if the string contains an opening @{ without a
63      * closing }
64      */

65     static void parseCompileProperties(String JavaDoc value, Vector JavaDoc fragments, Vector JavaDoc propertyRefs)
66             throws BuildException
67     {
68         int prev = 0;
69         int pos;
70         // search for the next instance of $ from the 'prev' position
71
while ((pos = value.indexOf("@", prev)) >= 0)
72         {
73
74             // if there was any text before this, add it as a fragment
75
// TODO, this check could be modified to go if pos>prev;
76
// seems like this current version could stick empty strings
77
// into the list
78
if (pos > 0)
79             {
80                 fragments.addElement(value.substring(prev, pos));
81             }
82             // if we are at the end of the string, we tack on a $
83
// then move past it
84
if (pos == (value.length() - 1))
85             {
86                 fragments.addElement("@");
87                 prev = pos + 1;
88             }
89             else if (value.charAt(pos + 1) != '{')
90             {
91                 // peek ahead to see if the next char is a property or not
92
// not a property: insert the char as a literal
93
/*
94                  * fragments.addElement(value.substring(pos + 1, pos + 2)); prev = pos + 2;
95                  */

96                 if (value.charAt(pos + 1) == '@')
97                 {
98                     // backwards compatibility two $ map to one mode
99
fragments.addElement("@");
100                     prev = pos + 2;
101                 }
102                 else
103                 {
104                     // new behaviour: $X maps to $X for all values of X!='$'
105
fragments.addElement(value.substring(pos, pos + 2));
106                     prev = pos + 2;
107                 }
108
109             }
110             else
111             {
112                 // property found, extract its name or bail on a typo
113
int endName = value.indexOf('}', pos);
114                 if (endName < 0)
115                 {
116                     throw new BuildException("Syntax error in property: " + value);
117                 }
118                 String JavaDoc propertyName = value.substring(pos + 2, endName);
119                 fragments.addElement(null);
120                 propertyRefs.addElement(propertyName);
121                 prev = endName + 1;
122             }
123         }
124         // no more @ signs found
125
// if there is any tail to the file, append it
126
if (prev < value.length())
127         {
128             fragments.addElement(value.substring(prev));
129         }
130     }
131
132     ConfigHolder(Project project)
133     {
134         this.project = project;
135     }
136
137     /**
138      * Called by ant to set the config element content. The content is scanned
139      * for @{x} style property references and replaced with the x project
140      * property.
141      *
142      * @param rawText - the raw config element body text.
143      */

144     public void addText(String JavaDoc rawText)
145     {
146         // Locate the @{x} references
147
Vector JavaDoc fragments = new Vector JavaDoc();
148         Vector JavaDoc propertyRefs = new Vector JavaDoc();
149         parseCompileProperties(rawText, fragments, propertyRefs);
150
151         // Replace the references with the project property value
152
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
153         Enumeration JavaDoc i = fragments.elements();
154         Enumeration JavaDoc j = propertyRefs.elements();
155
156         while (i.hasMoreElements())
157         {
158             String JavaDoc fragment = (String JavaDoc) i.nextElement();
159             if (fragment == null)
160             {
161                 String JavaDoc propertyName = (String JavaDoc) j.nextElement();
162                 Object JavaDoc replacement = null;
163
164                 // try to get it from the project
165
if (replacement == null)
166                 {
167                     replacement = project.getProperty(propertyName);
168                 }
169
170                 if (replacement == null)
171                 {
172                     project.log("Property @{" + propertyName + "} has not been set",
173                             Project.MSG_VERBOSE);
174                 }
175                 if (replacement != null)
176                     fragment = replacement.toString();
177                 else
178                     fragment = "@{" + propertyName + "}";
179             }
180             sb.append(fragment);
181         }
182
183         installText = sb.toString();
184     }
185
186     /**
187      * Get the config element body text with @{x} property references replaced
188      *
189      * @return the processed config element body text.
190      */

191     public String JavaDoc getText()
192     {
193         return installText;
194     }
195
196 }
197
Popular Tags