File Source: relativeuridecorator.java

         /* 
    P/P   *  Method: net.sourceforge.pebble.decorator.RelativeUriDecorator__static_init
          */
     1  package net.sourceforge.pebble.decorator;
     2  
     3  import net.sourceforge.pebble.domain.Attachment;
     4  import net.sourceforge.pebble.domain.BlogEntry;
     5  import net.sourceforge.pebble.domain.StaticPage;
     6  import net.sourceforge.pebble.api.decorator.ContentDecoratorContext;
     7  
     8  /**
     9   * Translates relative URIs in blog entries and static pages into absolute URLs.
    10   * 
    11   * @author Simon Brown
    12   */
         /* 
    P/P   *  Method: void net.sourceforge.pebble.decorator.RelativeUriDecorator()
          */
    13  public class RelativeUriDecorator extends ContentDecoratorSupport {
    14  
    15    /**
    16     * Decorates the specified blog entry.
    17     *
    18     * @param context   the context in which the decoration is running
    19     * @param blogEntry the blog entry to be decorated
    20     */
    21    public void decorate(ContentDecoratorContext context, BlogEntry blogEntry) {
             /* 
    P/P       *  Method: void decorate(ContentDecoratorContext, BlogEntry)
              * 
              *  Preconditions:
              *    blogEntry != null
              *    this.blog != null
              * 
              *  Test Vectors:
              *    java.lang.String:startsWith(...)@28: {0}, {1}
              * 
              *  Preconditions:
              *    init'ed(blogEntry.attachment)
              *    blogEntry.propertyChangeSupport != null
              *    blogEntry.body != null
              *    blogEntry.excerpt != null
              *    (soft) blogEntry.attachment.url != null
              * 
              *  Postconditions:
              *    blogEntry.attachment.url != null
              *    blogEntry.body != null
              *    init'ed(blogEntry.excerpt)
              * 
              *  Test Vectors:
              *    blogEntry.attachment: Addr_Set{null}, Inverse{null}
              * 
              *  Preconditions:
              *    (soft) net/sourceforge/pebble/domain/BlogManager.instance != null
              *    (soft) init'ed(net/sourceforge/pebble/domain/BlogManager.instance.multiBlog)
              *    (soft) init'ed(this.blog.id)
              */
    22      blogEntry.setBody(replaceCommonUris(blogEntry.getBody()));
    23      blogEntry.setExcerpt(replaceCommonUris(blogEntry.getExcerpt()));
    24  
    25      Attachment attachment = blogEntry.getAttachment();
    26      if (attachment != null) {
    27        String attachmentUrl = attachment.getUrl();
    28        if (attachmentUrl.startsWith("./")) {
    29          attachment.setUrl(getBlog().getUrl() + attachmentUrl.substring(2));
    30        }
    31      }
    32    }
    33  
    34    /**
    35     * Decorates the specified static page.
    36     *
    37     * @param context    the context in which the decoration is running
    38     * @param staticPage the static page to be decorated
    39     */
    40    public void decorate(ContentDecoratorContext context, StaticPage staticPage) {
             /* 
    P/P       *  Method: void decorate(ContentDecoratorContext, StaticPage)
              * 
              *  Preconditions:
              *    staticPage != null
              *    this.blog != null
              *    staticPage.body != null
              *    staticPage.propertyChangeSupport != null
              * 
              *  Postconditions:
              *    staticPage.body != null
              * 
              *  Preconditions:
              *    (soft) net/sourceforge/pebble/domain/BlogManager.instance != null
              *    (soft) init'ed(net/sourceforge/pebble/domain/BlogManager.instance.multiBlog)
              *    (soft) init'ed(this.blog.id)
              */
    41      staticPage.setBody(replaceCommonUris(staticPage.getBody()));
    42    }
    43  
    44    /**
    45     * Helper method to replace common relative URIs with their absolute values.
    46     *
    47     * @param s   the String containing relative URIs
    48     * @return    a new String containing absolute URLs
    49     */
    50    private String replaceCommonUris(String s) {
             /* 
    P/P       *  Method: String replaceCommonUris(String)
              * 
              *  Preconditions:
              *    s != null
              *    this.blog != null
              * 
              *  Postconditions:
              *    return_value != null
              * 
              *  Preconditions:
              *    (soft) net/sourceforge/pebble/domain/BlogManager.instance != null
              *    (soft) init'ed(net/sourceforge/pebble/domain/BlogManager.instance.multiBlog)
              *    (soft) init'ed(this.blog.id)
              */
    51      s = s.replaceAll("href=\"\\./", "href=\"" + getBlog().getUrl());
    52      s = s.replaceAll("href='\\./", "href='" + getBlog().getUrl());
    53      s = s.replaceAll("src=\"\\./", "src=\"" + getBlog().getUrl());
    54      s = s.replaceAll("src='\\./", "src='" + getBlog().getUrl());
    55      return s;
    56    }
    57  
    58  }








SofCheck Inspector Build Version : 2.22510
relativeuridecorator.java 2010-Jun-25 19:40:32
relativeuridecorator.class 2010-Jul-19 20:23:40