Source Code Cross Referenced for KeySubst.java in  » Build » ANT » org » apache » tools » ant » taskdefs » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Build » ANT » org.apache.tools.ant.taskdefs 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         *
017:         */
018:
019:        package org.apache.tools.ant.taskdefs;
020:
021:        import java.io.BufferedReader;
022:        import java.io.BufferedWriter;
023:        import java.io.File;
024:        import java.io.FileReader;
025:        import java.io.FileWriter;
026:        import java.io.IOException;
027:        import java.util.Hashtable;
028:        import java.util.StringTokenizer;
029:        import org.apache.tools.ant.BuildException;
030:        import org.apache.tools.ant.Task;
031:
032:        /**
033:         * Keyword substitution. Input file is written to output file.
034:         * Do not make input file same as output file.
035:         * Keywords in input files look like this: @foo@. See the docs for the
036:         * setKeys method to understand how to do the substitutions.
037:         *
038:         * @since Ant 1.1
039:         * @deprecated KeySubst is deprecated since Ant 1.1. Use Filter + Copy
040:         * instead.
041:         */
042:        public class KeySubst extends Task {
043:            private File source = null;
044:            private File dest = null;
045:            private String sep = "*";
046:            private Hashtable replacements = new Hashtable();
047:
048:            /**
049:             * Do the execution.
050:             * @throws BuildException on error
051:             */
052:            public void execute() throws BuildException {
053:                log("!! KeySubst is deprecated. Use Filter + Copy instead. !!");
054:                log("Performing Substitutions");
055:                if (source == null || dest == null) {
056:                    log("Source and destinations must not be null");
057:                    return;
058:                }
059:                BufferedReader br = null;
060:                BufferedWriter bw = null;
061:                try {
062:                    br = new BufferedReader(new FileReader(source));
063:                    dest.delete();
064:                    bw = new BufferedWriter(new FileWriter(dest));
065:
066:                    String line = null;
067:                    String newline = null;
068:                    line = br.readLine();
069:                    while (line != null) {
070:                        if (line.length() == 0) {
071:                            bw.newLine();
072:                        } else {
073:                            newline = KeySubst.replace(line, replacements);
074:                            bw.write(newline);
075:                            bw.newLine();
076:                        }
077:                        line = br.readLine();
078:                    }
079:                    bw.flush();
080:                } catch (IOException ioe) {
081:                    ioe.printStackTrace();
082:                } finally {
083:                    if (bw != null) {
084:                        try {
085:                            bw.close();
086:                        } catch (IOException e) {
087:                            // ignore
088:                        }
089:                    }
090:                    if (br != null) {
091:                        try {
092:                            br.close();
093:                        } catch (IOException e) {
094:                            // ignore
095:                        }
096:                    }
097:                }
098:            }
099:
100:            /**
101:             * Set the source file.
102:             * @param s the source file
103:             */
104:            public void setSrc(File s) {
105:                this .source = s;
106:            }
107:
108:            /**
109:             * Set the destination file.
110:             * @param dest the destination file
111:             */
112:            public void setDest(File dest) {
113:                this .dest = dest;
114:            }
115:
116:            /**
117:             * Sets the separator between name=value arguments
118:             * in setKeys(). By default it is "*".
119:             * @param sep the separator string
120:             */
121:            public void setSep(String sep) {
122:                this .sep = sep;
123:            }
124:
125:            /**
126:             * Sets the keys.
127:             *
128:             * Format string is like this:
129:             *   <p>
130:             *   name=value*name2=value
131:             *   <p>
132:             *   Names are case sensitive.
133:             *   <p>
134:             *   Use the setSep() method to change the * to something else
135:             *   if you need to use * as a name or value.
136:             * @param keys a <code>String</code> value
137:             */
138:            public void setKeys(String keys) {
139:                if (keys != null && keys.length() > 0) {
140:                    StringTokenizer tok = new StringTokenizer(keys, this .sep,
141:                            false);
142:                    while (tok.hasMoreTokens()) {
143:                        String token = tok.nextToken().trim();
144:                        StringTokenizer itok = new StringTokenizer(token, "=",
145:                                false);
146:
147:                        String name = itok.nextToken();
148:                        String value = itok.nextToken();
149:                        replacements.put(name, value);
150:                    }
151:                }
152:            }
153:
154:            /**
155:             * A test method.
156:             * @param args not used
157:             */
158:            public static void main(String[] args) {
159:                try {
160:                    Hashtable hash = new Hashtable();
161:                    hash.put("VERSION", "1.0.3");
162:                    hash.put("b", "ffff");
163:                    System.out.println(KeySubst.replace(
164:                            "$f ${VERSION} f ${b} jj $", hash));
165:                } catch (Exception e) {
166:                    e.printStackTrace();
167:                }
168:            }
169:
170:            /**
171:             * Does replacement on text using the hashtable of keys.
172:             * @param origString an input string
173:             * @param keys       mapping of keys to values
174:             * @return the string with the replacements in it.
175:             * @throws BuildException on error
176:             */
177:            public static String replace(String origString, Hashtable keys)
178:                    throws BuildException {
179:                StringBuffer finalString = new StringBuffer();
180:                int index = 0;
181:                int i = 0;
182:                String key = null;
183:                while ((index = origString.indexOf("${", i)) > -1) {
184:                    key = origString.substring(index + 2, origString.indexOf(
185:                            "}", index + 3));
186:                    finalString.append(origString.substring(i, index));
187:                    if (keys.containsKey(key)) {
188:                        finalString.append(keys.get(key));
189:                    } else {
190:                        finalString.append("${");
191:                        finalString.append(key);
192:                        finalString.append("}");
193:                    }
194:                    i = index + 3 + key.length();
195:                }
196:                finalString.append(origString.substring(i));
197:                return finalString.toString();
198:            }
199:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.