Search in sources :

Example 6 with ParseException

use of org.xwiki.rendering.parser.ParseException in project xwiki-platform by xwiki.

the class DefaultWikiMacroFactory method buildMacro.

/**
 * Creates a {@link WikiMacro} from an {@link XWikiDocument} which contains a macro definition.
 *
 * @param doc the {@link XWikiDocument} to look for a macro definition
 * @return the {@link WikiMacro} found inside the document
 * @throws WikiMacroException when an invalid macro definition or no macro definition was found
 */
private WikiMacro buildMacro(XWikiDocument doc) throws WikiMacroException {
    DocumentReference documentReference = doc.getDocumentReference();
    // Check whether this document contains a macro definition.
    BaseObject macroDefinition = doc.getObject(WIKI_MACRO_CLASS);
    if (null == macroDefinition) {
        throw new WikiMacroException(String.format("No macro definition found in document : [%s]", documentReference));
    }
    // Extract macro definition.
    String macroId = macroDefinition.getStringValue(MACRO_ID_PROPERTY);
    String macroName = macroDefinition.getStringValue(MACRO_NAME_PROPERTY);
    // The macro description as plain text
    String macroDescription = macroDefinition.getStringValue(MACRO_DESCRIPTION_PROPERTY);
    String macroDefaultCategory = macroDefinition.getStringValue(MACRO_DEFAULT_CATEGORY_PROPERTY);
    WikiMacroVisibility macroVisibility = WikiMacroVisibility.fromString(macroDefinition.getStringValue(MACRO_VISIBILITY_PROPERTY));
    boolean macroSupportsInlineMode = (macroDefinition.getIntValue(MACRO_INLINE_PROPERTY) == 0) ? false : true;
    String macroContentType = macroDefinition.getStringValue(MACRO_CONTENT_TYPE_PROPERTY);
    // The macro content description as plain text
    String macroContentDescription = macroDefinition.getStringValue(MACRO_CONTENT_DESCRIPTION_PROPERTY);
    String macroCode = macroDefinition.getStringValue(MACRO_CODE_PROPERTY);
    // Verify macro id.
    if (StringUtils.isEmpty(macroId)) {
        throw new WikiMacroException(String.format("Incomplete macro definition in [%s], macro id is empty", documentReference));
    }
    // Verify macro name.
    if (StringUtils.isEmpty(macroName)) {
        macroName = macroId;
        this.logger.debug(String.format("Incomplete macro definition in [%s], macro name is empty", documentReference));
    }
    // Verify macro description.
    if (StringUtils.isEmpty(macroDescription)) {
        this.logger.debug(String.format("Incomplete macro definition in [%s], macro description is empty", documentReference));
    }
    // Verify default macro category.
    if (StringUtils.isEmpty(macroDefaultCategory)) {
        macroDefaultCategory = null;
        this.logger.debug(String.format("Incomplete macro definition in [%s], default macro category is empty", documentReference));
    }
    // Verify macro content type.
    if (StringUtils.isEmpty(macroContentType)) {
        macroContentType = MACRO_CONTENT_OPTIONAL;
    }
    // Verify macro content description.
    if (!macroContentType.equals(MACRO_CONTENT_EMPTY) && StringUtils.isEmpty(macroContentDescription)) {
        String errorMsg = "Incomplete macro definition in [%s], macro content description is empty";
        this.logger.debug(String.format(errorMsg, documentReference));
        macroContentDescription = "Macro content";
    }
    // Verify macro code.
    if (StringUtils.isEmpty(macroCode)) {
        throw new WikiMacroException(String.format("Incomplete macro definition in [%s], macro code is empty", documentReference));
    }
    // Extract macro parameters.
    List<WikiMacroParameterDescriptor> parameterDescriptors = new ArrayList<WikiMacroParameterDescriptor>();
    Vector<BaseObject> macroParameters = doc.getObjects(WIKI_MACRO_PARAMETER_CLASS);
    if (null != macroParameters) {
        for (BaseObject macroParameter : macroParameters) {
            // Vectors can contain null values
            if (null == macroParameter) {
                continue;
            }
            // Extract parameter definition.
            String parameterName = macroParameter.getStringValue(PARAMETER_NAME_PROPERTY);
            String parameterDescription = macroParameter.getStringValue(PARAMETER_DESCRIPTION_PROPERTY);
            boolean parameterMandatory = (macroParameter.getIntValue(PARAMETER_MANDATORY_PROPERTY) == 0) ? false : true;
            String parameterDefaultValue = macroParameter.getStringValue(PARAMETER_DEFAULT_VALUE_PROPERTY);
            // Verify parameter name.
            if (StringUtils.isEmpty(parameterName)) {
                throw new WikiMacroException(String.format("Incomplete macro definition in [%s], macro parameter name is empty", documentReference));
            }
            // Verify parameter description.
            if (StringUtils.isEmpty(parameterDescription)) {
                String errorMessage = "Incomplete macro definition in [%s], macro parameter description is empty";
                this.logger.debug(String.format(errorMessage, documentReference));
            }
            // If field empty, assume no default value was provided.
            if (StringUtils.isEmpty(parameterDefaultValue)) {
                parameterDefaultValue = null;
            }
            // Create the parameter descriptor.
            parameterDescriptors.add(new WikiMacroParameterDescriptor(parameterName, parameterDescription, parameterMandatory, parameterDefaultValue));
        }
    }
    // Create macro content descriptor.
    ContentDescriptor contentDescriptor = null;
    if (!macroContentType.equals(MACRO_CONTENT_EMPTY)) {
        contentDescriptor = new DefaultContentDescriptor(macroContentDescription, macroContentType.equals(MACRO_CONTENT_MANDATORY));
    }
    // Create macro descriptor.
    // Note that we register wiki macros for all syntaxes FTM and there's currently no way to restrict a wiki
    // macro for a given syntax only.
    MacroId id = new MacroId(macroId);
    MacroDescriptor macroDescriptor = new WikiMacroDescriptor(id, macroName, macroDescription, macroDefaultCategory, macroVisibility, contentDescriptor, parameterDescriptors);
    XDOM xdom;
    try {
        xdom = parser.parse(macroCode, doc.getSyntax(), documentReference);
    } catch (MissingParserException ex) {
        throw new WikiMacroException("Could not find a parser for macro content", ex);
    } catch (ParseException ex) {
        throw new WikiMacroException("Error while parsing macro content", ex);
    }
    // Create & return the macro.
    return new DefaultWikiMacro(documentReference, doc.getAuthorReference(), macroSupportsInlineMode, macroDescriptor, xdom, doc.getSyntax(), this.componentManager);
}
Also used : DefaultContentDescriptor(org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor) MissingParserException(org.xwiki.rendering.parser.MissingParserException) XDOM(org.xwiki.rendering.block.XDOM) WikiMacroException(org.xwiki.rendering.macro.wikibridge.WikiMacroException) ArrayList(java.util.ArrayList) MacroId(org.xwiki.rendering.macro.MacroId) WikiMacroDescriptor(org.xwiki.rendering.macro.wikibridge.WikiMacroDescriptor) WikiMacroParameterDescriptor(org.xwiki.rendering.macro.wikibridge.WikiMacroParameterDescriptor) DefaultWikiMacro(org.xwiki.rendering.internal.macro.wikibridge.DefaultWikiMacro) BaseObject(com.xpn.xwiki.objects.BaseObject) DefaultContentDescriptor(org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor) ContentDescriptor(org.xwiki.rendering.macro.descriptor.ContentDescriptor) MacroDescriptor(org.xwiki.rendering.macro.descriptor.MacroDescriptor) WikiMacroDescriptor(org.xwiki.rendering.macro.wikibridge.WikiMacroDescriptor) ParseException(org.xwiki.rendering.parser.ParseException) WikiMacroVisibility(org.xwiki.rendering.macro.wikibridge.WikiMacroVisibility) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 7 with ParseException

use of org.xwiki.rendering.parser.ParseException in project xwiki-platform by xwiki.

the class AbstractDocumentTitleDisplayer method parseTitle.

/**
 * Parses the given title as plain text and returns the generated XDOM.
 *
 * @param title the title to be parsed
 * @return the XDOM generated from parsing the title as plain text
 */
protected XDOM parseTitle(String title) {
    try {
        XDOM xdom = plainTextParser.parse(new StringReader(title));
        parserUtils.removeTopLevelParagraph(xdom.getChildren());
        return xdom;
    } catch (ParseException e) {
        throw new RuntimeException(e);
    }
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) StringReader(java.io.StringReader) ParseException(org.xwiki.rendering.parser.ParseException)

Example 8 with ParseException

use of org.xwiki.rendering.parser.ParseException in project xwiki-platform by xwiki.

the class PygmentsParser method highlight.

@Override
public List<Block> highlight(String syntaxId, Reader source) throws ParseException {
    String code;
    try {
        code = IOUtils.toString(source);
    } catch (IOException e) {
        throw new ParseException("Failed to read source", e);
    }
    if (code.length() == 0) {
        return Collections.emptyList();
    }
    List<Block> blocks;
    try {
        blocks = highlight(syntaxId, code);
    } catch (ScriptException e) {
        throw new ParseException("Failed to highlight code", e);
    }
    // TODO: there is a bug in Pygments that makes it always put a newline at the end of the content
    if (code.charAt(code.length() - 1) != '\n' && !blocks.isEmpty() && blocks.get(blocks.size() - 1) instanceof NewLineBlock) {
        blocks.remove(blocks.size() - 1);
    }
    return blocks;
}
Also used : ScriptException(javax.script.ScriptException) Block(org.xwiki.rendering.block.Block) NewLineBlock(org.xwiki.rendering.block.NewLineBlock) IOException(java.io.IOException) ParseException(org.xwiki.rendering.parser.ParseException) NewLineBlock(org.xwiki.rendering.block.NewLineBlock)

Example 9 with ParseException

use of org.xwiki.rendering.parser.ParseException in project xwiki-platform by xwiki.

the class MessageFormatTranslationMessage method render.

@Override
public Block render(Locale locale, Collection<TranslationBundle> bundles, Object... parameters) {
    // Directly return cache if any available
    if (parameters.length == 0 && this.noParamCache != null) {
        return this.noParamCache.clone();
    }
    // Format the message
    String result;
    if (parameters.length > 0) {
        try {
            result = MessageFormat.format(this.message, parameters);
        } catch (IllegalArgumentException e) {
            // TODO: log the error ?
            result = this.message;
        }
    } else {
        result = this.message;
    }
    // Parse it to rendering blocks
    Block block;
    try {
        List<Block> blocks = this.plainParser.parse(new StringReader(result)).getChildren();
        PARSERUTILS.removeTopLevelParagraph(blocks);
        if (blocks.size() == 0) {
            block = new CompositeBlock();
        } else if (blocks.size() == 1) {
            block = blocks.get(0);
        } else {
            block = new CompositeBlock(blocks);
        }
        // Store cache of the message if there is no parameters
        if (parameters.length == 0) {
            this.noParamCache = block.clone();
        }
    } catch (ParseException e) {
        // Should never happen since plain text parser cannot fail
        block = null;
    }
    return block;
}
Also used : StringReader(java.io.StringReader) Block(org.xwiki.rendering.block.Block) CompositeBlock(org.xwiki.rendering.block.CompositeBlock) CompositeBlock(org.xwiki.rendering.block.CompositeBlock) ParseException(org.xwiki.rendering.parser.ParseException)

Example 10 with ParseException

use of org.xwiki.rendering.parser.ParseException in project xwiki-platform by xwiki.

the class DefaultXDOMOfficeDocumentBuilder method build.

@Override
public XDOMOfficeDocument build(XHTMLOfficeDocument xhtmlOfficeDocument) throws OfficeImporterException {
    Document xhtmlDoc = xhtmlOfficeDocument.getContentDocument();
    HTMLUtils.stripHTMLEnvelope(xhtmlDoc);
    XDOM xdom = null;
    try {
        xdom = this.xHtmlParser.parse(new StringReader(HTMLUtils.toString(xhtmlDoc)));
    } catch (ParseException ex) {
        throw new OfficeImporterException("Error: Could not parse xhtml office content.", ex);
    }
    return new XDOMOfficeDocument(xdom, xhtmlOfficeDocument.getArtifacts(), this.componentManager);
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) StringReader(java.io.StringReader) OfficeImporterException(org.xwiki.officeimporter.OfficeImporterException) ParseException(org.xwiki.rendering.parser.ParseException) XHTMLOfficeDocument(org.xwiki.officeimporter.document.XHTMLOfficeDocument) Document(org.w3c.dom.Document) XDOMOfficeDocument(org.xwiki.officeimporter.document.XDOMOfficeDocument) XDOMOfficeDocument(org.xwiki.officeimporter.document.XDOMOfficeDocument)

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