001: /*
002: * $Id: org.eclipse.jdt.ui.prefs 5004 2006-03-17 20:47:08 -0800 (Fri, 17 Mar
003: * 2006) eelco12 $ $Revision: 5004 $ $Date: 2006-03-17 20:47:08 -0800 (Fri, 17
004: * Mar 2006) $
005: *
006: * ==============================================================================
007: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
008: * use this file except in compliance with the License. You may obtain a copy of
009: * the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
015: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
016: * License for the specific language governing permissions and limitations under
017: * the License.
018: */
019: package wicket.markup.html;
020:
021: import java.util.HashSet;
022: import java.util.Set;
023:
024: import wicket.util.lang.Packages;
025:
026: /**
027: * Default implementation of {@link IPackageResourceGuard}. By default, the
028: * extensions 'properties', 'html', 'class' and 'java' are blocked.
029: *
030: * @author eelcohillenius
031: */
032: public class PackageResourceGuard implements IPackageResourceGuard {
033: /** Set of extensions that are not allowed access. */
034: private Set blockedExtensions = new HashSet(4);
035:
036: /**
037: * Construct.
038: */
039: public PackageResourceGuard() {
040: blockedExtensions.add("properties");
041: blockedExtensions.add("class");
042: blockedExtensions.add("java");
043: blockedExtensions.add("html");
044: }
045:
046: /**
047: * @see wicket.markup.html.IPackageResourceGuard#accept(java.lang.Class,
048: * java.lang.String)
049: */
050: public boolean accept(Class scope, String path) {
051: String absolutePath = Packages.absolutePath(scope, path);
052: return acceptAbsolutePath(absolutePath);
053: }
054:
055: /**
056: * Whether the provided absolute path is accepted.
057: *
058: * @param path
059: * The absolute path, starting from the class root (packages are
060: * seperated with forward slashes instead of dots).
061: * @return True if accepted, false otherwise.
062: */
063: protected boolean acceptAbsolutePath(String path) {
064: int ixExtension = path.lastIndexOf('.');
065: int len = path.length();
066: final String ext;
067: if (ixExtension <= 0 || ixExtension == len
068: || (path.lastIndexOf('/') + 1) == ixExtension) {
069: ext = null;
070: } else {
071: ext = path.substring(ixExtension + 1).toLowerCase();
072: }
073: return acceptExtension(ext);
074: }
075:
076: /**
077: * Whether the provided extension is accepted.
078: *
079: * @param extension
080: * The extension, starting from the class root (packages are
081: * seperated with forward slashes instead of dots).
082: * @return True if accepted, false otherwise.
083: */
084: protected boolean acceptExtension(String extension) {
085: return (!blockedExtensions.contains(extension));
086: }
087:
088: /**
089: * Gets the set of extensions that are not allowed access.
090: *
091: * @return The set of extensions that are not allowed access
092: */
093: protected final Set getBlockedExtensions() {
094: return blockedExtensions;
095: }
096:
097: /**
098: * Sets the set of extensions that are not allowed access.
099: *
100: * @param blockedExtensions
101: * Set of extensions that are not allowed access
102: */
103: protected final void setBlockedExtensions(Set blockedExtensions) {
104: this.blockedExtensions = blockedExtensions;
105: }
106: }
|