001 package de.java2html.javasource;
002 
003 /**
004  * This class represents java source code in a parsed, but still flat style.
005  * It contains the raw text along with an array of source type entries
006  * ({@link de.java2html.javasource.SourceType}) for each character.
007  <code>JavaSource</code> objects are created using the
008  {@link de.java2html.javasource.JavaSourceParser}.
009  
010  * A <code>JavaSource</code> object can be pretty-printed to HTML by using the
011  {@link de.java2html.converter.JavaSource2HTMLConverter}.
012  *
013  * For questions, suggestions, bug-reports, enhancement-requests etc. I may be contacted at:
014  *   <a href="mailto:markus@jave.de">markus@jave.de</a>
015  *
016  * The Java2html home page is located at:
017  *   <a href="http://www.java2html.de">http://www.java2html.de</a>
018  *
019  @author  <a href="mailto:markus@jave.de">Markus Gebhard</a>
020  *
021  <code>Copyright (C) Markus Gebhard 2000-2003
022  *
023  * This program is free software; you can redistribute it and/or
024  * modify it under the terms of the GNU General Public License
025  * as published by the Free Software Foundation; either version 2
026  * of the License, or (at your option) any later version.
027  *
028  * This program is distributed in the hope that it will be useful,
029  * but WITHOUT ANY WARRANTY; without even the implied warranty of
030  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
031  * GNU General Public License for more details.
032  
033  * You should have received a copy of the GNU General Public License
034  * along with this program; if not, write to the Free Software
035  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.</code>
036  *
037  */
038 public class TypedSource {
039   /** The source code as raw text */
040   private final String source;
041 
042   /** Flags for every character in the source code telling
043    the type. */
044   private final SourceType[] sourceTypes;
045 
046   private final JavaSourceStatistic statistics;
047 
048   public TypedSource(String source, SourceType[] sourceTypes, JavaSourceStatistic statistics) {
049     this.source = source;
050     this.statistics = statistics;
051     this.sourceTypes = sourceTypes;
052   }
053 
054   public SourceType[] getClassification() {
055     return sourceTypes;
056   }
057 
058   public String getCode() {
059     return source;
060   }
061 
062   /**
063    * Debug output of the code
064    */
065   public void print() {
066     System.out.println("------------------------------");
067     int start = 0;
068     int end = 0;
069 
070     while (start < sourceTypes.length) {
071       while (end < sourceTypes.length - && sourceTypes[end + 1== sourceTypes[start]) {
072         ++end;
073       }
074 
075       print(start, end);
076 
077       start = end + 1;
078       end = start;
079     }
080   }
081 
082   protected void print(int start, int end) {
083     System.out.print(sourceTypes[start": ");
084     System.out.println("@" + source.substring(start, end + 1).replace('\n''#'"@");
085   }
086 
087   /**
088    * Returns statistical information as String
089    @deprecated As of 26.02.2006 (Markus Gebhard), replaced by {@link #getStatistic()}
090    */
091   @Deprecated
092   public String getStatisticsString() {
093     /* output format (example):
094      Lines total: 127  Code: 57  Comments: 16  Empty: 54
095      3164 Characters, maximum line length: 95                */
096     return statistics.getScreenString("\n");
097   }
098 
099   public String getFileName() {
100     return getStatistic().getFileName();
101   }
102 
103   public void setFileName(String fileName) {
104     getStatistic().setFileName(fileName);
105   }
106 
107   public int getLineCount() {
108     return statistics.getLineCount();
109   }
110 
111   public int getMaxLineLength() {
112     return statistics.getMaxLineLength();
113   }
114 
115   public JavaSourceStatistic getStatistic() {
116     return statistics;
117   }
118 
119   public TypedSourceIterator getIterator() {
120     return new TypedSourceIterator(this);
121   }
122 }