如何使用Confluence SDK使用WikiToXhtmlMigrator进行转换

问题描述 投票:0回答:1

我正在尝试将WikiMarkup数据转换为Confluence的基于XHTML的格式。我看到有两种不同的方法可以做到这一点。

1)使用rest / XML基于Rpc的API convertWikiToStorageFormat(String token,String markup)来做同样的事情。 2)其他方法是使用WikiToXhtmlMigrator来执行相同的任务。由于我使用Confluence SDK,我选择第二种方式来做同样的事情。

public String getXHTMLConvertedTextFromWiki(String markupData){
    String content = null;
    try {

        RenderContext renderContext = new RenderContext();
        DefaultConversionContext defaultConversionContext = new DefaultConversionContext(renderContext);
        ExceptionTolerantMigrator wikiToXhtmlMigrator = (ExceptionTolerantMigrator) ContainerManager.getComponent("wikiToXhtmlMigrator");
        content = wikiToXhtmlMigrator.migrate(markupData, defaultConversionContext).getContent();

    }catch(Exception e){
        e.printStackTrace();
    }
    return content;
}

但总是,我得到了例外:

[INFO] [talledLocalContainer] java.lang.NullPointerException
[INFO] [talledLocalContainer]   at com.atlassian.confluence.content.render.xhtml.migration.WikiToXhtmlMigrator.migrate(WikiToXhtmlMigrator.java:57)
[INFO] [talledLocalContainer]   at com.atlassian.confluence.content.render.xhtml.migration.WikiToXhtmlMigrator.migrate(WikiToXhtmlMigrator.java:69)

我尝试通过在渲染上下文中设置All来推动渲染模式但是得到了相同的异常。

renderContext.pushRenderMode(RenderMode.ALL);

请让我知道此示例中的问题是什么,并显示正确的方法。

confluence
1个回答
0
投票

能够通过下面的代码将标记转换为Xhtml。

public String getXHTMLConvertedTextFromWiki(String wikiText){
    String content = null;
    try {

        List<RuntimeException> exceptions = newArrayList();
        XhtmlContent xhtmlContent = (XhtmlContent)ContainerManager.getComponent("xhtmlContent");
        content = xhtmlContent.convertWikiToStorage(wikiText, new DefaultConversionContext(new PageContext()),exceptions);
         for(RuntimeException e : exceptions){
             log.error(e.getMessage());
         }
    }catch(Exception e){
        e.printStackTrace();
    }
    return content;
}

请确保,如果从外部源读取,则在读取页面标记数据时不应丢失换行符。

© www.soinside.com 2019 - 2024. All rights reserved.