1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.model.reference

File BlockReference.java

 

Coverage histogram

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

Code metrics

8
17
8
1
136
54
15
0.88
2.12
8
1.88

Classes

Class Line # Actions
BlockReference 35 17 0% 15 4
0.878787987.9%
 

Contributing tests

This file is covered by 10 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.model.reference;
21   
22    import org.xwiki.model.EntityType;
23   
24    /**
25    * Reference to a block, a structured part of the content of a document or an object property.
26    *
27    * While other references are generally unique, defined application wide, and usable to reach their target instance,
28    * the meaning of block references depends on their usage, may not be unique and are not necessarily a way to reach
29    * the referenced instance. We may have different kind of block references, for different purposes (for example
30    * identifying a header in the content, linking signature to macro block, etc...).
31    *
32    * @version $Id: 70e4310fdac55d33cf2d4a65b89cdbafe6aeb315 $
33    * @since 6.0M1
34    */
 
35    public class BlockReference extends EntityReference
36    {
37    /**
38    * Constructor which would raise exceptions if the source entity reference does not have the appropriate type or
39    * parent, etc.
40    *
41    * @param reference the raw reference to build this block reference from
42    */
 
43  6 toggle public BlockReference(EntityReference reference)
44    {
45  6 super(reference);
46    }
47   
48    /**
49    * Clone an BlockReference, but replace one of the parent in the chain by a new one.
50    *
51    * @param reference the reference that is cloned
52    * @param oldReference the old parent that will be replaced
53    * @param newReference the new parent that will replace oldReference in the chain
54    */
 
55  0 toggle protected BlockReference(EntityReference reference, EntityReference oldReference, EntityReference newReference)
56    {
57  0 super(reference, oldReference, newReference);
58    }
59   
60    /**
61    * @param blockName the name of the block
62    */
 
63  4 toggle public BlockReference(String blockName)
64    {
65  4 super(blockName, EntityType.BLOCK);
66    }
67   
68    /**
69    * @param blockName the name of the block
70    * @param documentReference the reference of the parent document of the block
71    */
 
72  6 toggle public BlockReference(String blockName, DocumentReference documentReference)
73    {
74  6 super(blockName, EntityType.BLOCK, documentReference);
75    }
76   
77    /**
78    * @param blockName the name of the block
79    * @param objectPropertyReference the reference of the parent object property of the block
80    */
 
81  1 toggle public BlockReference(String blockName, ObjectPropertyReference objectPropertyReference)
82    {
83  1 super(blockName, EntityType.BLOCK, objectPropertyReference);
84    }
85   
86    /**
87    * {@inheritDoc}
88    * <p>
89    * Overridden to check the type to be an block type.
90    * </p>
91    *
92    * @see org.xwiki.model.reference.EntityReference#setType(org.xwiki.model.EntityType)
93    */
 
94  19 toggle @Override
95    protected void setType(EntityType type)
96    {
97  19 if (type != EntityType.BLOCK) {
98  1 throw new IllegalArgumentException("Invalid type [" + type + "] for a block reference");
99    }
100   
101  18 super.setType(EntityType.BLOCK);
102    }
103   
104    /**
105    * {@inheritDoc}
106    * <p>
107    * Overridden to ensure that the parent of a block is either a document or an object property.
108    * </p>
109    *
110    * @see org.xwiki.model.reference.EntityReference#setParent(org.xwiki.model.reference.EntityReference)
111    */
 
112  18 toggle @Override
113    protected void setParent(EntityReference parent)
114    {
115  18 if (parent == null || parent instanceof DocumentReference || parent instanceof ObjectPropertyReference) {
116  15 super.setParent(parent);
117  15 return;
118    }
119   
120  3 if ((parent.getType() != EntityType.DOCUMENT && parent.getType() != EntityType.OBJECT_PROPERTY)) {
121  1 throw new IllegalArgumentException("Invalid parent reference [" + parent + "] in a block reference");
122    }
123   
124  2 if (parent.getType() == EntityType.DOCUMENT) {
125  1 super.setParent(new DocumentReference(parent));
126    } else {
127  1 super.setParent(new ObjectPropertyReference(parent));
128    }
129    }
130   
 
131  0 toggle @Override
132    public BlockReference replaceParent(EntityReference oldParent, EntityReference newParent)
133    {
134  0 return new BlockReference(this, oldParent, newParent);
135    }
136    }