001: /* ====================================================================
002: * The JRefactory License, Version 1.0
003: *
004: * Copyright (c) 2001 JRefactory. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by the
021: * JRefactory (http://www.sourceforge.org/projects/jrefactory)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. The names "JRefactory" must not be used to endorse or promote
026: * products derived from this software without prior written
027: * permission. For written permission, please contact seguin@acm.org.
028: *
029: * 5. Products derived from this software may not be called "JRefactory",
030: * nor may "JRefactory" appear in their name, without prior written
031: * permission of Chris Seguin.
032: *
033: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
034: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
035: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
036: * DISCLAIMED. IN NO EVENT SHALL THE CHRIS SEGUIN OR
037: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
038: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
039: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
040: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
041: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
042: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
043: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
044: * SUCH DAMAGE.
045: * ====================================================================
046: *
047: * This software consists of voluntary contributions made by many
048: * individuals on behalf of JRefactory. For more information on
049: * JRefactory, please see
050: * <http://www.sourceforge.org/projects/jrefactory>.
051: */
052: package org.acm.seguin.summary;
053:
054: import java.io.File;
055: import java.io.ObjectOutputStream;
056: import java.io.ObjectInputStream;
057: import java.io.IOException;
058: import java.util.LinkedList;
059: import java.util.HashMap;
060: import java.util.Iterator;
061:
062: /**
063: * Creates a summary of a package
064: *
065: *@author Chris Seguin
066: *@created May 5, 1999
067: *@since 2.6.31
068: */
069: public class PackageSummary extends Summary {
070: private LinkedList fileList;
071: // Instance Variables
072: private String name;
073:
074: // Class Variables
075: private static HashMap packageMap;
076:
077: /**
078: * Constructor for the package summary
079: *
080: *@since 2.6.31
081: *@param packageName the name of the package
082: */
083: protected PackageSummary(String packageName) {
084: // Initialize package summary - packages have no parents
085: super (null);
086:
087: name = packageName.intern();
088: fileList = null;
089: }
090:
091: /**
092: * Get a package summary object
093: *
094: *@since 2.6.31
095: *@return all package summaries
096: */
097: public static Iterator getAllPackages() {
098: if (packageMap == null) {
099: init();
100: }
101:
102: return packageMap.values().iterator();
103: }
104:
105: /**
106: * Get the directory associated with this package
107: *
108: *@since 2.6.31
109: *@return a file or null if none
110: */
111: public File getDirectory() {
112: Iterator iter = getFileSummaries();
113: if (iter == null) {
114: return null;
115: }
116:
117: while (iter.hasNext()) {
118: FileSummary next = (FileSummary) iter.next();
119: File result = next.getFile();
120: if (result != null) {
121: result = result.getParentFile();
122: if (result != null) {
123: return result;
124: }
125: }
126: }
127:
128: return null;
129: }
130:
131: /**
132: * Return an iterator of the files
133: *
134: *@since 2.6.31
135: *@return the iterator
136: */
137: public Iterator getFileSummaries() {
138: if (fileList == null) {
139: return null;
140: }
141:
142: return fileList.iterator();
143: }
144:
145: /**
146: * Get a file summary by file name
147: *
148: *@since 2.6.31
149: *@param name the name of the file summary
150: *@return the file summary if it is found and null otherwise
151: */
152: public FileSummary getFileSummary(String name) {
153: // Check for null pointers
154: if (name == null) {
155: return null;
156: }
157:
158: // Local Variables
159: if (fileList != null) {
160: Iterator iter = fileList.iterator();
161:
162: // Check for it
163: while (iter.hasNext()) {
164: FileSummary next = (FileSummary) iter.next();
165: if (name.equals(next.getName())) {
166: return next;
167: }
168: }
169: }
170:
171: // Hmm... not found
172: return null;
173: }
174:
175: /**
176: * Get the name of the package
177: *
178: *@since 2.6.31
179: *@return the package name
180: */
181: public String getName() {
182: return name;
183: }
184:
185: /**
186: * Get a package summary object
187: *
188: *@since 2.6.31
189: *@param name the name of the package that we are creating
190: *@return The PackageSummary value
191: */
192: public static PackageSummary getPackageSummary(String name) {
193: if (packageMap == null) {
194: init();
195: }
196:
197: PackageSummary result = (PackageSummary) packageMap.get(name);
198: if (result == null) {
199: result = new PackageSummary(name);
200: packageMap.put(name, result);
201: }
202:
203: return result;
204: }
205:
206: /**
207: * Determines if it is the top level package
208: *
209: *@since 2.6.31
210: *@return true if it is the top level
211: */
212: public boolean isTopLevel() {
213: return ((name == null) || (name.length() == 0));
214: }
215:
216: /**
217: * Provide method to visit a node
218: *
219: *@since 2.6.31
220: *@param visitor the visitor
221: *@param data the data for the visit
222: *@return some new data
223: */
224: public Object accept(SummaryVisitor visitor, Object data) {
225: return visitor.visit(this , data);
226: }
227:
228: /**
229: * Add a file summary
230: *
231: *@since 2.6.31
232: *@param fileSummary the file summary that we are adding
233: */
234: protected void addFileSummary(FileSummary fileSummary) {
235: if (fileSummary != null) {
236: if (fileList == null) {
237: initFileList();
238: }
239:
240: fileList.add(fileSummary);
241: }
242: }
243:
244: /**
245: * Delete a file summary
246: *
247: *@since 2.6.31
248: *@param fileSummary the file summary object that we are removing
249: */
250: public void deleteFileSummary(FileSummary fileSummary) {
251: if (fileSummary != null) {
252: if (fileList == null) {
253: initFileList();
254: }
255:
256: fileList.remove(fileSummary);
257: }
258: }
259:
260: /**
261: * Initialization method
262: *
263: *@since 2.6.31
264: */
265: private static void init() {
266: if (packageMap == null) {
267: packageMap = new HashMap();
268: }
269: }
270:
271: /**
272: * Initialize the file list
273: *
274: *@since 2.6.31
275: */
276: private void initFileList() {
277: fileList = new LinkedList();
278: }
279:
280: /**
281: * Loads all the packages from the object input stream
282: *
283: *@since 2.6.31
284: *@param in Description of Parameter
285: *@exception IOException Description of Exception
286: */
287: public static void loadAll(ObjectInputStream in) throws IOException {
288: try {
289: packageMap = (HashMap) in.readObject();
290:
291: if ((packageMap == null) || (packageMap.values() == null)) {
292: return;
293: }
294:
295: Iterator iter = packageMap.values().iterator();
296: while (iter.hasNext()) {
297: System.out.print("*");
298: PackageSummary nextPackage = (PackageSummary) iter
299: .next();
300: Iterator iter2 = nextPackage.getFileSummaries();
301: while ((iter2 != null) && iter2.hasNext()) {
302: System.out.print(".");
303: FileSummary nextFile = (FileSummary) iter2.next();
304: FileSummary.register(nextFile);
305: }
306: }
307:
308: System.out.println(" ");
309: } catch (ClassNotFoundException cnfe) {
310: packageMap = null;
311: cnfe.printStackTrace(System.out);
312: }
313: }
314:
315: /**
316: * Removes all the packages to allow them to be reloaded
317: *
318: *@since 2.6.31
319: */
320: public static void removeAll() {
321: packageMap = null;
322: init();
323: FileSummary.removeAll();
324: }
325:
326: /**
327: * Saves all the packages to an object output stream
328: *
329: *@since 2.6.31
330: *@param out Description of Parameter
331: *@exception IOException Description of Exception
332: */
333: public static void saveAll(ObjectOutputStream out)
334: throws IOException {
335: out.writeObject(packageMap);
336: }
337:
338: /**
339: * Converts this object to a string
340: *
341: *@since 2.6.31
342: *@return the string
343: */
344: public String toString() {
345: if (!isTopLevel()) {
346: return name;
347: } else {
348: return "<Top Level Package>";
349: }
350: }
351: }
352: // EOF
|