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

File DocumentReferenceTypeSerializer.java

 

Coverage histogram

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

Code metrics

6
15
3
1
121
61
6
0.4
5
3
2

Classes

Class Line # Actions
DocumentReferenceTypeSerializer 42 15 0% 6 1
0.958333395.8%
 

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.rendering.internal.renderer.xwiki20.reference;
21   
22    import javax.inject.Named;
23    import javax.inject.Singleton;
24   
25    import org.apache.commons.lang3.StringUtils;
26    import org.xwiki.component.annotation.Component;
27    import org.xwiki.rendering.internal.parser.xwiki20.XWiki20LinkReferenceParser;
28    import org.xwiki.rendering.listener.reference.DocumentResourceReference;
29    import org.xwiki.rendering.listener.reference.ResourceReference;
30    import org.xwiki.rendering.renderer.reference.ResourceReferenceTypeSerializer;
31   
32    /**
33    * Serialize a link reference pointing to a document using the format
34    * {@code (document reference)(#anchor)(?query string)}.
35    *
36    * @version $Id: 1f1df278a0fd0b5fb43793bab8ba92b1a4d2cf08 $
37    * @since 2.5RC1
38    */
39    @Component
40    @Named("xwiki/2.0/doc")
41    @Singleton
 
42    public class DocumentReferenceTypeSerializer implements ResourceReferenceTypeSerializer
43    {
44    /**
45    * Escapes to add when rendering a link reference part.
46    */
47    private static final String[] ESCAPE_REPLACEMENTS_REFERENCE = new String[] {
48    XWiki20LinkReferenceParser.ESCAPE_CHAR + XWiki20LinkReferenceParser.SEPARATOR_QUERYSTRING,
49    XWiki20LinkReferenceParser.ESCAPE_CHAR + XWiki20LinkReferenceParser.SEPARATOR_INTERWIKI,
50    XWiki20LinkReferenceParser.ESCAPE_CHAR + XWiki20LinkReferenceParser.SEPARATOR_ANCHOR };
51   
52    /**
53    * Replacement chars for the escapes to add to the reference part.
54    */
55    private static final String[] ESCAPES_REFERENCE = new String[] {
56    XWiki20LinkReferenceParser.SEPARATOR_QUERYSTRING,
57    XWiki20LinkReferenceParser.SEPARATOR_INTERWIKI,
58    XWiki20LinkReferenceParser.SEPARATOR_ANCHOR };
59   
60    /**
61    * Escapes to add when rendering a link query string, anchor or interwiki part.
62    */
63    private static final String[] ESCAPE_REPLACEMENTS_EXTRA = new String[] {
64    XWiki20LinkReferenceParser.ESCAPE_CHAR + XWiki20LinkReferenceParser.SEPARATOR_QUERYSTRING,
65    XWiki20LinkReferenceParser.ESCAPE_CHAR + XWiki20LinkReferenceParser.SEPARATOR_INTERWIKI,
66    XWiki20LinkReferenceParser.ESCAPE_CHAR + XWiki20LinkReferenceParser.SEPARATOR_ANCHOR,
67    "" + XWiki20LinkReferenceParser.ESCAPE_CHAR + XWiki20LinkReferenceParser.ESCAPE_CHAR };
68   
69    /**
70    * Replacement chars for the escapes to add to the query string, anchor or interwiki part.
71    */
72    private static final String[] ESCAPES_EXTRA = new String[] {
73    XWiki20LinkReferenceParser.SEPARATOR_QUERYSTRING,
74    XWiki20LinkReferenceParser.SEPARATOR_INTERWIKI,
75    XWiki20LinkReferenceParser.SEPARATOR_ANCHOR,
76    "" + XWiki20LinkReferenceParser.ESCAPE_CHAR };
77   
 
78  20 toggle @Override
79    public String serialize(ResourceReference reference)
80    {
81  20 StringBuilder buffer = new StringBuilder();
82   
83  20 if (reference.getReference() != null) {
84    // Make sure we escape special chars: # and ? as they have special meaning in links, but only for
85    // links to documents. Also escape \ since it's the escape char.
86  20 String normalizedReference = addEscapesToReferencePart(reference.getReference());
87  20 buffer.append(normalizedReference);
88    }
89   
90  20 String anchor = reference.getParameter(DocumentResourceReference.ANCHOR);
91  20 if (anchor != null) {
92  3 buffer.append('#');
93  3 buffer.append(addEscapesToExtraParts(anchor));
94    }
95  20 String queryString = reference.getParameter(DocumentResourceReference.QUERY_STRING);
96  20 if (queryString != null) {
97  2 buffer.append('?');
98  2 buffer.append(addEscapesToExtraParts(queryString));
99    }
100   
101  20 return buffer.toString();
102    }
103   
104    /**
105    * @param text the reference to which to add escapes to
106    * @return the modified text
107    */
 
108  21 toggle protected String addEscapesToReferencePart(String text)
109    {
110  21 return StringUtils.replaceEach(text, ESCAPES_REFERENCE, ESCAPE_REPLACEMENTS_REFERENCE);
111    }
112   
113    /**
114    * @param text the query string and anchor parts to which to add escapes to
115    * @return the modified text
116    */
 
117  7 toggle protected String addEscapesToExtraParts(String text)
118    {
119  7 return StringUtils.replaceEach(text, ESCAPES_EXTRA, ESCAPE_REPLACEMENTS_EXTRA);
120    }
121    }