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
48   163   13   12
18   98   0.27   4
4     3.25  
1    
 
  XWikiReferenceTagHandler       Line # 46 48 0% 13 4 94.3% 0.94285715
 
  (219)
 
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.parser.xhtml.wikimodel;
21   
22    import java.util.Collections;
23   
24    import org.xwiki.rendering.wikimodel.WikiParameter;
25    import org.xwiki.rendering.wikimodel.WikiParameters;
26    import org.xwiki.rendering.wikimodel.WikiReference;
27    import org.xwiki.rendering.wikimodel.impl.WikiScannerContext;
28    import org.xwiki.rendering.wikimodel.xhtml.handler.ReferenceTagHandler;
29    import org.xwiki.rendering.wikimodel.xhtml.impl.XhtmlHandler.TagStack;
30    import org.xwiki.rendering.wikimodel.xhtml.impl.XhtmlHandler.TagStack.TagContext;
31    import org.xwiki.rendering.internal.parser.wikimodel.DefaultXWikiGeneratorListener;
32    import org.xwiki.rendering.internal.parser.wikimodel.XWikiGeneratorListener;
33    import org.xwiki.rendering.internal.parser.xhtml.XHTMLParser;
34    import org.xwiki.rendering.listener.MetaData;
35    import org.xwiki.rendering.renderer.PrintRenderer;
36    import org.xwiki.rendering.renderer.PrintRendererFactory;
37    import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter;
38   
39    /**
40    * Override the default WikiModel Reference handler to handle XWiki references since we store some information in
41    * comments. We also need to handle the span elements introduced by XWiki.
42    *
43    * @version $Id: 04adf14728e7ce6b351106f681562febf4dc453d $
44    * @since 1.7M1
45    */
 
46    public class XWikiReferenceTagHandler extends ReferenceTagHandler
47    {
48    private XHTMLParser parser;
49   
50    private PrintRendererFactory xwikiSyntaxPrintRendererFactory;
51   
52    /**
53    * @since 2.2.5
54    * @todo Remove the need to pass a Parser when WikiModel implements support for wiki syntax in links. See
55    * http://code.google.com/p/wikimodel/issues/detail?id=87
56    */
 
57  219 toggle public XWikiReferenceTagHandler(XHTMLParser parser, PrintRendererFactory xwikiSyntaxPrintRendererFactory)
58    {
59  219 this.parser = parser;
60  219 this.xwikiSyntaxPrintRendererFactory = xwikiSyntaxPrintRendererFactory;
61    }
62   
 
63  250 toggle @Override
64    public void initialize(TagStack stack)
65    {
66  250 stack.setStackParameter("isInLink", false);
67  250 stack.setStackParameter("isFreeStandingLink", false);
68  250 stack.setStackParameter("linkParameters", WikiParameters.EMPTY);
69    }
70   
 
71  60 toggle @Override
72    protected void begin(TagContext context)
73    {
74  60 boolean isInLink = (Boolean) context.getTagStack().getStackParameter("isInLink");
75  60 if (isInLink) {
76  45 XWikiGeneratorListener listener =
77    (XWikiGeneratorListener) context.getTagStack().getStackParameter("linkListener");
78  45 context.getTagStack().pushScannerContext(new WikiScannerContext(listener));
79   
80    // Ensure we simulate a new document being parsed
81  45 context.getScannerContext().beginDocument();
82   
83    // Verify if it's a freestanding link and if so save the information so that we can get it in
84    // XWikiCommentHandler.
85  45 if (isFreeStandingReference(context)) {
86  7 context.getTagStack().setStackParameter("isFreeStandingLink", true);
87    } else {
88  38 context.getTagStack().setStackParameter("linkParameters",
89    removeMeaningfulParameters(context.getParams()));
90    }
91   
92  45 setAccumulateContent(false);
93  15 } else if (!isFreeStandingReference(context)) {
94  15 WikiParameter ref = context.getParams().getParameter("href");
95   
96  15 if (ref != null) {
97  7 DefaultWikiPrinter printer = new DefaultWikiPrinter();
98   
99  7 PrintRenderer linkLabelRenderer = this.xwikiSyntaxPrintRendererFactory.createRenderer(printer);
100    // Make sure to flush whatever the renderer implementation
101  7 linkLabelRenderer.beginDocument(MetaData.EMPTY);
102   
103  7 XWikiGeneratorListener xwikiListener =
104    this.parser.createXWikiGeneratorListener(linkLabelRenderer, null);
105  7 context.getTagStack().pushScannerContext(new WikiScannerContext(xwikiListener));
106   
107    // Ensure we simulate a new document being parsed
108  7 context.getScannerContext().beginDocument();
109    } else {
110  8 WikiParameter idName = context.getParams().getParameter("id");
111   
112  8 if (idName == null) {
113  6 idName = context.getParams().getParameter("name");
114    }
115   
116  8 if (idName != null) {
117  6 WikiParameter parameter = new WikiParameter("name", idName.getValue());
118  6 WikiParameters parameters = new WikiParameters(Collections.singletonList(parameter));
119  6 context.getScannerContext().onExtensionBlock(DefaultXWikiGeneratorListener.EXT_ID, parameters);
120    }
121    }
122    } else {
123  0 super.begin(context);
124    }
125    }
126   
 
127  60 toggle @Override
128    protected void end(TagContext context)
129    {
130  60 boolean isInLink = (Boolean) context.getTagStack().getStackParameter("isInLink");
131  60 if (isInLink) {
132    // Ensure we simulate a document parsing end
133  45 context.getScannerContext().endDocument();
134   
135  45 context.getTagStack().popScannerContext();
136  15 } else if (!isFreeStandingReference(context)) {
137  15 WikiParameters parameters = context.getParams();
138   
139  15 WikiParameter ref = parameters.getParameter("href");
140   
141  15 if (ref != null) {
142    // Ensure we simulate a document parsing end
143  7 context.getScannerContext().endDocument();
144   
145  7 WikiScannerContext scannerContext = context.getTagStack().popScannerContext();
146   
147  7 XWikiGeneratorListener xwikiListener = (XWikiGeneratorListener) scannerContext.getfListener();
148  7 PrintRenderer linkLabelRenderer = (PrintRenderer) xwikiListener.getListener();
149   
150    // Make sure to flush whatever the renderer implementation
151  7 linkLabelRenderer.endDocument(MetaData.EMPTY);
152   
153  7 String label = linkLabelRenderer.getPrinter().toString();
154  7 WikiReference reference =
155    new WikiReference(ref.getValue(), label, removeMeaningfulParameters(parameters));
156   
157  7 context.getScannerContext().onReference(reference);
158    }
159    } else {
160  0 super.end(context);
161    }
162    }
163    }