1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package signature.io.html;
18 
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.Map;
22 
23 import org.antlr.stringtemplate.StringTemplate;
24 
25 import signature.compare.model.IAnnotationFieldDelta;
26 import signature.compare.model.IClassDefinitionDelta;
27 import signature.compare.model.IConstructorDelta;
28 import signature.compare.model.IEnumConstantDelta;
29 import signature.compare.model.IFieldDelta;
30 import signature.compare.model.IMethodDelta;
31 import signature.compare.model.impl.SigDelta;
32 
33 public class ClassOverviewPage implements IEmitter {
34 
35     private static final String PAGE = "ClassOverviewPage";
36 
37     private final IClassDefinitionDelta classDelta;
38 
39     private FieldByNameComparator<IFieldDelta> fieldComparator =
40             new FieldByNameComparator<IFieldDelta>();
41     private ArrayList<IFieldDelta> removedFields;
42     private ArrayList<IFieldDelta> addedFields;
43     private ArrayList<IFieldDelta> changedFields;
44 
45     private FieldByNameComparator<IAnnotationFieldDelta> annotationfieldComparator =
46             new FieldByNameComparator<IAnnotationFieldDelta>();
47     private ArrayList<IAnnotationFieldDelta> removedAnnotationFields;
48     private ArrayList<IAnnotationFieldDelta> addedAnnotationFields;
49     private ArrayList<IAnnotationFieldDelta> changedAnnotationFields;
50 
51     private FieldByNameComparator<IEnumConstantDelta> enumConstantComparator =
52             new FieldByNameComparator<IEnumConstantDelta>();
53     private ArrayList<IEnumConstantDelta> removedEnumConstants;
54     private ArrayList<IEnumConstantDelta> addedEnumConstants;
55     private ArrayList<IEnumConstantDelta> changedEnumConstants;
56 
57     private ExecutableMemberComparator constructorComparator =
58             new ExecutableMemberComparator();
59     private ArrayList<IConstructorDelta> removedConstructors;
60     private ArrayList<IConstructorDelta> addedConstructors;
61     private ArrayList<IConstructorDelta> changedConstructors;
62 
63     private ExecutableMemberComparator methodComparator =
64             new ExecutableMemberComparator();
65     private ArrayList<IMethodDelta> removedMethods;
66     private ArrayList<IMethodDelta> addedMethods;
67     private ArrayList<IMethodDelta> changedMethods;
68 
69     private final Map<String, String> commonInfos;
70 
ClassOverviewPage(IClassDefinitionDelta classDelta, Map<String, String> commonInfos)71     public ClassOverviewPage(IClassDefinitionDelta classDelta,
72             Map<String, String> commonInfos) {
73         this.classDelta = classDelta;
74         this.commonInfos = commonInfos;
75         prepareData();
76     }
77 
prepareData()78     private void prepareData() {
79         if (classDelta.getFieldDeltas() != null) {
80             prepareFieldDeltas();
81         }
82         if (classDelta.getAnnotationFieldDeltas() != null) {
83             prepareAnnotationFieldDeltas();
84         }
85         if (classDelta.getEnumConstantDeltas() != null) {
86             prepareEnumConstantDeltas();
87         }
88         if (classDelta.getConstructorDeltas() != null) {
89             prepareConstructorDeltas();
90         }
91         if (classDelta.getMethodDeltas() != null) {
92             prepareMethodDeltas();
93         }
94     }
95 
prepareFieldDeltas()96     private void prepareFieldDeltas() {
97         removedFields = new ArrayList<IFieldDelta>(SigDelta
98                 .getRemoved(classDelta.getFieldDeltas()));
99         Collections.sort(removedFields, fieldComparator);
100 
101         addedFields = new ArrayList<IFieldDelta>(SigDelta.getAdded(classDelta
102                 .getFieldDeltas()));
103         Collections.sort(addedFields, fieldComparator);
104 
105         changedFields = new ArrayList<IFieldDelta>(SigDelta
106                 .getChanged(classDelta.getFieldDeltas()));
107         Collections.sort(changedFields, fieldComparator);
108     }
109 
prepareAnnotationFieldDeltas()110     private void prepareAnnotationFieldDeltas() {
111         removedAnnotationFields = new ArrayList<IAnnotationFieldDelta>(SigDelta
112                 .getRemoved(classDelta.getAnnotationFieldDeltas()));
113         Collections.sort(removedAnnotationFields, annotationfieldComparator);
114 
115         addedAnnotationFields = new ArrayList<IAnnotationFieldDelta>(SigDelta
116                 .getAdded(classDelta.getAnnotationFieldDeltas()));
117         Collections.sort(addedAnnotationFields, annotationfieldComparator);
118 
119         changedAnnotationFields = new ArrayList<IAnnotationFieldDelta>(SigDelta
120                 .getChanged(classDelta.getAnnotationFieldDeltas()));
121         Collections.sort(changedAnnotationFields, annotationfieldComparator);
122     }
123 
prepareEnumConstantDeltas()124     private void prepareEnumConstantDeltas() {
125         removedEnumConstants = new ArrayList<IEnumConstantDelta>(SigDelta
126                 .getRemoved(classDelta.getEnumConstantDeltas()));
127         Collections.sort(removedEnumConstants, enumConstantComparator);
128 
129         addedEnumConstants = new ArrayList<IEnumConstantDelta>(SigDelta
130                 .getAdded(classDelta.getEnumConstantDeltas()));
131         Collections.sort(addedEnumConstants, enumConstantComparator);
132 
133         changedEnumConstants = new ArrayList<IEnumConstantDelta>(SigDelta
134                 .getChanged(classDelta.getEnumConstantDeltas()));
135         Collections.sort(changedEnumConstants, enumConstantComparator);
136     }
137 
prepareConstructorDeltas()138     private void prepareConstructorDeltas() {
139         removedConstructors = new ArrayList<IConstructorDelta>(SigDelta
140                 .getRemoved(classDelta.getConstructorDeltas()));
141         Collections.sort(removedConstructors, constructorComparator);
142 
143         addedConstructors = new ArrayList<IConstructorDelta>(SigDelta
144                 .getAdded(classDelta.getConstructorDeltas()));
145         Collections.sort(addedConstructors, constructorComparator);
146 
147         changedConstructors = new ArrayList<IConstructorDelta>(SigDelta
148                 .getChanged(classDelta.getConstructorDeltas()));
149         Collections.sort(changedConstructors, constructorComparator);
150     }
151 
prepareMethodDeltas()152     private void prepareMethodDeltas() {
153         removedMethods = new ArrayList<IMethodDelta>(SigDelta
154                 .getRemoved(classDelta.getMethodDeltas()));
155         Collections.sort(removedMethods, methodComparator);
156 
157         addedMethods = new ArrayList<IMethodDelta>(SigDelta.getAdded(classDelta
158                 .getMethodDeltas()));
159         Collections.sort(addedMethods, methodComparator);
160 
161         changedMethods = new ArrayList<IMethodDelta>(SigDelta
162                 .getChanged(classDelta.getMethodDeltas()));
163         Collections.sort(changedMethods, methodComparator);
164     }
165 
writeTo(StringBuilder b)166     public void writeTo(StringBuilder b) {
167         StringTemplate template = TemplateStore.getStringTemplate(PAGE);
168 
169         template.setAttribute("class_delta", classDelta);
170         boolean annotationDelta = classDelta.getAnnotationDeltas() != null;
171         boolean modifierDelta = classDelta.getModifierDeltas() != null;
172         boolean typeVariableDelta = classDelta.getTypeVariableDeltas() != null;
173         boolean superClassDelta = classDelta.getSuperClassDelta() != null;
174         boolean interfaceDelta = classDelta.getInterfaceDeltas() != null;
175         boolean hasSignatureDelta = annotationDelta || modifierDelta
176                 || typeVariableDelta || superClassDelta || interfaceDelta;
177 
178         template.setAttribute("has_class_signature_delta", hasSignatureDelta);
179 
180         template.setAttribute("removed_fields", removedFields);
181         template.setAttribute("added_fields", addedFields);
182         template.setAttribute("changed_fields", changedFields);
183 
184         template.setAttribute("removed_annotation_fields",
185                 removedAnnotationFields);
186         template.setAttribute("added_annotation_fields", addedAnnotationFields);
187         template.setAttribute("changed_annotation_fields",
188                 changedAnnotationFields);
189 
190         template.setAttribute("removed_enum_constants", removedEnumConstants);
191         template.setAttribute("added_enum_constants", addedEnumConstants);
192         template.setAttribute("changed_enum_constants", changedEnumConstants);
193 
194         template.setAttribute("removed_constructors", removedConstructors);
195         template.setAttribute("added_constructors", addedConstructors);
196         template.setAttribute("changed_constructors", changedConstructors);
197 
198         template.setAttribute("removed_methods", removedMethods);
199         template.setAttribute("added_methods", addedMethods);
200         template.setAttribute("changed_methods", changedMethods);
201 
202         template.setArgumentContext(commonInfos);
203         b.append(template.toString());
204     }
205 }
206