001/*
002 * Copyright 2012 Atteo.
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 */
016package org.atteo.evo.config.doclet;
017
018public class HtmlWriter {
019    private StringBuilder builder = new StringBuilder();
020
021    public HtmlWriter append(String str) {
022        builder.append(str);
023        return this;
024    }
025
026    public HtmlWriter lt() {
027        builder.append("<code class=\"xml plain\">&lt;</code>");
028        return this;
029    }
030
031    public HtmlWriter gt() {
032        builder.append("<code class=\"xml plain\">&gt;</code>");
033        return this;
034    }
035
036    public HtmlWriter keyword(String name) {
037        builder.append("<code class=\"xml keyword\">");
038        builder.append(name);
039        builder.append("</code>");
040        return this;
041    }
042
043    public HtmlWriter newline() {
044        builder.append("<br/>\n");
045        return this;
046    }
047
048    @Override
049    public String toString() {
050        return builder.toString();
051    }
052
053    public HtmlWriter indent(int level) {
054        for (int i = 0; i < level; i++) {
055            builder.append("&nbsp;&nbsp;&nbsp;&nbsp;");
056        }
057        return this;
058    }
059
060    public HtmlWriter comment(String comment, int level) {
061        if (comment == null) {
062            return this;
063        }
064        String[] parts = comment.split("\n");
065        if (parts.length == 1) {
066            indent(level);
067            builder.append("&lt;!-- ");
068            builder.append(comment).append(" --&gt;");
069            newline();
070        } else {
071            indent(level);
072            builder.append("&lt;!--");
073            newline();
074
075            for (int i = 0; i < parts.length; i++) {
076                indent(level + 1);
077                builder.append(parts[i].trim());
078                newline();
079            }
080
081            indent(level);
082            builder.append("--&gt;");
083            newline();
084        }
085
086        return this;
087    }
088
089    public HtmlWriter defaultValue(String defaultValue) {
090        if (defaultValue == null) {
091            builder.append("...");
092        } else {
093            builder.append(defaultValue);
094        }
095        return this;
096    }
097}