001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.resource.bundles; 018 019import java.util.ArrayList; 020import java.util.Arrays; 021import java.util.LinkedHashSet; 022import java.util.List; 023import java.util.Set; 024 025import org.apache.wicket.Application; 026import org.apache.wicket.ResourceBundles; 027import org.apache.wicket.markup.head.HeaderItem; 028import org.apache.wicket.request.resource.IResource; 029import org.apache.wicket.request.resource.ResourceReference; 030 031/** 032 * A resource reference that wraps another resource to make it into a bundle. The resources that are 033 * provided by the wrapped reference, have to be added with 034 * {@link #addProvidedResources(org.apache.wicket.markup.head.HeaderItem...)}. Normally, you will 035 * have to register this bundle in {@link ResourceBundles} under 036 * {@link Application#getResourceBundles()}. Dependencies are inherited from the provided resources 037 * if the bundle does not provide them. 038 * 039 * @author papegaaij 040 */ 041public class ResourceBundleReference extends ResourceReference implements IResourceBundle 042{ 043 private static final long serialVersionUID = 1L; 044 045 private final ResourceReference bundleRef; 046 047 private final List<HeaderItem> providedResources; 048 049 /** 050 * Creates a new bundle reference from the given reference. 051 * 052 * @param bundleRef 053 */ 054 public ResourceBundleReference(ResourceReference bundleRef) 055 { 056 super(bundleRef.getScope(), bundleRef.getName(), bundleRef.getLocale(), 057 bundleRef.getStyle(), bundleRef.getVariation()); 058 059 this.bundleRef = bundleRef; 060 providedResources = new ArrayList<HeaderItem>(); 061 } 062 063 /** 064 * @return The resource reference that is served for this bundle 065 */ 066 public ResourceReference getBundleReference() 067 { 068 return bundleRef; 069 } 070 071 /** 072 * Adds the {@link HeaderItem}s that this bundle provides. 073 * 074 * @param items 075 */ 076 public void addProvidedResources(HeaderItem... items) 077 { 078 providedResources.addAll(Arrays.asList(items)); 079 } 080 081 @Override 082 public IResource getResource() 083 { 084 return getBundleReference().getResource(); 085 } 086 087 088 @Override 089 public Iterable<? extends HeaderItem> getProvidedResources() 090 { 091 return providedResources; 092 } 093 094 @Override 095 public List<HeaderItem> getDependencies() 096 { 097 Set<HeaderItem> ret = new LinkedHashSet<>(); 098 for (HeaderItem curProvided : providedResources) 099 { 100 for (HeaderItem curDependency : curProvided.getDependencies()) 101 ret.add(curDependency); 102 } 103 for (HeaderItem curProvided : providedResources) 104 { 105 ret.remove(curProvided); 106 } 107 List<HeaderItem> dependencies = super.getDependencies(); 108 dependencies.addAll(ret); 109 return dependencies; 110 } 111}