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

File PathStringDocumentReferenceResolver.java

 

Coverage histogram

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

Code metrics

4
15
1
1
76
42
4
0.27
15
1
4

Classes

Class Line # Actions
PathStringDocumentReferenceResolver 48 15 0% 4 1
0.9595%
 

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.model.internal.reference;
21   
22    import java.io.File;
23    import java.io.UnsupportedEncodingException;
24    import java.net.URLDecoder;
25    import java.util.ArrayList;
26    import java.util.Collections;
27    import java.util.List;
28   
29    import javax.inject.Named;
30    import javax.inject.Singleton;
31   
32    import org.apache.commons.lang3.CharEncoding;
33    import org.xwiki.component.annotation.Component;
34    import org.xwiki.model.reference.DocumentReference;
35    import org.xwiki.model.reference.DocumentReferenceResolver;
36   
37    /**
38    * Creates a {@link DocumentReference} from a string representation that has the "wiki/space1/.../spaceN/page" format,
39    * where each path component is URL encoded. The current implementation only supports absolute references
40    * (i.e up to the wiki part). We made this to work with {@link PathStringEntityReferenceSerializer}.
41    *
42    * @version $Id: 166de148d608f554927330f9dde0908f1c1382ee $
43    * @since 5.3M1
44    */
45    @Component
46    @Named("path")
47    @Singleton
 
48    public class PathStringDocumentReferenceResolver implements DocumentReferenceResolver<String>
49    {
 
50  2 toggle @Override
51    public DocumentReference resolve(String path, Object... args)
52    {
53  2 try {
54  2 File file = new File(path);
55    // The last segment is the page name
56  2 String page = URLDecoder.decode(file.getName(), CharEncoding.UTF_8);
57    // All parent segments are space segment till the top level segment which is the wiki name
58  2 File current = file;
59  2 String segmentName = null;
60  2 List<String> spaceNames = new ArrayList<>();
61  7 while (current.getParentFile() != null) {
62  5 current = current.getParentFile();
63  5 segmentName = URLDecoder.decode(current.getName(), CharEncoding.UTF_8);
64  5 if (current.getParentFile() != null) {
65  3 spaceNames.add(segmentName);
66    }
67    }
68  2 Collections.reverse(spaceNames);
69  2 String wiki = segmentName;
70  2 return new DocumentReference(wiki, spaceNames, page);
71    } catch (UnsupportedEncodingException e) {
72    // This will never happen, UTF-8 is always available
73  0 throw new RuntimeException("UTF-8 encoding is not present on the system!", e);
74    }
75    }
76    }