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

File DefaultConverter.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

0
16
1
1
99
58
5
0.31
16
1
5

Classes

Class Line # Actions
DefaultConverter 51 16 0% 5 4
0.764705976.5%
 

Contributing tests

This file is covered by 10 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.converter;
21   
22    import java.io.Reader;
23   
24    import javax.inject.Inject;
25    import javax.inject.Named;
26    import javax.inject.Provider;
27    import javax.inject.Singleton;
28   
29    import org.xwiki.component.annotation.Component;
30    import org.xwiki.component.manager.ComponentLookupException;
31    import org.xwiki.component.manager.ComponentManager;
32    import org.xwiki.rendering.block.XDOM;
33    import org.xwiki.rendering.converter.ConversionException;
34    import org.xwiki.rendering.converter.Converter;
35    import org.xwiki.rendering.parser.ParseException;
36    import org.xwiki.rendering.parser.Parser;
37    import org.xwiki.rendering.renderer.BlockRenderer;
38    import org.xwiki.rendering.renderer.printer.WikiPrinter;
39    import org.xwiki.rendering.syntax.Syntax;
40    import org.xwiki.rendering.transformation.TransformationContext;
41    import org.xwiki.rendering.transformation.TransformationException;
42    import org.xwiki.rendering.transformation.TransformationManager;
43   
44    /**
45    * Default implementation for {@link Converter}.
46    *
47    * @version $Id: 878aac9bc77408be2906b13137dea092a32dd662 $
48    */
49    @Component
50    @Singleton
 
51    public class DefaultConverter implements Converter
52    {
53    /**
54    * Used to lookup parser and renderer.
55    */
56    @Inject
57    @Named("context")
58    private Provider<ComponentManager> componentManagerProvider;
59   
60    /**
61    * Used to execute transformations.
62    */
63    @Inject
64    private TransformationManager transformationManager;
65   
 
66  10 toggle @Override
67    public void convert(Reader source, Syntax sourceSyntax, Syntax targetSyntax, WikiPrinter printer)
68    throws ConversionException
69    {
70    // Step 1: Find the parser and generate a XDOM
71  10 XDOM xdom;
72  10 try {
73  10 Parser parser = this.componentManagerProvider.get().getInstance(Parser.class, sourceSyntax.toIdString());
74  10 xdom = parser.parse(source);
75    } catch (ComponentLookupException e) {
76  0 throw new ConversionException("Failed to locate Parser for syntax [" + sourceSyntax + "]", e);
77    } catch (ParseException e) {
78  0 throw new ConversionException("Failed to parse input source", e);
79    }
80   
81    // Step 2: Run transformations
82  10 try {
83  10 TransformationContext context = new TransformationContext(xdom, sourceSyntax);
84  10 context.setTargetSyntax(targetSyntax);
85  10 this.transformationManager.performTransformations(xdom, context);
86    } catch (TransformationException e) {
87  0 throw new ConversionException("Failed to execute some transformations", e);
88    }
89   
90    // Step 3: Locate the Renderer and render the content in the passed printer
91  10 BlockRenderer renderer;
92  10 try {
93  10 renderer = this.componentManagerProvider.get().getInstance(BlockRenderer.class, targetSyntax.toIdString());
94    } catch (ComponentLookupException e) {
95  0 throw new ConversionException("Failed to locate Renderer for syntax [" + targetSyntax + "]", e);
96    }
97  10 renderer.render(xdom, printer);
98    }
99    }