1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.wysiwyg.server.internal.converter; |
21 |
|
|
22 |
|
import java.io.StringReader; |
23 |
|
|
24 |
|
import javax.inject.Inject; |
25 |
|
import javax.inject.Named; |
26 |
|
import javax.inject.Singleton; |
27 |
|
|
28 |
|
import org.slf4j.Logger; |
29 |
|
import org.xwiki.component.annotation.Component; |
30 |
|
import org.xwiki.component.manager.ComponentManager; |
31 |
|
import org.xwiki.gwt.wysiwyg.client.cleaner.HTMLCleaner; |
32 |
|
import org.xwiki.gwt.wysiwyg.client.converter.HTMLConverter; |
33 |
|
import org.xwiki.rendering.block.XDOM; |
34 |
|
import org.xwiki.rendering.internal.transformation.MutableRenderingContext; |
35 |
|
import org.xwiki.rendering.listener.MetaData; |
36 |
|
import org.xwiki.rendering.parser.ParseException; |
37 |
|
import org.xwiki.rendering.parser.Parser; |
38 |
|
import org.xwiki.rendering.parser.StreamParser; |
39 |
|
import org.xwiki.rendering.renderer.BlockRenderer; |
40 |
|
import org.xwiki.rendering.renderer.PrintRendererFactory; |
41 |
|
import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter; |
42 |
|
import org.xwiki.rendering.renderer.printer.WikiPrinter; |
43 |
|
import org.xwiki.rendering.syntax.Syntax; |
44 |
|
import org.xwiki.rendering.syntax.SyntaxFactory; |
45 |
|
import org.xwiki.rendering.transformation.RenderingContext; |
46 |
|
import org.xwiki.rendering.transformation.Transformation; |
47 |
|
import org.xwiki.rendering.transformation.TransformationContext; |
48 |
|
import org.xwiki.rendering.transformation.TransformationException; |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
@version |
54 |
|
|
55 |
|
@Component |
56 |
|
@Singleton |
|
|
| 89.2% |
Uncovered Elements: 4 (37) |
Complexity: 7 |
Complexity Density: 0.21 |
|
57 |
|
public class DefaultHTMLConverter implements HTMLConverter |
58 |
|
{ |
59 |
|
private static final String TRANSFORMATION_ID = "wysiwygtxid"; |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
@Inject |
65 |
|
private Logger logger; |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
@Inject |
71 |
|
private HTMLCleaner htmlCleaner; |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
@Inject |
77 |
|
@Named("xhtml/1.0") |
78 |
|
private Parser xhtmlParser; |
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
@Inject |
84 |
|
@Named("xhtml/1.0") |
85 |
|
private StreamParser xhtmlStreamParser; |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
@Inject |
91 |
|
private SyntaxFactory syntaxFactory; |
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
@Inject |
97 |
|
private RenderingContext renderingContext; |
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
@see |
107 |
|
|
108 |
|
|
109 |
|
@Inject |
110 |
|
@Named("macro") |
111 |
|
private Transformation macroTransformation; |
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
@Inject |
117 |
|
@Named("annotatedxhtml/1.0") |
118 |
|
private BlockRenderer xhtmlRenderer; |
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
@Inject |
125 |
|
@Named("context") |
126 |
|
private ComponentManager contextComponentManager; |
127 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.25 |
|
128 |
11 |
@Override... |
129 |
|
public String fromHTML(String dirtyHTML, String syntaxId) |
130 |
|
{ |
131 |
11 |
try { |
132 |
|
|
133 |
11 |
String html = this.htmlCleaner.clean(dirtyHTML); |
134 |
|
|
135 |
|
|
136 |
|
|
137 |
11 |
WikiPrinter printer = new DefaultWikiPrinter(); |
138 |
11 |
PrintRendererFactory printRendererFactory = |
139 |
|
this.contextComponentManager.getInstance(PrintRendererFactory.class, syntaxId); |
140 |
11 |
this.xhtmlStreamParser.parse(new StringReader(html), printRendererFactory.createRenderer(printer)); |
141 |
|
|
142 |
9 |
return printer.toString(); |
143 |
|
} catch (Exception e) { |
144 |
2 |
this.logger.error(e.getLocalizedMessage(), e); |
145 |
2 |
throw new RuntimeException("Exception while parsing HTML", e); |
146 |
|
} |
147 |
|
} |
148 |
|
|
|
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 2 |
Complexity Density: 0.22 |
|
149 |
23 |
@Override... |
150 |
|
public String toHTML(String source, String syntaxId) |
151 |
|
{ |
152 |
23 |
try { |
153 |
|
|
154 |
23 |
Parser parser = this.contextComponentManager.getInstance(Parser.class, syntaxId); |
155 |
23 |
XDOM xdom = parser.parse(new StringReader(source)); |
156 |
|
|
157 |
|
|
158 |
23 |
executeMacroTransformation(xdom, this.syntaxFactory.createSyntaxFromIdString(syntaxId)); |
159 |
|
|
160 |
|
|
161 |
23 |
WikiPrinter printer = new DefaultWikiPrinter(); |
162 |
23 |
this.xhtmlRenderer.render(xdom, printer); |
163 |
|
|
164 |
23 |
return printer.toString(); |
165 |
|
} catch (Exception e) { |
166 |
0 |
this.logger.error(e.getLocalizedMessage(), e); |
167 |
0 |
throw new RuntimeException("Exception while rendering HTML", e); |
168 |
|
} |
169 |
|
} |
170 |
|
|
|
|
| 81.8% |
Uncovered Elements: 2 (11) |
Complexity: 2 |
Complexity Density: 0.18 |
|
171 |
1 |
@Override... |
172 |
|
public String parseAndRender(String dirtyHTML, String syntaxId) |
173 |
|
{ |
174 |
1 |
try { |
175 |
|
|
176 |
1 |
String html = this.htmlCleaner.clean(dirtyHTML); |
177 |
|
|
178 |
|
|
179 |
1 |
XDOM xdom = this.xhtmlParser.parse(new StringReader(html)); |
180 |
|
|
181 |
|
|
182 |
|
|
183 |
|
|
184 |
1 |
Syntax syntax = this.syntaxFactory.createSyntaxFromIdString(syntaxId); |
185 |
1 |
xdom.getMetaData().addMetaData(MetaData.SYNTAX, syntax); |
186 |
|
|
187 |
|
|
188 |
1 |
executeMacroTransformation(xdom, this.syntaxFactory.createSyntaxFromIdString(syntaxId)); |
189 |
|
|
190 |
|
|
191 |
1 |
WikiPrinter printer = new DefaultWikiPrinter(); |
192 |
1 |
this.xhtmlRenderer.render(xdom, printer); |
193 |
|
|
194 |
1 |
return printer.toString(); |
195 |
|
} catch (Exception e) { |
196 |
0 |
this.logger.error(e.getLocalizedMessage(), e); |
197 |
0 |
throw new RuntimeException("Exception while refreshing HTML", e); |
198 |
|
} |
199 |
|
} |
200 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
201 |
24 |
private void executeMacroTransformation(XDOM xdom, Syntax syntax) throws TransformationException, ParseException... |
202 |
|
{ |
203 |
24 |
TransformationContext txContext = new TransformationContext(); |
204 |
23 |
txContext.setXDOM(xdom); |
205 |
24 |
txContext.setSyntax(syntax); |
206 |
|
|
207 |
|
|
208 |
|
|
209 |
|
|
210 |
24 |
txContext.setId(TRANSFORMATION_ID); |
211 |
|
|
212 |
24 |
((MutableRenderingContext) this.renderingContext).transformInContext(this.macroTransformation, txContext, xdom); |
213 |
|
} |
214 |
|
} |