KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > codegen > jet > JETCharDataGenerator


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: JETCharDataGenerator.java,v 1.3 2005/06/08 06:15:56 nickb Exp $
16  */

17 package org.eclipse.emf.codegen.jet;
18
19
20
21 /**
22  * The JETCharDataGenerator generates strings for the character data present in the template file.
23  */

24 public class JETCharDataGenerator
25   implements JETGenerator
26 {
27   protected static final String JavaDoc FUNCTION_CALL_BEGIN = "stringBuffer.append(";
28   protected static final String JavaDoc FUNCTION_CALL_END = ");";
29   protected static final String JavaDoc NEW_LINE_BEGIN = "NL + ";
30   protected static final String JavaDoc NEW_LINE_MIDDLE = "\" + NL + \"";
31   protected static final String JavaDoc NEW_LINE_END = " + NL";
32
33   protected char[] characters;
34
35   public JETCharDataGenerator(char[] characters)
36   {
37     this.characters = characters;
38   }
39
40   public String JavaDoc generate()
41   {
42     StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc(characters.length + 16);
43     stringBuffer.append(FUNCTION_CALL_BEGIN);
44     stringBuffer.append(generateCharData());
45     stringBuffer.append(FUNCTION_CALL_END);
46     return stringBuffer.toString();
47   }
48
49   protected String JavaDoc generateCharData()
50   {
51     StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc();
52     StringBuffer JavaDoc stringBufferTail = new StringBuffer JavaDoc();
53     int limit = characters.length - 1;
54
55      buildEnd:
56      while (limit >= 0)
57      {
58        char ch = characters[limit];
59        switch (ch)
60        {
61          case '\r':
62          {
63            break;
64          }
65          case '\n':
66          {
67            stringBufferTail.append(NEW_LINE_END);
68            break;
69          }
70          default:
71          {
72            limit++;
73            break buildEnd;
74          }
75        }
76        limit--;
77      }
78
79      if (limit < 0)
80      {
81        if (stringBufferTail.length() == 0)
82        {
83          stringBuffer.append("\"\"");
84        }
85        else
86        {
87          stringBuffer.append(stringBufferTail.toString().substring(3));
88        }
89      }
90      else
91      {
92        int i = 0;
93        buildFront:
94        for (; i <= limit; i ++ )
95        {
96          char ch = characters[i];
97          switch (ch)
98          {
99            case '\r':
100            {
101              break;
102            }
103            case '\n':
104            {
105              stringBuffer.append(NEW_LINE_BEGIN);
106              break;
107            }
108            default:
109            {
110              break buildFront;
111            }
112          }
113        }
114  
115        stringBuffer.append('"');
116        for (; i < limit; i++)
117        {
118          char ch = characters[i];
119          switch (ch)
120          {
121            case '"':
122            {
123              stringBuffer.append("\\\"");
124              break;
125            }
126            case '\\':
127            {
128              stringBuffer.append("\\\\");
129              break;
130            }
131           case '\r':
132           {
133             continue;
134           }
135           case '\n':
136           {
137             stringBuffer.append(NEW_LINE_MIDDLE);
138             break;
139           }
140           case '\t':
141           {
142             stringBuffer.append("\\t");
143             break;
144           }
145           default:
146           {
147             stringBuffer.append(ch);
148           }
149         }
150       }
151       stringBuffer.append('"');
152       stringBuffer.append(stringBufferTail.toString());
153     }
154     return stringBuffer.toString();
155   }
156 }
157
Popular Tags