Skip to content

Commit bf90ae8

Browse files
committed
Trim trailing whitespace when writing lines in java ref.
1 parent 99f0ddf commit bf90ae8

1 file changed

Lines changed: 38 additions & 37 deletions

File tree

java_generate/ReferenceGenerator/src/writers/TemplateWriter.java

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,46 @@
77
import java.util.HashMap;
88

99
public class TemplateWriter extends BaseWriter {
10-
10+
1111
public static String varPrefix = "<!-- ";
1212
public static String varSuffix = " -->";
1313
static String[] genericFields = {"classname", "returns", "related", "parameters", "syntax", "webcontentpath"};
1414
static String[] navFields = {"isLibrary", "isAlphabetical", "isLanguage"};
15-
15+
1616
public TemplateWriter()
1717
{
1818
}
19-
19+
2020
public void write( String templateName, HashMap<String, String> vars, String outputName ) throws IOException
2121
{
2222
for(String s : genericFields){
2323
if( ! vars.containsKey(s)){
2424
vars.put(s, "");
2525
}
2626
}
27-
27+
2828
int unsetNavCount = 0;
2929
for(String s : navFields){
3030
if(!vars.containsKey(s)){
3131
vars.put(s, "");
3232
unsetNavCount++;
3333
}else if(!vars.get(s).equals("")){
34-
vars.put(s, "class='active'");
34+
vars.put(s, "class='active'");
3535
}
36-
}
37-
36+
}
37+
3838
if(unsetNavCount == navFields.length){
3939
vars.put("isLanguage", "class='active'");
4040
}
41-
41+
4242
Boolean written = write( templateName, vars, outputName, false );
4343
write( templateName, vars, outputName, true );
4444
if( written && Shared.i().isNoisy() )
45-
{
45+
{
4646
System.out.println("Wrote " + outputName + " from template");
4747
}
4848
}
49-
49+
5050
// returns a relative path to root (e.g. "../../" from "path/to/File.ext", or "" for "File.txt")
5151
public String getRelativePathToRoot( String path )
5252
{
@@ -58,7 +58,7 @@ public String getRelativePathToRoot( String path )
5858
}
5959
return ret;
6060
}
61-
61+
6262
private Boolean write( String templateName, HashMap<String, String> vars, String outputName, Boolean isLocal ) throws IOException
6363
{
6464
String[] templateFile = FileUtils.loadStrings(Shared.i().TEMPLATE_DIRECTORY() + templateName);
@@ -73,57 +73,57 @@ private Boolean write( String templateName, HashMap<String, String> vars, String
7373
vars.put( "webcontentpath", "/" );
7474
vars.put("navigation", writePartial("nav.web.template.html", vars));
7575
}
76-
76+
7777
File f = new File( getWriterPath( outputName, isLocal ) );
78-
78+
7979
if( ! f.exists() )
80-
{
80+
{
8181
BufferedWriter out = makeWriter(outputName, isLocal);
82-
82+
8383
for( String line : templateFile )
8484
{
85-
//check if it contains a variable we want to replace, then replace it
86-
line = writeLine(line, vars, false);
85+
// check if it contains a variable we want to replace, then replace it
86+
line = writeLine(line, vars, false);
8787
output.add(line);
8888
}
8989
for( String line : output )
9090
{
91-
out.write(line+"\n");
91+
out.write(line+"\n");
9292
}
9393
out.close();
94-
94+
9595
return true;
9696
}
9797
else
9898
{
9999
return false;
100100
}
101101
}
102-
102+
103103
public String writePartial( String templateName, HashMap<String, String> vars )
104104
{ //use to write partials to be assigned to vars keys
105105
String[] templateFile = FileUtils.loadStrings(Shared.i().TEMPLATE_DIRECTORY()+templateName);
106106
String ret = "";
107-
107+
108108
for( String line : templateFile )
109109
{
110110
line = writeLine(line, vars, false );
111111
ret = ret.concat(line+"\n");
112112
}
113-
113+
114114
return ret;
115115
}
116-
116+
117117
public String writeLoop( String templateName, ArrayList<HashMap<String, String>> varSet )
118118
{
119119
return writeLoop(templateName, varSet, "\n");
120120
}
121-
121+
122122
public String writeLoop( String templateName, ArrayList<HashMap<String, String>> varSet, String separator )
123123
{
124124
String[] templateFile = FileUtils.loadStrings(Shared.i().TEMPLATE_DIRECTORY()+templateName);
125125
String ret = "";
126-
126+
127127
int index = 0;
128128
for( HashMap<String, String> vars : varSet )
129129
{
@@ -138,7 +138,7 @@ public String writeLoop( String templateName, ArrayList<HashMap<String, String>>
138138
}
139139
return ret;
140140
}
141-
141+
142142
private String writeLine(String line, HashMap<String, String> map, boolean isFinalLine )
143143
{
144144
for( String key : map.keySet())
@@ -149,20 +149,20 @@ private String writeLine(String line, HashMap<String, String> map, boolean isFin
149149
value = value.replace("$", "\\$");
150150
// what variable in html the value should replace
151151
String var = varPrefix + key + varSuffix;
152-
152+
153153
// place our value into the html
154154
line = line.replaceFirst(var, value);
155-
155+
156156
// find html that requires presence or lack of value
157157
String requireStart = varPrefix + "require:" + key + varSuffix;
158158
String requireEnd = varPrefix + "end" + varSuffix;
159159
String requireAbsenceStart = varPrefix + "unless:" + key + varSuffix;
160160
String unlessLastStart = varPrefix + "unless:last_fragment" + varSuffix;
161-
161+
162162
if(value.equals(""))
163163
{ //remove html around things that are absent (like images)
164164
while(line.contains(requireStart))
165-
{
165+
{
166166
String sub = line.substring(line.indexOf(requireStart), line.indexOf(requireEnd) + requireEnd.length());
167167
line = line.replace(sub, "");
168168
}
@@ -171,29 +171,30 @@ private String writeLine(String line, HashMap<String, String> map, boolean isFin
171171
{
172172
// remove things that should only exist in absence of this value
173173
while(line.contains(requireAbsenceStart))
174-
{
174+
{
175175
String sub = line.substring(line.indexOf(requireAbsenceStart), line.indexOf(requireEnd) + requireEnd.length());
176176
line = line.replace(sub, "");
177177
}
178178
}
179-
179+
180180
if( isFinalLine )
181181
{
182182
while(line.contains(unlessLastStart))
183-
{
183+
{
184184
String sub = line.substring(line.indexOf(unlessLastStart), line.indexOf(requireEnd) + requireEnd.length());
185185
line = line.replace(sub, "");
186186
}
187187
}
188-
188+
189189
// finally, remove all the meta junk
190190
line = line.replaceAll(requireStart, "");
191191
line = line.replaceAll(requireEnd, "");
192192
line = line.replaceAll(requireAbsenceStart, "");
193193
line = line.replaceAll(unlessLastStart, "");
194194
}
195-
}
196-
return line;
195+
}
196+
// Strip trailing whitespace (trim() removes beginning and end)
197+
return line.replaceAll("\\s+$", "");
197198
}
198-
199+
199200
}

0 commit comments

Comments
 (0)