/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 *
 */
package org.xwiki.rendering.example;

import java.io.StringReader;

import org.junit.Test;
import org.junit.Assert;

import org.xwiki.component.embed.EmbeddableComponentManager;
import org.xwiki.rendering.block.XDOM;
import org.xwiki.rendering.parser.Parser;
import org.xwiki.rendering.parser.Syntax;
import org.xwiki.rendering.renderer.PrintRendererFactory;
import org.xwiki.rendering.renderer.Renderer;
import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter;
import org.xwiki.rendering.renderer.printer.WikiPrinter;
import org.xwiki.rendering.transformation.TransformationManager;

public class ExampleTest
{
    @Test
    public void renderXWiki20SyntaxAsXHTML() throws Exception
    {
        // Initialize Rendering components and allow getting instances
        EmbeddableComponentManager ecm = new EmbeddableComponentManager(this.getClass().getClassLoader());
        ecm.initialize();
        
        // Parse XWiki 2.0 Syntax
        Parser parser = ecm.lookup(Parser.class, Syntax.XWIKI_2_0.toIdString());
        XDOM xdom = parser.parse(new StringReader("This is **bold**"));
        
        // Run macros
        TransformationManager txManager = ecm.lookup(TransformationManager.class);
        txManager.performTransformations(xdom, parser.getSyntax());

        // Generate HTML for example
        WikiPrinter printer = new DefaultWikiPrinter();
        PrintRendererFactory rf = ecm.lookup(PrintRendererFactory.class);
        Renderer htmlRenderer = rf.createRenderer(Syntax.XHTML_1_0, printer);
        
        xdom.traverse(htmlRenderer);

        Assert.assertEquals("<p>This is <strong>bold</strong></p>", printer.toString());
    }
}

@@ -956,16 +956,46 @@
 gReader;
+%0Aimport java.util.Collections;
 %0A%0Aimport
@@ -1131,20 +1131,194 @@
 g.block.
-XDOM
+Block;%0Aimport org.xwiki.rendering.block.FormatBlock;%0Aimport org.xwiki.rendering.block.LinkBlock;%0Aimport org.xwiki.rendering.block.XDOM;%0Aimport org.xwiki.rendering.listener.Format
 ;%0Aimport
@@ -1686,16 +1686,157 @@
 nager;%0A%0A
+/**%0A * Examples of using the XWiki Rendering API standalone, using the Embedded Component Manager.%0A *  %0A * @version $Id$%0A * @since 2.0M1%0A */%0A
 public c
@@ -2637,16 +2637,17 @@
 Factory 
+p
 rf = ecm
@@ -2715,16 +2715,17 @@
 derer = 
+p
 rf.creat
@@ -2904,10 +2904,1380 @@
 ;%0A    %7D%0A
+    %0A    @Test%0A    public void makeAllLinksItalic() throws Exception%0A    %7B%0A        // Initialize Rendering components and allow getting instances%0A        EmbeddableComponentManager ecm = new EmbeddableComponentManager(this.getClass().getClassLoader());%0A        ecm.initialize();%0A        %0A        // Parse XWiki 2.0 Syntax%0A        Parser parser = ecm.lookup(Parser.class, Syntax.XWIKI_2_0.toIdString());%0A        XDOM xdom = parser.parse(new StringReader(%22This a %5B%5Blink%3EMyPage%5D%5D%22));%0A        %0A        // Find all links and make them italic%0A        for (LinkBlock block : xdom.getChildrenByType(LinkBlock.class, true)) %7B%0A            // Note: in XWiki Core 2.0 the API has been improved and there's no need to clone the%0A            // block anymore and the replace() API has been modified.%0A            Block newBlock = new FormatBlock(Collections.%3CBlock%3EsingletonList(block.clone()), Format.ITALIC);%0A            block.replace(Collections.%3CBlock%3EsingletonList(newBlock));%0A        %7D%0A        %0A        // Generate XWiki 2.0 Syntax for example%0A        WikiPrinter printer = new DefaultWikiPrinter();%0A        PrintRendererFactory prf = ecm.lookup(PrintRendererFactory.class);%0A        Renderer xwikiRenderer = prf.createRenderer(Syntax.XWIKI_2_0, printer);%0A%0A        xdom.traverse(xwikiRenderer);%0A%0A        Assert.assertEquals(%22This a //%5B%5Blink%3EMyPage%5D%5D//%22, printer.toString());%0A    %7D%0A
 %7D%0A

Add new example