KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > TemplateReader


1 /*
2   Copyright (C) 2001-2002 Laurent Martelli
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful, but
10   WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.aspects.gui;
20
21 import java.io.*;
22 import java.util.Map JavaDoc;
23
24 /**
25  * This is unused class to create printable templates.
26  *
27  * <p>Template Reader : a stream reader for Template
28  * Substitute %param% with the value of template.getParams().get(param)
29  **/

30
31 public class TemplateReader extends Reader {
32
33    Map JavaDoc params;
34    StringBuffer JavaDoc buffer;
35    Reader in;
36
37    /* The char used to delimitate params */
38    int delim_char = '%';
39
40    public TemplateReader(Reader in, Map JavaDoc params)
41    {
42       this.in = in;
43       this.params = params;
44       this.buffer = new StringBuffer JavaDoc();
45    }
46
47    public int read(char[] b, int off, int len) throws IOException
48    {
49       /* copied from gnu/regexp/REFilterReader.java */
50       int i;
51       int ok = 0;
52       while (len-- > 0) {
53          i = read();
54          if (i == -1) return (ok == 0) ? -1 : ok;
55          b[off++] = (char) i;
56          ok++;
57       }
58       return ok;
59    }
60    
61    /* Read a single char */
62    public int read() throws IOException
63    {
64       if (buffer.length()==0) {
65          int b = in.read();
66          if (b != -1) {
67             if (b == delim_char) {
68                StringWriter param = new StringWriter();
69                b = in.read();
70                if (b != delim_char) {
71                   while(b!=delim_char && b!=-1) {
72                      param.write(b);
73                      b = in.read();
74                   }
75                   buffer.append(params.get(param.toString()));
76                   b = buffer.charAt(0);
77                   buffer.deleteCharAt(0);
78                }
79             }
80          }
81          return b;
82       } else {
83          int b = buffer.charAt(0);
84          buffer.deleteCharAt(0);
85          return b;
86       }
87    }
88
89    public void close() throws IOException
90    {
91       in.close();
92    }
93 }
94
Popular Tags