📄 packageresource.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.wicket.markup.html;import java.io.File;import java.io.IOException;import java.net.JarURLConnection;import java.net.URI;import java.net.URISyntaxException;import java.net.URL;import java.net.URLConnection;import java.util.ArrayList;import java.util.Enumeration;import java.util.List;import java.util.Locale;import java.util.jar.JarEntry;import java.util.jar.JarFile;import java.util.regex.Pattern;import javax.servlet.http.HttpServletResponse;import org.apache.wicket.AbortException;import org.apache.wicket.Application;import org.apache.wicket.RequestCycle;import org.apache.wicket.SharedResources;import org.apache.wicket.WicketRuntimeException;import org.apache.wicket.protocol.http.WebRequestCycle;import org.apache.wicket.protocol.http.servlet.AbortWithWebErrorCodeException;import org.apache.wicket.util.lang.Classes;import org.apache.wicket.util.lang.PackageName;import org.apache.wicket.util.lang.Packages;import org.apache.wicket.util.resource.IResourceStream;import org.apache.wicket.util.string.Strings;import org.apache.wicket.util.time.Time;import org.apache.wicket.util.watch.IModifiable;import org.slf4j.Logger;import org.slf4j.LoggerFactory;/** * Represents a localizable static resource. * <p> * Use like eg: * * <pre> * PackageResource IMG_UNKNOWN = PackageResource.get(EditPage.class, "questionmark.gif"); * </pre> * * where the static resource references image 'questionmark.gif' from the the package that EditPage * is in to get a package resource. * </p> * * @author Jonathan Locke * @author Eelco Hillenius */public class PackageResource extends WebResource implements IModifiable{ /** * Exception thrown when the creation of a package resource is not allowed. */ public static final class PackageResourceBlockedException extends WicketRuntimeException { private static final long serialVersionUID = 1L; /** * Construct. * * @param message */ public PackageResourceBlockedException(String message) { super(message); } } /** * common extension pattern for css files; matches all files with extension 'css'. * * @deprecated Will be removed in 2.0; contribute resources one by one instead */ public static final Pattern EXTENSION_CSS = Pattern.compile(".*\\.css"); /** * common extension pattern for javascript files; matches all files with extension 'js'. * * @deprecated Will be removed in 2.0; contribute resources one by one instead */ public static final Pattern EXTENSION_JS = Pattern.compile(".*\\.js"); /** log. */ private static final Logger log = LoggerFactory.getLogger(PackageResource.class); private static final long serialVersionUID = 1L; /** * Binds the resources that match the provided pattern to the given application object. Will * create any resources if not already in the shared resources of the application object. * * @param application * The application to bind to. * @param scope * The scope of the resource. * @param pattern * A regular expression to match against the contents of the package of the provided * scope class (eg ".*\\.js" will add all the files with extension * "js" from that package). * * @deprecated Since Wicket 1.2.1 this method is effectively a no-op. * {@link PackageResource package resources} are automatically tried and bound as * shared resources so that they don't have to be pre-registered anymore. Will be * removed in 2.0 */ public static void bind(Application application, Class scope, Pattern pattern) { } /** * Binds the resources that match the provided pattern to the given application object. Will * create any resources if not already in the shared resources of the application object and * does that recursively when the recurse parameter is true, or just for the scoped package if * that parameter is false * * @param application * The application to bind to. * @param scope * The scope of the resource. * @param pattern * A regular expression to match against the contents of the package of the provided * scope class (eg ".*\\.js" will add all the files with extension * "js" from that package). * @param recurse * Whether this method should recurse into sub packages * * @deprecated Since Wicket 1.2.1 this method is effectively a no-op. * {@link PackageResource package resources} are automatically tried and bound as * shared resources so that they don't have to be pre-registered anymore. Will be * removed in 2.0 */ public static void bind(Application application, Class scope, Pattern pattern, boolean recurse) { } /** * Binds a resource to the given application object. Will create the resource if not already in * the shared resources of the application object. * * @param application * The application to bind to. * @param scope * The scope of the resource. * @param name * The name of the resource (like "myfile.js") * @throw IllegalArgumentException when the requested package resource was not found */ public static void bind(Application application, Class scope, String name) { bind(application, scope, name, null, null); } /** * Binds a resource to the given application object. Will create the resource if not already in * the shared resources of the application object. * * @param application * The application to bind to. * @param scope * The scope of the resource. * @param name * The name of the resource (like "myfile.js") * @param locale * The locale of the resource. * @throw IllegalArgumentException when the requested package resource was not found */ public static void bind(Application application, Class scope, String name, Locale locale) { bind(application, scope, name, locale, null); } /** * Binds a resource to the given application object. Will create the resource if not already in * the shared resources of the application object. * * @param application * The application to bind to. * @param scope * The scope of the resource. * @param name * The name of the resource (like "myfile.js") * @param locale * The locale of the resource. * @param style * The style of the resource. * @throw IllegalArgumentException when the requested package resource was not found */ public static void bind(Application application, Class scope, String name, Locale locale, String style) { if (name == null) { throw new IllegalArgumentException("argument name may not be null"); } // first check on a direct hit for efficiency if (exists(scope, name, locale, style)) { // we have got a hit, so we may safely assume the name // argument is not a regular expression, and can thus // just add the resource and return get(scope, name, locale, style); } else { throw new IllegalArgumentException("no package resource was found for scope " + scope + ", name " + name + ", locale " + locale + ", style " + style); } } /** * Gets whether a resource for a given set of criteria exists. * * @param scope * This argument will be used to get the class loader for loading the package * resource, and to determine what package it is in. Typically this is the class in * which you call this method * @param path * The path to the resource * @param locale * The locale of the resource * @param style * The style of the resource (see {@link org.apache.wicket.Session}) * @return true if a resource could be loaded, false otherwise */ public static boolean exists(final Class scope, final String path, final Locale locale, final String style) { String absolutePath = Packages.absolutePath(scope, path); return Application.get().getResourceSettings().getResourceStreamLocator().locate(scope, absolutePath, style, locale, null) != null; } /** * Gets non-localized resources for a given set of criteria. Multiple resource can be loaded for * the same criteria if they match the pattern. If no resources were found, this method returns * null. * * @param scope * This argument will be used to get the class loader for loading the package * resource, and to determine what package it is in. Typically this is the calling * class/ the class in which you call this method * @param pattern * Regexp pattern to match resources * @return The resources, never null but may be empty * @deprecated Will be removed in 2.0; contribute resources one by one instead */ public static PackageResource[] get(Class scope, Pattern pattern) { return get(scope, pattern, false); } /** * Gets non-localized resources for a given set of criteria. Multiple resource can be loaded for * the same criteria if they match the pattern. If no resources were found, this method returns * null. * * @param scope * This argument will be used to get the class loader for loading the package * resource, and to determine what package it is in. Typically this is the calling * class/ the class in which you call this method * @param pattern * Regexp pattern to match resources * @param recurse * Whether this method should recurse into sub packages * @return The resources, never null but may be empty * @deprecated Will be removed in 2.0; contribute resources one by one instead */ public static PackageResource[] get(Class scope, Pattern pattern, boolean recurse) { final List resources = new ArrayList(); String packageRef = Strings.replaceAll(PackageName.forClass(scope).getName(), ".", "/") .toString(); ClassLoader loader = scope.getClassLoader(); try { // loop through the resources of the package Enumeration packageResources = loader.getResources(packageRef); while (packageResources.hasMoreElements()) { URL resource = (URL)packageResources.nextElement(); URLConnection connection = resource.openConnection(); if (connection instanceof JarURLConnection) { JarFile jf = ((JarURLConnection)connection).getJarFile(); scanJarFile(scope, pattern, recurse, resources, packageRef, jf); } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -