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
14   135   6   2.8
0   58   0.43   5
5     1.2  
1    
 
  AbstractWikiModelParser       Line # 44 14 0% 6 1 94.7% 0.94736844
 
  (967)
 
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.wikimodel;
21   
22    import java.io.Reader;
23   
24    import javax.inject.Inject;
25    import javax.inject.Named;
26   
27    import org.xwiki.rendering.wikimodel.IWikiParser;
28    import org.xwiki.rendering.block.XDOM;
29    import org.xwiki.rendering.internal.parser.XDOMGeneratorListener;
30    import org.xwiki.rendering.listener.Listener;
31    import org.xwiki.rendering.parser.ResourceReferenceParser;
32    import org.xwiki.rendering.parser.ParseException;
33    import org.xwiki.rendering.parser.Parser;
34    import org.xwiki.rendering.parser.StreamParser;
35    import org.xwiki.rendering.renderer.PrintRendererFactory;
36    import org.xwiki.rendering.util.IdGenerator;
37   
38    /**
39    * Common code for all WikiModel-based parsers.
40    *
41    * @version $Id: 6bd18e66158a6e907b9f7409da661c07486c1077 $
42    * @since 1.5M2
43    */
 
44    public abstract class AbstractWikiModelParser implements Parser, WikiModelStreamParser
45    {
46    /**
47    * Used by the XWiki Generator Listener to generate unique header ids.
48    */
49    @Inject
50    @Named("plain/1.0")
51    protected PrintRendererFactory plainRendererFactory;
52   
53    /**
54    * @return the WikiModel parser instance to use to parse input content.
55    * @throws ParseException when there's a problem creating an instance of the parser to use
56    */
57    public abstract IWikiParser createWikiModelParser() throws ParseException;
58   
59    /**
60    * @return the parser to use when parsing link references. We need to parse link references to transform them from
61    * a string representation coming from WikiModel into a
62    * {@link org.xwiki.rendering.listener.reference.ResourceReference} object.
63    * @since 2.5RC1
64    */
65    public abstract ResourceReferenceParser getLinkReferenceParser();
66   
67    /**
68    * @return the parser to use when parsing image references. We need to parse image references to transform them from
69    * a string representation coming from WikiModel into a
70    * {@link org.xwiki.rendering.listener.reference.ResourceReference} object.
71    * @since 2.5RC1
72    */
73    public abstract ResourceReferenceParser getImageReferenceParser();
74   
75    /**
76    * @return the syntax parser to use for parsing link labels, since wikimodel does not support wiki syntax in links
77    * and they need to be handled in the {@link XWikiGeneratorListener}. By default, the link label parser is
78    * the same one as the source parser (this), but you should overwrite this method if you need to use a
79    * special parser.
80    * @see XDOMGeneratorListener
81    * @see <a href="http://code.google.com/p/wikimodel/issues/detail?id=87">wikimodel issue 87</a>
82    * @since 2.1RC1
83    */
84    // TODO: Remove this method when the parser will not need to be passed to the XDOMGeneratorListener anymore.
 
85  909 toggle protected StreamParser getLinkLabelParser()
86    {
87  909 return this;
88    }
89   
 
90  1009 toggle @Override
91    public XDOM parse(Reader source) throws ParseException
92    {
93  1009 IdGenerator idGenerator = new IdGenerator();
94  1009 XDOMGeneratorListener listener = new XDOMGeneratorListener();
95  1009 parse(source, listener, idGenerator);
96   
97  1009 XDOM xdom = listener.getXDOM();
98  1009 xdom.setIdGenerator(idGenerator);
99   
100  1009 return xdom;
101    }
102   
 
103  119 toggle @Override
104    public void parse(Reader source, Listener listener) throws ParseException
105    {
106  119 IdGenerator idGenerator = new IdGenerator();
107   
108  119 parse(source, listener, idGenerator);
109    }
110   
 
111  876 toggle @Override
112    public XWikiGeneratorListener createXWikiGeneratorListener(Listener listener, IdGenerator idGenerator)
113    {
114  876 return new DefaultXWikiGeneratorListener(getLinkLabelParser(), listener, getLinkReferenceParser(),
115    getImageReferenceParser(), this.plainRendererFactory, idGenerator, getSyntax());
116    }
117   
118    /**
119    * @param source the content to parse
120    * @param listener receive event for each element
121    * @param idGenerator unique id tool generator
122    * @throws ParseException if the source cannot be read or an unexpected error happens during the parsing. Parsers
123    * should be written to not generate any error as much as possible.
124    * @since 2.1RC1
125    */
 
126  1128 toggle private void parse(Reader source, Listener listener, IdGenerator idGenerator) throws ParseException
127    {
128  1128 IWikiParser parser = createWikiModelParser();
129  1128 try {
130  1128 parser.parse(source, createXWikiGeneratorListener(listener, idGenerator));
131    } catch (Exception e) {
132  0 throw new ParseException("Failed to parse input source", e);
133    }
134    }
135    }