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;
016
017import java.io.IOException;
018import java.io.Writer;
019
020/**
021 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
022 */
023public class TocText implements WikiText {
024
025    protected String title = "Table of Contents";
026
027    public TocText(String title) {
028        if (title != null) {
029            title = title.trim();
030            if (title.length() > 0) {
031                this.title = title;
032            }
033        }
034    }
035
036    public void writeTo(WikiSerializerHandler handler, Writer writer) throws IOException {
037        printToc(handler, writer);
038    }
039
040    public void printToc(WikiSerializerHandler serializer, Writer writer) throws IOException {
041        printTocHeader(serializer, writer, title);
042        Toc.Entry h = serializer.toc.head.firstChild;
043        if (h != null) {
044            prinEntry(serializer, writer, h);
045        }
046        printTocFooter(serializer, writer);
047    }
048
049    private void prinEntry(WikiSerializerHandler serializer, Writer writer, Toc.Entry entry) throws IOException {
050        printHeading(serializer, writer, entry);
051        if (entry.firstChild != null) {
052            writer.write("<ol>" + WikiWriter.LINE_SEP);
053            prinEntry(serializer, writer, entry.firstChild);
054            writer.write("</ol>" + WikiWriter.LINE_SEP);
055        }
056        if (entry.next != null) {
057            prinEntry(serializer, writer, entry.next);
058        }
059    }
060
061    protected void printTocHeader(WikiSerializerHandler serializer, Writer writer, String title) throws IOException {
062        writer.write("<table class=\"toc\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">");
063        writer.write(WikiWriter.LINE_SEP);
064        writer.write("<tr><td>");
065        writer.write("<div class=\"tocTitle\">" + title + "</div>");
066        writer.write("</td></tr>");
067        writer.write(WikiWriter.LINE_SEP);
068        writer.write("<tr><td>");
069        writer.write(WikiWriter.LINE_SEP);
070        writer.write("<ol class=\"contentToc\">");
071        writer.write(WikiWriter.LINE_SEP);
072    }
073
074    protected void printTocFooter(WikiSerializerHandler serializer, Writer writer) throws IOException {
075        writer.write("</ol>");
076        writer.write(WikiWriter.LINE_SEP);
077        writer.write("</td></tr>");
078        writer.write(WikiWriter.LINE_SEP);
079        writer.write("</table>");
080        writer.write(WikiWriter.LINE_SEP);
081    }
082
083    protected void printHeading(WikiSerializerHandler serializer, Writer writer, Toc.Entry entry) throws IOException {
084        writer.write("<li><a href=\"#heading_" + entry.id + "\">" + entry.title + "</a></li>" + WikiWriter.LINE_SEP);
085    }
086
087}