KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > imports > PkgControl


1 ////////////////////////////////////////////////////////////////////////////////
2
// checkstyle: Checks Java source code for adherence to a set of rules.
3
// Copyright (C) 2001-2005 Oliver Burn
4
//
5
// This library is free software; you can redistribute it and/or
6
// modify it under the terms of the GNU Lesser General Public
7
// License as published by the Free Software Foundation; either
8
// version 2.1 of the License, or (at your option) any later version.
9
//
10
// This library is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
// Lesser General Public License for more details.
14
//
15
// You should have received a copy of the GNU Lesser General Public
16
// License along with this library; if not, write to the Free Software
17
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
////////////////////////////////////////////////////////////////////////////////
19
package com.puppycrawl.tools.checkstyle.checks.imports;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.List JavaDoc;
25
26 /**
27  * Represents the a tree of guards for controlling whether packages are allowed
28  * to be used. Each instance must have a single parent or be the root node.
29  * Each instance may have zero or more children.
30  *
31  * @author Oliver Burn
32  */

33 class PkgControl
34 {
35     /** List of {@link Guard} objects to check. */
36     private final LinkedList JavaDoc mGuards = new LinkedList JavaDoc();
37     /** List of children {@link PkgControl} objects. */
38     private final List JavaDoc mChildren = new ArrayList JavaDoc();
39     /** The parent. Null indicates we are the root node. */
40     private final PkgControl mParent;
41     /** The full package name for the node. */
42     private final String JavaDoc mFullPackage;
43
44     /**
45      * Construct a root node.
46      * @param aPkgName the name of the package.
47      */

48     PkgControl(final String JavaDoc aPkgName)
49     {
50         assert aPkgName != null;
51         mParent = null;
52         mFullPackage = aPkgName;
53     }
54
55     /**
56      * Construct a child node.
57      * @param aParent the parent node.
58      * @param aSubPkg the sub package name.
59      */

60     PkgControl(final PkgControl aParent, final String JavaDoc aSubPkg)
61     {
62         assert aParent != null;
63         assert aSubPkg != null;
64         mParent = aParent;
65         mFullPackage = aParent.getFullPackage() + "." + aSubPkg;
66         mParent.mChildren.add(this);
67     }
68
69     /**
70      * Adds a guard to the node.
71      * @param aThug the guard to be added.
72      */

73     void addGuard(final Guard aThug)
74     {
75         mGuards.addFirst(aThug);
76     }
77
78     /**
79      * @return the full package name represented by the node.
80      */

81     String JavaDoc getFullPackage()
82     {
83         return mFullPackage;
84     }
85
86     /**
87      * Search down the tree to locate the finest match for a supplied package.
88      * @param aForPkg the package to search for.
89      * @return the finest match, or null if no match at all.
90      */

91     PkgControl locateFinest(final String JavaDoc aForPkg)
92     {
93         // Check if we are a match.
94
// This algormithm should be improved to check for a trailing "."
95
// or nothing following.
96
if (!aForPkg.startsWith(getFullPackage())) {
97             return null;
98         }
99
100         // Check if any of the children match.
101
final Iterator JavaDoc it = mChildren.iterator();
102         while (it.hasNext()) {
103             final PkgControl pc = (PkgControl) it.next();
104             final PkgControl match = pc.locateFinest(aForPkg);
105             if (match != null) {
106                 return match;
107             }
108         }
109
110         // No match so I am the best there is.
111
return this;
112     }
113
114     /**
115      * Returns whether a package is allowed to be used. The algorithm checks
116      * with the current node for a result, and if none is found then calls
117      * its parent looking for a match. This will recurse looking for match.
118      * If there is no clear result then {@link AccessResult#UNKNOWN} is
119      * returned.
120      * @param aForImport the package to check on.
121      * @param aInPkg the package doing the import.
122      * @return an {@link AccessResult}.
123      */

124     AccessResult checkAccess(final String JavaDoc aForImport, final String JavaDoc aInPkg)
125     {
126         final AccessResult retVal = localCheckAccess(aForImport, aInPkg);
127         if (retVal != AccessResult.UNKNOWN) {
128             return retVal;
129         }
130         else if (mParent == null) {
131             // we are the top, so default to not allowed.
132
return AccessResult.DISALLOWED;
133         }
134
135         return mParent.checkAccess(aForImport, aInPkg);
136     }
137
138     /**
139      * Checks whether any of the guards for this node control access to
140      * a specified package.
141      * @param aForImport the package to check.
142      * @param aInPkg the package doing the import.
143      * @return an {@link AccessResult}.
144      */

145     private AccessResult localCheckAccess(final String JavaDoc aForImport,
146         final String JavaDoc aInPkg)
147     {
148         final Iterator JavaDoc it = mGuards.iterator();
149         while (it.hasNext()) {
150             final Guard g = (Guard) it.next();
151             // Check if a Guard is only meant to be applied locally.
152
if (g.isLocalOnly() && !mFullPackage.equals(aInPkg)) {
153                 continue;
154             }
155             final AccessResult result = g.verifyImport(aForImport);
156             if (result != AccessResult.UNKNOWN) {
157                 return result;
158             }
159         }
160         return AccessResult.UNKNOWN;
161     }
162 }
163
Popular Tags