|
| 1 | +/* |
| 2 | + * See the NOTICE file distributed with this work for additional |
| 3 | + * information regarding copyright ownership. |
| 4 | + * |
| 5 | + * This is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU Lesser General Public License as |
| 7 | + * published by the Free Software Foundation; either version 2.1 of |
| 8 | + * the License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * This software 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 software; if not, write to the Free |
| 17 | + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 18 | + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. |
| 19 | + */ |
| 20 | +package org.xwiki.rendering.internal.resolver; |
| 21 | + |
| 22 | +import java.lang.reflect.ParameterizedType; |
| 23 | + |
| 24 | +import javax.inject.Inject; |
| 25 | +import javax.inject.Named; |
| 26 | +import javax.inject.Provider; |
| 27 | +import javax.inject.Singleton; |
| 28 | + |
| 29 | +import org.xwiki.component.annotation.Component; |
| 30 | +import org.xwiki.component.manager.ComponentLookupException; |
| 31 | +import org.xwiki.component.manager.ComponentManager; |
| 32 | +import org.xwiki.component.util.DefaultParameterizedType; |
| 33 | +import org.xwiki.model.EntityType; |
| 34 | +import org.xwiki.model.reference.EntityReference; |
| 35 | +import org.xwiki.model.reference.EntityReferenceResolver; |
| 36 | +import org.xwiki.rendering.listener.reference.ResourceReference; |
| 37 | + |
| 38 | +/** |
| 39 | + * Default entry point to convert a resource reference into an entity reference. |
| 40 | + * |
| 41 | + * @version $Id$ |
| 42 | + * @since 17.0.0RC1 |
| 43 | + */ |
| 44 | +@Component |
| 45 | +@Named("relative") |
| 46 | +@Singleton |
| 47 | +public class RelativeResourceReferenceEntityReferenceResolver implements EntityReferenceResolver<ResourceReference> |
| 48 | +{ |
| 49 | + /** |
| 50 | + * Type instance for EntityReferenceResolver<ResourceReference>. |
| 51 | + */ |
| 52 | + public static final ParameterizedType TYPE_RESOURCEREFERENCE = |
| 53 | + new DefaultParameterizedType(null, EntityReferenceResolver.class, ResourceReference.class); |
| 54 | + |
| 55 | + private static final String RELATIVE_HINT = "relative/%s"; |
| 56 | + |
| 57 | + @Inject |
| 58 | + @Named("context") |
| 59 | + private Provider<ComponentManager> componentManagerProvider; |
| 60 | + |
| 61 | + @Override |
| 62 | + public EntityReference resolve(ResourceReference resourceReference, EntityType type, Object... parameters) |
| 63 | + { |
| 64 | + if (resourceReference == null) { |
| 65 | + return null; |
| 66 | + } |
| 67 | + |
| 68 | + if (this.componentManagerProvider.get().hasComponent(TYPE_RESOURCEREFERENCE, |
| 69 | + getRelativeHint(resourceReference))) { |
| 70 | + EntityReferenceResolver<ResourceReference> resolver; |
| 71 | + try { |
| 72 | + resolver = this.componentManagerProvider.get().getInstance(TYPE_RESOURCEREFERENCE, |
| 73 | + getRelativeHint(resourceReference)); |
| 74 | + } catch (ComponentLookupException e) { |
| 75 | + throw new RuntimeException( |
| 76 | + String.format("Unknown error when trying to load resolver for reference [%s]", resourceReference), |
| 77 | + e); |
| 78 | + } |
| 79 | + |
| 80 | + return resolver.resolve(resourceReference, type, parameters); |
| 81 | + } |
| 82 | + |
| 83 | + // Unsupported resource reference type |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + private String getRelativeHint(ResourceReference resourceReference) |
| 88 | + { |
| 89 | + return String.format(RELATIVE_HINT, resourceReference.getType().getScheme()); |
| 90 | + } |
| 91 | +} |
0 commit comments