use of org.xwiki.rendering.parser.ParseException in project xwiki-platform by xwiki.
the class LinkLabelGeneratorChainingListener method endLink.
/**
* {@inheritDoc}
*
* @see org.xwiki.rendering.listener.chaining.AbstractChainingListener#endLink(
* org.xwiki.rendering.listener.reference.ResourceReference , boolean, java.util.Map)
* @since 2.5RC1
*/
@Override
public void endLink(ResourceReference reference, boolean freestanding, Map<String, String> parameters) {
// behaviour
if (getEmptyBlockState().isCurrentContainerBlockEmpty()) {
// get the link label
// FIXME: this should be generated by the link label generator, for all cases, so that the
// link label can be changed with whichever generator, that can handle all cases
String linkLabel = reference.getReference();
ResourceType resourceType = reference.getType();
if (ResourceType.DOCUMENT.equals(resourceType) || ResourceType.SPACE.equals(resourceType)) {
linkLabel = linkLabelGenerator.generate(reference);
}
// create the span around the label signaling that this is a generated label
Map<String, String> formatParams = new HashMap<String, String>();
formatParams.put("class", "wikigeneratedlinkcontent");
// the same as this.format. TODO: decide which one is more semantic.
super.beginFormat(Format.NONE, formatParams);
// parse the linkLabel with a stream parser and an inline filter
WrappingListener inlineFilterListener = new InlineFilterListener();
inlineFilterListener.setWrappedListener(getListenerChain().getNextListener(getClass()));
try {
linkLabelParser.parse(new StringReader(linkLabel), inlineFilterListener);
} catch (ParseException e) {
// couldn't parse it, send it raw (interesting)
super.onRawText(linkLabel, linkLabelParser.getSyntax());
}
super.endFormat(Format.NONE, formatParams);
}
// end the link
super.endLink(reference, freestanding, parameters);
}
use of org.xwiki.rendering.parser.ParseException in project xwiki-platform by xwiki.
the class XWikiDocument method setSyntaxId.
/**
* Note that this method cannot be removed for now since it's used by Hibernate for saving a XWikiDocument.
*
* @param syntaxId the new syntax id to set (e.g. {@code xwiki/2.0}, {@code xwiki/2.1}, etc)
* @see #getSyntaxId()
* @deprecated since 2.3M1, use {link #setSyntax(Syntax)} instead
*/
@Deprecated
public void setSyntaxId(String syntaxId) {
Syntax syntax;
// syntax/1.0 syntax.
if (StringUtils.isBlank(syntaxId)) {
syntax = Syntax.XWIKI_1_0;
} else {
try {
syntax = Syntax.valueOf(syntaxId);
} catch (ParseException e) {
syntax = getDefaultDocumentSyntax();
LOGGER.warn("Failed to set syntax [" + syntaxId + "] for [" + getDefaultEntityReferenceSerializer().serialize(getDocumentReference()) + "], setting syntax [" + syntax.toIdString() + "] instead.", e);
}
}
setSyntax(syntax);
}
use of org.xwiki.rendering.parser.ParseException in project xwiki-platform by xwiki.
the class AbstractScriptMacro method parseScriptResult.
/**
* Convert script result as a {@link Block} list.
*
* @param content the script result to parse.
* @param parameters the macro parameters.
* @param context the context of the macro transformation.
* @return the {@link Block}s.
* @throws MacroExecutionException Failed to find source parser.
* @since 2.1M1
*/
protected List<Block> parseScriptResult(String content, P parameters, MacroTransformationContext context) throws MacroExecutionException {
List<Block> result;
if (parameters.isWiki()) {
result = parseSourceSyntax(content, context);
} else {
try {
result = this.plainTextParser.parse(new StringReader(content)).getChildren();
} catch (ParseException e) {
// String.
throw new MacroExecutionException("Failed to parse link label as plain text", e);
}
}
// 3) If in inline mode remove any top level paragraph
if (context.isInline()) {
this.parserUtils.removeTopLevelParagraph(result);
// TODO: use inline parser instead
if (!result.isEmpty() && result.get(0) instanceof MacroBlock && !((MacroBlock) result.get(0)).isInline()) {
MacroBlock macro = (MacroBlock) result.get(0);
result.set(0, new MacroBlock(macro.getId(), macro.getParameters(), macro.getContent(), true));
}
}
return result;
}
use of org.xwiki.rendering.parser.ParseException in project xwiki-platform by xwiki.
the class BlocksGeneratorPygmentsListener method format.
@Override
public void format(String tokenType, String value, Map<String, Object> style) {
List<Block> blockList;
if (StringUtils.isNotEmpty(value)) {
try {
blockList = this.plainTextParser.parse(new StringReader(value)).getChildren().get(0).getChildren();
} catch (ParseException e) {
// String.
throw new RuntimeException("Failed to parse [" + value + "] as plain text.", e);
}
String styleParameter = formatStyle(style);
FormatBlock formatBlock = null;
if (styleParameter.length() > 0) {
formatBlock = new FormatBlock(blockList, Format.NONE);
formatBlock.setParameter("style", styleParameter);
this.blocks.add(formatBlock);
} else {
this.blocks.addAll(blockList);
}
}
}
use of org.xwiki.rendering.parser.ParseException in project xwiki-platform by xwiki.
the class ParameterTranslationMessageElement method render.
@Override
public Block render(Locale locale, Collection<TranslationBundle> bundles, Object... parameters) {
Object parameter = parameters[index];
Block block;
if (parameter instanceof Block) {
block = (Block) parameter;
} else if (parameter != null) {
try {
XDOM xdom = this.plainParser.parse(new StringReader(parameter.toString()));
List<Block> blocks = xdom.getChildren();
PARSERUTILS.removeTopLevelParagraph(blocks);
if (blocks.isEmpty()) {
block = null;
} else if (blocks.size() == 1) {
block = blocks.get(0);
} else {
block = new CompositeBlock(blocks);
}
} catch (ParseException e) {
// make the #render fail instead ?
block = null;
}
} else {
block = null;
}
return block;
}
Aggregations