1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.internal.transformation.macro

File CurrentMacroEntityReferenceResolver.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

4
10
1
1
83
42
4
0.4
10
1
4

Classes

Class Line # Actions
CurrentMacroEntityReferenceResolver 46 10 0% 4 0
1.0100%
 

Contributing tests

This file is covered by 4 tests. .

Source view

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.transformation.macro;
21   
22    import javax.inject.Inject;
23    import javax.inject.Named;
24    import javax.inject.Singleton;
25   
26    import org.xwiki.component.annotation.Component;
27    import org.xwiki.model.EntityType;
28    import org.xwiki.model.reference.EntityReference;
29    import org.xwiki.model.reference.EntityReferenceResolver;
30    import org.xwiki.rendering.block.Block;
31    import org.xwiki.rendering.block.MetaDataBlock;
32    import org.xwiki.rendering.block.match.MetadataBlockMatcher;
33    import org.xwiki.rendering.listener.MetaData;
34   
35    /**
36    * Resolves a String reference, usually passed as a Macro parameter, into a {@link EntityReference}, using any
37    * {@link MetaDataBlock} with a {@link MetaData#BASE} setting to resolve the any relative reference parts into a fully
38    * resolved object.
39    *
40    * @version $Id: 9d6092462e28b2271be595275a7b7873aa0e779b $
41    * @since 5.0M1
42    */
43    @Component
44    @Named("macro")
45    @Singleton
 
46    public class CurrentMacroEntityReferenceResolver implements EntityReferenceResolver<String>
47    {
48    /**
49    * Used to resolve references.
50    */
51    @Inject
52    @Named("current")
53    private EntityReferenceResolver<String> currentEntityReferenceResolver;
54   
 
55  1173 toggle @Override
56    public EntityReference resolve(String representation, EntityType entityType, Object... parameters)
57    {
58    // There must one parameter and it must be of type Block
59  1173 if (parameters.length != 1 || !(parameters[0] instanceof Block)) {
60  2 throw new IllegalArgumentException(String.format("You must pass one parameter of type [%s]",
61    Block.class.getName()));
62    }
63   
64  1171 Block currentBlock = (Block) parameters[0];
65   
66  1171 EntityReference result;
67   
68  1171 MetaDataBlock metaDataBlock =
69    currentBlock.getFirstBlock(new MetadataBlockMatcher(MetaData.BASE), Block.Axes.ANCESTOR);
70   
71    // If no Source MetaData was found resolve against the current entity as a failsafe solution.
72  1171 if (metaDataBlock == null) {
73  41 result = this.currentEntityReferenceResolver.resolve(representation, entityType);
74    } else {
75  1130 String sourceMetaData = (String) metaDataBlock.getMetaData().getMetaData(MetaData.BASE);
76  1130 result =
77    this.currentEntityReferenceResolver.resolve(representation, entityType,
78    this.currentEntityReferenceResolver.resolve(sourceMetaData, EntityType.DOCUMENT));
79    }
80   
81  1171 return result;
82    }
83    }