ClassResolver.java
Alexander Hristov
0001
0020package org.java2web.html;
0021
0022import java.io.BufferedReader;
0023import java.io.File;
0024import java.io.FileReader;
0025import java.io.IOException;
0026import java.util.ArrayList;
0027import java.util.HashMap;
0028
0029
0036public class ClassResolver {
0037  
0042  protected HashMap<String, ArrayList<String>> classes = new HashMap<String,ArrayList<String>>();
0043  
0044  
0047  protected ArrayList<String> imports = new ArrayList<String>();
0048  
0049  
0050  
0054  public void addClass(String className) {
0055    int dot = className.lastIndexOf(".");
0056    if (dot == -1) throw new IllegalArgumentException("The class name must be fully qualified, and it is not ("+className+")");
0057    String simpleClassName = className.substring(dot+1);
0058    ArrayList<String> possibilities = classes.get(simpleClassName);
0059    if (possibilities == null) {
0060      possibilities = new ArrayList<String>();
0061      classes.put(simpleClassName,possibilities);
0062    }
0063    possibilities.add(className);
0064  }
0065  
0066  
0072  public int addJavadocPackage(String rootPath, String packageName ) {
0073    int read = 0;
0074    File packagePath = new File(rootPath,packageName.replace('.',File.separatorChar));
0075    if (!packagePath.exists()) return 0;
0076    String[] contents = packagePath.list();
0077    for (String name : contents) {
0078      int dot = name.indexOf(".");
0079      if (dot == -1) continue;
0080      String extension = name.substring(dot+1).toUpperCase();
0081      if (!extension.equals("HTML")) continue;
0082      addClass(packageName+"."+name.substring(0,dot));
0083      read++;
0084    }
0085    return read;
0086  }
0087  
0088  
0095  public int  addJavadocSet(String rootPath) throws IOException {
0096    int read = 0;
0097    File packageList = new File(rootPath,"package-list");
0098    if (!packageList.exists()) return 0;
0099    BufferedReader in = null;
0100    try {
0101      in = new BufferedReader(new FileReader(packageList));
0102      String packageName;
0103      while ( (packageName = in.readLine()) != null) {
0104        read += addJavadocPackage(rootPath,packageName);
0105      }
0106    } finally {
0107      if (in != null) try { in.close(); } catch (IOException e) {}
0108    }
0109    return read;
0110  }
0111  
0112  
0117  public void addImport(String importSequence) {
0118    importSequence = importSequence.trim();
0119    if (importSequence.endsWith(".*")) {
0120      importSequence = importSequence.substring(0,importSequence.length()-2);
0121    }
0122    imports.add(importSequence);
0123  }
0124  
0125  
0128  public void clearImports() {
0129    imports.clear();
0130  }
0131  
0132  
0138  public String findClass(String className) {
0139    
// Is it already qualified?
0140 if (className.indexOf(".") != -1) 0141 return className; 0142 0143
// See if we have an explicit import
0144 for (String imp : imports ) { 0145 if (imp.endsWith("."+className)) 0146 return imp; 0147 } 0148 0149 0150
// No implicit import - we must search the list of previously read classes
0151 ArrayList<String> possibilities = classes.get(className); 0152 0153
// If we have nothing for this class, then we cannot recognize the package
0154 if (possibilities == null) return null; 0155 0156 0157
// If there is only one variant, link to it.
0158
// We might be tempted to check whether this package has been really imported, but this is not
0159
// appropriate for two reasons:
0160
// - The class we are parsing might have resided in the same package as the one we are looking for - thus no import
0161
// - We might be converting into HTML just a code snippet, so we might not be seeing any imports
0162 if (possibilities.size() == 1) 0163 return possibilities.get(0);
// fqcn = fully qualified class name
0164 0165 0166
// We have several possibilites. See which one we have imported
0167 for (String qualifiedClassName : possibilities) { 0168 int dot = qualifiedClassName.lastIndexOf("."); 0169 String packageName = qualifiedClassName.substring(0,dot); 0170 for (String imp : imports) { 0171 if (imp.equals(packageName)) return qualifiedClassName; 0172 } 0173 } 0174 0175
// We have several possibilities, but none of them was explicitly imported. In such case
0176
// test if one of the possibilities is java.lang - which does not need an explicit import
0177 for (String qualifiedClassName : possibilities) { 0178 if (qualifiedClassName.equals("java.lang."+className)) 0179 return "java.lang."+className; 0180 } 0181 return null; 0182 } 0183} 0184
classes
imports
addClass (...)
addImport (...)
addJavadocPackage (...)
addJavadocSet (...)
clearImports (...)
findClass (...)
Coloring style :