1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.wikimodel.xhtml.handler

File CommentHandler.java

 

Coverage histogram

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

Code metrics

14
26
1
1
104
49
8
0.31
26
1
8

Classes

Class Line # Actions
CommentHandler 37 26 0% 8 2
0.951219595.1%
 

Contributing tests

This file is covered by 28 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.wikimodel.xhtml.handler;
21   
22    import java.util.ArrayList;
23    import java.util.List;
24   
25    import org.xwiki.rendering.wikimodel.WikiParameter;
26    import org.xwiki.rendering.wikimodel.WikiParameters;
27    import org.xwiki.rendering.wikimodel.impl.WikiScannerUtil;
28    import org.xwiki.rendering.wikimodel.xhtml.impl.TagStack;
29   
30    /**
31    * Handle Macro definitions in comments (we store macro definitions in a comment since it wouldn't be possible at all to
32    * reconstruct them from the result of their execution).
33    *
34    * @version $Id: 586d09323cd0f42406c79f60d4930d45792d7a8e $
35    * @since 4.0M1
36    */
 
37    public class CommentHandler
38    {
39    private static final String MACRO_SEPARATOR = "|-|";
40   
 
41  95 toggle public void onComment(String content, TagStack stack)
42    {
43    // Format of a macro definition in comment:
44    // <!--startmacro:velocity|-||-|
45    // Some **content**
46    // --><p>Some <strong>content</strong></p><!--stopmacro-->
47  95 if (content.startsWith("startmacro:")) {
48  38 if (!stack.shouldIgnoreElements()) {
49  37 String macroName;
50  37 WikiParameters macroParams = WikiParameters.EMPTY;
51  37 String macroContent = null;
52   
53  37 String macroString = content.substring("startmacro:".length());
54   
55  37 int index = macroString.indexOf(MACRO_SEPARATOR);
56   
57  37 if (index != -1) {
58    // Extract macro name
59  37 macroName = macroString.substring(0, index);
60   
61    // Remove macro name part and continue parsing
62  37 macroString = macroString.substring(index + MACRO_SEPARATOR.length());
63   
64  37 index = macroString.indexOf(MACRO_SEPARATOR);
65  37 if (index != -1) {
66    // Extract macro parameters
67  20 List<WikiParameter> parameters = new ArrayList<WikiParameter>();
68  20 index = WikiScannerUtil.splitToPairs(macroString, parameters, null, MACRO_SEPARATOR);
69  20 macroParams = new WikiParameters(parameters);
70   
71    // Extract macro content
72  20 if (macroString.length() > index) {
73  19 macroContent = macroString.substring(index + MACRO_SEPARATOR.length());
74    }
75    } else {
76    // There is only parameters remaining in the string, the
77    // macro does not have content
78    // Extract macro parameters
79  17 macroParams = WikiParameters.newWikiParameters(macroString);
80    }
81    } else {
82    // There is only macro name, the macro does not have
83    // parameters
84    // or content
85  0 macroName = macroString;
86    }
87   
88    // If we're inside a block element then issue an inline macro
89    // event
90    // otherwise issue a block macro event
91  37 if (stack.isInsideBlockElement()) {
92  12 stack.getScannerContext().onMacroInline(macroName, macroParams, macroContent);
93    } else {
94  25 TagHandler.sendEmptyLines(stack);
95  25 stack.getScannerContext().onMacroBlock(macroName, macroParams, macroContent);
96    }
97    }
98   
99  38 stack.setIgnoreElements();
100  57 } else if (content.startsWith("stopmacro")) {
101  38 stack.unsetIgnoreElements();
102    }
103    }
104    }