1 package net.sourceforge.jenesis4java.impl;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
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
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
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
87
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
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 }