1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.contract.model.util; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.Collections; |
20 | |
import java.util.Comparator; |
21 | |
import java.util.Enumeration; |
22 | |
import java.util.List; |
23 | |
import java.util.Stack; |
24 | |
|
25 | |
import org.kuali.student.contract.model.MessageStructure; |
26 | |
import org.kuali.student.contract.model.Service; |
27 | |
import org.kuali.student.contract.model.ServiceContractModel; |
28 | |
import org.kuali.student.contract.model.XmlType; |
29 | |
import org.kuali.student.contract.writer.HtmlWriter; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | 0 | public class HtmlContractWriter { |
36 | |
|
37 | |
private HtmlWriter writer; |
38 | |
private ServiceContractModel model; |
39 | |
private ModelFinder finder; |
40 | |
private String directory; |
41 | |
|
42 | |
public HtmlContractWriter(String directory, |
43 | 0 | ServiceContractModel model) { |
44 | 0 | this.writer = new HtmlWriter(directory, "index.html", |
45 | |
"Service Contracts Index"); |
46 | 0 | this.directory = directory; |
47 | 0 | this.model = model; |
48 | 0 | this.finder = new ModelFinder(this.model); |
49 | 0 | } |
50 | |
|
51 | |
public void write() { |
52 | 0 | this.writeIndexPage(); |
53 | 0 | for (Service service : model.getServices()) { |
54 | 0 | HtmlContractServiceWriter swriter = new HtmlContractServiceWriter(service, |
55 | |
directory, |
56 | |
model); |
57 | 0 | swriter.write(); |
58 | 0 | } |
59 | 0 | for (XmlType xmlType : model.getXmlTypes()) { |
60 | 0 | HtmlContractMessageStructureWriter msWriter = |
61 | |
new HtmlContractMessageStructureWriter( |
62 | |
xmlType, |
63 | |
directory, |
64 | |
model); |
65 | 0 | msWriter.write(); |
66 | 0 | } |
67 | 0 | } |
68 | 0 | private static final Comparator<XmlType> XML_TYPE_NAME_COMPARATOR = |
69 | 0 | new Comparator<XmlType>() { |
70 | |
|
71 | |
@Override |
72 | |
public int compare(XmlType e1, XmlType e2) { |
73 | 0 | return e1.getName().toLowerCase().compareTo(e2.getName().toLowerCase()); |
74 | |
} |
75 | |
}; |
76 | 0 | private static final Comparator<Service> SERVICE_NAME_COMPARATOR = |
77 | 0 | new Comparator<Service>() { |
78 | |
|
79 | |
@Override |
80 | |
public int compare(Service e1, Service e2) { |
81 | 0 | return e1.getName().compareTo(e2.getName()); |
82 | |
} |
83 | |
}; |
84 | 0 | private static final Comparator<Service> SERVICE_IMPL_NAME_COMPARATOR = |
85 | 0 | new Comparator<Service>() { |
86 | |
|
87 | |
@Override |
88 | |
public int compare(Service e1, Service e2) { |
89 | 0 | String x1 = calcArea(e1) + "." + e1.getName(); |
90 | 0 | String x2 = calcArea(e2) + "." + e2.getName(); |
91 | 0 | return x1.compareTo(x2); |
92 | |
} |
93 | |
}; |
94 | |
|
95 | |
private void writeIndexPage() { |
96 | 0 | writer.print("<a href=\"index.html\">home</a>"); |
97 | 0 | writer.writeTag("h1", "Service Contracts"); |
98 | 0 | writer.indentPrintln( |
99 | |
"<div class=\"panel\" style=\"background-color: rgb(255, 255, 255); border: 1px solid rgb(204, 204, 204);\">"); |
100 | 0 | writer.indentPrintln( |
101 | |
"<div class=\"panelHeader\" style=\"border-bottom: 1px solid rgb(204, 204, 204); background-color: rgb(238, 238, 238);\">"); |
102 | 0 | writer.indentPrintln("<b><a name=\"Services\"></a>Services</b>"); |
103 | 0 | writer.indentPrintln( |
104 | |
"</div><div class=\"panelContent\" style=\"background-color: rgb(255, 255, 255);\">"); |
105 | 0 | writer.indentPrintln("<ul>"); |
106 | 0 | List<Service> services = new ArrayList(model.getServices()); |
107 | 0 | Collections.sort(services, SERVICE_IMPL_NAME_COMPARATOR); |
108 | 0 | String oldArea = ""; |
109 | 0 | for (Service service : services) { |
110 | 0 | String newArea = calcArea(service); |
111 | 0 | if (!newArea.equals(oldArea)) { |
112 | 0 | if (!oldArea.isEmpty()) { |
113 | 0 | writer.indentPrintln("</ul>"); |
114 | 0 | writer.decrementIndent(); |
115 | |
} |
116 | 0 | writer.indentPrintln("<li>" + calcArea(service) + "</li>"); |
117 | 0 | writer.incrementIndent(); |
118 | 0 | writer.indentPrintln("<ul>"); |
119 | 0 | oldArea = newArea; |
120 | |
} |
121 | 0 | writer.indentPrint("<li>"); |
122 | 0 | writer.print("<a href=\"" + service.getName() + ".html" |
123 | |
+ "\">" + service.getName() + "</a>"); |
124 | 0 | writer.print("</li>"); |
125 | 0 | } |
126 | 0 | writer.indentPrintln("</ul>"); |
127 | 0 | writer.decrementIndent(); |
128 | 0 | writer.indentPrintln("</ul>"); |
129 | 0 | writer.indentPrintln("</div>"); |
130 | 0 | writer.indentPrintln("</div>"); |
131 | |
|
132 | 0 | this.writeMainOrRootList(); |
133 | |
|
134 | 0 | this.writeAlphabeticalList(); |
135 | |
|
136 | 0 | writer.writeHeaderBodyAndFooterOutToFile(); |
137 | |
|
138 | 0 | } |
139 | |
|
140 | |
private static String calcArea(Service service) { |
141 | 0 | return calcArea(service.getImplProject()); |
142 | |
} |
143 | |
|
144 | |
private static String calcArea(String implProject) { |
145 | |
|
146 | 0 | if (implProject.startsWith("org.kuali.student")) { |
147 | 0 | return "Kuali Student Services"; |
148 | |
} |
149 | 0 | if (implProject.startsWith("org.kuali.")) { |
150 | 0 | implProject = implProject.substring("org.kuali.".length()); |
151 | |
} |
152 | 0 | if (implProject.contains(".api.")) { |
153 | 0 | implProject = implProject.substring(0, implProject.indexOf(".api.")); |
154 | |
} |
155 | 0 | return implProject; |
156 | |
} |
157 | |
|
158 | |
private void writeMainOrRootList() { |
159 | 0 | Stack<String> stack = new Stack(); |
160 | 0 | List<XmlType> types = this.getMainMessageStructures(); |
161 | 0 | writer.indentPrintln( |
162 | |
"<div class=\"panel\" style=\"background-color: rgb(255, 255, 255); border: 1px solid rgb(204, 204, 204);\">"); |
163 | 0 | writer.indentPrintln( |
164 | |
"<div class=\"panelHeader\" style=\"border-bottom: 1px solid rgb(204, 204, 204); background-color: rgb(238, 238, 238);\">"); |
165 | 0 | writer.indentPrintln( |
166 | |
"<b><a name=\"MessageStructures\"></a> " + types.size() + " Main (root) Message Structures</b>"); |
167 | 0 | writer.indentPrintln( |
168 | |
"</div><div class=\"panelContent\" style=\"background-color: rgb(255, 255, 255);\">"); |
169 | 0 | writer.indentPrintln("<ul>"); |
170 | 0 | for (XmlType type : types) { |
171 | 0 | this.writeLink(type); |
172 | 0 | if (!stack.contains(type.getName())) { |
173 | 0 | stack.push(type.getName()); |
174 | 0 | this.writeSubStructures(type, stack); |
175 | 0 | stack.pop(); |
176 | |
} |
177 | |
} |
178 | 0 | writer.indentPrintln("</ul>"); |
179 | 0 | writer.indentPrintln("</div>"); |
180 | 0 | writer.indentPrintln("</div>"); |
181 | 0 | } |
182 | |
|
183 | |
private String stripListOffEnd(String name) { |
184 | 0 | if (name.endsWith("List")) { |
185 | 0 | return name.substring(0, name.length() - "List".length()); |
186 | |
} |
187 | 0 | return name; |
188 | |
} |
189 | |
|
190 | |
private void writeSubStructures(XmlType type, Stack<String> stack) { |
191 | 0 | boolean first = true; |
192 | 0 | for (MessageStructure ms : finder.findMessageStructures(type.getName())) { |
193 | 0 | XmlType st = finder.findXmlType(this.stripListOffEnd(ms.getType())); |
194 | 0 | if (st == null) { |
195 | 0 | throw new NullPointerException(ms.getType() + " does not exist in list of types with parents " + calcParents(stack)); |
196 | |
} |
197 | 0 | if (!st.getPrimitive().equalsIgnoreCase(XmlType.COMPLEX)) { |
198 | 0 | continue; |
199 | |
} |
200 | 0 | if (first) { |
201 | 0 | first = false; |
202 | 0 | writer.indentPrintln("<ul>"); |
203 | |
} |
204 | 0 | this.writeLink(st); |
205 | 0 | if (!stack.contains(st.getName())) { |
206 | 0 | stack.push(st.getName()); |
207 | 0 | this.writeSubStructures(st, stack); |
208 | 0 | stack.pop(); |
209 | |
} |
210 | 0 | } |
211 | 0 | if (!first) { |
212 | 0 | writer.indentPrintln("</ul>"); |
213 | |
} |
214 | 0 | } |
215 | |
|
216 | |
private String calcParents(Stack<String> stack) { |
217 | 0 | StringBuilder sb = new StringBuilder(); |
218 | 0 | String dot = ""; |
219 | 0 | Enumeration<String> en = stack.elements(); |
220 | 0 | while (en.hasMoreElements()) { |
221 | 0 | sb.append(dot); |
222 | 0 | dot = "."; |
223 | 0 | sb.append(en.nextElement()); |
224 | |
} |
225 | 0 | return sb.toString(); |
226 | |
} |
227 | |
|
228 | |
private void writeLink(XmlType type) { |
229 | 0 | writer.indentPrint("<li>"); |
230 | 0 | writer.print("<a href=\"" + type.getName() + ".html" |
231 | |
+ "\">" + type.getName() + "</a>"); |
232 | 0 | writer.print("</li>"); |
233 | 0 | } |
234 | |
|
235 | |
private List<XmlType> getMainMessageStructures() { |
236 | 0 | List<XmlType> types = new ArrayList(model.getXmlTypes().size()); |
237 | 0 | for (XmlType type : this.getComplexMessageStructures()) { |
238 | 0 | if (isMainMessageStructure(type)) { |
239 | 0 | types.add(type); |
240 | |
} |
241 | |
} |
242 | 0 | Collections.sort(types, XML_TYPE_NAME_COMPARATOR); |
243 | 0 | return types; |
244 | |
} |
245 | |
|
246 | |
private boolean isMainMessageStructure(XmlType xmlType) { |
247 | 0 | if (!HtmlContractMessageStructureWriter.calcOtherXmlTypeUsages(model, |
248 | |
xmlType).isEmpty()) { |
249 | 0 | return false; |
250 | |
} |
251 | 0 | return true; |
252 | |
} |
253 | |
|
254 | |
private List<XmlType> getComplexMessageStructures() { |
255 | 0 | List<XmlType> types = new ArrayList(model.getXmlTypes().size()); |
256 | 0 | for (XmlType type : model.getXmlTypes()) { |
257 | 0 | if (type.getPrimitive() == null) { |
258 | 0 | throw new NullPointerException(type.getName() |
259 | |
+ " has no primitive flag set"); |
260 | |
} |
261 | 0 | if (type.getPrimitive().equals(XmlType.COMPLEX)) { |
262 | 0 | types.add(type); |
263 | |
} |
264 | |
} |
265 | 0 | Collections.sort(types, XML_TYPE_NAME_COMPARATOR); |
266 | 0 | return types; |
267 | |
} |
268 | |
|
269 | |
private void writeAlphabeticalList() { |
270 | 0 | List<XmlType> types = this.getComplexMessageStructures(); |
271 | 0 | writer.indentPrintln( |
272 | |
"<div class=\"panel\" style=\"background-color: rgb(255, 255, 255); border: 1px solid rgb(204, 204, 204);\">"); |
273 | 0 | writer.indentPrintln( |
274 | |
"<div class=\"panelHeader\" style=\"border-bottom: 1px solid rgb(204, 204, 204); background-color: rgb(238, 238, 238);\">"); |
275 | 0 | writer.indentPrintln( |
276 | |
"<b><a name=\"MessageStructures\"></a>All " + types.size() + " Message Structures in Alphabetical Order</b>"); |
277 | 0 | writer.indentPrintln( |
278 | |
"</div><div class=\"panelContent\" style=\"background-color: rgb(255, 255, 255);\">"); |
279 | 0 | writer.indentPrintln("<ul>"); |
280 | 0 | for (XmlType type : types) { |
281 | 0 | this.writeLink(type); |
282 | |
} |
283 | 0 | writer.indentPrintln("</ul>"); |
284 | 0 | writer.indentPrintln("</div>"); |
285 | 0 | writer.indentPrintln("</div>"); |
286 | 0 | } |
287 | |
} |