Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / tools / gnu / classpath / tools / giop / grmic / Generator.java
blob17ab821ecc937000f10fce64582b7827193c6ae5
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)
9 any later version.
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
19 02110-1301 USA.
22 package gnu.classpath.tools.giop.grmic;
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;
31 import java.util.Map;
33 /**
34 * Contains basic methods, used in code generation.
36 * @author Audrius Meskauskas, Lithuania (audriusa@Bioinformatics.org)
38 public class Generator
40 /**
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);
51 if (in == null)
52 throw new InternalError(getClass().getName() + ": no resource "
53 + resourcePath);
55 BufferedReader r = new BufferedReader(new InputStreamReader(in));
56 StringBuffer b = new StringBuffer();
58 String s;
59 try
61 while ((s = r.readLine()) != null)
63 b.append(s);
64 b.append('\n');
66 r.close();
68 catch (IOException e)
70 InternalError ierr = new InternalError("No expected resource " + name);
71 ierr.initCause(e);
72 throw ierr;
75 return b.toString();
78 /**
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.
83 * @param template
84 * the template string
85 * @param variables
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));
92 String s;
93 StringBuffer b = new StringBuffer(template.length());
94 try
96 Iterator iter;
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();
105 String variable;
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));
114 b.append(s);
115 b.append('\n');
117 r.close();
119 catch (IOException e)
121 // This should never happen.
122 InternalError ierr = new InternalError("");
123 ierr.initCause(e);
124 throw ierr;
126 return b.toString();