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
25   182   7   4.17
0   108   0.28   6
6     1.17  
1    
 
  XHTMLParser       Line # 62 25 0% 7 1 96.8% 0.9677419
 
  (219)
 
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.util.HashMap;
23    import java.util.Map;
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.manager.ComponentManager;
31    import org.xwiki.rendering.internal.parser.wikimodel.AbstractWikiModelParser;
32    import org.xwiki.rendering.internal.parser.wikimodel.XWikiGeneratorListener;
33    import org.xwiki.rendering.internal.parser.xhtml.wikimodel.XHTMLXWikiGeneratorListener;
34    import org.xwiki.rendering.internal.parser.xhtml.wikimodel.XWikiCommentHandler;
35    import org.xwiki.rendering.internal.parser.xhtml.wikimodel.XWikiDivisionTagHandler;
36    import org.xwiki.rendering.internal.parser.xhtml.wikimodel.XWikiHeaderTagHandler;
37    import org.xwiki.rendering.internal.parser.xhtml.wikimodel.XWikiImageTagHandler;
38    import org.xwiki.rendering.internal.parser.xhtml.wikimodel.XWikiReferenceTagHandler;
39    import org.xwiki.rendering.internal.parser.xhtml.wikimodel.XWikiSpanTagHandler;
40    import org.xwiki.rendering.internal.parser.xhtml.wikimodel.XWikiTableDataTagHandler;
41    import org.xwiki.rendering.listener.Listener;
42    import org.xwiki.rendering.parser.ParseException;
43    import org.xwiki.rendering.parser.ResourceReferenceParser;
44    import org.xwiki.rendering.parser.StreamParser;
45    import org.xwiki.rendering.renderer.PrintRendererFactory;
46    import org.xwiki.rendering.syntax.Syntax;
47    import org.xwiki.rendering.util.IdGenerator;
48    import org.xwiki.rendering.wikimodel.IWikiParser;
49    import org.xwiki.rendering.wikimodel.xhtml.XhtmlParser;
50    import org.xwiki.rendering.wikimodel.xhtml.handler.TagHandler;
51    import org.xwiki.xml.XMLReaderFactory;
52   
53    /**
54    * Parses XHTML and generate a {@link org.xwiki.rendering.block.XDOM} object.
55    *
56    * @version $Id: 08f59eb9f35787c3db2f04d6178df6e0820f21af $
57    * @since 1.5M2
58    */
59    @Component
60    @Named("xhtml/1.0")
61    @Singleton
 
62    public class XHTMLParser extends AbstractWikiModelParser
63    {
64    /**
65    * The parser used for the link label parsing. For (x)html parsing, this will be an xwiki 2.0 parser, since it's
66    * more convenient to pass link labels in xwiki syntax. See referred resource for more details.
67    *
68    * @see XWikiCommentHandler#handleLinkCommentStop(String, org.xwiki.rendering.wikimodel.xhtml.impl.XhtmlHandler.TagStack)
69    */
70    @Inject
71    @Named("xwiki/2.0")
72    private StreamParser xwikiParser;
73   
74    /**
75    * @see #getLinkReferenceParser()
76    */
77    @Inject
78    @Named("link")
79    private ResourceReferenceParser linkReferenceParser;
80   
81    /**
82    * @see #getImageReferenceParser()
83    */
84    @Inject
85    @Named("image")
86    private ResourceReferenceParser imageReferenceParser;
87   
88    @Inject
89    @Named("xwiki/2.1")
90    private PrintRendererFactory xwikiSyntaxPrintRendererFactory;
91   
92    @Inject
93    private ComponentManager componentManager;
94   
95    @Inject
96    @Named("xhtmlmarker")
97    private ResourceReferenceParser xhtmlMarkerResourceReferenceParser;
98   
99    /**
100    * A special factory that create foolproof XML reader that have the following characteristics:
101    * <ul>
102    * <li>Use DTD caching when the underlying XML parser is Xerces</li>
103    * <li>Ignore SAX callbacks when the parser parses the DTD</li>
104    * <li>Accumulate onCharacters() calls since SAX parser may normally call this event several times.</li>
105    * <li>Remove non-semantic white spaces where needed</li>
106    * <li>Resolve DTDs locally to speed DTD loading/validation</li>
107    * </ul>
108    */
109    @Inject
110    @Named("xwiki")
111    private XMLReaderFactory xmlReaderFactory;
112   
 
113  269 toggle @Override
114    public Syntax getSyntax()
115    {
116  269 return Syntax.XHTML_1_0;
117    }
118   
 
119  271 toggle @Override
120    public StreamParser getLinkLabelParser()
121    {
122  271 return this.xwikiParser;
123    }
124   
 
125  219 toggle @Override
126    public IWikiParser createWikiModelParser() throws ParseException
127    {
128    // Override some of the WikiModel XHTML parser tag handlers to introduce our own logic.
129  219 Map<String, TagHandler> handlers = new HashMap<String, TagHandler>();
130  219 TagHandler handler = new XWikiHeaderTagHandler();
131  219 handlers.put("h1", handler);
132  219 handlers.put("h2", handler);
133  219 handlers.put("h3", handler);
134  219 handlers.put("h4", handler);
135  219 handlers.put("h5", handler);
136  219 handlers.put("h6", handler);
137  219 handlers.put("a", new XWikiReferenceTagHandler(this, this.xwikiSyntaxPrintRendererFactory));
138  219 handlers.put("img", new XWikiImageTagHandler());
139  219 handlers.put("span", new XWikiSpanTagHandler());
140  219 handlers.put("div", new XWikiDivisionTagHandler());
141  219 handlers.put("th", new XWikiTableDataTagHandler());
142   
143  219 XhtmlParser parser = new XhtmlParser();
144  219 parser.setExtraHandlers(handlers);
145  219 parser.setCommentHandler(new XWikiCommentHandler(this.componentManager, this,
146    this.xwikiSyntaxPrintRendererFactory, this.xhtmlMarkerResourceReferenceParser));
147   
148    // Construct our own XML filter chain since we want to use our own Comment filter.
149  219 try {
150  219 parser.setXmlReader(this.xmlReaderFactory.createXMLReader());
151    } catch (Exception e) {
152  0 throw new ParseException("Failed to create XML reader", e);
153    }
154   
155  219 return parser;
156    }
157   
 
158  271 toggle @Override
159    public ResourceReferenceParser getLinkReferenceParser()
160    {
161  271 return this.linkReferenceParser;
162    }
163   
164    /**
165    * {@inheritDoc}
166    *
167    * @see org.xwiki.rendering.internal.parser.wikimodel.AbstractWikiModelParser#getImageReferenceParser()
168    * @since 2.5RC1
169    */
 
170  271 toggle @Override
171    public ResourceReferenceParser getImageReferenceParser()
172    {
173  271 return this.imageReferenceParser;
174    }
175   
 
176  271 toggle @Override
177    public XWikiGeneratorListener createXWikiGeneratorListener(Listener listener, IdGenerator idGenerator)
178    {
179  271 return new XHTMLXWikiGeneratorListener(getLinkLabelParser(), listener, getLinkReferenceParser(),
180    getImageReferenceParser(), this.plainRendererFactory, idGenerator, getSyntax());
181    }
182    }