1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rendering.internal.macro.context; |
21 |
|
|
22 |
|
import java.util.Arrays; |
23 |
|
import java.util.HashMap; |
24 |
|
import java.util.List; |
25 |
|
import java.util.Map; |
26 |
|
|
27 |
|
import javax.inject.Inject; |
28 |
|
import javax.inject.Named; |
29 |
|
import javax.inject.Singleton; |
30 |
|
|
31 |
|
import org.xwiki.bridge.DocumentAccessBridge; |
32 |
|
import org.xwiki.bridge.DocumentModelBridge; |
33 |
|
import org.xwiki.component.annotation.Component; |
34 |
|
import org.xwiki.model.reference.DocumentReference; |
35 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
36 |
|
import org.xwiki.rendering.block.Block; |
37 |
|
import org.xwiki.rendering.block.MetaDataBlock; |
38 |
|
import org.xwiki.rendering.block.XDOM; |
39 |
|
import org.xwiki.rendering.listener.MetaData; |
40 |
|
import org.xwiki.rendering.macro.AbstractMacro; |
41 |
|
import org.xwiki.rendering.macro.MacroContentParser; |
42 |
|
import org.xwiki.rendering.macro.MacroExecutionException; |
43 |
|
import org.xwiki.rendering.macro.context.ContextMacroParameters; |
44 |
|
import org.xwiki.rendering.macro.context.TransformationContextMode; |
45 |
|
import org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor; |
46 |
|
import org.xwiki.rendering.transformation.MacroTransformationContext; |
47 |
|
import org.xwiki.rendering.transformation.TransformationContext; |
48 |
|
import org.xwiki.rendering.transformation.TransformationManager; |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
@version |
54 |
|
@since |
55 |
|
|
56 |
|
@Component |
57 |
|
@Named("context") |
58 |
|
@Singleton |
|
|
| 89.1% |
Uncovered Elements: 5 (46) |
Complexity: 11 |
Complexity Density: 0.33 |
|
59 |
|
public class ContextMacro extends AbstractMacro<ContextMacroParameters> |
60 |
|
{ |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
private static final String DESCRIPTION = "Executes content in the context of the passed document"; |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
private static final String CONTENT_DESCRIPTION = "The content to execute"; |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
@Inject |
75 |
|
private DocumentAccessBridge documentAccessBridge; |
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
@Inject |
81 |
|
private MacroContentParser contentParser; |
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
@Inject |
87 |
|
@Named("macro") |
88 |
|
private DocumentReferenceResolver<String> macroDocumentReferenceResolver; |
89 |
|
|
90 |
|
@Inject |
91 |
|
private TransformationManager transformationManager; |
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
96 |
6 |
public ContextMacro()... |
97 |
|
{ |
98 |
6 |
super("Context", DESCRIPTION, new DefaultContentDescriptor(CONTENT_DESCRIPTION), ContextMacroParameters.class); |
99 |
|
|
100 |
|
|
101 |
|
|
102 |
6 |
setPriority(10); |
103 |
6 |
setDefaultCategory(DEFAULT_CATEGORY_DEVELOPMENT); |
104 |
|
} |
105 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
106 |
0 |
@Override... |
107 |
|
public boolean supportsInlineMode() |
108 |
|
{ |
109 |
0 |
return true; |
110 |
|
} |
111 |
|
|
|
|
| 92.3% |
Uncovered Elements: 3 (39) |
Complexity: 9 |
Complexity Density: 0.31 |
|
112 |
6 |
@Override... |
113 |
|
public List<Block> execute(ContextMacroParameters parameters, String content, MacroTransformationContext context) |
114 |
|
throws MacroExecutionException |
115 |
|
{ |
116 |
6 |
if (parameters.getDocument() == null) { |
117 |
1 |
throw new MacroExecutionException("You must specify a 'document' parameter pointing to the document to " |
118 |
|
+ "set in the context as the current document."); |
119 |
|
} |
120 |
|
|
121 |
5 |
DocumentReference referencedDocReference = |
122 |
|
this.macroDocumentReferenceResolver.resolve(parameters.getDocument(), context.getCurrentMacroBlock()); |
123 |
|
|
124 |
5 |
boolean currentContextHasProgrammingRights = this.documentAccessBridge.hasProgrammingRights(); |
125 |
|
|
126 |
5 |
List<Block> result; |
127 |
5 |
try { |
128 |
5 |
Map<String, Object> backupObjects = new HashMap<>(); |
129 |
5 |
try { |
130 |
5 |
this.documentAccessBridge.pushDocumentInContext(backupObjects, referencedDocReference); |
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
135 |
5 |
if (this.documentAccessBridge.hasProgrammingRights() && !currentContextHasProgrammingRights) { |
136 |
1 |
throw new MacroExecutionException("Current document must have programming rights since the " |
137 |
|
+ "context document provided [" + parameters.getDocument() + "] has programming rights."); |
138 |
|
} |
139 |
|
|
140 |
4 |
MetaData metadata = new MetaData(); |
141 |
4 |
metadata.addMetaData(MetaData.SOURCE, parameters.getDocument()); |
142 |
4 |
metadata.addMetaData(MetaData.BASE, parameters.getDocument()); |
143 |
|
|
144 |
4 |
XDOM xdom = this.contentParser.parse(content, context, false, metadata, false); |
145 |
|
|
146 |
|
|
147 |
4 |
if (parameters.getTransformationContext() == TransformationContextMode.DOCUMENT |
148 |
|
|| parameters.getTransformationContext() == TransformationContextMode.TRANSFORMATIONS) |
149 |
|
{ |
150 |
|
|
151 |
|
|
152 |
|
|
153 |
1 |
DocumentModelBridge referencedDoc = this.documentAccessBridge.getDocument(referencedDocReference); |
154 |
1 |
XDOM referencedXDOM = referencedDoc.getXDOM(); |
155 |
|
|
156 |
1 |
if (parameters.getTransformationContext() == TransformationContextMode.TRANSFORMATIONS) { |
157 |
|
|
158 |
|
|
159 |
|
|
160 |
|
|
161 |
1 |
TransformationContext referencedTxContext = |
162 |
|
new TransformationContext(referencedXDOM, referencedDoc.getSyntax()); |
163 |
1 |
this.transformationManager.performTransformations(referencedXDOM, referencedTxContext); |
164 |
|
} |
165 |
|
|
166 |
|
|
167 |
|
|
168 |
1 |
TransformationContext txContext = |
169 |
|
new TransformationContext(referencedXDOM, referencedDoc.getSyntax()); |
170 |
1 |
this.transformationManager.performTransformations(xdom, txContext); |
171 |
|
} |
172 |
|
|
173 |
|
|
174 |
|
|
175 |
4 |
result = Arrays.asList((Block) new MetaDataBlock(xdom.getChildren(), xdom.getMetaData())); |
176 |
|
|
177 |
|
} finally { |
178 |
5 |
this.documentAccessBridge.popDocumentFromContext(backupObjects); |
179 |
|
} |
180 |
|
} catch (Exception e) { |
181 |
1 |
if (e instanceof MacroExecutionException) { |
182 |
1 |
throw (MacroExecutionException) e; |
183 |
|
} else { |
184 |
0 |
throw new MacroExecutionException( |
185 |
|
String.format("Failed to render page in the context of [%s]", referencedDocReference), e); |
186 |
|
} |
187 |
|
} |
188 |
|
|
189 |
4 |
return result; |
190 |
|
} |
191 |
|
} |