| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.component.wiki.internal; |
| 21 |
|
|
| 22 |
|
import java.lang.reflect.Method; |
| 23 |
|
import java.util.HashMap; |
| 24 |
|
import java.util.Map; |
| 25 |
|
|
| 26 |
|
import javax.inject.Inject; |
| 27 |
|
import javax.inject.Named; |
| 28 |
|
import javax.inject.Singleton; |
| 29 |
|
|
| 30 |
|
import org.xwiki.bridge.DocumentAccessBridge; |
| 31 |
|
import org.xwiki.component.annotation.Component; |
| 32 |
|
import org.xwiki.component.wiki.WikiComponentRuntimeException; |
| 33 |
|
import org.xwiki.context.Execution; |
| 34 |
|
import org.xwiki.model.reference.DocumentReference; |
| 35 |
|
import org.xwiki.properties.ConverterManager; |
| 36 |
|
import org.xwiki.properties.converter.ConversionException; |
| 37 |
|
import org.xwiki.rendering.block.XDOM; |
| 38 |
|
import org.xwiki.rendering.internal.transformation.MutableRenderingContext; |
| 39 |
|
import org.xwiki.rendering.renderer.BlockRenderer; |
| 40 |
|
import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter; |
| 41 |
|
import org.xwiki.rendering.renderer.printer.WikiPrinter; |
| 42 |
|
import org.xwiki.rendering.syntax.Syntax; |
| 43 |
|
import org.xwiki.rendering.transformation.RenderingContext; |
| 44 |
|
import org.xwiki.rendering.transformation.Transformation; |
| 45 |
|
import org.xwiki.rendering.transformation.TransformationContext; |
| 46 |
|
import org.xwiki.rendering.transformation.TransformationException; |
| 47 |
|
|
| 48 |
|
|
| 49 |
|
@link |
| 50 |
|
|
| 51 |
|
@version |
| 52 |
|
@since |
| 53 |
|
|
| 54 |
|
@Component |
| 55 |
|
@Singleton |
| |
|
| 0% |
Uncovered Elements: 46 (46) |
Complexity: 13 |
Complexity Density: 0.39 |
|
| 56 |
|
public class DefaultWikiComponentMethodExecutor implements WikiComponentMethodExecutor |
| 57 |
|
{ |
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
private static final String XWIKI_CONTEXT_DOC_KEY = "doc"; |
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
@Inject |
| 67 |
|
private Execution execution; |
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
@Inject |
| 73 |
|
private RenderingContext renderingContext; |
| 74 |
|
|
| 75 |
|
|
| 76 |
|
|
| 77 |
|
|
| 78 |
|
@Inject |
| 79 |
|
@Named("macro") |
| 80 |
|
private Transformation macroTransformation; |
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
| 84 |
|
|
| 85 |
|
@Inject |
| 86 |
|
@Named("plain/1.0") |
| 87 |
|
private BlockRenderer blockRenderer; |
| 88 |
|
|
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
|
@Inject |
| 93 |
|
private ConverterManager converterManager; |
| 94 |
|
|
| 95 |
|
|
| 96 |
|
|
| 97 |
|
|
| 98 |
|
@Inject |
| 99 |
|
private DocumentAccessBridge dab; |
| 100 |
|
|
| 101 |
|
|
| 102 |
|
|
| 103 |
|
|
| 104 |
|
@param |
| 105 |
|
@param |
| 106 |
|
|
| |
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 4 |
Complexity Density: 0.67 |
|
| 107 |
0 |
private void prepareMethodContext(Map<String, Object> methodContext, Object[] args)... |
| 108 |
|
{ |
| 109 |
0 |
methodContext.put(OUTPUT_KEY, new WikiMethodOutputHandler()); |
| 110 |
|
|
| 111 |
0 |
Map<Integer, Object> inputs = new HashMap<Integer, Object>(); |
| 112 |
0 |
if (args != null && args.length > 0) { |
| 113 |
|
|
| 114 |
0 |
for (int i = 0; i < args.length; i++) { |
| 115 |
0 |
inputs.put(i, args[i]); |
| 116 |
|
} |
| 117 |
|
} |
| 118 |
0 |
methodContext.put(INPUT_KEY, inputs); |
| 119 |
|
} |
| 120 |
|
|
| 121 |
|
|
| 122 |
|
|
| 123 |
|
|
| 124 |
|
|
| 125 |
|
@param |
| 126 |
|
@param |
| 127 |
|
@return |
| 128 |
|
@throws |
| 129 |
|
|
| |
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
| 130 |
0 |
private Object castRenderedContent(XDOM xdom, Method method) throws WikiComponentRuntimeException... |
| 131 |
|
{ |
| 132 |
|
|
| 133 |
|
|
| 134 |
0 |
WikiPrinter printer = new DefaultWikiPrinter(); |
| 135 |
0 |
blockRenderer.render(xdom, printer); |
| 136 |
0 |
String contentResult = printer.toString(); |
| 137 |
|
|
| 138 |
|
|
| 139 |
0 |
try { |
| 140 |
0 |
return converterManager.convert(method.getGenericReturnType(), contentResult); |
| 141 |
|
} catch (ConversionException e) { |
| 142 |
|
|
| 143 |
0 |
throw new WikiComponentRuntimeException( |
| 144 |
|
String.format("Failed to convert result [%s] to type [%s] for method [%s.%s]", |
| 145 |
|
contentResult, |
| 146 |
|
method.getGenericReturnType(), |
| 147 |
|
method.getDeclaringClass().getName(), |
| 148 |
|
method.getName()), e); |
| 149 |
|
} |
| 150 |
|
} |
| 151 |
|
|
| |
|
| 0% |
Uncovered Elements: 27 (27) |
Complexity: 7 |
Complexity Density: 0.33 |
|
| 152 |
0 |
@Override... |
| 153 |
|
public Object execute(Method method, Object[] args, DocumentReference componentDocumentReference, XDOM xdom, |
| 154 |
|
Syntax syntax, Map<String, Object> methodContext) |
| 155 |
|
throws WikiComponentRuntimeException |
| 156 |
|
{ |
| 157 |
|
|
| 158 |
0 |
Map<Object, Object> xwikiContext = (Map<Object, Object>) execution.getContext().getProperty("xwikicontext"); |
| 159 |
0 |
this.prepareMethodContext(methodContext, args); |
| 160 |
0 |
xwikiContext.put("method", methodContext); |
| 161 |
|
|
| 162 |
0 |
Object contextDoc = xwikiContext.get(XWIKI_CONTEXT_DOC_KEY); |
| 163 |
|
|
| 164 |
0 |
try { |
| 165 |
|
|
| 166 |
|
|
| 167 |
0 |
try { |
| 168 |
0 |
xwikiContext.put(XWIKI_CONTEXT_DOC_KEY, dab.getDocument(componentDocumentReference)); |
| 169 |
|
} catch (Exception e) { |
| 170 |
0 |
throw new WikiComponentRuntimeException(String.format( |
| 171 |
|
"Failed to load wiki component document [%s]", componentDocumentReference), e); |
| 172 |
|
} |
| 173 |
|
|
| 174 |
|
|
| 175 |
|
|
| 176 |
0 |
XDOM transformedXDOM = xdom.clone(); |
| 177 |
|
|
| 178 |
|
|
| 179 |
0 |
try { |
| 180 |
0 |
TransformationContext transformationContext = new TransformationContext(transformedXDOM, syntax); |
| 181 |
0 |
transformationContext.setId(method.getClass().getName() + "#" + method.getName()); |
| 182 |
0 |
((MutableRenderingContext) renderingContext).transformInContext(macroTransformation, |
| 183 |
|
transformationContext, transformedXDOM); |
| 184 |
|
} catch (TransformationException e) { |
| 185 |
0 |
throw new WikiComponentRuntimeException(String.format( |
| 186 |
|
"Error while executing wiki component macro transformation for method [%s]", method.getName()), e); |
| 187 |
|
} |
| 188 |
|
|
| 189 |
0 |
if (!method.getReturnType().getName().equals("void")) { |
| 190 |
0 |
if (methodContext.get(OUTPUT_KEY) != null |
| 191 |
|
&& ((WikiMethodOutputHandler) |
| 192 |
|
methodContext.get(OUTPUT_KEY)).getValue() != null) { |
| 193 |
0 |
return method.getReturnType().cast(((WikiMethodOutputHandler) |
| 194 |
|
methodContext.get(OUTPUT_KEY)).getValue()); |
| 195 |
|
} else { |
| 196 |
0 |
return this.castRenderedContent(transformedXDOM, method); |
| 197 |
|
} |
| 198 |
|
} else { |
| 199 |
0 |
return null; |
| 200 |
|
} |
| 201 |
|
} finally { |
| 202 |
0 |
if (contextDoc != null) { |
| 203 |
0 |
xwikiContext.put(XWIKI_CONTEXT_DOC_KEY, contextDoc); |
| 204 |
|
} |
| 205 |
|
} |
| 206 |
|
} |
| 207 |
|
} |