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:[email protected]">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 public void writeTo(WikiSerializerHandler handler, Writer writer) throws IOException { 044 printToc(handler, writer); 045 } 046 047 public void printToc(WikiSerializerHandler serializer, Writer writer) throws IOException { 048 printTocHeader(serializer, writer, title); 049 Toc.Entry h = serializer.toc.head.firstChild; 050 if (h != null) { 051 prinEntry(serializer, writer, h); 052 } 053 printTocFooter(serializer, writer); 054 } 055 056 private void prinEntry(WikiSerializerHandler serializer, Writer writer, Toc.Entry entry) throws IOException { 057 printHeading(serializer, writer, entry); 058 if (entry.firstChild != null) { 059 writer.write("<ol>" + WikiWriter.LINE_SEP); 060 prinEntry(serializer, writer, entry.firstChild); 061 writer.write("</ol>" + WikiWriter.LINE_SEP); 062 } 063 if (entry.next != null) { 064 prinEntry(serializer, writer, entry.next); 065 } 066 } 067 068 protected void printTocHeader(WikiSerializerHandler serializer, Writer writer, String title) throws IOException { 069 writer.write("<table class=\"toc\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">"); 070 writer.write(WikiWriter.LINE_SEP); 071 writer.write("<tr><td>"); 072 writer.write("<div class=\"tocTitle\">" + title + "</div>"); 073 writer.write("</td></tr>"); 074 writer.write(WikiWriter.LINE_SEP); 075 writer.write("<tr><td>"); 076 writer.write(WikiWriter.LINE_SEP); 077 writer.write("<ol class=\"contentToc\">"); 078 writer.write(WikiWriter.LINE_SEP); 079 } 080 081 protected void printTocFooter(WikiSerializerHandler serializer, Writer writer) throws IOException { 082 writer.write("</ol>"); 083 writer.write(WikiWriter.LINE_SEP); 084 writer.write("</td></tr>"); 085 writer.write(WikiWriter.LINE_SEP); 086 writer.write("</table>"); 087 writer.write(WikiWriter.LINE_SEP); 088 } 089 090 protected void printHeading(WikiSerializerHandler serializer, Writer writer, Toc.Entry entry) throws IOException { 091 writer.write("<li><a href=\"#heading_" + entry.id + "\">" + entry.title + "</a></li>" + WikiWriter.LINE_SEP); 092 } 093 094}