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

File XHTMLXWikiGeneratorListener.java

 

Coverage histogram

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

Code metrics

6
25
4
1
148
68
7
0.28
6.25
4
1.75

Classes

Class Line # Actions
XHTMLXWikiGeneratorListener 44 25 0% 7 0
1.0100%
 

Contributing tests

This file is covered by 303 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.xhtml.wikimodel;
21   
22    import java.util.Map;
23    import java.util.regex.Matcher;
24    import java.util.regex.Pattern;
25   
26    import org.apache.commons.lang3.tuple.Pair;
27    import org.xwiki.rendering.internal.parser.wikimodel.DefaultXWikiGeneratorListener;
28    import org.xwiki.rendering.listener.Listener;
29    import org.xwiki.rendering.listener.reference.ResourceReference;
30    import org.xwiki.rendering.listener.reference.ResourceType;
31    import org.xwiki.rendering.parser.ResourceReferenceParser;
32    import org.xwiki.rendering.parser.StreamParser;
33    import org.xwiki.rendering.renderer.PrintRendererFactory;
34    import org.xwiki.rendering.syntax.Syntax;
35    import org.xwiki.rendering.util.IdGenerator;
36    import org.xwiki.rendering.wikimodel.WikiReference;
37   
38    /**
39    * WikiModel listener bridge for the XHTML Syntax.
40    *
41    * @version $Id: 62e65804f752b9d9e88a0789208995812852e0cf $
42    * @since 2.5RC1
43    */
 
44    public class XHTMLXWikiGeneratorListener extends DefaultXWikiGeneratorListener
45    {
46    /**
47    * URL matching pattern.
48    */
49    private static final Pattern URL_SCHEME_PATTERN = Pattern.compile("[a-zA-Z0-9+.-]*://");
50   
51    /**
52    * @param parser the parser to use to parse link labels
53    * @param listener the XWiki listener to which to forward WikiModel events
54    * @param linkReferenceParser the parser to parse link references
55    * @param imageReferenceParser the parser to parse image references
56    * @param plainRendererFactory used to generate header ids
57    * @param idGenerator used to generate header ids
58    * @param syntax the syntax of the parsed source
59    * @since 3.0M3
60    */
 
61  417 toggle public XHTMLXWikiGeneratorListener(StreamParser parser, Listener listener,
62    ResourceReferenceParser linkReferenceParser, ResourceReferenceParser imageReferenceParser,
63    PrintRendererFactory plainRendererFactory, IdGenerator idGenerator, Syntax syntax)
64    {
65  417 super(parser, listener, linkReferenceParser, imageReferenceParser, plainRendererFactory, idGenerator, syntax);
66    }
67   
 
68  79 toggle @Override
69    public void onReference(WikiReference reference)
70    {
71    // We need to handle 2 cases:
72    // - when the passed reference is an instance of XWikiWikiReference, i.e. when a XHTML comment defining a XWiki
73    // link has been specified and the XHTML parser has recognized it and thus is passing a typed reference to us.
74    // - when the passed reference is not an instance of XWikiWikiReference which will happen if there's no special
75    // XHTML comment defining a XWiki link. In this case, we need to figure out what how to consider the passed
76    // reference.
77   
78  79 ResourceReference resourceReference;
79  79 boolean isFreeStanding;
80  79 if (!(reference instanceof XWikiWikiReference)) {
81  22 resourceReference = computeResourceReference(reference.getLink());
82  22 isFreeStanding = false;
83    } else {
84  57 XWikiWikiReference xwikiReference = (XWikiWikiReference) reference;
85  57 resourceReference = xwikiReference.getReference();
86  57 isFreeStanding = xwikiReference.isFreeStanding();
87   
88  57 flushFormat();
89    }
90   
91    // Consider query string and anchor as ResourceReference parameters and the rest as generic parameters
92  79 Pair<Map<String, String>, Map<String, String>> parameters =
93    convertAndSeparateParameters(reference.getParameters());
94   
95  79 resourceReference.setParameters(parameters.getLeft());
96  79 onReference(resourceReference, reference.getLabel(), isFreeStanding, parameters.getRight(), false);
97    }
98   
 
99  29 toggle @Override
100    public void onImage(WikiReference reference)
101    {
102    // We need to handle 2 cases:
103    // - when the passed reference is an instance of XWikiWikiReference, i.e. when a XHTML comment defining a XWiki
104    // image has been specified
105    // - when the passed reference is not an instance of XWikiWikiReference which will happen if there's no special
106    // XHTML comment defining a XWiki image
107  29 if (!(reference instanceof XWikiWikiReference)) {
108  6 super.onImage(reference);
109    } else {
110  23 XWikiWikiReference xwikiReference = (XWikiWikiReference) reference;
111  23 ResourceReference resourceReference = xwikiReference.getReference();
112   
113  23 flushFormat();
114   
115  23 onImage(resourceReference, xwikiReference.isFreeStanding(),
116    convertParameters(xwikiReference.getParameters()));
117    }
118    }
119   
120    /**
121    * Recognize the passed reference and figure out what type of link it should be:
122    * <ul>
123    * <li>UC1: the reference points to a valid URL, we return a reference of type "url",
124    * e.g. {@code http://server/path/reference#anchor}</li>
125    * <li>UC2: the reference is not a valid URL, we return a reference of type "path",
126    * e.g. {@code path/reference#anchor}</li>
127    * </ul>
128    *
129    * @param rawReference the full reference (e.g. "/some/path/something#other")
130    * @return the properly typed {@link ResourceReference} matching the use cases
131    */
 
132  22 toggle private ResourceReference computeResourceReference(String rawReference)
133    {
134  22 ResourceReference reference;
135   
136    // Do we have a valid URL?
137  22 Matcher matcher = URL_SCHEME_PATTERN.matcher(rawReference);
138  22 if (matcher.lookingAt()) {
139    // We have UC1
140  9 reference = new ResourceReference(rawReference, ResourceType.URL);
141    } else {
142    // We have UC2
143  13 reference = new ResourceReference(rawReference, ResourceType.PATH);
144    }
145   
146  22 return reference;
147    }
148    }