1 /* Generator.java -- Generic code generator.
2 Copyright (C) 2006 Free Software Foundation
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 package gnu
.classpath
.tools
.rmic
;
24 import java
.io
.BufferedReader
;
25 import java
.io
.IOException
;
26 import java
.io
.InputStream
;
27 import java
.io
.InputStreamReader
;
28 import java
.io
.StringReader
;
29 import java
.util
.Collection
;
30 import java
.util
.Iterator
;
34 * Contains basic methods, used in code generation.
36 * @author Audrius Meskauskas, Lithuania (audriusa@Bioinformatics.org)
38 public class Generator
41 * Get resource with the given name, as string.
43 * @param name the resource name
44 * @return the resourse string (in subfolder /templates).
46 public String
getResource(String name
)
48 String resourcePath
= "templates/" + name
;
49 InputStream in
= getClass().getResourceAsStream(resourcePath
);
52 throw new InternalError(getClass().getName() + ": no resource "
55 BufferedReader r
= new BufferedReader(new InputStreamReader(in
));
56 StringBuilder b
= new StringBuilder();
61 while ((s
= r
.readLine()) != null)
70 InternalError ierr
= new InternalError("No expected resource " + name
);
79 * Replace the variable references (starting from #) in the template string by
80 * the values, present in the given map. The strings, not present in the
81 * variable map, are ignored.
86 * the map of variables (name to value) to replace.
87 * @return the string with replaced values.
89 public String
replaceAll(String template
, Map variables
)
91 BufferedReader r
= new BufferedReader(new StringReader(template
));
93 StringBuilder b
= new StringBuilder(template
.length());
97 Collection vars
= variables
.keySet();
98 while ((s
= r
.readLine()) != null)
100 // At least one variable must appear in the string to make
101 // the string scan sensible.
102 if (s
.indexOf('#') >= 0)
104 iter
= vars
.iterator();
106 while (iter
.hasNext())
108 variable
= (String
) iter
.next();
109 if (s
.indexOf(variable
) >= 0)
110 s
= s
.replaceAll(variable
,
111 (String
) variables
.get(variable
));
119 catch (IOException e
)
121 // This should never happen.
122 InternalError ierr
= new InternalError("");