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

File AbstractWikiModelParser.java

 

Coverage histogram

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

Code metrics

0
14
5
1
135
58
6
0.43
2.8
5
1.2

Classes

Class Line # Actions
AbstractWikiModelParser 44 14 0% 6 0
1.0100%
 

Contributing tests

This file is covered by 1338 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.wikimodel;
21   
22    import java.io.Reader;
23   
24    import javax.inject.Inject;
25    import javax.inject.Named;
26   
27    import org.xwiki.rendering.block.XDOM;
28    import org.xwiki.rendering.internal.parser.XDOMGeneratorListener;
29    import org.xwiki.rendering.listener.Listener;
30    import org.xwiki.rendering.parser.ParseException;
31    import org.xwiki.rendering.parser.Parser;
32    import org.xwiki.rendering.parser.ResourceReferenceParser;
33    import org.xwiki.rendering.parser.StreamParser;
34    import org.xwiki.rendering.renderer.PrintRendererFactory;
35    import org.xwiki.rendering.util.IdGenerator;
36    import org.xwiki.rendering.wikimodel.IWikiParser;
37   
38    /**
39    * Common code for all WikiModel-based parsers.
40    *
41    * @version $Id: 901a0e9ab9b3886df5f49590f2f6e432c8b192db $
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  22951 toggle protected StreamParser getLinkLabelParser()
86    {
87  22950 return this;
88    }
89   
 
90  22278 toggle @Override
91    public XDOM parse(Reader source) throws ParseException
92    {
93  22278 IdGenerator idGenerator = new IdGenerator();
94  22275 XDOMGeneratorListener listener = new XDOMGeneratorListener();
95  22277 parse(source, listener, idGenerator);
96   
97  22278 XDOM xdom = listener.getXDOM();
98  22277 xdom.setIdGenerator(idGenerator);
99   
100  22276 return xdom;
101    }
102   
 
103  1019 toggle @Override
104    public void parse(Reader source, Listener listener) throws ParseException
105    {
106  1019 IdGenerator idGenerator = new IdGenerator();
107   
108  1019 parse(source, listener, idGenerator);
109    }
110   
 
111  3503 toggle @Override
112    public XWikiGeneratorListener createXWikiGeneratorListener(Listener listener, IdGenerator idGenerator)
113    {
114  3503 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  23292 toggle protected void parse(Reader source, Listener listener, IdGenerator idGenerator) throws ParseException
127    {
128  23292 IWikiParser parser = createWikiModelParser();
129  23291 try {
130  23293 parser.parse(source, createXWikiGeneratorListener(listener, idGenerator));
131    } catch (Exception e) {
132  2 throw new ParseException("Failed to parse input source", e);
133    }
134    }
135    }