Save This Page
Home » sitemesh-2.3 » com.opensymphony.module » sitemesh » filter » [javadoc | source]
    1   /* This software is published under the terms of the OpenSymphony Software
    2    * License version 1.1, of which a copy has been included with this
    3    * distribution in the LICENSE.txt file. */
    4   package com.opensymphony.module.sitemesh.filter;
    5   
    6   import java.io.PrintWriter;
    7   import java.io.IOException;
    8   import java.io.Writer;
    9   
   10   /**
   11    * Provides a PrintWriter that routes through to another PrintWriter, however the destination
   12    * can be changed at any point. The destination can be passed in using a factory, so it will not be created
   13    * until it's actually needed.
   14    *
   15    * @author Joe Walnes
   16    * @version $Revision: 1.1 $
   17    */
   18   public class RoutablePrintWriter extends PrintWriter {
   19   
   20       private PrintWriter destination;
   21       private DestinationFactory factory;
   22   
   23       /**
   24        * Factory to lazily instantiate the destination.
   25        */
   26       public static interface DestinationFactory {
   27           PrintWriter activateDestination() throws IOException;
   28       }
   29   
   30       public RoutablePrintWriter(DestinationFactory factory) {
   31           super(new NullWriter());
   32           this.factory = factory;
   33       }
   34   
   35       private PrintWriter getDestination() {
   36           if (destination == null) {
   37               try {
   38                   destination = factory.activateDestination();
   39               } catch (IOException e) {
   40                   setError();
   41               }
   42           }
   43           return destination;
   44       }
   45   
   46       public void updateDestination(DestinationFactory factory) {
   47           destination = null;
   48           this.factory = factory;
   49       }
   50   
   51       public void close() {
   52           getDestination().close();
   53       }
   54   
   55       public void println(Object x) {
   56           getDestination().println(x);
   57       }
   58   
   59       public void println(String x) {
   60           getDestination().println(x);
   61       }
   62   
   63       public void println(char x[]) {
   64           getDestination().println(x);
   65       }
   66   
   67       public void println(double x) {
   68           getDestination().println(x);
   69       }
   70   
   71       public void println(float x) {
   72           getDestination().println(x);
   73       }
   74   
   75       public void println(long x) {
   76           getDestination().println(x);
   77       }
   78   
   79       public void println(int x) {
   80           getDestination().println(x);
   81       }
   82   
   83       public void println(char x) {
   84           getDestination().println(x);
   85       }
   86   
   87       public void println(boolean x) {
   88           getDestination().println(x);
   89       }
   90   
   91       public void println() {
   92           getDestination().println();
   93       }
   94   
   95       public void print(Object obj) {
   96           getDestination().print(obj);
   97       }
   98   
   99       public void print(String s) {
  100           getDestination().print(s);
  101       }
  102   
  103       public void print(char s[]) {
  104           getDestination().print(s);
  105       }
  106   
  107       public void print(double d) {
  108           getDestination().print(d);
  109       }
  110   
  111       public void print(float f) {
  112           getDestination().print(f);
  113       }
  114   
  115       public void print(long l) {
  116           getDestination().print(l);
  117       }
  118   
  119       public void print(int i) {
  120           getDestination().print(i);
  121       }
  122   
  123       public void print(char c) {
  124           getDestination().print(c);
  125       }
  126   
  127       public void print(boolean b) {
  128           getDestination().print(b);
  129       }
  130   
  131       public void write(String s) {
  132           getDestination().write(s);
  133       }
  134   
  135       public void write(String s, int off, int len) {
  136           getDestination().write(s, off, len);
  137       }
  138   
  139       public void write(char buf[]) {
  140           getDestination().write(buf);
  141       }
  142   
  143       public void write(char buf[], int off, int len) {
  144           getDestination().write(buf, off, len);
  145       }
  146   
  147       public void write(int c) {
  148           getDestination().write(c);
  149       }
  150   
  151       public boolean checkError() {
  152           return getDestination().checkError();
  153       }
  154   
  155       public void flush() {
  156           getDestination().flush();
  157       }
  158   
  159       /**
  160        * Just to keep super constructor for PrintWriter happy - it's never actually used.
  161        */
  162       private static class NullWriter extends Writer {
  163   
  164           protected NullWriter() {
  165               super();
  166           }
  167   
  168           public void write(char cbuf[], int off, int len) throws IOException {
  169               throw new UnsupportedOperationException();
  170           }
  171   
  172           public void flush() throws IOException {
  173               throw new UnsupportedOperationException();
  174           }
  175   
  176           public void close() throws IOException {
  177               throw new UnsupportedOperationException();
  178           }
  179   
  180       }
  181   
  182   }

Save This Page
Home » sitemesh-2.3 » com.opensymphony.module » sitemesh » filter » [javadoc | source]