Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/arranger/jarl/script/jarlsp/JarlSPLanguage.java


1   package com.arranger.jarl.script.jarlsp;
2   
3   import com.arranger.jarl.script.gsp.IGSPLanguage;
4   import com.arranger.jarl.script.gsp.GSPException;
5   
6   import java.util.*;
7   import java.io.CharArrayWriter;
8   import java.io.IOException;
9   import java.io.Writer;
10  
11  /**
12   * JarlSPLanguage created on Mar 5, 2003
13   */
14  public class JarlSPLanguage implements IGSPLanguage {
15  
16      protected List m_import = new ArrayList();
17      protected CharArrayWriter m_decl = new CharArrayWriter(0x1000);
18      protected CharArrayWriter m_content = new CharArrayWriter(0x010000);
19  
20      /**
21       * This should only be called after this language has been parsed.
22       *
23       * @param out the writer to take the content
24       * @param packageName the package of the resulting java class
25       * @param className  the name of the resulting java class
26       * @param bassClassName the java class that this java class extends
27       * @throws IOException
28       */
29      public void flush(Writer out,
30                        String packageName,
31                        String className,
32                        String bassClassName) throws IOException {
33          // head
34          out.write("// jarlml ");
35          out.write(new Date().toString());
36          out.write(LINEFEED);
37  
38          if (packageName != null) {
39              out.write("package ");
40              out.write(packageName);
41              out.write(';');
42              out.write(LINEFEED);
43              out.write(LINEFEED);
44          }
45  
46          // imports
47          for (Iterator it = m_import.iterator(); it.hasNext();) {
48              out.write("import ");
49              out.write(it.next().toString());
50              out.write(';');
51              out.write(LINEFEED);
52          }
53          out.write(LINEFEED);
54  
55          // class decl
56          out.write("public class ");
57          out.write(className);
58          out.write(" extends ");
59          out.write(bassClassName);
60          out.write(" {");
61          out.write(LINEFEED);
62          out.write(LINEFEED);
63  
64          // custom decls
65          m_decl.writeTo(out);
66          out.write(LINEFEED);
67          out.write(LINEFEED);
68  
69          // service method
70          out.write("public void execute() throws Exception {");
71          out.write(LINEFEED);
72          m_content.writeTo(out);
73          out.write(LINEFEED);
74          out.write("}");
75          out.write(LINEFEED);
76          out.write(LINEFEED);
77  
78          out.write("}");
79          out.write(LINEFEED);
80      }
81  
82      /**
83       * Code (<% %>).
84       */
85      public void code(char[] buffer, int start, int length) throws GSPException {
86          m_content.write(buffer, start, length);
87      }
88  
89      /**
90       * This is Code declaration.
91       *  eg <code>(<%! %>);</code>
92       */
93      public void decl(char[] buffer, int start, int length) throws GSPException {
94          m_decl.write(buffer, start, length);
95      }
96  
97      /**
98       * Code to evaluate as a string (<%= %>).
99       */
100     public void eval(char[] buffer, int start, int length) throws GSPException {
101         try {
102             m_content.write("print(");
103             m_content.write(buffer, start, length);
104             m_content.write(");");
105             m_content.write(LINEFEED);
106         } catch (IOException e) {
107             throw new GSPException(e);
108         }
109     }
110 
111     /**
112      * String to print.
113      */
114     public void print(char[] buffer, int start, int length) throws GSPException {
115         try {
116             m_content.write("print(\"");
117             encodeJavaStringLiteral(m_content, buffer, start, length);
118             m_content.write("\");");
119             m_content.write(LINEFEED);
120         } catch (IOException e) {
121             throw new GSPException(e);
122         }
123     }
124 
125     /**
126      * Processing directive.
127      */
128     public void directive(String directive, Map parameters) throws GSPException {
129         if ("page".equals(directive)) {
130             // import
131             String imprt = (String)parameters.get("import");
132             if (imprt != null) {
133                 StringTokenizer tokens = new StringTokenizer(imprt, ",");
134                 while (tokens.hasMoreTokens()) {
135                     m_import.add(tokens.nextToken());
136                 }
137             }
138         } else {
139             throw new GSPException("Unrecognized directive: " + directive);
140         }
141     }
142 
143     /**
144      * Start tag (<prefix:name foo="bar">).
145      */
146     public void tagStart(String prefix, String name, Map parameters) throws GSPException {
147         throw new GSPException("tagStart not supported: " + prefix + ":" + name);
148     }
149 
150     /**
151      * End tag (</prefix:name>).
152      */
153     public void tagEnd(String prefix, String name) throws GSPException {
154         throw new GSPException("tagEnd not supported: " + prefix + ":" + name);
155     }
156 
157     /**
158      * Escapes \ ' " \n \r while writing to ouput stream.
159      * @param out Output stream.
160      * @param buf Value to encode.
161      * @param off Starting index.
162      * @param len Number of chars to escape.
163      */
164     protected void encodeJavaStringLiteral(Writer out, char[] buf, int off, int len) throws IOException {
165         int end = off + len;
166 
167         for (int i = off; i < end; i++) {
168             switch (buf[i]) {
169                 case '\\':
170                 case '\"':
171                 case '\'':
172                     out.write('\\');
173                     out.write(buf[i]);
174                     break;
175                 case '\n':
176                     out.write("\\n");
177                     break;
178                 case '\r':
179                     out.write("\\r");
180                     break;
181                 default:
182                     out.write(buf[i]);
183                     break;
184             }
185         }
186     }
187 }