Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package edu.mit.simile.butterfly;
  
  
  import org.slf4j.Logger;
This is a special PrintWriter that is capable of incrementally looking for strings of the form "[#name#]" and replace the module "name" with the URL where the named module is actually mounted. This allows modules to be agnostic to where their dependencies are mount, making it easier to reuse modules across different web applications. NOTE: great care has been taken in making sure that this writer is incremental, meaning that works by minimizing the amount of buffering that it needs to operate. This is because we don't want such rewriting to be an impact for the perceived performance of responses.
 
 public class LinkRewriter extends PrintWriter {
 
     private static final Logger _logger = LoggerFactory.getLogger("butterfly.link_rewriter");
     
     private ButterflyModule _module;
     private PrintWriter _writer;
     private char[] _baseURL;
 
     public LinkRewriter(PrintWriter writerButterflyModule module) {
         this(writermodulenull);
     }
     
     public LinkRewriter(PrintWriter writerButterflyModule moduleString baseURL) {
         super(writer);
          = writer;
          = module;
          = (baseURL == null) ? null : baseURL.toCharArray();
     }
     
     public enum State {
         START, OPENING, OPENED, CLOSING, CLOSED, SLASHED, UNSLASHED
     }   
 
     private State _state = .;
     private int _start = -1;
     private int _end = -1;
     private StringBuffer _buffer = new StringBuffer(128);
     
     @Override
     public void write(char[] bufint offint len) {
         .trace("write char[] {} {}"offlen);
         if (off >= buf.lengthreturn;
         len = Math.min(off + lenbuf.length) - off;
         for (int i = offi < off + leni++) {
             char c = buf[i];
             .trace("'{}'"c);
             
             if ( == .) {
                 if (c == '[') {
                     .trace("start -> opening");
                      = .;
                      = i;
                 }
             } else if ( == .) {
                 if (c == '#') {
                     .trace("opening -> opened");
                      = .;
                 } else {
                     .trace("opening -> start");
                      = .;
                 }
             } else if ( == .) {
                 if (c == '#') {
                     .trace("opened -> closing");
                      = .;
                 } else if (!Character.isJavaIdentifierPart(c)) {
                     .trace("opened -> start");
                      = .;
                 }
             } else if ( == .) {
                 if (c == ']') {
                     .trace("closing -> closed");
                      = .;
                 } else {
                     .trace("closing -> start");
                      = .;
                 }
             } else if ( == .) {
                  = i - 1;
                 if (c == '/') {
                     .trace("closed -> slashed");
                      = .;
                 } else {
                     .trace("closed -> unslashed");
                      = .;
                 }
                 break;
             }
         }
        
        if ( == .) {
            .trace("pass along");
            int l = .length();
            if (l > 0) {
                char[] b = new char[l];
                .getChars(0, lb, 0);
                .write(b, 0, l);
                .setLength(0);
            }
            .write(bufofflen); // just pass the data along, since there is nothing to rewrite
        } else if ( == . ||  == .) {
            boolean slashed = ( == .);
             = .;
            .trace("closed");
            if (.length() > 0) {
                int l = .length();
                .trace("with leftovers: {}"l);
                .append(buf,off,len);
                len = .length();
                buf = new char[len];
                off = 0;
                 += l;
                .getChars(0, lenbuf, 0);
                .setLength(0);
            }
            .trace("{} {}");
            String name = new String(buf + 2,  -  - 3);
            .trace("name: {}"name);
            ButterflyModule module = .getModule(name);
        	MountPoint mountPoint = (module == null) ? null : module.getMountPoint();
            if (mountPoint != null) {
                .trace("module found");
                .write(bufoff - off);
                if (this. != null.write(, 0, .);
                char[] mountPointChars = mountPoint.getMountPoint().toCharArray();
                .write(mountPointChars, 0, slashed ? mountPointChars.length - 1 : mountPointChars.length);
                write(buf+1, off + len -  - 1);
            } else {
                .info("module NOT found");
                .write(bufoff - off); // write the part processed so far
                write(buflen - ( - off)); // and continue processing recursively
            }
        } else { // in case we are in an intermediate state, we can only store and wait for more data
            if (.length() == 0) {
                 -= off// adjust the starting point (only the first time)  
            }
            .append(buf,off,len); 
            .trace("saved leftovers: {} {}".length(), );
        }
    }
    // ---- all the write methods converge to the one above -----
    
    @Override
    public void write(char[] buf) {
        .trace("write char[]");
        write(buf, 0, buf.length);
    }
    @Override
    public void write(String s) {
        .trace("write string");
        write(s.toCharArray(), 0, s.length());
    }
    @Override
    public void write(String sint offint len) {
        .trace("write string off len");
        write(s.toCharArray(), offlen);
    }
    
    @Override
    public void write(int c) {
        .trace("write int");
        write(new char[] { (charc }, 0, 1);
    }
New to GrepCode? Check out our FAQ X