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 17 package com.google.doclava; 18 19 import com.google.doclava.apicheck.ApiCheck; 20 import com.google.doclava.apicheck.ApiInfo; 21 import com.google.doclava.apicheck.ApiParseException; 22 23 import java.net.MalformedURLException; 24 import java.net.URL; 25 import java.util.ArrayList; 26 import 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 */ 33 public 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 */ addSite(String name, URL site)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 tagAll(ClassInfo[] classDocs)52 public void tagAll(ClassInfo[] classDocs) { 53 for (FederatedSite site : federatedSites) { 54 applyFederation(site, classDocs); 55 } 56 } 57 applyFederation(FederatedSite federationSource, ClassInfo[] classDocs)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 federateMethods(FederatedSite site, ClassInfo federatedClass, ClassInfo localClass)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 federateConstructors(FederatedSite site, ClassInfo federatedClass, ClassInfo localClass)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 federateFields(FederatedSite site, ClassInfo federatedClass, ClassInfo localClass)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 federateClass(FederatedSite source, ClassInfo doc)109 private void federateClass(FederatedSite source, ClassInfo doc) { 110 doc.addFederatedReference(source); 111 } 112 federatePackage(FederatedSite source, PackageInfo pkg)113 private void federatePackage(FederatedSite source, PackageInfo pkg) { 114 pkg.addFederatedReference(source); 115 } 116 }