Search in sources :

Example 6 with DefaultWikiPrinter

use of org.xwiki.rendering.renderer.printer.DefaultWikiPrinter in project xwiki-platform by xwiki.

the class DocumentSolrMetadataExtractor method setFieldsInternal.

@Override
public boolean setFieldsInternal(LengthSolrInputDocument solrDocument, EntityReference entityReference) throws Exception {
    DocumentReference documentReference = new DocumentReference(entityReference);
    XWikiContext xcontext = this.xcontextProvider.get();
    XWikiDocument translatedDocument = getTranslatedDocument(documentReference);
    if (translatedDocument == null) {
        return false;
    }
    Locale locale = getLocale(documentReference);
    solrDocument.setField(FieldUtils.FULLNAME, localSerializer.serialize(documentReference));
    // Rendered title.
    String plainTitle = translatedDocument.getRenderedTitle(Syntax.PLAIN_1_0, xcontext);
    solrDocument.setField(FieldUtils.getFieldName(FieldUtils.TITLE, locale), plainTitle);
    // Raw Content
    solrDocument.setField(FieldUtils.getFieldName(FieldUtils.DOCUMENT_RAW_CONTENT, locale), translatedDocument.getContent());
    // Rendered content
    WikiPrinter plainContentPrinter = new DefaultWikiPrinter();
    this.renderer.render(translatedDocument.getXDOM(), plainContentPrinter);
    solrDocument.setField(FieldUtils.getFieldName(FieldUtils.DOCUMENT_RENDERED_CONTENT, locale), plainContentPrinter.toString());
    solrDocument.setField(FieldUtils.VERSION, translatedDocument.getVersion());
    solrDocument.setField(FieldUtils.COMMENT, translatedDocument.getComment());
    solrDocument.setField(FieldUtils.DOCUMENT_LOCALE, translatedDocument.getLocale().toString());
    // Add locale inheritance
    addLocales(translatedDocument, translatedDocument.getLocale(), solrDocument);
    // Get both serialized user reference string and pretty user name
    setAuthors(solrDocument, translatedDocument, entityReference);
    // Document dates.
    solrDocument.setField(FieldUtils.CREATIONDATE, translatedDocument.getCreationDate());
    solrDocument.setField(FieldUtils.DATE, translatedDocument.getContentUpdateDate());
    // Document translations have their own hidden fields
    solrDocument.setField(FieldUtils.HIDDEN, translatedDocument.isHidden());
    // Add any extra fields (about objects, etc.) that can improve the findability of the document.
    setExtras(documentReference, solrDocument, locale);
    return true;
}
Also used : Locale(java.util.Locale) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) XWikiContext(com.xpn.xwiki.XWikiContext) DocumentReference(org.xwiki.model.reference.DocumentReference) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter)

Example 7 with DefaultWikiPrinter

use of org.xwiki.rendering.renderer.printer.DefaultWikiPrinter in project xwiki-platform by xwiki.

the class DefaultWikiComponentMethodExecutor method castRenderedContent.

/**
 * Render a XDOM and return a value converted from the rendered content. The type matches the return value of the
 * passed method.
 *
 * @param xdom The XDOM to render
 * @param method The method called
 * @return A value matching the method return type
 * @throws WikiComponentRuntimeException When the conversion fails
 */
private Object castRenderedContent(XDOM xdom, Method method) throws WikiComponentRuntimeException {
    // Since no return value has been explicitly provided, we try to convert the result of the rendering
    // into the expected return type using a Converter.
    WikiPrinter printer = new DefaultWikiPrinter();
    blockRenderer.render(xdom, printer);
    String contentResult = printer.toString();
    // Do the conversion!
    try {
        return converterManager.convert(method.getGenericReturnType(), contentResult);
    } catch (ConversionException e) {
        // Surrender!
        throw new WikiComponentRuntimeException(String.format("Failed to convert result [%s] to type [%s] for method [%s.%s]", contentResult, method.getGenericReturnType(), method.getDeclaringClass().getName(), method.getName()), e);
    }
}
Also used : ConversionException(org.xwiki.properties.converter.ConversionException) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) WikiComponentRuntimeException(org.xwiki.component.wiki.WikiComponentRuntimeException)

Example 8 with DefaultWikiPrinter

use of org.xwiki.rendering.renderer.printer.DefaultWikiPrinter in project xwiki-platform by xwiki.

the class FeedPlugin method stripHtmlTags.

/**
 * @param originalString the string to strip the HTML tags from
 * @return the passed string, stripped from HTML markup
 * @throws ConversionException when the conversion fails
 */
private String stripHtmlTags(String originalString) throws ConversionException {
    if (this.syntaxConverter == null) {
        this.syntaxConverter = Utils.getComponent(Converter.class);
    }
    WikiPrinter printer = new DefaultWikiPrinter();
    this.syntaxConverter.convert(new StringReader(originalString), Syntax.HTML_4_01, Syntax.PLAIN_1_0, printer);
    return printer.toString();
}
Also used : DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) StringReader(java.io.StringReader) Converter(org.xwiki.rendering.converter.Converter) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter)

Example 9 with DefaultWikiPrinter

use of org.xwiki.rendering.renderer.printer.DefaultWikiPrinter in project xwiki-platform by xwiki.

the class LocalizationScriptService method render.

/**
 * @param keys the translations keys to try one by one
 * @param syntax the syntax in which to render the translation message
 * @param parameters the translation parameters
 * @param locale the {@link Locale} for which this translation is searched. The result might me associated to a
 *            different {@link Locale} (for example getting the {@code fr} translation when asking for the
 *            {@code fr_FR} one).
 * @return the rendered translation message, the key if no translation can be found and null if the rendering failed
 * @since 10.2
 */
public String render(Collection<String> keys, Syntax syntax, Collection<?> parameters, Locale locale) {
    if (CollectionUtils.isEmpty(keys)) {
        return null;
    }
    Translation translation = null;
    for (String key : keys) {
        if (key != null) {
            translation = this.localization.getTranslation(key, locale);
            if (translation != null) {
                break;
            }
        }
    }
    String result;
    if (translation != null) {
        Block block = parameters != null ? translation.render(locale, parameters.toArray()) : translation.render(locale);
        try {
            BlockRenderer renderer = this.componentManager.get().getInstance(BlockRenderer.class, syntax.toIdString());
            DefaultWikiPrinter wikiPrinter = new DefaultWikiPrinter();
            renderer.render(block, wikiPrinter);
            result = wikiPrinter.toString();
        } catch (ComponentLookupException e) {
            // TODO set current error
            result = null;
        }
    } else {
        result = null;
        for (String key : keys) {
            if (key != null) {
                result = key;
            }
        }
    }
    return result;
}
Also used : Translation(org.xwiki.localization.Translation) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) Block(org.xwiki.rendering.block.Block) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer)

Example 10 with DefaultWikiPrinter

use of org.xwiki.rendering.renderer.printer.DefaultWikiPrinter in project xwiki-platform by xwiki.

the class EmailTemplateRenderer method renderPlainText.

/**
 * Render a block to plain text syntax.
 * @param block block to render
 * @return the plain text rendered version of the block
 */
public String renderPlainText(Block block) {
    // TODO: this does not work at all (templates enforce HTML syntax I guess)
    WikiPrinter printer = new DefaultWikiPrinter();
    plainTextBlockRenderer.render(block, printer);
    return printer.toString();
}
Also used : DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter)

Aggregations

DefaultWikiPrinter (org.xwiki.rendering.renderer.printer.DefaultWikiPrinter)41 WikiPrinter (org.xwiki.rendering.renderer.printer.WikiPrinter)28 StringReader (java.io.StringReader)18 Test (org.junit.Test)12 Converter (org.xwiki.rendering.converter.Converter)12 XDOM (org.xwiki.rendering.block.XDOM)10 BlockRenderer (org.xwiki.rendering.renderer.BlockRenderer)8 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)7 Block (org.xwiki.rendering.block.Block)6 ParseException (org.xwiki.rendering.parser.ParseException)5 Parser (org.xwiki.rendering.parser.Parser)5 TransformationException (org.xwiki.rendering.transformation.TransformationException)4 XWikiException (com.xpn.xwiki.XWikiException)3 HashMap (java.util.HashMap)3 Expectations (org.jmock.Expectations)3 DocumentReference (org.xwiki.model.reference.DocumentReference)3 TransformationContext (org.xwiki.rendering.transformation.TransformationContext)3 XWikiContext (com.xpn.xwiki.XWikiContext)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 Reader (java.io.Reader)2