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

File InterWikiReferenceTypeSerializer.java

 

Coverage histogram

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

Code metrics

2
7
3
1
99
45
4
0.57
2.33
3
1.33

Classes

Class Line # Actions
InterWikiReferenceTypeSerializer 42 7 0% 4 1
0.916666791.7%
 

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.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.reference.GenericLinkReferenceParser;
28    import org.xwiki.rendering.internal.parser.xwiki20.XWiki20LinkReferenceParser;
29    import org.xwiki.rendering.listener.reference.InterWikiResourceReference;
30    import org.xwiki.rendering.listener.reference.ResourceReference;
31    import org.xwiki.rendering.renderer.reference.ResourceReferenceTypeSerializer;
32   
33    /**
34    * Serialize a link reference pointing to an interwiki link using the format {@code (interwiki path)@(interwiki alias)}.
35    *
36    * @version $Id: 71262508398312cc066d5b9bff976e246dba883c $
37    * @since 2.5RC1
38    */
39    @Component
40    @Named("xwiki/2.0/interwiki")
41    @Singleton
 
42    public class InterWikiReferenceTypeSerializer 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.ESCAPE_CHAR };
49   
50    /**
51    * Replacement chars for the escapes to add to the reference part.
52    */
53    private static final String[] ESCAPES_REFERENCE = new String[] {
54    "" + XWiki20LinkReferenceParser.ESCAPE_CHAR };
55   
56    /**
57    * Escapes to add when rendering a link query string, anchor or interwiki part.
58    */
59    private static final String[] ESCAPE_REPLACEMENTS_EXTRA = new String[] {
60    "" + XWiki20LinkReferenceParser.ESCAPE_CHAR + XWiki20LinkReferenceParser.SEPARATOR_INTERWIKI,
61    "" + XWiki20LinkReferenceParser.ESCAPE_CHAR + XWiki20LinkReferenceParser.ESCAPE_CHAR };
62   
63    /**
64    * Replacement chars for the escapes to add to the query string, anchor or interwiki part.
65    */
66    private static final String[] ESCAPES_EXTRA = new String[] {
67    XWiki20LinkReferenceParser.SEPARATOR_INTERWIKI,
68    "" + XWiki20LinkReferenceParser.ESCAPE_CHAR };
69   
 
70  2 toggle @Override
71    public String serialize(ResourceReference reference)
72    {
73  2 String interWikiAlias = reference.getParameter(InterWikiResourceReference.INTERWIKI_ALIAS);
74  2 String result = addEscapesToReferencePart(reference.getReference());
75  2 if (interWikiAlias != null) {
76  2 result = addEscapesToReferencePart(reference.getReference())
77    + GenericLinkReferenceParser.SEPARATOR_INTERWIKI + addEscapesToExtraParts(interWikiAlias);
78    }
79  2 return result;
80    }
81   
82    /**
83    * @param text the reference to which to add escapes to
84    * @return the modified text
85    */
 
86  4 toggle protected String addEscapesToReferencePart(String text)
87    {
88  4 return StringUtils.replaceEach(text, ESCAPES_REFERENCE, ESCAPE_REPLACEMENTS_REFERENCE);
89    }
90   
91    /**
92    * @param text the query string and anchor parts to which to add escapes to
93    * @return the modified text
94    */
 
95  2 toggle protected String addEscapesToExtraParts(String text)
96    {
97  2 return StringUtils.replaceEach(text, ESCAPES_EXTRA, ESCAPE_REPLACEMENTS_EXTRA);
98    }
99    }