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

File XHTMLParser.java

 

Coverage histogram

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

Code metrics

8
44
10
1
240
154
16
0.36
4.4
10
1.6

Classes

Class Line # Actions
XHTMLParser 67 44 0% 16 4
0.935483993.5%
 

Contributing tests

This file is covered by 283 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;
21   
22    import java.io.IOException;
23    import java.io.PushbackReader;
24    import java.io.Reader;
25    import java.util.Collections;
26    import java.util.HashMap;
27    import java.util.Map;
28   
29    import javax.inject.Inject;
30    import javax.inject.Named;
31    import javax.inject.Singleton;
32   
33    import org.xwiki.component.annotation.Component;
34    import org.xwiki.component.manager.ComponentManager;
35    import org.xwiki.rendering.block.XDOM;
36    import org.xwiki.rendering.internal.parser.wikimodel.AbstractWikiModelParser;
37    import org.xwiki.rendering.internal.parser.wikimodel.XWikiGeneratorListener;
38    import org.xwiki.rendering.internal.parser.xhtml.wikimodel.XHTMLXWikiGeneratorListener;
39    import org.xwiki.rendering.internal.parser.xhtml.wikimodel.XWikiCommentHandler;
40    import org.xwiki.rendering.internal.parser.xhtml.wikimodel.XWikiHeaderTagHandler;
41    import org.xwiki.rendering.internal.parser.xhtml.wikimodel.XWikiImageTagHandler;
42    import org.xwiki.rendering.internal.parser.xhtml.wikimodel.XWikiReferenceTagHandler;
43    import org.xwiki.rendering.internal.parser.xhtml.wikimodel.XWikiSpanTagHandler;
44    import org.xwiki.rendering.internal.parser.xhtml.wikimodel.XWikiTableDataTagHandler;
45    import org.xwiki.rendering.listener.Listener;
46    import org.xwiki.rendering.parser.ParseException;
47    import org.xwiki.rendering.parser.ResourceReferenceParser;
48    import org.xwiki.rendering.parser.StreamParser;
49    import org.xwiki.rendering.renderer.PrintRendererFactory;
50    import org.xwiki.rendering.syntax.Syntax;
51    import org.xwiki.rendering.util.IdGenerator;
52    import org.xwiki.rendering.wikimodel.IWikiParser;
53    import org.xwiki.rendering.wikimodel.xhtml.XhtmlParser;
54    import org.xwiki.rendering.wikimodel.xhtml.handler.DivisionTagHandler;
55    import org.xwiki.rendering.wikimodel.xhtml.handler.TagHandler;
56    import org.xwiki.xml.XMLReaderFactory;
57   
58    /**
59    * Parses XHTML and generate a {@link org.xwiki.rendering.block.XDOM} object.
60    *
61    * @version $Id: 0e9ac521a8f39fdff31eaca5475e893afbe410ef $
62    * @since 1.5M2
63    */
64    @Component
65    @Named("xhtml/1.0")
66    @Singleton
 
67    public class XHTMLParser extends AbstractWikiModelParser
68    {
69    /**
70    * The parser used for the link label parsing. For (x)html parsing, this will be an xwiki 2.0 parser, since it's
71    * more convenient to pass link labels in xwiki syntax. See referred resource for more details.
72    *
73    * @see XWikiCommentHandler#handleLinkCommentStop(String,
74    * org.xwiki.rendering.wikimodel.xhtml.impl.TagStack)
75    */
76    @Inject
77    @Named("xdom+xml/current")
78    private StreamParser xmlParser;
79   
80    @Inject
81    @Named("xdom+xml/current")
82    private PrintRendererFactory xmlRenderer;
83   
84    /**
85    * @see #getLinkReferenceParser()
86    */
87    @Inject
88    @Named("link")
89    private ResourceReferenceParser linkReferenceParser;
90   
91    /**
92    * @see #getImageReferenceParser()
93    */
94    @Inject
95    @Named("image")
96    private ResourceReferenceParser imageReferenceParser;
97   
98    @Inject
99    private ComponentManager componentManager;
100   
101    @Inject
102    @Named("xhtmlmarker")
103    private ResourceReferenceParser xhtmlMarkerResourceReferenceParser;
104   
105    /**
106    * A special factory that create foolproof XML reader that have the following characteristics:
107    * <ul>
108    * <li>Use DTD caching when the underlying XML parser is Xerces</li>
109    * <li>Ignore SAX callbacks when the parser parses the DTD</li>
110    * <li>Accumulate onCharacters() calls since SAX parser may normally call this event several times.</li>
111    * <li>Remove non-semantic white spaces where needed</li>
112    * <li>Resolve DTDs locally to speed DTD loading/validation</li>
113    * </ul>
114    */
115    @Inject
116    @Named("xwiki")
117    private XMLReaderFactory xmlReaderFactory;
118   
 
119  337 toggle @Override
120    public Syntax getSyntax()
121    {
122  337 return Syntax.XHTML_1_0;
123    }
124   
 
125  370 toggle @Override
126    public StreamParser getLinkLabelParser()
127    {
128  370 return this.xmlParser;
129    }
130   
 
131  294 toggle @Override
132    public IWikiParser createWikiModelParser() throws ParseException
133    {
134    // Override some of the WikiModel XHTML parser tag handlers to introduce our own logic.
135  294 Map<String, TagHandler> handlers = new HashMap<>();
136  294 TagHandler handler = new XWikiHeaderTagHandler();
137  294 handlers.put("h1", handler);
138  294 handlers.put("h2", handler);
139  294 handlers.put("h3", handler);
140  294 handlers.put("h4", handler);
141  294 handlers.put("h5", handler);
142  294 handlers.put("h6", handler);
143  294 handlers.put("a", new XWikiReferenceTagHandler(this, this.xmlRenderer));
144  294 handlers.put("img", new XWikiImageTagHandler());
145  294 handlers.put("span", new XWikiSpanTagHandler());
146    // Change the class value indicating that the division is an embedded document. We do this in order to be
147    // independent of WikiModel in what we expose to the outside world. Thus if one day we need to change to
148    // another implementation we won't be tied to WikiModel.
149  294 handlers.put("div", new DivisionTagHandler("xwiki-document"));
150  294 handlers.put("th", new XWikiTableDataTagHandler());
151   
152  294 XhtmlParser parser = new XhtmlParser();
153  294 parser.setExtraHandlers(handlers);
154  294 parser.setCommentHandler(new XWikiCommentHandler(this.componentManager, this,
155    this.xmlRenderer, this.xhtmlMarkerResourceReferenceParser));
156   
157    // Construct our own XML filter chain since we want to use our own Comment filter.
158  294 try {
159  294 parser.setXmlReader(this.xmlReaderFactory.createXMLReader());
160    } catch (Exception e) {
161  0 throw new ParseException("Failed to create XML reader", e);
162    }
163   
164  294 return parser;
165    }
166   
 
167  370 toggle @Override
168    public ResourceReferenceParser getLinkReferenceParser()
169    {
170  370 return this.linkReferenceParser;
171    }
172   
173    /**
174    * {@inheritDoc}
175    *
176    * @see org.xwiki.rendering.internal.parser.wikimodel.AbstractWikiModelParser#getImageReferenceParser()
177    * @since 2.5RC1
178    */
 
179  370 toggle @Override
180    public ResourceReferenceParser getImageReferenceParser()
181    {
182  370 return this.imageReferenceParser;
183    }
184   
 
185  370 toggle @Override
186    public XWikiGeneratorListener createXWikiGeneratorListener(Listener listener, IdGenerator idGenerator)
187    {
188  370 return new XHTMLXWikiGeneratorListener(getLinkLabelParser(), listener, getLinkReferenceParser(),
189    getImageReferenceParser(), this.plainRendererFactory, idGenerator, getSyntax());
190    }
191   
 
192  212 toggle @Override
193    public XDOM parse(Reader source) throws ParseException
194    {
195  212 Reader pushBackReader = getPushBackReader(source);
196  212 if (pushBackReader != null) {
197  211 return super.parse(pushBackReader);
198    } else {
199  1 return new XDOM(Collections.emptyList());
200    }
201    }
202   
 
203  83 toggle @Override
204    public void parse(Reader source, Listener listener) throws ParseException
205    {
206  83 Reader pushBackReader = getPushBackReader(source);
207  83 if (pushBackReader != null) {
208  83 super.parse(pushBackReader, listener);
209    }
210    }
211   
 
212  294 toggle @Override
213    protected void parse(Reader source, Listener listener, IdGenerator idGenerator) throws ParseException
214    {
215  294 Reader pushBackReader = getPushBackReader(source);
216  294 if (pushBackReader != null) {
217  294 super.parse(pushBackReader, listener, idGenerator);
218    }
219    }
220   
221    /**
222    * In order to handle empty content we use a {@link PushbackReader} to try to read one character from the stream
223    * and if we get -1 it means that the stream is empty and in this case we return an empty XDOM.
224    */
 
225  589 toggle private Reader getPushBackReader(Reader source) throws ParseException
226    {
227  589 PushbackReader pushbackReader = new PushbackReader(source);
228  589 int c;
229  589 try {
230  589 c = pushbackReader.read();
231  589 if (c == -1) {
232  1 return null;
233    }
234  588 pushbackReader.unread(c);
235    } catch (IOException e) {
236  0 throw new ParseException("Failed to find out if the source to parse is empty or not", e);
237    }
238  588 return pushbackReader;
239    }
240    }