Search in sources :

Example 1 with PrintRendererFactory

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));
}
Also used : PrintRenderer(org.xwiki.rendering.renderer.PrintRenderer) StreamParser(org.xwiki.rendering.parser.StreamParser) StringReader(java.io.StringReader) PrintRendererFactory(org.xwiki.rendering.renderer.PrintRendererFactory) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) HTMLCleaner(org.xwiki.wysiwyg.cleaner.HTMLCleaner) Test(org.junit.Test)

Example 2 with PrintRendererFactory

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);
}
Also used : MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) PrintRendererFactory(org.xwiki.rendering.renderer.PrintRendererFactory) MacroBlock(org.xwiki.rendering.block.MacroBlock) Test(org.junit.Test)

Example 3 with PrintRendererFactory

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());
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) ComponentManager(org.xwiki.component.manager.ComponentManager) XWikiContext(com.xpn.xwiki.XWikiContext) FilterException(org.xwiki.filter.FilterException) PrintRendererFactory(org.xwiki.rendering.renderer.PrintRendererFactory) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException) Date(java.util.Date)

Example 4 with PrintRendererFactory

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);
    }
}
Also used : DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) StringReader(java.io.StringReader) PrintRendererFactory(org.xwiki.rendering.renderer.PrintRendererFactory) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) ParseException(org.xwiki.rendering.parser.ParseException) TransformationException(org.xwiki.rendering.transformation.TransformationException)

Aggregations

PrintRendererFactory (org.xwiki.rendering.renderer.PrintRendererFactory)4 StringReader (java.io.StringReader)2 Test (org.junit.Test)2 DefaultWikiPrinter (org.xwiki.rendering.renderer.printer.DefaultWikiPrinter)2 WikiPrinter (org.xwiki.rendering.renderer.printer.WikiPrinter)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiException (com.xpn.xwiki.XWikiException)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 Date (java.util.Date)1 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)1 ComponentManager (org.xwiki.component.manager.ComponentManager)1 FilterException (org.xwiki.filter.FilterException)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1 MacroBlock (org.xwiki.rendering.block.MacroBlock)1 ParseException (org.xwiki.rendering.parser.ParseException)1 StreamParser (org.xwiki.rendering.parser.StreamParser)1 PrintRenderer (org.xwiki.rendering.renderer.PrintRenderer)1 MacroTransformationContext (org.xwiki.rendering.transformation.MacroTransformationContext)1 TransformationException (org.xwiki.rendering.transformation.TransformationException)1 HTMLCleaner (org.xwiki.wysiwyg.cleaner.HTMLCleaner)1