001/* 002 * Copyright 2012 Atteo. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.atteo.evo.config.doclet; 017 018import java.io.File; 019import java.io.IOException; 020import java.io.InputStream; 021import java.io.InputStreamReader; 022import java.net.URI; 023import java.net.URISyntaxException; 024import java.util.Arrays; 025import java.util.HashMap; 026import java.util.HashSet; 027import java.util.Map; 028import java.util.Set; 029 030import com.google.common.base.Charsets; 031import com.google.common.io.CharStreams; 032import com.sun.javadoc.ClassDoc; 033import com.sun.javadoc.PackageDoc; 034import com.sun.javadoc.RootDoc; 035 036public class LinkGenerator { 037 private Set<PackageDoc> local = new HashSet<PackageDoc>(); 038 private Map<String, String> remote = new HashMap<String, String>(); 039 040 public void map(RootDoc root) { 041 for (String[] option : root.options()) { 042 if ("-link".equals(option[0]) && option.length >= 2) { 043 map(option[1]); 044 } else if ("-linkoffline".equals(option[0]) && option.length >= 2) { 045 map(option[1]); 046 } 047 } 048 local.addAll(Arrays.asList(root.specifiedPackages())); 049 } 050 051 private void map(String url) { 052 URI packageListUri = null; 053 try { 054 packageListUri = new URI(url + "/package-list"); 055 InputStream stream = packageListUri.toURL().openStream(); 056 for (String line : CharStreams.readLines(new InputStreamReader(stream, Charsets.UTF_8))) { 057 String packageName = line.trim(); 058 if (packageName.isEmpty()) { 059 continue; 060 } 061 remote.put(packageName, url); 062 } 063 } catch (URISyntaxException e) { 064 throw new RuntimeException("Cannot resolve url: " + url, e); 065 } catch (IOException e) { 066 System.out.println("Cannot open address: " + packageListUri); 067 } 068 } 069 070 public String getUrl(ClassDoc klass, PackageDoc from) { 071 PackageDoc packageDoc = klass.containingPackage(); 072 if (local.contains(packageDoc)) { 073 return getRelativePath(klass, from); 074 } 075 String url = remote.get(packageDoc.name()); 076 077 if (url == null) { 078 return null; 079 } 080 081 return url + "/" + klass.qualifiedName().replaceAll("\\.", "/") + ".html"; 082 } 083 084 private String getRelativePath(ClassDoc klass, PackageDoc from) { 085 String fromPath = from.name(); 086 String toPath = klass.containingPackage().name(); 087 String[] fromSplitted = fromPath.split("\\."); 088 String[] toSplitted = toPath.split("\\."); 089 090 int i = 0; 091 092 while (i < fromSplitted.length && i < toSplitted.length) { 093 if (!fromSplitted[i].equals(toSplitted[i])) { 094 break; 095 } 096 i++; 097 } 098 StringBuilder result = new StringBuilder(); 099 for (int j = i; j < fromSplitted.length; j++) { 100 result.append(".."); 101 result.append(File.separatorChar); 102 } 103 for (int j = i; j < toSplitted.length; j++) { 104 result.append(toSplitted[j]); 105 result.append(File.separatorChar); 106 } 107 result.append(klass.name()); 108 result.append(".html"); 109 return result.toString(); 110 } 111}