Clover Coverage Report - XWiki Rendering - Parent POM 4.0-SNAPSHOT (Aggregated)
Coverage timestamp: Mon Mar 12 2012 18:03:13 CET
../../../../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
27   159   14   3.38
10   92   0.52   8
8     1.75  
1    
 
  XWikiSyntaxResourceRenderer       Line # 41 27 0% 14 0 100% 1.0
 
  (326)
 
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 java.util.Map;
23    import java.util.Stack;
24   
25    import org.apache.commons.lang3.StringUtils;
26    import org.xwiki.rendering.internal.parser.PlainTextStreamParser;
27    import org.xwiki.rendering.internal.renderer.ParametersPrinter;
28    import org.xwiki.rendering.internal.renderer.xwiki20.XWikiSyntaxEscapeWikiPrinter;
29    import org.xwiki.rendering.internal.renderer.xwiki20.XWikiSyntaxListenerChain;
30    import org.xwiki.rendering.listener.QueueListener.Event;
31    import org.xwiki.rendering.listener.chaining.EventType;
32    import org.xwiki.rendering.listener.reference.ResourceReference;
33    import org.xwiki.rendering.renderer.reference.ResourceReferenceSerializer;
34   
35    /**
36    * Logic to render a Resource Reference into XWiki Syntax 2.0.
37    *
38    * @version $Id: 07c37db785feb98c2435c87934005077054a8a27 $
39    * @since 2.0M3
40    */
 
41    public class XWikiSyntaxResourceRenderer
42    {
43    /**
44    * Separator to use between the link reference and link parameters.
45    */
46    protected static final String PARAMETER_SEPARATOR = "||";
47   
48    protected ParametersPrinter parametersPrinter = new ParametersPrinter();
49   
50    private Stack<Boolean> forceFullSyntax = new Stack<Boolean>();
51   
52    private XWikiSyntaxListenerChain listenerChain;
53   
54    private ResourceReferenceSerializer referenceSerializer;
55   
56    /**
57    * @since 2.5RC1
58    */
 
59  758 toggle public XWikiSyntaxResourceRenderer(XWikiSyntaxListenerChain listenerChain,
60    ResourceReferenceSerializer referenceSerializer)
61    {
62  758 this.listenerChain = listenerChain;
63  758 this.referenceSerializer = referenceSerializer;
64  758 this.forceFullSyntax.push(false);
65    }
66   
 
67  117 toggle public String serialize(ResourceReference reference, boolean isFreeStanding)
68    {
69  117 String result = this.referenceSerializer.serialize(reference);
70   
71  117 if (!isFreeStanding) {
72  84 result = result.replace("~", "~~").replace(">>", "~>~>").replace(PARAMETER_SEPARATOR, "~|~|");
73    }
74   
75  117 return result;
76    }
77   
 
78  116 toggle public void beginRenderLink(XWikiSyntaxEscapeWikiPrinter printer, ResourceReference reference,
79    boolean isFreeStandingURI, Map<String, String> parameters)
80    {
81    // find if the last printed char is part of a syntax (i.e. consumed by the parser before starting to parse the
82    // link)
83  116 boolean isLastSyntax = printer.getBuffer().length() == 0;
84   
85  116 printer.flush();
86   
87  116 if (forceFullSyntax(printer, isLastSyntax, isFreeStandingURI, parameters)) {
88  87 this.forceFullSyntax.push(true);
89   
90  87 printer.print("[[");
91    } else {
92  29 this.forceFullSyntax.push(false);
93    }
94    }
95   
 
96  78 toggle public boolean forceFullSyntax(XWikiSyntaxEscapeWikiPrinter printer, boolean isFreeStandingURI,
97    Map<String, String> parameters)
98    {
99  78 return forceFullSyntax(printer, true, isFreeStandingURI, parameters);
100    }
101   
 
102  194 toggle public boolean forceFullSyntax(XWikiSyntaxEscapeWikiPrinter printer, boolean isLastSyntax,
103    boolean isFreeStandingURI, Map<String, String> parameters)
104    {
105  194 Event nextEvent = this.listenerChain.getLookaheadChainingListener().getNextEvent();
106   
107    // force full syntax if
108    // 1: it's not a free standing URI
109    // 2: there is parameters
110    // 3: it follows a character which is not a white space (newline/space) and is not consumed by the parser (like
111    // a another link)
112    // 4: it's followed by a character which is not a white space (TODO: find a better way than this endless list of
113    // EventType test but it probably need some big refactoring of the printer and XWikiSyntaxResourceRenderer)
114  194 return !isFreeStandingURI
115    || !parameters.isEmpty()
116    || (!isLastSyntax && !printer.isAfterWhiteSpace() && (!PlainTextStreamParser.SPECIALSYMBOL_PATTERN.matcher(
117    String.valueOf(printer.getLastPrinted().charAt(printer.getLastPrinted().length() - 1))).matches()))
118    || (nextEvent != null && nextEvent.eventType != EventType.ON_SPACE
119    && nextEvent.eventType != EventType.ON_NEW_LINE && nextEvent.eventType != EventType.END_PARAGRAPH
120    && nextEvent.eventType != EventType.END_LINK && nextEvent.eventType != EventType.END_LIST_ITEM
121    && nextEvent.eventType != EventType.END_DEFINITION_DESCRIPTION
122    && nextEvent.eventType != EventType.END_DEFINITION_TERM
123    && nextEvent.eventType != EventType.END_QUOTATION_LINE && nextEvent.eventType != EventType.END_SECTION);
124    }
125   
 
126  77 toggle public void renderLinkContent(XWikiSyntaxEscapeWikiPrinter printer, String label)
127    {
128    // If there was some link content specified then output the character separator ">>".
129  77 if (!StringUtils.isEmpty(label)) {
130  26 printer.print(label);
131  26 printer.print(">>");
132    }
133    }
134   
 
135  116 toggle public void endRenderLink(XWikiSyntaxEscapeWikiPrinter printer, ResourceReference reference,
136    boolean isFreeStandingURI, Map<String, String> parameters)
137    {
138  116 printer.print(serialize(reference, isFreeStandingURI));
139   
140    // If there were parameters specified, print them
141  116 printParameters(printer, reference, parameters);
142   
143  116 if (this.forceFullSyntax.peek() || !isFreeStandingURI) {
144  87 printer.print("]]");
145    }
146   
147  116 this.forceFullSyntax.pop();
148    }
149   
 
150  95 toggle protected void printParameters(XWikiSyntaxEscapeWikiPrinter printer, ResourceReference resourceReference,
151    Map<String, String> parameters)
152    {
153    // If there were parameters specified, output them separated by the "||" characters
154  95 if (!parameters.isEmpty()) {
155  19 printer.print(PARAMETER_SEPARATOR);
156  19 printer.print(this.parametersPrinter.print(parameters, '~'));
157    }
158    }
159    }