1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.model.reference

File PageAttachmentReference.java

 

Coverage histogram

../../../../img/srcFileCovDistChart7.png
66% of files have more coverage

Code metrics

10
20
9
1
153
66
15
0.75
2.22
9
1.67

Classes

Class Line # Actions
PageAttachmentReference 35 20 0% 15 14
0.6410256664.1%
 

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.model.reference;
21   
22    import java.beans.Transient;
23   
24    import org.xwiki.model.EntityType;
25    import org.xwiki.stability.Unstable;
26   
27    /**
28    * Represents a reference to an Attachment (page reference and file name). Note that an attachment is always attached to
29    * a page.
30    *
31    * @version $Id: 300aef46f63d5951da499a902625690d0758dd78 $
32    * @since 10.6RC1
33    */
34    @Unstable
 
35    public class PageAttachmentReference extends EntityReference
36    {
37    /**
38    * Special constructor that transforms a generic entity reference into an {@link PageAttachmentReference}. It checks
39    * the validity of the passed reference (ie correct type and correct parent).
40    *
41    * @param reference the reference to be transformed
42    * @exception IllegalArgumentException if the passed reference is not a valid attachment reference
43    */
 
44  0 toggle public PageAttachmentReference(EntityReference reference)
45    {
46  0 super(reference);
47    }
48   
49    /**
50    * Clone an AttachmentReference, but replace one of the parent in the chain by a new one.
51    *
52    * @param reference the reference that is cloned
53    * @param oldReference the old parent that will be replaced
54    * @param newReference the new parent that will replace oldReference in the chain
55    * @since 3.3M2
56    */
 
57  0 toggle protected PageAttachmentReference(EntityReference reference, EntityReference oldReference,
58    EntityReference newReference)
59    {
60  0 super(reference, oldReference, newReference);
61    }
62   
63    /**
64    * Create a new attachment reference based on the attachment name and the parent page reference.
65    *
66    * @param fileName the name of the attachment
67    * @param parent the reference of the page
68    */
 
69  2 toggle public PageAttachmentReference(String fileName, PageReference parent)
70    {
71  2 super(fileName, EntityType.PAGE_ATTACHMENT, parent);
72    }
73   
74    /**
75    * Clone an PageAttachmentReference, but use the specified parent for its new parent.
76    *
77    * @param reference the reference to clone
78    * @param parent the new parent to use
79    * @since 10.8RC1
80    */
 
81  1 toggle public PageAttachmentReference(EntityReference reference, EntityReference parent)
82    {
83  1 super(reference, parent);
84    }
85   
86    /**
87    * {@inheritDoc} Overridden in order to verify the validity of the passed parent.
88    *
89    * @exception IllegalArgumentException if the passed parent is not a valid attachment reference parent (ie an
90    * attachment reference)
91    */
 
92  5 toggle @Override
93    protected void setParent(EntityReference parent)
94    {
95  5 if (parent instanceof PageReference) {
96  3 super.setParent(parent);
97  3 return;
98    }
99   
100  2 if (parent == null || parent.getType() != EntityType.PAGE) {
101  2 throw new IllegalArgumentException("Invalid parent reference [" + parent + "] in an attachment reference");
102    }
103   
104  0 super.setParent(new PageReference(parent));
105    }
106   
107    /**
108    * {@inheritDoc}
109    * <p>
110    * Overridden in order to verify the validity of the passed type.
111    * </p>
112    *
113    * @exception IllegalArgumentException if the passed type is not an attachment type
114    */
 
115  6 toggle @Override
116    protected void setType(EntityType type)
117    {
118  6 if (type != EntityType.PAGE_ATTACHMENT) {
119  1 throw new IllegalArgumentException("Invalid type [" + type + "] for an attachment reference");
120    }
121   
122  5 super.setType(type);
123    }
124   
125    /**
126    * @return the page reference contained in this attachment reference
127    */
 
128  0 toggle @Transient
129    public PageReference getPageReference()
130    {
131  0 return (PageReference) extractReference(EntityType.PAGE);
132    }
133   
 
134  0 toggle @Override
135    public PageAttachmentReference replaceParent(EntityReference oldParent, EntityReference newParent)
136    {
137  0 if (newParent == oldParent) {
138  0 return this;
139    }
140   
141  0 return new PageAttachmentReference(this, oldParent, newParent);
142    }
143   
 
144  2 toggle @Override
145    public PageAttachmentReference replaceParent(EntityReference newParent)
146    {
147  2 if (newParent == getParent()) {
148  1 return this;
149    }
150   
151  1 return new PageAttachmentReference(this, newParent);
152    }
153    }