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
60   168   16   30
28   108   0.27   2
2     8  
1    
 
  LinkSyntaxFilter       Line # 44 60 0% 16 3 96.7% 0.96666664
 
  (92)
 
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 java.util.regex.Matcher;
23    import java.util.regex.Pattern;
24   
25    import javax.inject.Inject;
26    import javax.inject.Named;
27    import javax.inject.Singleton;
28   
29    import org.xwiki.component.annotation.Component;
30    import org.xwiki.component.phase.Initializable;
31    import org.xwiki.component.phase.InitializationException;
32    import org.xwiki.rendering.parser.xwiki10.AbstractFilter;
33    import org.xwiki.rendering.parser.xwiki10.Filter;
34    import org.xwiki.rendering.parser.xwiki10.FilterContext;
35    import org.xwiki.rendering.parser.xwiki10.util.CleanUtil;
36   
37    /**
38    * @version $Id: 1291302567b5e09321fefcc64e749848c87a879d $
39    * @since 1.8M1
40    */
41    @Component
42    @Named("link")
43    @Singleton
 
44    public class LinkSyntaxFilter extends AbstractFilter implements Initializable
45    {
46    private static final Pattern LINKSYNTAX_PATTERN = Pattern.compile("\\[(.+?)\\]");
47   
48    /**
49    * URL matching pattern.
50    */
51    private static final Pattern URL_SCHEME_PATTERN = Pattern.compile("^[a-zA-Z0-9+.-]*://.*$");
52   
53    @Inject
54    @Named("escape20")
55    private Filter escape20Filter;
56   
 
57  92 toggle @Override
58    public void initialize() throws InitializationException
59    {
60    // make sure to match link before wiki syntax (which is 1000) since wiki syntax is not supported in links label
61    // in xwiki/1.0 syntax
62  92 setPriority(900);
63    }
64   
 
65  92 toggle @Override
66    public String filter(String content, FilterContext filterContext)
67    {
68  92 StringBuffer result = new StringBuffer();
69   
70  92 Matcher matcher = LINKSYNTAX_PATTERN.matcher(content);
71  92 int current = 0;
72  103 for (; matcher.find(); current = matcher.end()) {
73  11 String before = content.substring(current, matcher.start());
74   
75    // a standalone new line is not interpreted by XWiki 1.0 rendering
76  11 result.append(CleanUtil.removeTrailingNewLines(before, 1, true));
77   
78  11 StringBuffer linkResult = new StringBuffer();
79  11 linkResult.append("[[");
80   
81  11 String str = matcher.group(1);
82  11 if (str != null) {
83  11 str = str.trim();
84  11 String text = null, href = null, target = null;
85   
86    // Is there an alias like [alias|link] ?
87  11 int pipeIndex = str.indexOf('|');
88  11 int pipeLength = 1;
89  11 if (pipeIndex == -1) {
90  11 pipeIndex = str.indexOf('>');
91    }
92  11 if (pipeIndex == -1) {
93  5 pipeIndex = str.indexOf(">");
94  5 pipeLength = 4;
95    }
96  11 if (-1 != pipeIndex) {
97  6 text = str.substring(0, pipeIndex).trim();
98  6 str = str.substring(pipeIndex + pipeLength);
99    }
100   
101    // Is there a target like [alias|link|target] ?
102  11 pipeIndex = str.indexOf('|');
103  11 pipeLength = 1;
104  11 if (pipeIndex == -1) {
105  11 pipeIndex = str.indexOf('>');
106    }
107  11 if (pipeIndex == -1) {
108  10 pipeIndex = str.indexOf(">");
109  10 pipeLength = 4;
110    }
111  11 if (-1 != pipeIndex) {
112  1 target = str.substring(pipeIndex + pipeLength).trim();
113  1 str = str.substring(0, pipeIndex);
114    }
115    // Done splitting
116   
117    // Fill in missing components
118  11 href = str.trim();
119   
120    // Done, now print the link
121  11 if (text != null) {
122  6 linkResult.append(this.escape20Filter.filter(text, filterContext).replace("~", "~~")
123    .replace(">>", "~>~>").replace("||", "~|~|"));
124  6 linkResult.append(">>");
125    }
126   
127    // xwiki/1.0 and xwiki/2.0 syntaxes are not using query string and anchor in the same order
128  11 if (!URL_SCHEME_PATTERN.matcher(href).matches()) {
129  10 int anchorIndex = href.lastIndexOf('#');
130  10 if (anchorIndex == -1) {
131  9 anchorIndex = href.length();
132    }
133  10 int queryStringIndex = href.lastIndexOf('?', anchorIndex - 1);
134  10 if (queryStringIndex == -1) {
135  9 queryStringIndex = anchorIndex;
136    }
137  10 String anchor = href.substring(anchorIndex);
138  10 String queryString = href.substring(queryStringIndex, anchorIndex);
139  10 href = href.substring(0, queryStringIndex);
140   
141  10 linkResult.append(href);
142  10 linkResult.append(anchor);
143  10 linkResult.append(queryString);
144    } else {
145  1 linkResult.append(href);
146    }
147   
148    // Target
149  11 if (target != null) {
150  1 linkResult.append("||target=");
151  1 linkResult.append(target);
152    }
153    }
154   
155  11 linkResult.append("]]");
156   
157  11 result.append(CleanUtil.extractVelocity(linkResult, filterContext, true, true));
158    }
159   
160  92 if (current == 0) {
161  87 return content;
162    }
163   
164  5 result.append(content.substring(current));
165   
166  5 return result.toString();
167    }
168    }