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 org.antlr.stringtemplate.StringTemplate;
20 
21 import signature.compare.model.IApiDelta;
22 import signature.compare.model.IPackageDelta;
23 import signature.compare.model.impl.SigDelta;
24 
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.Map;
29 
30 public class ApiOverviewPage implements IEmitter {
31 
32     private final IApiDelta apiDelta;
33 
34     private List<IPackageDelta> removedPackages;
35     private List<IPackageDelta> addedPackages;
36     private List<IPackageDelta> changedPackages;
37     private Map<String, String> commonInfos;
38 
ApiOverviewPage(IApiDelta apiDelta, Map<String, String> commonInfos)39     public ApiOverviewPage(IApiDelta apiDelta,
40             Map<String, String> commonInfos) {
41         this.apiDelta = apiDelta;
42         this.commonInfos = commonInfos;
43         prepareData();
44     }
45 
prepareData()46     private void prepareData() {
47         if (apiDelta.getPackageDeltas().isEmpty()) {
48             commonInfos.put("no_delta", "no_delta");
49         }
50         removedPackages = new ArrayList<IPackageDelta>(SigDelta
51                 .getRemoved(apiDelta.getPackageDeltas()));
52         Collections.sort(removedPackages, new PackageByNameComparator());
53 
54         addedPackages = new ArrayList<IPackageDelta>(SigDelta.getAdded(apiDelta
55                 .getPackageDeltas()));
56         Collections.sort(addedPackages, new PackageByNameComparator());
57 
58         changedPackages = new ArrayList<IPackageDelta>(SigDelta
59                 .getChanged(apiDelta.getPackageDeltas()));
60         Collections.sort(changedPackages, new PackageByNameComparator());
61     }
62 
writeTo(StringBuilder b)63     public void writeTo(StringBuilder b) {
64         StringTemplate template = TemplateStore
65                 .getStringTemplate("ApiOverviewPage");
66         template.setArgumentContext(commonInfos);
67         template.setAttribute("removed_packages", removedPackages);
68         template.setAttribute("added_packages", addedPackages);
69         template.setAttribute("changed_packages", changedPackages);
70         b.append(template.toString());
71     }
72 }
73