View Javadoc

1   /***
2    * Fractal Task Codegen Framework
3    * Copyright (C) 2006-2007 STMicroelectronics
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   *
19   * Contact: fractal@objectweb.org
20   *
21   * Author: Matthieu Leclercq
22   */
23  
24  package org.objectweb.fractal.cecilia.adl.file;
25  
26  /**
27   * Utility class for source code generation.
28   */
29  public final class CodeWriter {
30  
31    private static final int    INDENTATION_SPACE = 2;
32    private final String        producerName;
33    private int                 indentation       = 0;
34    private boolean             newLine           = true;
35    private final StringBuilder buff              = new StringBuilder();
36  
37    // private int pendingChars = 0;
38  
39    /**
40     * @param producerName the name of the producer of the code.
41     */
42    public CodeWriter(final String producerName) {
43      this.producerName = producerName;
44      append("//+ Beginning of code produced by ").append(producerName).endl();
45    }
46  
47    /**
48     * Default constructor.
49     */
50    public CodeWriter() {
51      this.producerName = null;
52    }
53  
54    /**
55     * Appends a string.
56     * 
57     * @param s the code to appends.
58     * @return <code>this</code>.
59     */
60    public CodeWriter append(final String s) {
61      if (s == null) return this;
62      for (int i = 0; i < s.length(); i++) {
63        append(s.charAt(i));
64      }
65      return this;
66    }
67  
68    /**
69     * Appends a character.
70     * 
71     * @param c a character.
72     * @return <code>this</code>.
73     */
74    public CodeWriter append(final char c) {
75      if (newLine && (c == ' ' || c == '\t')) return this;
76  
77      if (c == '}') {
78        if (newLine && indentation >= INDENTATION_SPACE)
79          buff.delete(buff.length() - INDENTATION_SPACE, buff.length());
80        removeIndentLevel();
81      } else if (c == '{') addIndentLevel();
82      newLine = false;
83      buff.append(c);
84      if (c == '\n') {
85        newLine = true;
86        for (int j = 0; j < indentation; j++)
87          buff.append(' ');
88      }
89      return this;
90    }
91  
92    /**
93     * Appends a string and ends the current line.
94     * 
95     * @param s the code to appends.
96     * @return <code>this</code>.
97     */
98    public CodeWriter appendln(final String s) {
99      if (s == null) return this;
100     append(s).append('\n');
101     return this;
102   }
103 
104   /**
105    * Appends an integer.
106    * 
107    * @param i the integer to appends.
108    * @return <code>this</code>.
109    */
110   public CodeWriter append(final int i) {
111     buff.append(i);
112     return this;
113   }
114 
115   /**
116    * Ends the current line.
117    * 
118    * @return <code>this</code>.
119    */
120   public CodeWriter endl() {
121     append('\n');
122     return this;
123   }
124 
125   protected CodeWriter addIndentLevel() {
126     indentation += INDENTATION_SPACE;
127     return this;
128   }
129 
130   protected CodeWriter removeIndentLevel() {
131     if (indentation >= INDENTATION_SPACE) indentation -= INDENTATION_SPACE;
132     return this;
133   }
134 
135   @Override
136   public String toString() {
137     if (producerName != null) {
138       if (!newLine) endl();
139       append("//- End of code produced by ").append(producerName).endl();
140     }
141     return buff.toString();
142   }
143 
144 }