001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2012 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * Sonar is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.colorizer;
021    
022    import java.util.HashMap;
023    import java.util.Map;
024    
025    public class HtmlCodeBuilder implements Appendable {
026    
027      private StringBuilder colorizedCode = new StringBuilder();
028      private Map variables = new HashMap(); // stateful data
029    
030      public Appendable append(CharSequence csq) {
031        for (int i = 0; i < csq.length(); i++) {
032          append(csq.charAt(i));
033        }
034        return this;
035      }
036    
037      public Appendable append(char c) {
038        if (c == '<') {
039          colorizedCode.append("&lt;");
040        } else if (c == '>') {
041          colorizedCode.append("&gt;");
042        } else {
043          colorizedCode.append(c);
044        }
045        return this;
046      }
047    
048      public Appendable append(CharSequence csq, int start, int end) {
049        for (int i = start; i < end; i++) {
050          append(csq.charAt(i));
051        }
052        return this;
053      }
054    
055      public void appendWithoutTransforming(String htmlTag) {
056        colorizedCode.append(htmlTag);
057      }
058    
059      public String toString() {
060        return colorizedCode.toString();
061      }
062    
063      public StringBuilder getColorizedCode() {
064        return colorizedCode;
065      }
066    
067      /**
068       * Save a stateful variable.
069       * 
070       * @param key
071       *          can NOT be null
072       * @param value
073       *          can be null
074       */
075      public void setVariable(Object key, Object value) {
076        variables.put(key, value);
077      }
078    
079      /**
080       * Get a stateful variable. Return null if not found.
081       */
082      public Object getVariable(Object key) {
083        return variables.get(key);
084      }
085    
086      /**
087       * Get a stateful variable. Return the default value if not found.
088       */
089      public Object getVariable(Object key, Object defaultValue) {
090        Object result = variables.get(key);
091        if (result == null) {
092          result = defaultValue;
093        }
094        return result;
095      }
096    
097      /**
098       * All stateful variables
099       */
100      public Map getVariables() {
101        return variables;
102      }
103    
104    }