use of org.xwiki.velocity.VelocityManager in project xwiki-platform by xwiki.
the class ExplicitlyAllowedValuesDBListQueryBuilder method evaluateVelocityCode.
private String evaluateVelocityCode(String code, String namespace) throws Exception {
VelocityManager velocityManager = this.velocityManagerProvider.get();
VelocityContext velocityContext = velocityManager.getVelocityContext();
VelocityEngine velocityEngine = velocityManager.getVelocityEngine();
StringWriter writer = new StringWriter();
velocityEngine.evaluate(velocityContext, writer, namespace, code);
return writer.toString();
}
use of org.xwiki.velocity.VelocityManager in project xwiki-platform by xwiki.
the class SxDocumentSource method getContent.
@Override
public String getContent() {
StringBuilder resultBuilder = new StringBuilder();
List<BaseObject> objects = this.document.getObjects(this.extension.getClassName());
if (objects != null) {
for (BaseObject sxObj : objects) {
if (sxObj == null) {
continue;
}
String sxContent = sxObj.getLargeStringValue(CONTENT_PROPERTY_NAME);
int parse = sxObj.getIntValue(PARSE_CONTENT_PROPERTY_NAME);
if ("LESS".equals(sxObj.getStringValue(CONTENT_TYPE_PROPERTY_NAME))) {
LESSCompiler lessCompiler = Utils.getComponent(LESSCompiler.class);
LESSResourceReferenceFactory lessResourceReferenceFactory = Utils.getComponent(LESSResourceReferenceFactory.class);
ObjectPropertyReference objectPropertyReference = new ObjectPropertyReference(CONTENT_PROPERTY_NAME, sxObj.getReference());
LESSResourceReference lessResourceReference = lessResourceReferenceFactory.createReferenceForXObjectProperty(objectPropertyReference);
try {
sxContent = lessCompiler.compile(lessResourceReference, true, (parse == 1), false);
} catch (LESSCompilerException e) {
// Set the error message in a CSS comment to help the developer understand why its SSX is not
// working (it will work only if the CSS minifier is not used).
sxContent = String.format("/* LESS errors while parsing skin extension [%s]. */\n/* %s */", sxObj.getStringValue(NAME_PROPERTY_NAME), ExceptionUtils.getRootCauseMessage(e));
}
} else if (parse == 1) {
try {
StringWriter writer = new StringWriter();
VelocityManager velocityManager = Utils.getComponent(VelocityManager.class);
VelocityContext vcontext = velocityManager.getVelocityContext();
velocityManager.getVelocityEngine().evaluate(vcontext, writer, this.document.getPrefixedFullName(), sxContent);
sxContent = writer.toString();
} catch (XWikiVelocityException ex) {
LOGGER.warn("Velocity errors while parsing skin extension [{}] with content [{}]: ", this.document.getPrefixedFullName(), sxContent, ExceptionUtils.getRootCauseMessage(ex));
}
}
// Also add a newline, in case the different object contents don't end with a blank
// line, and could cause syntax errors when concatenated.
resultBuilder.append(sxContent + "\n");
}
}
return resultBuilder.toString();
}
use of org.xwiki.velocity.VelocityManager in project xwiki-platform by xwiki.
the class XWiki method evaluateVelocity.
/**
* @param content the Velocity content to evaluate
* @param name the namespace under which to evaluate it (used for isolation)
* @return the evaluated content
* @since 7.2M1
*/
public String evaluateVelocity(String content, String name) {
try {
VelocityManager velocityManager = Utils.getComponent(VelocityManager.class);
VelocityContext velocityContext = velocityManager.getVelocityContext();
return evaluateVelocity(content, name, velocityContext);
} catch (Exception e) {
LOGGER.error("Error while parsing velocity template namespace [{}] with content:\n[{}]", name, content, e);
Object[] args = { name };
XWikiException xe = new XWikiException(XWikiException.MODULE_XWIKI_RENDERING, XWikiException.ERROR_XWIKI_RENDERING_VELOCITY_EXCEPTION, "Error while parsing velocity page {0}", e, args);
return Util.getHTMLExceptionMessage(xe, null);
}
}
use of org.xwiki.velocity.VelocityManager in project xwiki-platform by xwiki.
the class MockitoDownloadActionTest method renderWhenZipExplorerPluginURL.
@Test
public void renderWhenZipExplorerPluginURL() throws Exception {
DownloadAction action = new DownloadAction();
XWikiContext xcontext = this.oldcore.getXWikiContext();
// Set the Request URL
XWikiServletRequestStub request = new XWikiServletRequestStub();
request.setRequestURI("http://localhost:8080/xwiki/bin/download/space/page/file.ext/some/path");
xcontext.setRequest(request);
// Set the current doc and current wiki
XWikiDocument document = mock(XWikiDocument.class);
when(document.getAttachment("path")).thenReturn(null);
xcontext.setDoc(document);
xcontext.setWikiId("wiki");
xcontext.setAction("download");
// Set the Response
XWikiResponse response = mock(XWikiResponse.class);
StubServletOutputStream ssos = new StubServletOutputStream();
when(response.getOutputStream()).thenReturn(ssos);
xcontext.setResponse(response);
// Set the Resource Reference Manager used to parse the URL and extract the attachment name
ResourceReferenceManager rrm = this.oldcore.getMocker().registerMockComponent(ResourceReferenceManager.class);
when(rrm.getResourceReference()).thenReturn(new EntityResourceReference(new AttachmentReference("path", new DocumentReference("wiki", Arrays.asList("space", "page", "file.ext"), "some")), EntityResourceAction.VIEW));
// Setup the returned attachment
XWikiAttachment attachment = mock(XWikiAttachment.class);
when(attachment.getContentSize(xcontext)).thenReturn(100);
Date now = new Date();
when(attachment.getDate()).thenReturn(now);
when(attachment.getFilename()).thenReturn("file.ext");
when(attachment.getContentInputStream(xcontext)).thenReturn(new ByteArrayInputStream("test".getBytes()));
when(attachment.getMimeType(xcontext)).thenReturn("mimetype");
when(attachment.clone()).thenReturn(attachment);
// Configure an existing doc in the store
XWiki xwiki = this.oldcore.getSpyXWiki();
XWikiDocument backwardCompatDocument = new XWikiDocument(new DocumentReference("wiki", "space", "page"));
backwardCompatDocument.addAttachment(attachment);
xwiki.saveDocument(backwardCompatDocument, xcontext);
// Make sure the user has permission to access the doc
doReturn(true).when(xwiki).checkAccess(eq("download"), any(XWikiDocument.class), any(XWikiContext.class));
// Setup ExecutionContextManager & VelocityManager using in the context backup
ExecutionContextManager ecm = this.oldcore.getMocker().registerMockComponent(ExecutionContextManager.class);
ExecutionContext ec = this.oldcore.getExecutionContext();
when(ecm.clone(ec)).thenReturn(ec);
VelocityManager vm = this.oldcore.getMocker().registerMockComponent(VelocityManager.class);
// Set the Plugin Manager
XWikiPluginManager pluginManager = mock(XWikiPluginManager.class);
when(pluginManager.downloadAttachment(attachment, xcontext)).thenReturn(attachment);
doReturn(pluginManager).when(this.oldcore.getSpyXWiki()).getPluginManager();
assertNull(action.render(xcontext));
// This is the test, we verify what is set in the response
verify(response).setContentType("mimetype");
verify(response).setHeader("Accept-Ranges", "bytes");
verify(response).addHeader("Content-disposition", "attachment; filename*=utf-8''file.ext");
verify(response).setDateHeader("Last-Modified", now.getTime());
verify(response).setContentLength(100);
assertEquals("test", ssos.baos.toString());
}
use of org.xwiki.velocity.VelocityManager in project xwiki-platform by xwiki.
the class DisplayMacroTest method runDisplayMacroWithPreVelocity.
private List<Block> runDisplayMacroWithPreVelocity(String velocity, String displayedContent) throws Exception {
VelocityManager velocityManager = getComponentManager().getInstance(VelocityManager.class);
StringWriter writer = new StringWriter();
velocityManager.getVelocityEngine().evaluate(velocityManager.getVelocityContext(), writer, "wiki:Space.DisplayingPage", velocity);
return runDisplayMacro(displayedContent);
}
Aggregations