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

File VfsResourceReference.java

 

Coverage histogram

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

Code metrics

6
23
10
1
162
89
13
0.57
2.3
10
1.3

Classes

Class Line # Actions
VfsResourceReference 41 23 0% 13 6
0.8461538684.6%
 

Contributing tests

This file is covered by 21 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.vfs;
21   
22    import java.net.URI;
23    import java.util.ArrayList;
24    import java.util.Arrays;
25    import java.util.List;
26   
27    import org.apache.commons.lang3.StringUtils;
28    import org.apache.commons.lang3.builder.EqualsBuilder;
29    import org.apache.commons.lang3.builder.HashCodeBuilder;
30    import org.apache.commons.lang3.builder.ToStringBuilder;
31    import org.xwiki.resource.AbstractResourceReference;
32    import org.xwiki.resource.ResourceType;
33    import org.xwiki.text.XWikiToStringBuilder;
34   
35    /**
36    * Represents a reference to a VFS resource.
37    *
38    * @version $Id: 660bf426bd3a680e51886860cb511e2f69224db7 $
39    * @since 7.4M2
40    */
 
41    public class VfsResourceReference extends AbstractResourceReference
42    {
43    /**
44    * Represents a VFS Resource Type.
45    */
46    public static final ResourceType TYPE = new ResourceType("vfs");
47   
48    private static final String RESOURCE_PATH_SEPARATOR = "/";
49   
50    private URI uri;
51   
52    private List<String> pathSegments;
53   
54    /**
55    * @param uri the URI pointing to the archive (without the path inside the archive),
56    * e.g. {@code attach:space.page@attachment}
57    * @param pathSegments see {@link #getPathSegments()}
58    */
 
59  37 toggle public VfsResourceReference(URI uri, List<String> pathSegments)
60    {
61  37 setType(TYPE);
62  37 this.uri = uri;
63  37 this.pathSegments = new ArrayList<>(pathSegments);
64    }
65   
66    /**
67    * @param uri the URI pointing to the archive (without the path inside the archive),
68    * e.g. {@code attach:space.page@attachment}
69    * @param pathSegments see {@link #getPathSegments()}, specified as "/"-separated string (e.g. "path/to/file")
70    */
 
71  33 toggle public VfsResourceReference(URI uri, String pathSegments)
72    {
73  33 this(uri, Arrays.asList(StringUtils.split(pathSegments, RESOURCE_PATH_SEPARATOR)));
74    }
75   
76    /**
77    * @param fullURI the full opaque URI containing both the reference to the archive and the path to the entry inside
78    * it, e.g. {@code attach:space.page@attachment/path/to/file}. Note that this constructor requires that any
79    * "/" character inside the reference to the archive be URL-encoded
80    */
 
81  12 toggle public VfsResourceReference(URI fullURI)
82    {
83    // Find the first "/" and consider that everything after is the path
84  12 this(URI.create(StringUtils.substringBefore(fullURI.toString(), RESOURCE_PATH_SEPARATOR)),
85    StringUtils.substringAfter(fullURI.toString(), RESOURCE_PATH_SEPARATOR));
86    }
87   
88    /**
89    * @return the URI to the VFS (e.g. {@code attach:space.page@file.zip}, {@code http://server/path/to/zip})
90    */
 
91  62 toggle public URI getURI()
92    {
93  62 return this.uri;
94    }
95   
96    /**
97    * @return the list of segments pointing to the relative location of a resource in the VFS (e.g. {@code {"some",
98    * "directory", "file.txt"}} for {@code some/directory/file.txt}
99    */
 
100  34 toggle public List<String> getPathSegments()
101    {
102  34 return this.pathSegments;
103    }
104   
105    /**
106    * @return the String representation with "/" separating each VFS path segment, e.g. {@code some/directory/file.txt}
107    */
 
108  10 toggle public String getPath()
109    {
110  10 return StringUtils.join(getPathSegments(), RESOURCE_PATH_SEPARATOR);
111    }
112   
 
113  2 toggle @Override
114    public int hashCode()
115    {
116  2 return new HashCodeBuilder(7, 7)
117    .append(getURI())
118    .append(getPathSegments())
119    .append(getType())
120    .append(getParameters())
121    .toHashCode();
122    }
123   
 
124  8 toggle @Override
125    public boolean equals(Object object)
126    {
127  8 if (object == null) {
128  0 return false;
129    }
130  8 if (object == this) {
131  0 return true;
132    }
133  8 if (object.getClass() != getClass()) {
134  0 return false;
135    }
136  8 VfsResourceReference rhs = (VfsResourceReference) object;
137  8 return new EqualsBuilder()
138    .append(getURI(), rhs.getURI())
139    .append(getPathSegments(), rhs.getPathSegments())
140    .append(getType(), rhs.getType())
141    .append(getParameters(), rhs.getParameters())
142    .isEquals();
143    }
144   
 
145  3 toggle @Override
146    public String toString()
147    {
148  3 ToStringBuilder builder = new XWikiToStringBuilder(this);
149  3 builder.append("uri", getURI());
150  3 builder.append("path", getPath());
151  3 builder.append("parameters", getParameters());
152  3 return builder.toString();
153    }
154   
155    /**
156    * @return the resource reference as a URI
157    */
 
158  1 toggle public URI toURI()
159    {
160  1 return URI.create(String.format("%s/%s", getURI().toString(), getPath()));
161    }
162    }