Search in sources :

Example 1 with ParseException

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);
}
Also used : InlineFilterListener(org.xwiki.rendering.listener.InlineFilterListener) HashMap(java.util.HashMap) WrappingListener(org.xwiki.rendering.listener.WrappingListener) StringReader(java.io.StringReader) ResourceType(org.xwiki.rendering.listener.reference.ResourceType) ParseException(org.xwiki.rendering.parser.ParseException)

Example 2 with ParseException

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);
}
Also used : ParseException(org.xwiki.rendering.parser.ParseException) Syntax(org.xwiki.rendering.syntax.Syntax)

Example 3 with ParseException

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;
}
Also used : StringReader(java.io.StringReader) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) MacroBlock(org.xwiki.rendering.block.MacroBlock) Block(org.xwiki.rendering.block.Block) ParseException(org.xwiki.rendering.parser.ParseException) MacroBlock(org.xwiki.rendering.block.MacroBlock)

Example 4 with ParseException

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);
        }
    }
}
Also used : StringReader(java.io.StringReader) Block(org.xwiki.rendering.block.Block) FormatBlock(org.xwiki.rendering.block.FormatBlock) ParseException(org.xwiki.rendering.parser.ParseException) FormatBlock(org.xwiki.rendering.block.FormatBlock)

Example 5 with ParseException

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;
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) StringReader(java.io.StringReader) Block(org.xwiki.rendering.block.Block) CompositeBlock(org.xwiki.rendering.block.CompositeBlock) CompositeBlock(org.xwiki.rendering.block.CompositeBlock) List(java.util.List) ParseException(org.xwiki.rendering.parser.ParseException)

Aggregations

ParseException (org.xwiki.rendering.parser.ParseException)13 StringReader (java.io.StringReader)9 Block (org.xwiki.rendering.block.Block)6 XDOM (org.xwiki.rendering.block.XDOM)4 CompositeBlock (org.xwiki.rendering.block.CompositeBlock)3 MacroExecutionException (org.xwiki.rendering.macro.MacroExecutionException)2 Syntax (org.xwiki.rendering.syntax.Syntax)2 BaseObject (com.xpn.xwiki.objects.BaseObject)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Locale (java.util.Locale)1 ScriptException (javax.script.ScriptException)1 Test (org.junit.Test)1 Document (org.w3c.dom.Document)1 Translation (org.xwiki.localization.Translation)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1 OfficeImporterException (org.xwiki.officeimporter.OfficeImporterException)1 XDOMOfficeDocument (org.xwiki.officeimporter.document.XDOMOfficeDocument)1