1. Project Clover database Wed Oct 21 2015 17:09:14 CEST
  2. Package org.xwiki.rendering.internal.parser.xwiki10

File GroovyFilter.java

 

Coverage histogram

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

Code metrics

8
20
2
1
90
52
6
0.3
10
2
3

Classes

Class Line # Actions
GroovyFilter 39 20 0% 6 1
0.9666666496.7%
 

Contributing tests

This file is covered by 105 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.parser.xwiki10;
21   
22    import javax.inject.Named;
23    import javax.inject.Singleton;
24   
25    import org.xwiki.component.annotation.Component;
26    import org.xwiki.component.phase.Initializable;
27    import org.xwiki.component.phase.InitializationException;
28    import org.xwiki.rendering.parser.xwiki10.AbstractFilter;
29    import org.xwiki.rendering.parser.xwiki10.FilterContext;
30    import org.xwiki.rendering.parser.xwiki10.util.CleanUtil;
31   
32    /**
33    * @version $Id: f129aa11f4cd2adbf96f50bccfde42dadec5bc2b $
34    * @since 1.8RC3
35    */
36    @Component
37    @Named("groovy")
38    @Singleton
 
39    public class GroovyFilter extends AbstractFilter implements Initializable
40    {
41    public static final String GROOVY_BEGIN = "<%";
42   
43    public static final String GROOVY_END = "%>";
44   
 
45  105 toggle @Override
46    public void initialize() throws InitializationException
47    {
48  105 setPriority(30);
49    }
50   
 
51  105 toggle @Override
52    public String filter(String content, FilterContext filterContext)
53    {
54  105 StringBuffer result = new StringBuffer();
55   
56  105 String currentContent = content;
57  108 for (int index = currentContent.indexOf(GROOVY_BEGIN); index != -1; index =
58    currentContent.indexOf(GROOVY_BEGIN)) {
59  3 result.append(currentContent.substring(0, index));
60   
61  3 currentContent = currentContent.substring(index + 2);
62   
63  3 int endIndex = currentContent.indexOf(GROOVY_END);
64   
65  3 String groovyContent;
66  3 if (endIndex != -1) {
67  2 groovyContent = currentContent.substring(0, endIndex);
68  2 currentContent = currentContent.substring(endIndex + GROOVY_END.length());
69    } else {
70  1 groovyContent = currentContent;
71  1 currentContent = "";
72    }
73   
74  3 if (groovyContent.trim().length() > 0) {
75  3 boolean newline = currentContent.startsWith("\n");
76   
77  3 result
78    .append(filterContext.addProtectedContent("{{groovy}}" + groovyContent + "{{/groovy}}", !newline));
79   
80  3 if (newline) {
81  1 currentContent = CleanUtil.setLeadingNewLines(currentContent, 2);
82    }
83    }
84    }
85   
86  105 result.append(currentContent);
87   
88  105 return result.toString();
89    }
90    }