Home | History | Annotate | Download | only in html
      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.Version;
     22 import signature.compare.model.IApiDelta;
     23 import signature.compare.model.IClassDefinitionDelta;
     24 import signature.compare.model.IDelta;
     25 import signature.compare.model.IPackageDelta;
     26 import signature.compare.model.impl.SigDelta;
     27 import signature.io.IApiDeltaExternalizer;
     28 import signature.model.IClassDefinition;
     29 import signature.model.IPackage;
     30 
     31 import java.io.File;
     32 import java.io.FileOutputStream;
     33 import java.io.IOException;
     34 import java.text.DateFormat;
     35 import java.util.Date;
     36 import java.util.HashMap;
     37 import java.util.Map;
     38 import java.util.Set;
     39 
     40 public class HtmlDeltaExternalizer implements IApiDeltaExternalizer {
     41 
     42     private static final String OVERVIEW_PAGE_NAME = "changes.html";
     43     private static final String STYLE_SHEET_NAME = "styles.css";
     44     private static final String DELTA_FOLDER = "changes" + File.separator;
     45 
     46     public void externalize(String location, IApiDelta apiDelta)
     47             throws IOException {
     48         if (!location.endsWith(File.separator)) {
     49             location += File.separator;
     50         }
     51 
     52         File directory = new File(location);
     53         if (!directory.exists()) {
     54             directory.mkdirs();
     55         }
     56 
     57         copyStyleSheet(location);
     58 
     59         Map<String, String> commonInfos = new HashMap<String, String>();
     60         commonInfos.put("creation_time", DateFormat.getDateTimeInstance()
     61                 .format(new Date()));
     62         commonInfos.put("from_desc", apiDelta.getFrom().getName());
     63         commonInfos.put("to_desc", apiDelta.getTo().getName());
     64 
     65         // write overview page
     66         StringBuilder content = new StringBuilder();
     67         ApiOverviewPage apiOverviewPage = new ApiOverviewPage(apiDelta,
     68                 commonInfos);
     69         apiOverviewPage.writeTo(content);
     70         writeToFile(location + OVERVIEW_PAGE_NAME, content.toString());
     71 
     72         // write package overview
     73         Set<IPackageDelta> changedPackages = SigDelta.getChanged(apiDelta
     74                 .getPackageDeltas());
     75         if (!changedPackages.isEmpty()) {
     76 
     77             File file = new File(location + DELTA_FOLDER);
     78             if (!file.exists()) {
     79                 file.mkdir();
     80             }
     81 
     82             for (IPackageDelta packageDelta : changedPackages) {
     83                 content = new StringBuilder();
     84                 PackageOverviewPage packagePage = new PackageOverviewPage(
     85                         packageDelta, commonInfos);
     86                 packagePage.writeTo(content);
     87                 IPackage aPackage = getAnElement(packageDelta);
     88                 String packageOverviewFileName = location + DELTA_FOLDER
     89                         + "pkg_" + aPackage.getName() + ".html";
     90                 writeToFile(packageOverviewFileName, content.toString());
     91 
     92                 // write class overviews
     93                 for (IClassDefinitionDelta classDelta : packageDelta
     94                         .getClassDeltas()) {
     95                     content = new StringBuilder();
     96                     ClassOverviewPage classPage = new ClassOverviewPage(
     97                             classDelta, commonInfos);
     98                     classPage.writeTo(content);
     99                     IClassDefinition aClass = getAnElement(classDelta);
    100                     String classOverviewFileName = location + DELTA_FOLDER
    101                             + aPackage.getName() + "." + aClass.getName()
    102                             + ".html";
    103                     writeToFile(classOverviewFileName, content.toString());
    104                 }
    105             }
    106         }
    107         // write class overview
    108     }
    109 
    110     private static <T> T getAnElement(IDelta<T> delta) {
    111         if (delta.getFrom() != null) {
    112             return delta.getFrom();
    113         } else {
    114             return delta.getTo();
    115         }
    116     }
    117 
    118     private void copyStyleSheet(String directory) throws IOException {
    119         StringTemplate template = TemplateStore.getStringTemplate("Styles");
    120         template.setAttribute("version", Version.VERSION);
    121         writeToFile(directory + STYLE_SHEET_NAME, template.toString());
    122     }
    123 
    124     private void writeToFile(String fileName, String content)
    125             throws IOException {
    126         FileOutputStream fileOutputStream = new FileOutputStream(fileName);
    127         fileOutputStream.write(content.getBytes());
    128         fileOutputStream.flush();
    129         fileOutputStream.close();
    130     }
    131 }
    132