1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.rendering.block.match

File MetadataBlockMatcher.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
41% of files have more coverage

Code metrics

2
11
4
1
93
35
5
0.45
2.75
4
1.25

Classes

Class Line # Actions
MetadataBlockMatcher 32 11 0% 5 3
0.823529482.4%
 

Contributing tests

This file is covered by 118 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.block.match;
21   
22    import org.xwiki.rendering.block.Block;
23    import org.xwiki.rendering.block.MetaDataBlock;
24    import org.xwiki.rendering.listener.MetaData;
25   
26    /**
27    * Implementation of {@link BlockMatcher} which matches {@link MetaData} information.
28    *
29    * @version $Id: d939875149aba6eb28434f256722080eca505af7 $
30    * @since 3.0M3
31    */
 
32    public class MetadataBlockMatcher extends ClassBlockMatcher
33    {
34    /**
35    * The key of the {@link MetaData}.
36    */
37    private String metadataKey;
38   
39    /**
40    * The value of the {@link MetaData}.
41    */
42    private Object metadataValue;
43   
44    /**
45    * Match {@link MetaDataBlock} containing the provided key.
46    *
47    * @param metadataKey the key of the {@link MetaData}
48    */
 
49  250695 toggle public MetadataBlockMatcher(String metadataKey)
50    {
51  250692 this(metadataKey, null);
52    }
53   
54    /**
55    * Match {@link MetaDataBlock} containing the provided key/value pair.
56    *
57    * @param metadataKey the key of the {@link MetaData}
58    * @param metadataValue the value of the {@link MetaData}
59    */
 
60  250694 toggle public MetadataBlockMatcher(String metadataKey, Object metadataValue)
61    {
62  250694 super(MetaDataBlock.class);
63   
64  250694 this.metadataKey = metadataKey;
65  250693 this.metadataValue = metadataValue;
66    }
67   
 
68  568533 toggle @Override
69    public boolean match(Block block)
70    {
71  568548 return super.match(block) && matchMetadata(((MetaDataBlock) block).getMetaData());
72    }
73   
74    /**
75    * Matches the {@link MetaData} for provided key and value.
76    *
77    * @param metadata the {@link MetaData} to analyze
78    * @return true is the {@link MetaData} is matched, false otherwise
79    */
 
80  256609 toggle private boolean matchMetadata(MetaData metadata)
81    {
82  256610 boolean match;
83   
84  256613 if (this.metadataValue != null) {
85  0 Object value = metadata.getMetaData(this.metadataKey);
86  0 match = value != null && value.equals(this.metadataValue);
87    } else {
88  256610 match = metadata.contains(this.metadataKey);
89    }
90   
91  256615 return match;
92    }
93    }