001/* 
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 *
012 * $Id$
013 */
014
015package org.nuxeo.ecm.platform.rendering.wiki.extensions;
016
017import java.io.IOException;
018import java.io.Writer;
019
020import org.nuxeo.ecm.platform.rendering.fm.extensions.BlockWriter;
021import org.nuxeo.ecm.platform.rendering.wiki.WikiSerializerHandler;
022import org.nuxeo.ecm.platform.rendering.wiki.WikiWriter;
023
024/**
025 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
026 */
027public class WikiBlockWriter extends WikiWriter {
028
029    protected final String blockName;
030
031    public WikiBlockWriter(WikiWriter parent, String blockName) {
032        super(parent);
033        this.blockName = blockName;
034    }
035
036    @Override
037    public void writeTo(WikiSerializerHandler handler, Writer writer) throws IOException {
038        if (writer instanceof BlockWriter) {
039            BlockWriter parentWriter = (BlockWriter) writer;
040            BlockWriter bw = new BlockWriter("__dynamic__wiki", blockName, parentWriter.getRegistry());
041            boolean parentSuppressOutput = parentWriter.getSuppressOutput();
042            try {
043                parentWriter.setSuppressOutput(true);
044                super.writeTo(handler, bw);
045                parentWriter.writeBlock(bw);
046            } finally {
047                parentWriter.setSuppressOutput(parentSuppressOutput);
048            }
049        } else {
050            throw new IllegalArgumentException("Unsupported target writer");
051        }
052    }
053
054}