001    /*
002      Copyright (C) 2001-2002 Laurent Martelli
003      
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful, but
010      WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012      Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public
015      License along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017      USA */
018    
019    package org.objectweb.jac.aspects.gui;
020    
021    import java.io.*;
022    import java.util.Map;
023    
024    /**
025     * This is unused class to create printable templates.
026     *
027     * <p>Template Reader : a stream reader for Template
028     * Substitute %param% with the value of template.getParams().get(param)
029     **/
030    
031    public class TemplateReader extends Reader {
032    
033       Map params;
034       StringBuffer buffer;
035       Reader in;
036    
037       /* The char used to delimitate params */
038       int delim_char = '%';
039    
040       public TemplateReader(Reader in, Map params) 
041       {
042          this.in = in;
043          this.params = params;
044          this.buffer = new StringBuffer();
045       }
046    
047       public int read(char[] b, int off, int len) throws IOException
048       {
049          /* copied from gnu/regexp/REFilterReader.java */
050          int i;
051          int ok = 0;
052          while (len-- > 0) {
053             i = read();
054             if (i == -1) return (ok == 0) ? -1 : ok;
055             b[off++] = (char) i;
056             ok++;
057          }
058          return ok;
059       }
060       
061       /* Read a single char */
062       public int read() throws IOException 
063       {
064          if (buffer.length()==0) {
065             int b = in.read();
066             if (b != -1) {
067                if (b == delim_char) {
068                   StringWriter param = new StringWriter();
069                   b = in.read();
070                   if (b != delim_char) {
071                      while(b!=delim_char && b!=-1) {
072                         param.write(b);
073                         b = in.read();
074                      }
075                      buffer.append(params.get(param.toString()));
076                      b = buffer.charAt(0);
077                      buffer.deleteCharAt(0);
078                   }
079                }
080             } 
081             return b;
082          } else {
083             int b = buffer.charAt(0);
084             buffer.deleteCharAt(0);
085             return b;
086          }
087       }
088    
089       public void close() throws IOException 
090       {
091          in.close();
092       }
093    }