1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.vfs.internal.attach

File XWikiModelNode.java

 

Coverage histogram

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

Code metrics

6
28
6
1
142
80
13
0.46
4.67
6
2.17

Classes

Class Line # Actions
XWikiModelNode 44 28 0% 13 5
0.87587.5%
 

Contributing tests

This file is covered by 1 test. .

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.vfs.internal.attach;
21   
22    import java.io.IOException;
23    import java.net.URI;
24   
25    import javax.inject.Provider;
26   
27    import org.xwiki.component.manager.ComponentManager;
28    import org.xwiki.model.reference.DocumentReference;
29    import org.xwiki.model.reference.DocumentReferenceResolver;
30   
31    import com.xpn.xwiki.XWikiContext;
32    import com.xpn.xwiki.doc.XWikiAttachment;
33    import com.xpn.xwiki.doc.XWikiDocument;
34   
35    import net.java.truevfs.kernel.spec.FsNodeName;
36   
37    /**
38    * Decorator for an {@code AttachNode} to provide all XWiki Model API to get the Attachment corresponding to the VFS
39    * node.
40    *
41    * @version $Id: fbc8bc6da421058dc870908217489b25977d03e7 $
42    * @since 7.4M2
43    */
 
44    public class XWikiModelNode
45    {
46    private ComponentManager componentManager;
47   
48    private DocumentReference reference;
49   
50    private XWikiAttachment attachment;
51   
52    private XWikiContext xcontext;
53   
54    private URI uri;
55   
56    private String name;
57   
 
58  4 toggle XWikiModelNode(AttachController controller, FsNodeName name)
59    {
60  4 this.name = name.toString();
61  4 this.uri = controller.resolve(name).getUri();
62  4 this.componentManager = controller.getComponentManager();
63    }
64   
 
65  8 toggle private ComponentManager getComponentManager()
66    {
67  8 return this.componentManager;
68    }
69   
70    /**
71    * @return the reference to the Document holding the archive attachment
72    * @throws IOException when an error accessing the Document occurs
73    */
 
74  4 toggle public DocumentReference getDocumentReference() throws IOException
75    {
76  4 if (this.reference == null) {
77  4 try {
78    // Use a default resolver (and not a current one) since we don't have any context, we're in a new
79    // request.
80  4 DocumentReferenceResolver<String> documentReferenceResolver =
81    getComponentManager().getInstance(DocumentReferenceResolver.TYPE_STRING);
82  4 this.reference = documentReferenceResolver.resolve(this.uri.getAuthority());
83    } catch (Exception e) {
84  0 throw new IOException(
85    String.format("Failed to compute Document reference for [%s]", this.uri), e);
86    }
87    }
88  4 return this.reference;
89    }
90   
91    /**
92    * @return the current XWiki Context
93    * @throws IOException if an error occurs retrieving the context
94    */
 
95  12 toggle public XWikiContext getXWikiContext() throws IOException
96    {
97  12 if (this.xcontext == null) {
98  4 try {
99  4 Provider<XWikiContext> xcontextProvider = getComponentManager().getInstance(XWikiContext.TYPE_PROVIDER);
100  4 this.xcontext = xcontextProvider.get();
101    } catch (Exception e) {
102  0 throw new IOException(String.format("Failed to get XWiki Context for [%s]", this.uri), e);
103    }
104    }
105  12 return this.xcontext;
106    }
107   
108    /**
109    * @return true if the attachment exists or false otherwise
110    * @since 7.4.1
111    * @since 8.0M1
112    */
 
113  2 toggle public boolean hasAttachment()
114    {
115  2 boolean result;
116  2 try {
117  2 getAttachment();
118  2 result = true;
119    } catch (Exception e) {
120  0 result = false;
121    }
122  2 return result;
123    }
124   
125    /**
126    * @return the archive attachment itself
127    * @throws IOException when an error accessing the Attachment occurs
128    */
 
129  8 toggle public XWikiAttachment getAttachment() throws IOException
130    {
131  8 if (this.attachment == null) {
132  4 try {
133  4 XWikiDocument document = getXWikiContext().getWiki().getDocument(
134    getDocumentReference(), getXWikiContext());
135  4 this.attachment = document.getAttachment(this.name);
136    } catch (Exception e) {
137  0 throw new IOException(String.format("Failed to get Attachment for [%s]", this.uri), e);
138    }
139    }
140  8 return this.attachment;
141    }
142    }