1. Project Clover database Sat Feb 2 2019 06:45:20 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 333 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.XWikiDivTagHandler;
41    import org.xwiki.rendering.internal.parser.xhtml.wikimodel.XWikiHeaderTagHandler;
42    import org.xwiki.rendering.internal.parser.xhtml.wikimodel.XWikiImageTagHandler;
43    import org.xwiki.rendering.internal.parser.xhtml.wikimodel.XWikiReferenceTagHandler;
44    import org.xwiki.rendering.internal.parser.xhtml.wikimodel.XWikiSpanTagHandler;
45    import org.xwiki.rendering.internal.parser.xhtml.wikimodel.XWikiTableDataTagHandler;
46    import org.xwiki.rendering.listener.Listener;
47    import org.xwiki.rendering.parser.ParseException;
48    import org.xwiki.rendering.parser.ResourceReferenceParser;
49    import org.xwiki.rendering.parser.StreamParser;
50    import org.xwiki.rendering.renderer.PrintRendererFactory;
51    import org.xwiki.rendering.syntax.Syntax;
52    import org.xwiki.rendering.util.IdGenerator;
53    import org.xwiki.rendering.wikimodel.IWikiParser;
54    import org.xwiki.rendering.wikimodel.xhtml.XhtmlParser;
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: 4c0e234939d20c8ca8652fdbbf72f35fe770506b $
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  1156 toggle @Override
120    public Syntax getSyntax()
121    {
122  1156 return Syntax.XHTML_1_0;
123    }
124   
 
125  422 toggle @Override
126    public StreamParser getLinkLabelParser()
127    {
128  422 return this.xmlParser;
129    }
130   
 
131  334 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  334 Map<String, TagHandler> handlers = new HashMap<>();
136  334 TagHandler handler = new XWikiHeaderTagHandler();
137  334 handlers.put("h1", handler);
138  334 handlers.put("h2", handler);
139  334 handlers.put("h3", handler);
140  334 handlers.put("h4", handler);
141  334 handlers.put("h5", handler);
142  334 handlers.put("h6", handler);
143  334 handlers.put("a", new XWikiReferenceTagHandler(this, this.xmlRenderer));
144  334 handlers.put("img", new XWikiImageTagHandler());
145  334 handlers.put("span", new XWikiSpanTagHandler(this.componentManager, this));
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  334 handlers.put("div", new XWikiDivTagHandler("xwiki-document", this.componentManager, this));
150  334 handlers.put("th", new XWikiTableDataTagHandler());
151   
152  334 XhtmlParser parser = new XhtmlParser();
153  334 parser.setExtraHandlers(handlers);
154  334 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  334 try {
159  334 parser.setXmlReader(this.xmlReaderFactory.createXMLReader());
160    } catch (Exception e) {
161  0 throw new ParseException("Failed to create XML reader", e);
162    }
163   
164  334 return parser;
165    }
166   
 
167  422 toggle @Override
168    public ResourceReferenceParser getLinkReferenceParser()
169    {
170  422 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  422 toggle @Override
180    public ResourceReferenceParser getImageReferenceParser()
181    {
182  422 return this.imageReferenceParser;
183    }
184   
 
185  422 toggle @Override
186    public XWikiGeneratorListener createXWikiGeneratorListener(Listener listener, IdGenerator idGenerator)
187    {
188  422 return new XHTMLXWikiGeneratorListener(getLinkLabelParser(), listener, getLinkReferenceParser(),
189    getImageReferenceParser(), this.plainRendererFactory, idGenerator, getSyntax());
190    }
191   
 
192  262 toggle @Override
193    public XDOM parse(Reader source) throws ParseException
194    {
195  262 Reader pushBackReader = getPushBackReader(source);
196  262 if (pushBackReader != null) {
197  261 return super.parse(pushBackReader);
198    } else {
199  1 return new XDOM(Collections.emptyList());
200    }
201    }
202   
 
203  73 toggle @Override
204    public void parse(Reader source, Listener listener) throws ParseException
205    {
206  73 Reader pushBackReader = getPushBackReader(source);
207  73 if (pushBackReader != null) {
208  73 super.parse(pushBackReader, listener);
209    }
210    }
211   
 
212  334 toggle @Override
213    protected void parse(Reader source, Listener listener, IdGenerator idGenerator) throws ParseException
214    {
215  334 Reader pushBackReader = getPushBackReader(source);
216  334 if (pushBackReader != null) {
217  334 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  669 toggle private Reader getPushBackReader(Reader source) throws ParseException
226    {
227  669 PushbackReader pushbackReader = new PushbackReader(source);
228  669 int c;
229  669 try {
230  669 c = pushbackReader.read();
231  669 if (c == -1) {
232  1 return null;
233    }
234  668 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  668 return pushbackReader;
239    }
240    }