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

File ConfluenceXWikiGeneratorListener.java

 

Coverage histogram

../../../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

52
77
3
1
208
137
30
0.39
25.67
3
10

Classes

Class Line # Actions
ConfluenceXWikiGeneratorListener 47 77 0% 30 16
0.878787987.9%
 

Contributing tests

This file is covered by 21 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.confluencexhtml.wikimodel;
21   
22    import java.util.Collections;
23   
24    import org.xwiki.rendering.internal.parser.confluencexhtml.wikimodel.AttachmentTagHandler.ConfluenceAttachment;
25    import org.xwiki.rendering.internal.parser.wikimodel.WikiModelParserUtils;
26    import org.xwiki.rendering.internal.parser.xhtml.wikimodel.XHTMLXWikiGeneratorListener;
27    import org.xwiki.rendering.listener.Listener;
28    import org.xwiki.rendering.listener.reference.AttachmentResourceReference;
29    import org.xwiki.rendering.listener.reference.DocumentResourceReference;
30    import org.xwiki.rendering.listener.reference.ResourceReference;
31    import org.xwiki.rendering.listener.reference.ResourceType;
32    import org.xwiki.rendering.listener.reference.UserResourceReference;
33    import org.xwiki.rendering.parser.ParseException;
34    import org.xwiki.rendering.parser.ResourceReferenceParser;
35    import org.xwiki.rendering.parser.StreamParser;
36    import org.xwiki.rendering.renderer.PrintRendererFactory;
37    import org.xwiki.rendering.syntax.Syntax;
38    import org.xwiki.rendering.util.IdGenerator;
39    import org.xwiki.rendering.wikimodel.WikiReference;
40   
41    /**
42    * WikiModel listener bridge for the XHTML Syntax.
43    *
44    * @version $Id: 80b4bf0935da272c16cfae29a5382b7857ba5025 $
45    * @since 2.5RC1
46    */
 
47    public class ConfluenceXWikiGeneratorListener extends XHTMLXWikiGeneratorListener
48    {
49    private StreamParser plainParser;
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  47 toggle public ConfluenceXWikiGeneratorListener(StreamParser parser, Listener listener,
62    ResourceReferenceParser linkReferenceParser, ResourceReferenceParser imageReferenceParser,
63    PrintRendererFactory plainRendererFactory, IdGenerator idGenerator, Syntax syntax, StreamParser plainParser)
64    {
65  47 super(parser, listener, linkReferenceParser, imageReferenceParser, plainRendererFactory, idGenerator, syntax);
66   
67  47 this.plainParser = plainParser;
68    }
69   
 
70  77 toggle @Override
71    public void onReference(WikiReference reference)
72    {
73  77 if (reference instanceof ConfluenceLinkWikiReference) {
74  74 ConfluenceLinkWikiReference confluenceReference = (ConfluenceLinkWikiReference) reference;
75   
76  74 ResourceReference resourceReference = null;
77   
78  74 if (confluenceReference.getPage() != null) {
79  48 StringBuilder str = new StringBuilder();
80  48 if (confluenceReference.getSpace() != null) {
81  5 str.append(confluenceReference.getSpace());
82  5 str.append('.');
83    }
84  48 if (confluenceReference.getPage() != null) {
85  48 str.append(confluenceReference.getPage());
86  0 } else if (confluenceReference.getSpace() != null) {
87  0 str.append("WebHome");
88    }
89   
90  48 DocumentResourceReference documentResourceReference = new DocumentResourceReference(str.toString());
91   
92  48 if (confluenceReference.getAnchor() != null) {
93  1 documentResourceReference.setAnchor(confluenceReference.getAnchor());
94    }
95   
96  48 resourceReference = documentResourceReference;
97  26 } else if (confluenceReference.getSpace() != null) {
98  2 DocumentResourceReference documentResourceReference =
99    new DocumentResourceReference(confluenceReference.getSpace() + ".WebHome");
100   
101  2 if (confluenceReference.getAnchor() != null) {
102  1 documentResourceReference.setAnchor(confluenceReference.getAnchor());
103    }
104   
105  2 resourceReference = documentResourceReference;
106  24 } else if (confluenceReference.getAttachment() != null) {
107  9 ConfluenceAttachment attachment = confluenceReference.getAttachment();
108   
109  9 StringBuilder str = new StringBuilder();
110  9 if (attachment.space != null) {
111  1 str.append(attachment.space);
112  1 str.append('.');
113    }
114  9 if (attachment.page != null) {
115  1 str.append(attachment.page);
116  1 str.append('@');
117  8 } else if (attachment.space != null) {
118  0 str.append("WebHome");
119  0 str.append('@');
120    }
121   
122  9 if (attachment.user != null) {
123    // TODO
124    }
125   
126  9 str.append(attachment.filename);
127   
128  9 AttachmentResourceReference attachmentResourceReference =
129    new AttachmentResourceReference(str.toString());
130   
131  9 if (confluenceReference.getAnchor() != null) {
132  1 attachmentResourceReference.setAnchor(confluenceReference.getAnchor());
133    }
134   
135  9 resourceReference = attachmentResourceReference;
136  15 } else if (confluenceReference.getUser() != null) {
137  15 UserResourceReference userResourceReference = new UserResourceReference(confluenceReference.getUser());
138   
139  15 if (confluenceReference.getAnchor() != null) {
140  1 userResourceReference.setAnchor(confluenceReference.getAnchor());
141    }
142   
143  15 resourceReference = userResourceReference;
144    }
145   
146  74 if (resourceReference != null) {
147    // Since WikiModel doesn't handle syntax in link labels and thus doesn't have begin/end events for
148    // links, we
149    // need to call the XWiki events and use an inline parser to parse the syntax in the label.
150  74 getListener().beginLink(resourceReference, false, Collections.<String, String>emptyMap());
151  74 if (reference.getLabel() != null) {
152  14 try {
153  14 WikiModelParserUtils parserUtils = new WikiModelParserUtils();
154  14 parserUtils.parseInline(this.plainParser, reference.getLabel(), getListener(), false);
155    } catch (ParseException e) {
156    // TODO supposedly impossible with plain test parser
157    }
158    }
159  74 getListener().endLink(resourceReference, false, Collections.<String, String>emptyMap());
160    }
161    } else {
162  3 super.onReference(reference);
163    }
164    }
165   
 
166  60 toggle @Override
167    public void onImage(WikiReference reference)
168    {
169  60 if (reference instanceof ConfluenceImageWikiReference) {
170  60 ConfluenceImageWikiReference confluenceReference = (ConfluenceImageWikiReference) reference;
171   
172  60 ResourceReference resourceReference = null;
173   
174  60 if (confluenceReference.getAttachment() != null) {
175  59 ConfluenceAttachment attachment = confluenceReference.getAttachment();
176   
177  59 StringBuilder str = new StringBuilder();
178  59 if (attachment.space != null) {
179  2 str.append(attachment.space);
180  2 str.append('.');
181    }
182  59 if (attachment.page != null) {
183  24 str.append(attachment.page);
184  24 str.append('@');
185  35 } else if (attachment.space != null) {
186  1 str.append("WebHome");
187  1 str.append('@');
188    }
189   
190  59 if (attachment.user != null) {
191    // TODO
192    }
193   
194  59 str.append(attachment.filename);
195   
196  59 resourceReference = new AttachmentResourceReference(str.toString());
197  1 } else if (confluenceReference.getURL() != null) {
198  1 resourceReference = new ResourceReference(confluenceReference.getURL(), ResourceType.URL);
199    }
200   
201  60 if (resourceReference != null) {
202  60 onImage(resourceReference, false, Collections.<String, String>emptyMap());
203    }
204    } else {
205  0 super.onImage(reference);
206    }
207    }
208    }