001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.rendering.wiki;
023
024import java.io.IOException;
025import java.io.Writer;
026
027/**
028 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
029 */
030public class TocText implements WikiText {
031
032    protected String title = "Table of Contents";
033
034    public TocText(String title) {
035        if (title != null) {
036            title = title.trim();
037            if (title.length() > 0) {
038                this.title = title;
039            }
040        }
041    }
042
043    @Override
044    public void writeTo(WikiSerializerHandler handler, Writer writer) throws IOException {
045        printToc(handler, writer);
046    }
047
048    public void printToc(WikiSerializerHandler serializer, Writer writer) throws IOException {
049        printTocHeader(serializer, writer, title);
050        Toc.Entry h = serializer.toc.head.firstChild;
051        if (h != null) {
052            prinEntry(serializer, writer, h);
053        }
054        printTocFooter(serializer, writer);
055    }
056
057    private void prinEntry(WikiSerializerHandler serializer, Writer writer, Toc.Entry entry) throws IOException {
058        printHeading(serializer, writer, entry);
059        if (entry.firstChild != null) {
060            writer.write("<ol>" + WikiWriter.LINE_SEP);
061            prinEntry(serializer, writer, entry.firstChild);
062            writer.write("</ol>" + WikiWriter.LINE_SEP);
063        }
064        if (entry.next != null) {
065            prinEntry(serializer, writer, entry.next);
066        }
067    }
068
069    protected void printTocHeader(WikiSerializerHandler serializer, Writer writer, String title) throws IOException {
070        writer.write("<table class=\"toc\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">");
071        writer.write(WikiWriter.LINE_SEP);
072        writer.write("<tr><td>");
073        writer.write("<div class=\"tocTitle\">" + title + "</div>");
074        writer.write("</td></tr>");
075        writer.write(WikiWriter.LINE_SEP);
076        writer.write("<tr><td>");
077        writer.write(WikiWriter.LINE_SEP);
078        writer.write("<ol class=\"contentToc\">");
079        writer.write(WikiWriter.LINE_SEP);
080    }
081
082    protected void printTocFooter(WikiSerializerHandler serializer, Writer writer) throws IOException {
083        writer.write("</ol>");
084        writer.write(WikiWriter.LINE_SEP);
085        writer.write("</td></tr>");
086        writer.write(WikiWriter.LINE_SEP);
087        writer.write("</table>");
088        writer.write(WikiWriter.LINE_SEP);
089    }
090
091    protected void printHeading(WikiSerializerHandler serializer, Writer writer, Toc.Entry entry) throws IOException {
092        writer.write("<li><a href=\"#heading_" + entry.id + "\">" + entry.title + "</a></li>" + WikiWriter.LINE_SEP);
093    }
094
095}