1/*
2 * Copyright (C) 2010 Google Inc.
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
17package com.google.doclava;
18
19import com.google.doclava.apicheck.ApiCheck;
20import com.google.doclava.apicheck.ApiInfo;
21import com.google.doclava.apicheck.ApiParseException;
22
23import java.net.MalformedURLException;
24import java.net.URL;
25import java.util.ArrayList;
26import java.util.List;
27
28/**
29 * Cross-references documentation among different libraries. A FederationTagger
30 * is populated with a list of {@link FederatedSite} objects which are linked
31 * against when overlapping content is discovered.
32 */
33public final class FederationTagger {
34  private final List<FederatedSite> federatedSites = new ArrayList<FederatedSite>();
35
36  /**
37   * Adds a Doclava documentation site for federation. Accepts the base URL of
38   * the remote API.
39   */
40  public void addSite(String name, URL site) {
41    try {
42      federatedSites.add(new FederatedSite(name, site));
43    } catch (ApiParseException e) {
44      String error = "Could not add site for federation: " + site;
45      if (e.getMessage() != null) {
46        error += ": " + e.getMessage();
47      }
48      Errors.error(Errors.NO_FEDERATION_DATA, null, error);
49    }
50  }
51
52  public void tagAll(ClassInfo[] classDocs) {
53    for (FederatedSite site : federatedSites) {
54      applyFederation(site, classDocs);
55    }
56  }
57
58  private void applyFederation(FederatedSite federationSource, ClassInfo[] classDocs) {
59    for (ClassInfo classDoc : classDocs) {
60      PackageInfo packageSpec
61          = federationSource.apiInfo().getPackages().get(classDoc.containingPackage().name());
62
63      if (packageSpec == null) {
64        continue;
65      }
66
67      ClassInfo classSpec = packageSpec.allClasses().get(classDoc.name());
68
69      if (classSpec == null) {
70        continue;
71      }
72
73      federateMethods(federationSource, classSpec, classDoc);
74      federateConstructors(federationSource, classSpec, classDoc);
75      federateFields(federationSource, classSpec, classDoc);
76      federateClass(federationSource, classDoc);
77      federatePackage(federationSource, classDoc.containingPackage());
78    }
79  }
80
81  private void federateMethods(FederatedSite site, ClassInfo federatedClass, ClassInfo localClass) {
82    for (MethodInfo method : localClass.methods()) {
83      for (ClassInfo superclass : federatedClass.hierarchy()) {
84        if (superclass.allMethods().containsKey(method.getHashableName())) {
85          method.addFederatedReference(site);
86          break;
87        }
88      }
89    }
90  }
91
92  private void federateConstructors(FederatedSite site, ClassInfo federatedClass,
93      ClassInfo localClass) {
94    for (MethodInfo constructor : localClass.constructors()) {
95      if (federatedClass.hasConstructor(constructor)) {
96        constructor.addFederatedReference(site);
97      }
98    }
99  }
100
101  private void federateFields(FederatedSite site, ClassInfo federatedClass, ClassInfo localClass) {
102    for (FieldInfo field : localClass.fields()) {
103      if (federatedClass.allFields().containsKey(field.name())) {
104        field.addFederatedReference(site);
105      }
106    }
107  }
108
109  private void federateClass(FederatedSite source, ClassInfo doc) {
110    doc.addFederatedReference(source);
111  }
112
113  private void federatePackage(FederatedSite source, PackageInfo pkg) {
114    pkg.addFederatedReference(source);
115  }
116}