| 
  ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| This report was generated with an evaluation server license. Purchase Clover or configure your license. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| PhotoDecorator | Line # 53 | 39 | 0% | 11 | 1 | 98,1% | 
0.9814815
 | 
                
                                    |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (6) | |||
| Result | |||
| 
            0.9444444
                         | 
        
            net.sourceforge.pebble.decorator.PhotoDecoratorTest.testEscapeTextInExcerpt
             
            net.sourceforge.pebble.decorator.PhotoDecoratorTest.testEscapeTextInExcerpt
         | 
        1 PASS | |
| 
            0.9444444
                         | 
        
            net.sourceforge.pebble.decorator.PhotoDecoratorTest.testEscapeTextInBody
             
            net.sourceforge.pebble.decorator.PhotoDecoratorTest.testEscapeTextInBody
         | 
        1 PASS | |
| 
            0.9444444
                         | 
        
            net.sourceforge.pebble.decorator.PhotoDecoratorTest.testEscapeTextInExcerpt
             
            net.sourceforge.pebble.decorator.PhotoDecoratorTest.testEscapeTextInExcerpt
         | 
        1 PASS | |
| 
            0.9444444
                         | 
        
            net.sourceforge.pebble.decorator.PhotoDecoratorTest.testEscapeTextInBody
             
            net.sourceforge.pebble.decorator.PhotoDecoratorTest.testEscapeTextInBody
         | 
        1 PASS | |
| 
            0.9259259
                         | 
        
            net.sourceforge.pebble.decorator.PhotoDecoratorTest.testEscapeTextInBodyOfStaticPage
             
            net.sourceforge.pebble.decorator.PhotoDecoratorTest.testEscapeTextInBodyOfStaticPage
         | 
        1 PASS | |
| 
            0.9259259
                         | 
        
            net.sourceforge.pebble.decorator.PhotoDecoratorTest.testEscapeTextInBodyOfStaticPage
             
            net.sourceforge.pebble.decorator.PhotoDecoratorTest.testEscapeTextInBodyOfStaticPage
         | 
        1 PASS | |
| 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 | */ | |
| 53 | public class PhotoDecorator extends ContentDecoratorSupport { | |
| 54 | ||
| 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 | 20 | 
         public void decorate(ContentDecoratorContext context, BlogEntry blogEntry) {     | 
  
| 67 | 20 | blogEntry.setBody(markup(blogEntry.getBody())); | 
| 68 | 20 | 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 | 10 | 
         public void decorate(ContentDecoratorContext context, StaticPage staticPage) {     | 
  
| 78 | 10 | staticPage.setBody(markup(staticPage.getBody())); | 
| 79 | } | |
| 80 | ||
| 81 | 50 | 
         private String markup(String content) {     | 
  
| 82 | // is there work to do? | |
| 83 | 50 | if (content == null || content.length() == 0) { | 
| 84 | 32 | 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 | 18 | Pattern p = Pattern.compile(PHOTOS_START_TAG + ".+?" + PHOTOS_END_TAG, | 
| 92 | Pattern.CASE_INSENSITIVE | Pattern.DOTALL); | |
| 93 | 18 | Matcher m = p.matcher(content); | 
| 94 | ||
| 95 | // while there are blocks to be escaped | |
| 96 | 30 | while (m.find()) { | 
| 97 | 12 | int start = m.start(); | 
| 98 | 12 | int end = m.end(); | 
| 99 | ||
| 100 | // grab the text, strip off the "photos" tags and transform it | |
| 101 | 12 | String textToMarkup = content.substring(start, end); | 
| 102 | 12 | textToMarkup = textToMarkup.substring(PHOTOS_START_TAG.length(), textToMarkup.length() - PHOTOS_END_TAG.length()); | 
| 103 | ||
| 104 | 12 | StringBuffer buf = new StringBuffer(); | 
| 105 | 12 | buf.append("<div class=\"photos\">\n"); | 
| 106 | ||
| 107 | 12 | try { | 
| 108 | 12 | BufferedReader reader = new BufferedReader(new StringReader(textToMarkup)); | 
| 109 | 12 | String line = reader.readLine(); | 
| 110 | 12 | buf.append("<div>\n"); | 
| 111 | 12 | boolean foundPhotos = false; | 
| 112 | 54 | while (line != null) { | 
| 113 | 42 | if (line.trim().equals("")) { | 
| 114 | 18 | if (foundPhotos) { | 
| 115 | 6 | buf.append("</div>\n"); | 
| 116 | 6 | buf.append("<div>\n"); | 
| 117 | } | |
| 118 | } else { | |
| 119 | 24 | String[] tokens = line.split("\\|"); | 
| 120 | 24 | buf.append("<img src=\""); | 
| 121 | 24 | buf.append(tokens[0]); | 
| 122 | 24 | buf.append("\" class=\"photo\" alt=\""); | 
| 123 | 24 | if (tokens.length == 2) { | 
| 124 | 18 | buf.append(tokens[1]); | 
| 125 | } | |
| 126 | 24 | buf.append("\" />\n"); | 
| 127 | 24 | foundPhotos = true; | 
| 128 | } | |
| 129 | ||
| 130 | 42 | line = reader.readLine(); | 
| 131 | } | |
| 132 | 12 | buf.append("</div>\n"); | 
| 133 | } catch (IOException ioe) { | |
| 134 | 0 | log.warn(ioe); | 
| 135 | } | |
| 136 | ||
| 137 | 12 | buf.append("</div>"); | 
| 138 | ||
| 139 | // now add it back into the original text | |
| 140 | 12 | content = content.substring(0, start) + buf.toString() + content.substring(end, content.length()); | 
| 141 | 12 | m = p.matcher(content); | 
| 142 | } | |
| 143 | ||
| 144 | 18 | return content; | 
| 145 | } | |
| 146 | ||
| 147 | } | |
  | 
||||||||||||