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

File AttachmentReference.java

 

Coverage histogram

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

Code metrics

6
14
7
1
125
48
11
0.79
2
7
1.57

Classes

Class Line # Actions
AttachmentReference 33 14 0% 11 0
1.0100%
 

Contributing tests

This file is covered by 121 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   
26    /**
27    * Represents a reference to an Attachment (document reference and file name). Note that an attachment is always
28    * attached to a document.
29    *
30    * @version $Id: 962a3598d321fa0c1e5673e69372504760d454e2 $
31    * @since 2.2M1
32    */
 
33    public class AttachmentReference extends EntityReference
34    {
35    /**
36    * Special constructor that transforms a generic entity reference into an {@link AttachmentReference}. It checks the
37    * validity of the passed reference (ie correct type and correct parent).
38    *
39    * @param reference the reference to be transformed
40    * @exception IllegalArgumentException if the passed reference is not a valid attachment reference
41    */
 
42  998 toggle public AttachmentReference(EntityReference reference)
43    {
44  998 super(reference);
45    }
46   
47    /**
48    * Clone an AttachmentReference, but replace one of the parent in the chain by a new one.
49    *
50    * @param reference the reference that is cloned
51    * @param oldReference the old parent that will be replaced
52    * @param newReference the new parent that will replace oldReference in the chain
53    * @since 3.3M2
54    */
 
55  1 toggle protected AttachmentReference(EntityReference reference, EntityReference oldReference, EntityReference newReference)
56    {
57  1 super(reference, oldReference, newReference);
58    }
59   
60    /**
61    * Create a new attachment reference based on the attachment name and the parent document reference.
62    *
63    * @param fileName the name of the attachment
64    * @param parent the reference of the document
65    */
 
66  1301 toggle public AttachmentReference(String fileName, DocumentReference parent)
67    {
68  1301 super(fileName, EntityType.ATTACHMENT, parent);
69    }
70   
71    /**
72    * {@inheritDoc}
73    *
74    * Overridden in order to verify the validity of the passed parent.
75    *
76    * @exception IllegalArgumentException if the passed parent is not a valid attachment reference parent (ie an
77    * attachment reference)
78    */
 
79  2299 toggle @Override
80    protected void setParent(EntityReference parent)
81    {
82  2298 if (parent instanceof DocumentReference) {
83  1677 super.setParent(parent);
84  1678 return;
85    }
86   
87  621 if (parent == null || parent.getType() != EntityType.DOCUMENT) {
88  2 throw new IllegalArgumentException("Invalid parent reference [" + parent + "] in an attachment reference");
89    }
90   
91  619 super.setParent(new DocumentReference(parent));
92    }
93   
94    /**
95    * {@inheritDoc}
96    *
97    * Overridden in order to verify the validity of the passed type.
98    *
99    * @exception IllegalArgumentException if the passed type is not an attachment type
100    */
 
101  2303 toggle @Override
102    protected void setType(EntityType type)
103    {
104  2301 if (type != EntityType.ATTACHMENT) {
105  1 throw new IllegalArgumentException("Invalid type [" + type + "] for an attachment reference");
106    }
107   
108  2301 super.setType(EntityType.ATTACHMENT);
109    }
110   
111    /**
112    * @return the document reference contained in this attachment reference
113    */
 
114  1850 toggle @Transient
115    public DocumentReference getDocumentReference()
116    {
117  1852 return (DocumentReference) extractReference(EntityType.DOCUMENT);
118    }
119   
 
120  1 toggle @Override
121    public AttachmentReference replaceParent(EntityReference oldParent, EntityReference newParent)
122    {
123  1 return new AttachmentReference(this, oldParent, newParent);
124    }
125    }