| 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.script; |
| 21 |
|
|
| 22 |
|
import javax.inject.Inject; |
| 23 |
|
import javax.inject.Named; |
| 24 |
|
import javax.inject.Provider; |
| 25 |
|
import javax.inject.Singleton; |
| 26 |
|
|
| 27 |
|
import org.slf4j.Logger; |
| 28 |
|
import org.xwiki.component.annotation.Component; |
| 29 |
|
import org.xwiki.component.manager.ComponentManager; |
| 30 |
|
import org.xwiki.gwt.wysiwyg.client.converter.HTMLConverter; |
| 31 |
|
import org.xwiki.model.reference.DocumentReference; |
| 32 |
|
import org.xwiki.rendering.parser.Parser; |
| 33 |
|
import org.xwiki.rendering.renderer.PrintRendererFactory; |
| 34 |
|
import org.xwiki.rendering.syntax.Syntax; |
| 35 |
|
import org.xwiki.security.authorization.ContextualAuthorizationManager; |
| 36 |
|
import org.xwiki.security.authorization.Right; |
| 37 |
|
import org.xwiki.wysiwyg.server.WysiwygEditorConfiguration; |
| 38 |
|
import org.xwiki.wysiwyg.server.WysiwygEditorScriptService; |
| 39 |
|
|
| 40 |
|
import com.xpn.xwiki.XWikiContext; |
| 41 |
|
import com.xpn.xwiki.XWikiException; |
| 42 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
| 43 |
|
|
| 44 |
|
|
| 45 |
|
@link |
| 46 |
|
|
| 47 |
|
@version |
| 48 |
|
|
| 49 |
|
@Component |
| 50 |
|
@Named("wysiwyg") |
| 51 |
|
@Singleton |
| |
|
| 46.6% |
Uncovered Elements: 31 (58) |
Complexity: 15 |
Complexity Density: 0.35 |
|
| 52 |
|
public class DefaultWysiwygEditorScriptService implements WysiwygEditorScriptService |
| 53 |
|
{ |
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
@see |
| 59 |
|
|
| 60 |
|
private static final String IS_IN_RENDERING_ENGINE = "isInRenderingEngine"; |
| 61 |
|
|
| 62 |
|
@Inject |
| 63 |
|
private Logger logger; |
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
@Inject |
| 69 |
|
@Named("context") |
| 70 |
|
private ComponentManager contextComponentManager; |
| 71 |
|
|
| 72 |
|
@Inject |
| 73 |
|
private ContextualAuthorizationManager authorization; |
| 74 |
|
|
| 75 |
|
|
| 76 |
|
|
| 77 |
|
|
| 78 |
|
@Inject |
| 79 |
|
private HTMLConverter htmlConverter; |
| 80 |
|
|
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
| 84 |
|
@Inject |
| 85 |
|
private WysiwygEditorConfiguration editorConfiguration; |
| 86 |
|
|
| 87 |
|
@Inject |
| 88 |
|
private Provider<XWikiContext> xcontextProvider; |
| 89 |
|
|
| |
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
| 90 |
552 |
@Override... |
| 91 |
|
public boolean isSyntaxSupported(String syntaxId) |
| 92 |
|
{ |
| 93 |
|
|
| 94 |
|
|
| 95 |
|
|
| 96 |
552 |
if (syntaxId.equals(Syntax.XHTML_1_0.toIdString())) { |
| 97 |
0 |
return false; |
| 98 |
|
} |
| 99 |
|
|
| 100 |
552 |
try { |
| 101 |
552 |
this.contextComponentManager.getInstance(Parser.class, syntaxId); |
| 102 |
551 |
this.contextComponentManager.getInstance(PrintRendererFactory.class, syntaxId); |
| 103 |
551 |
return true; |
| 104 |
|
} catch (Exception e) { |
| 105 |
1 |
return false; |
| 106 |
|
} |
| 107 |
|
} |
| 108 |
|
|
| |
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 3 |
Complexity Density: 0.3 |
|
| 109 |
0 |
@Override... |
| 110 |
|
public String parseAndRender(String html, String syntax) |
| 111 |
|
{ |
| 112 |
0 |
XWikiDocument originalSecurityDocument = setSecurityDocument(createSecurityDocument()); |
| 113 |
|
|
| 114 |
|
|
| 115 |
0 |
Object isInRenderingEngine = this.xcontextProvider.get().get(IS_IN_RENDERING_ENGINE); |
| 116 |
|
|
| 117 |
0 |
try { |
| 118 |
|
|
| 119 |
|
|
| 120 |
|
|
| 121 |
0 |
this.xcontextProvider.get().put(IS_IN_RENDERING_ENGINE, true); |
| 122 |
|
|
| 123 |
0 |
return this.htmlConverter.parseAndRender(html, syntax); |
| 124 |
|
} catch (Exception e) { |
| 125 |
|
|
| 126 |
0 |
return html; |
| 127 |
|
} finally { |
| 128 |
|
|
| 129 |
0 |
if (isInRenderingEngine != null) { |
| 130 |
0 |
this.xcontextProvider.get().put(IS_IN_RENDERING_ENGINE, isInRenderingEngine); |
| 131 |
|
} else { |
| 132 |
0 |
this.xcontextProvider.get().remove(IS_IN_RENDERING_ENGINE); |
| 133 |
|
} |
| 134 |
|
|
| 135 |
0 |
setSecurityDocument(originalSecurityDocument); |
| 136 |
|
} |
| 137 |
|
} |
| 138 |
|
|
| |
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 3 |
Complexity Density: 0.3 |
|
| 139 |
0 |
@Override... |
| 140 |
|
public String render(DocumentReference templateReference) |
| 141 |
|
{ |
| 142 |
0 |
if (!this.authorization.hasAccess(Right.VIEW, templateReference)) { |
| 143 |
0 |
return null; |
| 144 |
|
} |
| 145 |
|
|
| 146 |
0 |
XWikiContext xcontext = this.xcontextProvider.get(); |
| 147 |
0 |
try { |
| 148 |
0 |
XWikiDocument template = xcontext.getWiki().getDocument(templateReference, xcontext); |
| 149 |
0 |
String templateSyntax = template.getSyntax().toIdString(); |
| 150 |
0 |
String output = xcontext.getDoc().getRenderedContent(template.getContent(), templateSyntax, xcontext); |
| 151 |
|
|
| 152 |
0 |
return xcontext.getWiki().getPluginManager().endParsing(output, xcontext); |
| 153 |
|
} catch (XWikiException e) { |
| 154 |
0 |
this.logger.debug("Failed to render [{}].", templateReference, e); |
| 155 |
0 |
return null; |
| 156 |
|
} |
| 157 |
|
} |
| 158 |
|
|
| |
|
| 75% |
Uncovered Elements: 3 (12) |
Complexity: 3 |
Complexity Density: 0.3 |
|
| 159 |
22 |
@Override... |
| 160 |
|
public String toAnnotatedXHTML(String source, String syntaxId) |
| 161 |
|
{ |
| 162 |
22 |
XWikiDocument originalSecurityDocument = setSecurityDocument(createSecurityDocument()); |
| 163 |
|
|
| 164 |
|
|
| 165 |
22 |
Object isInRenderingEngine = this.xcontextProvider.get().get(IS_IN_RENDERING_ENGINE); |
| 166 |
|
|
| 167 |
22 |
try { |
| 168 |
|
|
| 169 |
|
|
| 170 |
|
|
| 171 |
22 |
this.xcontextProvider.get().put(IS_IN_RENDERING_ENGINE, true); |
| 172 |
|
|
| 173 |
22 |
return this.htmlConverter.toHTML(source, syntaxId); |
| 174 |
|
} catch (Exception e) { |
| 175 |
|
|
| 176 |
0 |
return source; |
| 177 |
|
} finally { |
| 178 |
|
|
| 179 |
22 |
if (isInRenderingEngine != null) { |
| 180 |
0 |
this.xcontextProvider.get().put(IS_IN_RENDERING_ENGINE, isInRenderingEngine); |
| 181 |
|
} else { |
| 182 |
22 |
this.xcontextProvider.get().remove(IS_IN_RENDERING_ENGINE); |
| 183 |
|
} |
| 184 |
|
|
| 185 |
22 |
setSecurityDocument(originalSecurityDocument); |
| 186 |
|
} |
| 187 |
|
} |
| 188 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 189 |
326 |
@Override... |
| 190 |
|
public WysiwygEditorConfiguration getConfig() |
| 191 |
|
{ |
| 192 |
326 |
return this.editorConfiguration; |
| 193 |
|
} |
| 194 |
|
|
| 195 |
|
|
| 196 |
|
|
| 197 |
|
|
| 198 |
|
|
| 199 |
|
|
| 200 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 201 |
22 |
private XWikiDocument createSecurityDocument()... |
| 202 |
|
{ |
| 203 |
22 |
XWikiContext xwikiContext = this.xcontextProvider.get(); |
| 204 |
|
|
| 205 |
22 |
XWikiDocument clonedDocument = xwikiContext.getDoc().clone(); |
| 206 |
22 |
clonedDocument.setContentAuthorReference(xwikiContext.getUserReference()); |
| 207 |
22 |
return clonedDocument; |
| 208 |
|
} |
| 209 |
|
|
| 210 |
|
|
| 211 |
|
|
| 212 |
|
|
| 213 |
|
@param |
| 214 |
|
@return |
| 215 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 216 |
44 |
private XWikiDocument setSecurityDocument(XWikiDocument document)... |
| 217 |
|
{ |
| 218 |
44 |
return (XWikiDocument) this.xcontextProvider.get().put(XWikiDocument.CKEY_SDOC, document); |
| 219 |
|
} |
| 220 |
|
} |