001/*
002 * Copyright (C) 2012 eXo Platform SAS.
003 *
004 * This is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU Lesser General Public License as
006 * published by the Free Software Foundation; either version 2.1 of
007 * the License, or (at your option) any later version.
008 *
009 * This software is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with this software; if not, write to the Free
016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018 */
019package org.crsh.text;
020
021import java.io.IOException;
022
023/** @author Julien Viet */
024public abstract class Format {
025
026  /** . */
027  public static final Text TEXT = new Text();
028
029  /** . */
030  public static final Ansi ANSI = new Ansi();
031
032  /** . */
033  public static final PreHtml PRE_HTML = new PreHtml();
034
035  public static class Text extends Format {
036    protected Text() {
037    }
038    @Override
039    public void begin(Appendable to) throws IOException {
040    }
041    @Override
042    public void write(CharSequence s, Appendable to) throws IOException {
043      if (s.length() > 0) {
044        to.append(s);
045      }
046    }
047    @Override
048    public void write(Style style, Appendable to) throws IOException {
049    }
050    @Override
051    public void cls(Appendable to) throws IOException {
052    }
053    @Override
054    public void end(Appendable to) throws IOException {
055    }
056  }
057
058  public static class Ansi extends Format {
059    protected Ansi() {
060    }
061    @Override
062    public void begin(Appendable to) throws IOException {
063    }
064    @Override
065    public void write(CharSequence s, Appendable to) throws IOException {
066      if (s.length() > 0) {
067        to.append(s);
068      }
069    }
070    @Override
071    public void write(Style style, Appendable to) throws IOException {
072      style.writeAnsiTo(to);
073    }
074    @Override
075    public void cls(Appendable to) throws IOException {
076    }
077    @Override
078    public void end(Appendable to) throws IOException {
079    }
080  }
081
082  public static class PreHtml extends Format {
083    protected PreHtml() {
084    }
085    @Override
086    public void begin(Appendable to) throws IOException {
087      to.append("<pre>");
088    }
089    @Override
090    public void write(CharSequence s, Appendable to) throws IOException {
091      if (s.length() > 0) {
092        for (int i = 0;i < s.length();i++) {
093          char c = s.charAt(i);
094          switch (c) {
095            case '>':
096              to.append("&gt;");
097              break;
098            case '<':
099              to.append("&lt;");
100              break;
101            case '&':
102              to.append("&amp;");
103              break;
104            case '\'':
105              to.append("&#039;");
106              break;
107            case '"':
108              to.append("&#034;");
109              break;
110            default:
111              to.append(c);
112          }
113        }
114      }
115    }
116    @Override
117    public void write(Style style, Appendable to) throws IOException {
118    }
119    @Override
120    public void cls(Appendable to) throws IOException {
121    }
122    @Override
123    public void end(Appendable to) throws IOException {
124      to.append("</pre>");
125    }
126  }
127
128  public abstract void begin(Appendable to) throws IOException;
129
130  public abstract void write(CharSequence s, Appendable to) throws IOException;
131
132  public abstract void write(Style style, Appendable to) throws IOException;
133
134  public abstract void cls(Appendable to) throws IOException;
135
136  public abstract void end(Appendable to) throws IOException;
137}