use of org.xwiki.rendering.renderer.PrintRendererFactory in project xwiki-platform by xwiki.
the class DefaultHTMLConverterTest method fromHTML.
/**
* Unit test for {@link DefaultHTMLConverter#fromHTML(String, String)}.
*/
@Test
public void fromHTML() throws Exception {
String html = "some HTML";
String syntaxId = "syntax/x.y";
// Verify the HTML is cleaned.
HTMLCleaner cleaner = mocker.getInstance(HTMLCleaner.class);
when(cleaner.clean(html)).thenReturn(html);
PrintRendererFactory printRendererFactory = this.mocker.registerMockComponent(PrintRendererFactory.class, syntaxId);
PrintRenderer printRenderer = mock(PrintRenderer.class);
when(printRendererFactory.createRenderer(any(WikiPrinter.class))).thenReturn(printRenderer);
Assert.assertEquals("", mocker.getComponentUnderTest().fromHTML(html, syntaxId));
// Verify the HTML is converted to the specified syntax.
StreamParser xhtmlStreamParser = mocker.getInstance(StreamParser.class, "xhtml/1.0");
verify(xhtmlStreamParser).parse(any(StringReader.class), same(printRenderer));
}
use of org.xwiki.rendering.renderer.PrintRendererFactory in project xwiki-platform by xwiki.
the class VelocityMacroIsolationTest method testVelocityMacroIsolation.
@Test
public void testVelocityMacroIsolation() throws Exception {
String expected = "beginDocument\n" + "beginParagraph\n" + "onSpecialSymbol [#]\n" + "onWord [testMacrosAreLocal]\n" + "onSpecialSymbol [(]\n" + "onSpecialSymbol [)]\n" + "endParagraph\n" + "endDocument";
VelocityMacroParameters params = new VelocityMacroParameters();
MacroTransformationContext context = new MacroTransformationContext();
context.setSyntax(Syntax.XWIKI_2_0);
context.setCurrentMacroBlock(new MacroBlock("velocity", Collections.<String, String>emptyMap(), false));
// Execute the velocity macro in the context of a first page
context.setId("page1");
this.velocityMacro.execute(params, "#macro(testMacrosAreLocal)mymacro#end", context);
// And then in the context of a second independent page
context.setId("page2");
PrintRendererFactory eventRendererFactory = getComponentManager().getInstance(PrintRendererFactory.class, "event/1.0");
assertBlocks(expected, this.velocityMacro.execute(params, "#testMacrosAreLocal()", context), eventRendererFactory);
}
use of org.xwiki.rendering.renderer.PrintRendererFactory in project xwiki-platform by xwiki.
the class XWikiDocumentOutputFilterStream method begin.
private void begin(FilterEventParameters parameters) throws FilterException {
DocumentReference documentReference = this.documentEntityResolver.resolve(this.currentEntityReference, getDefaultDocumentReference());
if (this.entity == null) {
this.entity = new XWikiDocument(documentReference, this.currentLocale);
} else {
this.entity.setDocumentReference(documentReference);
this.entity.setLocale(this.currentLocale);
}
// Find default author
DocumentReference defaultAuthorReference;
if (this.properties.isAuthorSet()) {
defaultAuthorReference = this.properties.getAuthor();
} else {
XWikiContext xcontext = xcontextProvider.get();
defaultAuthorReference = xcontext != null ? xcontext.getUserReference() : null;
}
this.entity.setCreationDate(getDate(WikiDocumentFilter.PARAMETER_CREATION_DATE, this.currentLocaleParameters, null));
this.entity.setCreatorReference(getUserReference(WikiDocumentFilter.PARAMETER_CREATION_AUTHOR, this.currentLocaleParameters, defaultAuthorReference));
this.entity.setDefaultLocale(this.currentDefaultLocale);
this.entity.setSyntax(getSyntax(WikiDocumentFilter.PARAMETER_SYNTAX, parameters, null));
this.entity.setParentReference(getEntityReference(WikiDocumentFilter.PARAMETER_PARENT, parameters, null));
this.entity.setCustomClass(getString(WikiDocumentFilter.PARAMETER_CUSTOMCLASS, parameters, null));
this.entity.setTitle(getString(WikiDocumentFilter.PARAMETER_TITLE, parameters, null));
this.entity.setDefaultTemplate(getString(WikiDocumentFilter.PARAMETER_DEFAULTTEMPLATE, parameters, null));
this.entity.setValidationScript(getString(WikiDocumentFilter.PARAMETER_VALIDATIONSCRIPT, parameters, null));
this.entity.setHidden(getBoolean(WikiDocumentFilter.PARAMETER_HIDDEN, parameters, false));
this.entity.setMinorEdit(getBoolean(WikiDocumentFilter.PARAMETER_REVISION_MINOR, parameters, false));
this.entity.setAuthorReference(getUserReference(WikiDocumentFilter.PARAMETER_REVISION_AUTHOR, parameters, defaultAuthorReference));
this.entity.setContentAuthorReference(getUserReference(WikiDocumentFilter.PARAMETER_CONTENT_AUTHOR, parameters, defaultAuthorReference));
String revisions = getString(XWikiWikiDocumentFilter.PARAMETER_JRCSREVISIONS, this.currentLocaleParameters, null);
if (revisions != null) {
try {
this.entity.setDocumentArchive(revisions);
} catch (XWikiException e) {
throw new FilterException("Failed to set document archive", e);
}
}
if (this.currentVersion != null && this.properties.isVersionPreserved()) {
if (VALID_VERSION.matcher(this.currentVersion).matches()) {
this.entity.setVersion(this.currentVersion);
} else if (NumberUtils.isDigits(this.currentVersion)) {
this.entity.setVersion(this.currentVersion + ".1");
} else {
// TODO: log something, probably a warning
}
}
this.entity.setDate(getDate(WikiDocumentFilter.PARAMETER_REVISION_DATE, parameters, new Date()));
this.entity.setComment(getString(WikiDocumentFilter.PARAMETER_REVISION_COMMENT, parameters, ""));
this.entity.setContentUpdateDate(getDate(WikiDocumentFilter.PARAMETER_CONTENT_DATE, parameters, new Date()));
if (this.contentListener != null) {
// Remember the current rendering context target syntax
this.previousTargetSyntax = this.renderingContext.getTargetSyntax();
}
if (parameters.containsKey(WikiDocumentFilter.PARAMETER_CONTENT)) {
this.entity.setContent(getString(WikiDocumentFilter.PARAMETER_CONTENT, parameters, null));
if (this.contentListener != null) {
// Cancel any existing content listener
this.currentWikiPrinter = null;
this.contentListener.setWrappedListener(null);
}
} else if (this.contentListener != null) {
if (this.properties != null && this.properties.getDefaultSyntax() != null) {
this.entity.setSyntax(this.properties.getDefaultSyntax());
} else {
// Make sure to set the default syntax if none were provided
this.entity.setSyntax(this.entity.getSyntax());
}
ComponentManager componentManager = this.componentManagerProvider.get();
String syntaxString = this.entity.getSyntax().toIdString();
if (componentManager.hasComponent(PrintRendererFactory.class, syntaxString)) {
PrintRendererFactory rendererFactory;
try {
rendererFactory = componentManager.getInstance(PrintRendererFactory.class, syntaxString);
} catch (ComponentLookupException e) {
throw new FilterException(String.format("Failed to find PrintRendererFactory for syntax [%s]", this.entity.getSyntax()), e);
}
this.currentWikiPrinter = new DefaultWikiPrinter();
((MutableRenderingContext) this.renderingContext).setTargetSyntax(rendererFactory.getSyntax());
this.contentListener.setWrappedListener(rendererFactory.createRenderer(this.currentWikiPrinter));
}
}
// Initialize the class
getBaseClassOutputFilterStream().setEntity(this.entity.getXClass());
}
use of org.xwiki.rendering.renderer.PrintRendererFactory in project xwiki-platform by xwiki.
the class DefaultHTMLConverter method fromHTML.
@Override
public String fromHTML(String dirtyHTML, String syntaxId) {
try {
// Clean
String html = this.htmlCleaner.clean(dirtyHTML);
// Parse & Render
// Note that transformations are not executed when converting XHTML to source syntax.
WikiPrinter printer = new DefaultWikiPrinter();
PrintRendererFactory printRendererFactory = this.contextComponentManager.getInstance(PrintRendererFactory.class, syntaxId);
this.xhtmlStreamParser.parse(new StringReader(html), printRendererFactory.createRenderer(printer));
return printer.toString();
} catch (Exception e) {
this.logger.error(e.getLocalizedMessage(), e);
throw new RuntimeException("Exception while parsing HTML", e);
}
}
Aggregations