View Javadoc

1   package net.sourceforge.argval.packageinfo.impl;
2   
3   
4   import java.util.Iterator;
5   import java.util.List;
6   import java.util.Set;
7   import java.util.TreeSet;
8   
9   import net.sourceforge.argval.collection.CollectionFilter;
10  import net.sourceforge.argval.collection.CollectionUtil;
11  import net.sourceforge.argval.collection.CollectionUtilConfig;
12  import net.sourceforge.argval.collection.CollectionUtilConfigFactory;
13  import net.sourceforge.argval.collection.Filter;
14  import net.sourceforge.argval.packageinfo.PackageInfo;
15  import net.sourceforge.argval.packageinfo.PackageInfoVisitor;
16  import net.sourceforge.argval.utils.StringUtil;
17  
18  
19  /**
20   * Creates a plain text which shows the visited PackageInfo title 
21   * and (implementation) version. And shows all the dot separated package
22   * names, which are associated with the <code>Package</code>. 
23   * 
24   * @author $Author: $
25   * @version $Revision: $
26   * Last modified: $Date: $
27   */
28  final public class SimplePackageInfoVisitor implements PackageInfoVisitor {
29      public final static int MAX_PACKAGE_NAME_DEPTH = Integer.MAX_VALUE;
30  
31      private static final String PACKAGE_NAME_SEPARATOR = ".";
32      private final static String CONFIG_PACKAGE_NAME = "package name";
33  	
34      private StringBuffer strBuf = new StringBuffer();
35      private int packageNameDepth = MAX_PACKAGE_NAME_DEPTH;
36  
37  //    private static CollectionUtilConfig config;
38      
39      private CollectionFilter collectionFilter = null;
40      private CollectionUtilConfigFactory configFactory = null;
41  
42  	
43  	/**
44  	 * Create a plain text, which will include all package names assosiated with the
45  	 * visited <code>PackageInfo</code> instances.
46  	 */
47  	public SimplePackageInfoVisitor(CollectionUtilConfigFactory configFactory, CollectionFilter collectionFilter) {
48  		super();
49          this.configFactory = configFactory;
50          this.collectionFilter = collectionFilter;
51          configFactory.create(CONFIG_PACKAGE_NAME, PACKAGE_NAME_SEPARATOR);
52  	}
53      public SimplePackageInfoVisitor() {
54          this(new CollectionUtilConfigFactory(), new CollectionFilter());
55      }
56  	/**
57  	 * Create a plain text, of all visited PackageInfo instances. With or without the
58  	 * package names (Java language dot separated names, like 
59  	 * <code>net.sourceforge.argval.packageinfo</code> ). 
60  	 *  
61  	 * @param  isIncludePackageNameActive  When <code>true</code> all package names 
62  	 *         assosiated with this <code>Package</code> are added to the text.
63  	 */
64  	public SimplePackageInfoVisitor(boolean isIncludePackageNameActive) {
65  		this((isIncludePackageNameActive) ? MAX_PACKAGE_NAME_DEPTH : 0);
66  	}
67      public SimplePackageInfoVisitor(int packageNameDepth) {
68          this(packageNameDepth, null);
69      }
70      public SimplePackageInfoVisitor(Filter filter) {
71          this(MAX_PACKAGE_NAME_DEPTH, filter);
72      }
73      public SimplePackageInfoVisitor(int packageNameDepth, Filter filter) {
74          this();
75          this.packageNameDepth = packageNameDepth;
76          this.collectionFilter = new CollectionFilter(filter);
77      }
78  	
79  
80  	/** {@inheritDoc} 
81  	 *
82  	 * Adds the details (title and version) of each <code>PackageInfo</code> instance and 
83  	 * all the package names (The Java language dot separated package names), which are 
84  	 * associated with this package.
85  	 */
86  	public void visit(PackageInfo packageInfo) {
87          if (PackageInfo.UNKNOWN_PACKAGE_TITLE.equals(packageInfo.getTitle())) {
88              // Only include this if package names (and class names) should be included.
89              if (packageNameDepth > 0) {
90                  //strBuf.append(packageInfo.getTitle().toUpperCase());
91                  strBuf.append("Loaded packages, from which no Specification/Implementation title (and version) is known");
92              }
93          }
94          else {
95              strBuf.append(packageInfo.getTitle());
96          }
97          if (packageInfo.getImplementationVersion() != null) {
98              strBuf.append("  [").append(packageInfo.getImplementationVersion()).append("]");
99          }
100         strBuf.append(StringUtil.LINE_SEPARATOR);
101         if (packageNameDepth > 0) {
102             // Only chop package names, if the given package name depth (to show) is smaller then the maximum one.
103             Set<String> compactNameSet = (packageNameDepth == MAX_PACKAGE_NAME_DEPTH) 
104                     ? convert(packageInfo.getNameSet())
105                     : chop(convert(packageInfo.getNameSet()), configFactory.getConfig(CONFIG_PACKAGE_NAME), packageNameDepth);
106             
107             // Filter away package names that start with 'java' or 'sun' a.k. all the JDK packages.
108             Set<String> filteredSet = collectionFilter.filter(compactNameSet);        
109 
110             // Add the package names to the text 
111             for (Iterator<String> nameIter = filteredSet.iterator(); nameIter.hasNext(); ) {
112 	            strBuf.append("  ").append(nameIter.next()).append(StringUtil.LINE_SEPARATOR);
113 	        }
114         }
115         strBuf.append(StringUtil.LINE_SEPARATOR);
116 	}
117     
118     
119     private TreeSet<String> convert(Set<?> originalSet) {
120         TreeSet<String> newSet = new TreeSet<String>();
121         for (Object object : originalSet) {
122             newSet.add(object.toString());
123         }
124         return newSet;
125     }
126     
127     
128     // TODO [2007.07.06 tv] Refactor this
129     private static TreeSet<String> chop(Set<String> nameSet, CollectionUtilConfig config, int maxDepth) {
130         TreeSet<String> compactNameSet = new TreeSet<String>();
131         for (Iterator<String> iter = nameSet.iterator(); iter.hasNext(); ) {
132             String name = (String)iter.next();
133 //            System.out.println("name: " + name);
134             List<String> nameList = CollectionUtil.stringToList(name, PACKAGE_NAME_SEPARATOR);
135 //            System.out.println(CollectionUtil.toString(nameList));
136             while (nameList.size() > maxDepth) {
137 //                System.out.println("Removing index [" + nameList.size() + "] element '" + nameList.get(nameList.size() - 1) + "'");
138                 nameList.remove(nameList.size() - 1);
139             }
140             String compactName = CollectionUtil.toString(nameList, config);
141             compactNameSet.add(compactName);
142 //            System.out.println(compactName);
143         }
144         return compactNameSet;
145     }
146 
147 
148     /** {@inheritDoc} */
149 	public String toString() {
150 		return strBuf.toString();
151 	}
152 
153 }