File Source: photodecorator.java

     1  /*
     2   * Copyright (c) 2003-2006, Simon Brown
     3   * All rights reserved.
     4   *
     5   * Redistribution and use in source and binary forms, with or without
     6   * modification, are permitted provided that the following conditions are met:
     7   *
     8   *   - Redistributions of source code must retain the above copyright
     9   *     notice, this list of conditions and the following disclaimer.
    10   *
    11   *   - Redistributions in binary form must reproduce the above copyright
    12   *     notice, this list of conditions and the following disclaimer in
    13   *     the documentation and/or other materials provided with the
    14   *     distribution.
    15   *
    16   *   - Neither the name of Pebble nor the names of its contributors may
    17   *     be used to endorse or promote products derived from this software
    18   *     without specific prior written permission.
    19   *
    20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    30   * POSSIBILITY OF SUCH DAMAGE.
    31   */
    32  package net.sourceforge.pebble.decorator;
    33  
    34  import net.sourceforge.pebble.api.decorator.ContentDecoratorContext;
    35  import net.sourceforge.pebble.domain.BlogEntry;
    36  import net.sourceforge.pebble.domain.StaticPage;
    37  import net.sourceforge.pebble.util.StringUtils;
    38  
    39  import java.util.regex.Pattern;
    40  import java.util.regex.Matcher;
    41  import java.io.BufferedReader;
    42  import java.io.StringReader;
    43  import java.io.IOException;
    44  
    45  import org.apache.commons.logging.Log;
    46  import org.apache.commons.logging.LogFactory;
    47  
    48  /**
    49   * Takes a simple description of photos and generates boilerplate markup.
    50   *
    51   * @author Simon Brown
    52   */
         /* 
    P/P   *  Method: void net.sourceforge.pebble.decorator.PhotoDecorator()
          */
    53  public class PhotoDecorator extends ContentDecoratorSupport {
    54  
           /* 
    P/P     *  Method: net.sourceforge.pebble.decorator.PhotoDecorator__static_init
            * 
            *  Postconditions:
            *    init'ed(log)
            */
    55    private static final Log log = LogFactory.getLog(PhotoDecorator.class);
    56  
    57    private static final String PHOTOS_START_TAG = "<photos>";
    58    private static final String PHOTOS_END_TAG = "</photos>";
    59  
    60    /**
    61     * Decorates the specified blog entry.
    62     *
    63     * @param context   the context in which the decoration is running
    64     * @param blogEntry the blog entry to be decorated
    65     */
    66    public void decorate(ContentDecoratorContext context, BlogEntry blogEntry) {
             /* 
    P/P       *  Method: void decorate(ContentDecoratorContext, BlogEntry)
              * 
              *  Preconditions:
              *    blogEntry != null
              *    blogEntry.propertyChangeSupport != null
              *    init'ed(blogEntry.body)
              *    init'ed(blogEntry.excerpt)
              * 
              *  Postconditions:
              *    blogEntry.body != null
              *    init'ed(blogEntry.excerpt)
              */
    67      blogEntry.setBody(markup(blogEntry.getBody()));
    68      blogEntry.setExcerpt(markup(blogEntry.getExcerpt()));
    69    }
    70  
    71    /**
    72     * Decorates the specified static page.
    73     *
    74     * @param context    the context in which the decoration is running
    75     * @param staticPage the static page to be decorated
    76     */
    77    public void decorate(ContentDecoratorContext context, StaticPage staticPage) {
             /* 
    P/P       *  Method: void decorate(ContentDecoratorContext, StaticPage)
              * 
              *  Preconditions:
              *    staticPage != null
              *    init'ed(staticPage.body)
              *    staticPage.propertyChangeSupport != null
              * 
              *  Postconditions:
              *    staticPage.body != null
              */
    78      staticPage.setBody(markup(staticPage.getBody()));
    79    }
    80  
    81    private String markup(String content) {
    82      // is there work to do?
             /* 
    P/P       *  Method: String markup(String)
              * 
              *  Presumptions:
              *    java.lang.String:length(...)@102 - java.lang.String:length(...)@102 in -232+1..231
              *    java.util.regex.Pattern:compile(...)@91 != null
              *    java.util.regex.Pattern:matcher(...)@93 != null
              *    org.apache.commons.logging.LogFactory:getLog(...)@55 != null
              *    tokens.length@119 >= 1
              * 
              *  Postconditions:
              *    return_value != null
              * 
              *  Test Vectors:
              *    content: Addr_Set{null}, Inverse{null}
              *    java.lang.String:equals(...)@113: {0}, {1}
              *    java.lang.String:length(...)@83: {1..232-1}, {0}
              *    java.util.regex.Matcher:find(...)@96: {1}, {0}
              *    tokens.length@119: {1, 3..+Inf}, {2}
              */
    83      if (content == null || content.length() == 0) {
    84        return "";
    85      }
    86  
    87      // this pattern says "take the shortest match you can find where there are
    88      // one or more characters between escape tags"
    89      //  - the match is case insensitive and DOTALL means that newlines are
    90      //  - considered as a character match
    91      Pattern p = Pattern.compile(PHOTOS_START_TAG + ".+?" + PHOTOS_END_TAG,
    92          Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
    93      Matcher m = p.matcher(content);
    94  
    95      // while there are blocks to be escaped
    96      while (m.find()) {
    97        int start = m.start();
    98        int end = m.end();
    99  
   100        // grab the text, strip off the "photos" tags and transform it
   101        String textToMarkup = content.substring(start, end);
   102        textToMarkup = textToMarkup.substring(PHOTOS_START_TAG.length(), textToMarkup.length() - PHOTOS_END_TAG.length());
   103  
   104        StringBuffer buf = new StringBuffer();
   105        buf.append("<div class=\"photos\">\n");
   106  
   107        try {
   108          BufferedReader reader = new BufferedReader(new StringReader(textToMarkup));
   109          String line = reader.readLine();
   110          buf.append("<div>\n");
   111          boolean foundPhotos = false;
   112          while (line != null) {
   113            if (line.trim().equals("")) {
   114              if (foundPhotos) {
   115                buf.append("</div>\n");
   116                buf.append("<div>\n");
   117              }
   118            } else {
   119              String[] tokens = line.split("\\|");
   120              buf.append("<img src=\"");
   121              buf.append(tokens[0]);
   122              buf.append("\" class=\"photo\" alt=\"");
   123              if (tokens.length == 2) {
   124                buf.append(tokens[1]);
   125              }
   126              buf.append("\" />\n");
   127              foundPhotos = true;
   128            }
   129  
   130            line = reader.readLine();
   131          }
   132          buf.append("</div>\n");
   133        } catch (IOException ioe) {
   134          log.warn(ioe);
   135        }
   136  
   137        buf.append("</div>");
   138  
   139        // now add it back into the original text
   140        content = content.substring(0, start) + buf.toString() + content.substring(end, content.length());
   141        m = p.matcher(content);
   142      }
   143  
   144      return content;
   145    }
   146  
   147  }








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