View Javadoc
1   package net.sourceforge.jenesis4java.impl;
2   
3   /*
4    * #%L
5    * Jenesis 4 Java Code Generator
6    * %%
7    * Copyright (C) 2000 - 2015 jenesis4java
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Lesser Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * #L%
23   */
24  
25  /**
26   * Copyright (C) 2008, 2010 Richard van Nieuwenhoven - ritchie [at] gmx [dot] at
27   * Copyright (C) 2000, 2001 Paul Cody Johnston - pcj@inxar.org <br>
28   * This file is part of Jenesis4java. Jenesis4java is free software: you can
29   * redistribute it and/or modify it under the terms of the GNU Lesser General
30   * Public License as published by the Free Software Foundation, either version 3
31   * of the License, or (at your option) any later version.<br>
32   * Jenesis4java is distributed in the hope that it will be useful, but WITHOUT
33   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
34   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
35   * details.<br>
36   * You should have received a copy of the GNU Lesser General Public License
37   * along with Jenesis4java. If not, see <http://www.gnu.org/licenses/>.
38   */
39  import java.util.Enumeration;
40  import java.util.List;
41  import java.util.Properties;
42  
43  import net.sourceforge.jenesis4java.CodeWriter;
44  import net.sourceforge.jenesis4java.Codeable;
45  import net.sourceforge.jenesis4java.Comment;
46  import net.sourceforge.jenesis4java.impl.util.BlockStyle;
47  
48  /**
49   * Base class for the block style classes.
50   */
51  abstract class MStyle implements BlockStyle {
52  
53      static class MStyleMap {
54  
55          Properties p;
56  
57          MStyleMap(Properties p) {
58              this.p = p;
59              initStyles();
60              initMap();
61          }
62  
63          void addStyle(String key, String className) {
64              try {
65                  // System.out.println("putting class for "+key);
66                  p.put(key, Class.forName(className).newInstance());
67              } catch (Exception ex) {
68                  ex.printStackTrace();
69              }
70          }
71  
72          BlockStyle get(String key) {
73              return (BlockStyle) p.get(key);
74          }
75  
76          void initMap() {
77              // get all the keys
78              Enumeration<?> e = p.propertyNames();
79  
80              String key;
81              while (e.hasMoreElements()) {
82                  key = (String) e.nextElement();
83                  if (key.startsWith("style.")) {
84                      continue;
85                  }
86                  // remap the object
87                  // System.out.println("getting key "+key+", property "+p.getProperty(key));
88                  Object remappedValue = p.get(p.getProperty(key));
89                  if (remappedValue != null) {
90                      p.put(key, remappedValue);
91                  }
92              }
93          }
94  
95          void initStyles() {
96              // get all the keys
97              Enumeration<?> e = p.propertyNames();
98  
99              String key;
100             while (e.hasMoreElements()) {
101                 key = (String) e.nextElement();
102                 if (key.startsWith("style.")) {
103                     addStyle(key, p.getProperty(key));
104                 }
105             }
106         }
107     }
108 
109     static class Lambda extends MStyle {
110 
111         @Override
112         public void toCode(CodeWriter out, List<? extends Codeable> codeableList, Comment ignored) {
113             if (!codeableList.isEmpty()) {
114                 out.write('{').indentLine().write(codeableList).dedentLine().write('}');
115             } else {
116                 out.write('{').newLine().write('}');
117             }
118         }
119     }
120 
121     static class Optional extends MStyle {
122 
123         @Override
124         public void toCode(CodeWriter out, List<? extends Codeable> codeableList, Comment ignored) {
125             if (codeableList.size() == 1) {
126                 out.indentLine().write(codeableList).dedentLine();
127             } else {
128                 if (!codeableList.isEmpty()) {
129                     out.space().write('{').indentLine().write(codeableList).dedentLine().write('}');
130                 } else {
131                     out.space().write('{').newLine().write('}');
132                 }
133             }
134         }
135     }
136 
137     static class SameLine extends MStyle {
138 
139         @Override
140         public void toCode(CodeWriter out, List<? extends Codeable> codeableList, Comment comment) {
141             if (!codeableList.isEmpty()) {
142                 out.space().write('{');
143                 MStyle.appendTrailingComment(out, comment);
144                 out.indentLine().write(codeableList).dedentLine().write('}');
145             } else {
146                 out.space().write('{');
147                 MStyle.appendTrailingComment(out, comment);
148                 out.ensureNewLine().write('}');
149             }
150         }
151     }
152 
153     static class SameLineTrailingWhitespace extends MStyle {
154 
155         private final SameLine decoratedElement = new SameLine();
156 
157         @Override
158         public void toCode(CodeWriter out, List<? extends Codeable> codeables, Comment inlineComment) {
159             decoratedElement.toCode(out, codeables, inlineComment);
160             out.space();
161         }
162     }
163 
164     static class Standard extends MStyle {
165 
166         @Override
167         public void toCode(CodeWriter out, List<? extends Codeable> codeableList, Comment comment) {
168             out.ensureNewLine();
169             if (!codeableList.isEmpty()) {
170                 out.write('{');
171                 MStyle.appendTrailingComment(out, comment);
172 
173                 out.indentLine().write(codeableList).dedentLine().write('}');
174             } else {
175                 out.write('{');
176                 MStyle.appendTrailingComment(out, comment);
177                 out.ensureNewLine().write('}');
178             }
179         }
180     }
181 
182     private static void appendTrailingComment(CodeWriter out, Comment comment) {
183         if (comment != null && comment.getType() == Comment.TRAILING) {
184             out.write(comment);
185         }
186     }
187 
188     MStyle() {
189     }
190 }