001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 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.io.Reader;
023    import java.util.List;
024    
025    public class CodeColorizer {
026    
027      private List<Tokenizer> tokenizers = null;
028    
029      public CodeColorizer(List<Tokenizer> tokenizers) {
030        this.tokenizers = tokenizers;
031      }
032    
033      public CodeColorizer(Format format) {
034        this.tokenizers = format.getTokenizers();
035      }
036    
037      public String toHtml(Reader code) {
038        return toHtml(code, null);
039      }
040    
041      public String toHtml(Reader code, HtmlOptions options) {
042        HtmlOptions opts = (options == null ? HtmlOptions.DEFAULT : options);
043        return new HtmlRenderer(opts).render(code, tokenizers);
044      }
045    
046      public static String javaToHtml(Reader code, HtmlOptions options) {
047        return new CodeColorizer(Format.JAVA).toHtml(code, options);
048      }
049    
050      public static String groovyToHtml(Reader code, HtmlOptions options) {
051        return new CodeColorizer(Format.GROOVY).toHtml(code, options);
052      }
053    
054      public static String getCss() {
055        return HtmlDecorator.getCss();
056      }
057    
058      public enum Format {
059        JAVA(JavaTokenizers.forHtml()), GROOVY(GroovyTokenizers.forHtml());
060    
061        private List<Tokenizer> tokenizers;
062    
063        Format(List<Tokenizer> tokenizers) {
064          this.tokenizers = tokenizers;
065        }
066    
067        public List<Tokenizer> getTokenizers() {
068          return tokenizers;
069        }
070      }
071    }