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;
}
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);
}
}
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();
}
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;
}
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();
}
Aggregations