File Source: LinkMarkupPlugin.java

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   *  contributor license agreements.  The ASF licenses this file to You
     4   * under the Apache License, Version 2.0 (the "License"); you may not
     5   * use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.  For additional information regarding
    15   * copyright in this work, please see the NOTICE file in the top level
    16   * directory of this distribution.
    17   */
    18  
    19  package org.apache.roller.weblogger.business.plugins.comment;
    20  
    21  import java.util.regex.Matcher;
    22  import java.util.regex.Pattern;
    23  
    24  import org.apache.roller.weblogger.pojos.WeblogEntryComment;
    25  import org.apache.roller.weblogger.util.Utilities;
    26  
    27  
    28  /**
    29   * Comment plugin which turns plain text URLs into hyperlinks using
    30   * the html anchor (<a>) tag.
    31   * 
    32   * Contributed by Matthew Montgomery.
    33   */
         /* 
    P/P   *  Method: void org.apache.roller.weblogger.business.plugins.comment.LinkMarkupPlugin()
          */
    34  public class LinkMarkupPlugin implements WeblogEntryCommentPlugin {
    35  
             /* 
    P/P       *  Method: org.apache.roller.weblogger.business.plugins.comment.LinkMarkupPlugin__static_init
              * 
              *  Postconditions:
              *    init'ed(pattern)
              */
    36      private static final Pattern pattern = Pattern.compile(
    37              "http[s]?://[^/][\\S]+", Pattern.CASE_INSENSITIVE);
    38      
    39      
    40      /**
    41       * Unique identifier.  This should never change. 
    42       */
    43      public String getId() {
                 /* 
    P/P           *  Method: String getId()
                  * 
                  *  Postconditions:
                  *    return_value == &"LinkMarkup"
                  */
    44          return "LinkMarkup";
    45      }
    46      
    47      
    48      /** Returns the display name of this Plugin. */
    49      public String getName() {
                 /* 
    P/P           *  Method: String getName()
                  * 
                  *  Postconditions:
                  *    return_value == &"Link Markup"
                  */
    50          return "Link Markup";
    51      }
    52      
    53      
    54      /** Briefly describes the function of the Plugin.  May contain HTML. */
    55      public String getDescription() {
                 /* 
    P/P           *  Method: String getDescription()
                  * 
                  *  Postconditions:
                  *    return_value == &"Automatically creates hyperlinks out of embedded URLs."
                  */
    56          return "Automatically creates hyperlinks out of embedded URLs.";
    57      }
    58      
    59      
    60      /**
    61       * Apply plugin to the specified text.
    62       *
    63       * @param comment The comment being rendered.
    64       * @param text String to which plugin should be applied.
    65       *
    66       * @return Results of applying plugin to string.
    67       */
    68      public String render(final WeblogEntryComment comment, String text) {
    69          StringBuilder result;
                 /* 
    P/P           *  Method: String render(WeblogEntryComment, String)
                  * 
                  *  Presumptions:
                  *    java.util.regex.Pattern:compile(...)@36 != null
                  *    java.util.regex.Pattern:matcher(...)@74 != null
                  * 
                  *  Postconditions:
                  *    init'ed(java.lang.StringBuilder:toString(...)._tainted)
                  *    return_value == &java.lang.StringBuilder:toString(...)
                  * 
                  *  Test Vectors:
                  *    text: Addr_Set{null}, Inverse{null}
                  *    java.util.regex.Matcher:find(...)@80: {0}, {1}
                  */
    70          result = new StringBuilder();
    71          
    72          if (text != null) {
    73              Matcher matcher;
    74              matcher = pattern.matcher(text);
    75  
    76              int start = 0;
    77              int end = text.length();
    78              
    79              while (start < end) {
    80                  if (matcher.find()) {
    81                      // Copy up to the match
    82                      result.append(text.substring(start, (matcher.start())));
    83  
    84                      // Copy the URL and create the hyperlink
    85                      // Unescape HTML as we don't know if that setting is on
    86                      String url;
    87                      url = Utilities.unescapeHTML(text.substring(
    88                              matcher.start(), matcher.end()));
    89  
    90                      // Build the anchor tag and escape HTML in the URL output
    91                      result.append("<a href=\"");
    92                      result.append(Utilities.escapeHTML(url));
    93                      result.append("\">");
    94                      result.append(Utilities.escapeHTML(url));
    95                      result.append("</a>");
    96  
    97                      // Increment the starting index
    98                      start = matcher.end();
    99                  }
   100                  else {
   101                      // Copy the remainder
   102                      result.append(text.substring(start, end));
   103  
   104                      // Increment the starting index to exit the loop
   105                      start = end;
   106                  }
   107              }
   108          }
   109          else {
   110              result.append(text);    
   111          }
   112  
   113          return result.toString();
   114      }
   115      
   116  }








SofCheck Inspector Build Version : 2.18479
LinkMarkupPlugin.java 2009-Jan-02 14:24:58
LinkMarkupPlugin.class 2009-Sep-04 03:12:31