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

File PathStringEntityReferenceSerializer.java

 

Coverage histogram

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

Code metrics

2
5
1
1
81
27
3
0.6
5
1
3

Classes

Class Line # Actions
PathStringEntityReferenceSerializer 41 5 0% 3 1
0.87587.5%
 

Contributing tests

This file is covered by 11 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.internal.reference;
21   
22    import java.io.UnsupportedEncodingException;
23    import java.net.URLEncoder;
24   
25    import javax.inject.Named;
26    import javax.inject.Singleton;
27   
28    import org.xwiki.component.annotation.Component;
29    import org.xwiki.model.reference.EntityReference;
30   
31    /**
32    * Generate a path representation of an entity reference (eg "Wiki/Space/Page" for a Document Reference in the
33    * "wiki" Wiki, the "space" Space and the "page" Page).
34    *
35    * @version $Id: f2cd9d79d1457d1b31790e7b87c320a13e804460 $
36    * @since 3.0M2
37    */
38    @Component
39    @Named("path")
40    @Singleton
 
41    public class PathStringEntityReferenceSerializer extends AbstractStringEntityReferenceSerializer
42    {
43    /**
44    * {@inheritDoc}
45    * <p>
46    * Add a segment to the path. All non-URL compatible characters are escaped in the URL-escape format
47    * (%NN). Dot (".") and Star ("*") characters are also encoded. If this is not the last segment in the reference,
48    * append a "/" separator between reference element.
49    * <p>
50    */
 
51  91 toggle @Override
52    protected void serializeEntityReference(EntityReference currentReference, StringBuilder representation,
53    boolean isLastReference, Object... parameters)
54    {
55  91 if (currentReference.getParent() != null) {
56    // Note: Java will convert the file separator to the proper separator for the underlying FileSystem.
57    // Note: Using "/" allows us to reuse the serialized result into URLs. Caveat: The % character might need
58    // to be escaped as %25 in this case as otherwise the browser will automatically decode % encoding.
59  62 representation.append('/');
60    }
61   
62  91 try {
63    // Note: We assume the FileSystem is case-sensitive. This is not the case for 16 bit Windows but we
64    // consider that we don't support these. If we wanted to support case-insensitive File systems we would
65    // need to escape all capital or lower-case letters.
66   
67    // Encode special non ASCII characters and handle "." and "*" in a special way since they're ASCII char
68    // (and thus not encoded by URLEncoder.encode()) but have some special meanings in some File systems:
69    // - On Unix a file starting with dot is a hidden file. On Windows the part after the last dot represents
70    // the file extension and a file cannot end with a dot
71    // - On Windows, the star is a wildcard.
72    // See https://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words and
73    // http://stackoverflow.com/questions/2304221/what-character-sequence-should-i-not-allow-in-a-filename
74  91 representation.append(
75    URLEncoder.encode(currentReference.getName(), "UTF-8").replace(".", "%2E").replace("*", "%2A"));
76    } catch (UnsupportedEncodingException e) {
77    // This will never happen, UTF-8 is always available
78  0 throw new RuntimeException("UTF-8 encoding is not present on the system!", e);
79    }
80    }
81    }