File Source: SmileysPlugin.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.entry;
    20  
    21  import java.util.Enumeration;
    22  import java.util.Map;
    23  import java.util.Properties;
    24  import java.util.regex.Matcher;
    25  import java.util.regex.Pattern;
    26  import org.apache.commons.lang.StringEscapeUtils;
    27  import org.apache.commons.logging.Log;
    28  import org.apache.commons.logging.LogFactory;
    29  import org.apache.roller.weblogger.WebloggerException;
    30  import org.apache.roller.weblogger.config.WebloggerRuntimeConfig;
    31  import org.apache.roller.weblogger.business.plugins.entry.WeblogEntryPlugin;
    32  import org.apache.roller.weblogger.pojos.WeblogEntry;
    33  import org.apache.roller.weblogger.pojos.Weblog;
    34  
    35  /**
    36   * Converts ascii emoticons into HTML image tags.
    37   */
    38  public class SmileysPlugin implements WeblogEntryPlugin {
    39      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.business.plugins.entry.SmileysPlugin__static_init
              * 
              *  Presumptions:
              *    org.apache.commons.logging.LogFactory:getLog(...)@40 != null
              * 
              *  Postconditions:
              *    escape_regex == &new char[](SmileysPlugin__static_init#4)
              *    imageTags == &new String[](SmileysPlugin__static_init#2)
              *    (soft) log != null
              *    smileyDefs == &new Properties(SmileysPlugin__static_init#3)
              *    smileyPatterns == &new Pattern[](SmileysPlugin__static_init#1)
              *    new Pattern[](SmileysPlugin__static_init#1) num objects == 1
              *    new Properties(SmileysPlugin__static_init#3) num objects == 1
              *    new String[](SmileysPlugin__static_init#2) num objects == 1
              *    new char[](SmileysPlugin__static_init#4) num objects == 1
              *    smileyPatterns.length == 0
              *    ...
              */
    40      private static Log log = LogFactory.getLog(SmileysPlugin.class);
    41      
    42      public static Pattern[] smileyPatterns = new Pattern[0]; // public for tests
    43      static String[] imageTags = new String[0];
    44      private static Properties smileyDefs = new Properties();
    45      
    46      private String name = "Emoticons";
    47      private String description = "Change ASCII emoticons to graphics.  " +
    48              ":-) becomes <img src='./images/smileys/smile.gif'>";
    49      
    50      
    51      static {
    52          try {
    53              smileyDefs.load(SmileysPlugin.class.getResourceAsStream("smileys.properties"));
    54          } catch (Exception e) {
    55              log.error("Unable to load smileys.properties", e);
    56          }
    57      }
    58  
    59      
             /* 
    P/P       *  Method: void org.apache.roller.weblogger.business.plugins.entry.SmileysPlugin()
              * 
              *  Preconditions:
              *    log != null
              * 
              *  Postconditions:
              *    this.description == &"Change ASCII emoticons to graphics.  :-) becomes <img src='..images.smileys.smile.gif'>"
              *    this.name == &"Emoticons"
              */
    60      public SmileysPlugin() {
    61          log.debug("SmileysPlugin instantiated.");
    62      }
    63      
    64      
    65      public String getName() {
                 /* 
    P/P           *  Method: String getName()
                  * 
                  *  Preconditions:
                  *    init'ed(this.name)
                  * 
                  *  Postconditions:
                  *    return_value == this.name
                  *    init'ed(return_value)
                  */
    66          return name;
    67      }
    68      
    69      
    70      public String getDescription() {
                 /* 
    P/P           *  Method: String getDescription()
                  * 
                  *  Preconditions:
                  *    init'ed(this.description)
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
    71          return StringEscapeUtils.escapeJavaScript(description);
    72      }
    73      
    74      
    75      /*
    76       * Convert the SmileyDefs into RegEx patterns and img tags for
    77       * later use.  Need an HttpServletRequest though so that we can
    78       * get the ServletContext Path.  But only do it once.
    79       */
    80      public synchronized void init(Weblog website) throws WebloggerException {
    81          // don't do this work if Smileys already loaded
                 /* 
    P/P           *  Method: void init(Weblog)
                  * 
                  *  Preconditions:
                  *    smileyPatterns != null
                  *    (soft) escape_regex != null
                  *    (soft) escape_regex.length <= 232-1
                  *    (soft) init'ed(escape_regex[...])
                  *    (soft) log != null
                  *    (soft) org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider != null
                  *    (soft) org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider.webloggerInstance != null
                  *    (soft) init'ed(org/apache/roller/weblogger/config/WebloggerRuntimeConfig.absoluteContextURL)
                  *    (soft) org/apache/roller/weblogger/config/WebloggerRuntimeConfig.log != null
                  *    (soft) smileyDefs != null
                  * 
                  *  Presumptions:
                  *    java.util.Enumeration:nextElement(...)@91 != null
                  *    java.util.Properties:propertyNames(...)@89 != null
                  *    java.util.Properties:size(...)@85 >= 1
                  *    java.util.Properties:size(...)@86 >= 1
                  * 
                  *  Postconditions:
                  *    imageTags == One-of{old imageTags, &new String[](init#2)}
                  *    init'ed(java.lang.StringBuilder:toString(...)._tainted)
                  *    smileyPatterns == One-of{old smileyPatterns, &new Pattern[](init#1)}
                  *    smileyPatterns != null
                  *    new Pattern[](init#1) num objects <= 1
                  *    (soft) new Pattern[](init#1).length in 1..232-1
                  *    init'ed(new Pattern[](init#1)[...])
                  *    new String[](init#2) num objects <= 1
                  *    (soft) new String[](init#2).length in 1..232-1
                  *    new String[](init#2)[...] == &java.lang.StringBuilder:toString(...)
                  * 
                  *  Test Vectors:
                  *    smileyPatterns.length: {1..+Inf}, {0}
                  *    java.util.Enumeration:hasMoreElements(...)@90: {0}, {1}
                  */
    82          if (SmileysPlugin.smileyPatterns.length < 1) {
    83              String baseURL = WebloggerRuntimeConfig.getAbsoluteContextURL();
    84              
    85              Pattern[] tempP = new Pattern[SmileysPlugin.smileyDefs.size()];
    86              String[] tempS = new String[SmileysPlugin.smileyDefs.size()];
    87              log.debug("# smileys: " + smileyDefs.size());
    88              int count = 0;
    89              Enumeration enum1 = SmileysPlugin.smileyDefs.propertyNames();
    90              while(enum1.hasMoreElements()) {
    91                  String smiley = (String)enum1.nextElement();
    92                  String smileyAlt = htmlEscape(smiley);
+   93                  tempP[count] = Pattern.compile(regexEscape(smiley));
+   94                  tempS[count] = "<img src=\"" +
    95                          baseURL + "/images/smileys/" +
    96                          smileyDefs.getProperty(smiley, "smile.gif") +
    97                          "\" class=\"smiley\"" +
    98                          " alt=\"" + smileyAlt + "\"" +
    99                          " title=\"" + smileyAlt +"\" />";
   100                  log.debug(smiley + "=" + tempS[count]);
   101                  count++;
   102              }
   103              SmileysPlugin.smileyPatterns = tempP;
   104              SmileysPlugin.imageTags = tempS;
   105          }
   106      }
   107      
   108      
   109      /**
   110       * Find occurences of ascii emoticons and turn them into HTML image pointers.
   111       */
   112      public String render(WeblogEntry entry, String text) {
                 /* 
    P/P           *  Method: String render(WeblogEntry, String)
                  * 
                  *  Preconditions:
                  *    smileyPatterns != null
                  *    smileyPatterns.length <= 232-1
                  *    (soft) imageTags != null
                  *    (soft) imageTags.length >= 1
                  *    (soft) smileyPatterns.length <= imageTags.length
                  *    (soft) init'ed(imageTags[...])
                  *    (soft) smileyPatterns[...] != null
                  * 
                  *  Presumptions:
                  *    java.util.regex.Pattern:matcher(...)@115 != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   113          Matcher matcher = null;
   114          for (int i=0; i<smileyPatterns.length; i++) {
   115              matcher = smileyPatterns[i].matcher(text);
   116              text = matcher.replaceAll(imageTags[i]);
   117          }
   118          return text;
   119      }
   120      
   121      
   122      /*
   123       * To display the smiley 'glyph' certain characters
   124       * must be HTML escaped.
   125       */
   126      private String htmlEscape(String smiley) {
                 /* 
    P/P           *  Method: String htmlEscape(String)
                  * 
                  *  Preconditions:
                  *    smiley != null
                  * 
                  *  Presumptions:
                  *    chars.length@127 <= 232-1
                  * 
                  *  Postconditions:
                  *    java.lang.StringBuffer:toString(...)._tainted == 0
                  *    return_value == &java.lang.StringBuffer:toString(...)
                  */
   127          char[] chars = smiley.toCharArray();
   128          StringBuffer buf = new StringBuffer();
   129          for (int i=0; i<chars.length; i++) {
+  130              if (chars[i] == '"') {
+  131                  buf.append(""");
+  132              } else if (chars[i] == '>') {
+  133                  buf.append(">");
+  134              } else if (chars[i] == '<') {
+  135                  buf.append("<");
   136              } else {
+  137                  buf.append(chars[i]);
   138              }
   139          }
   140          return buf.toString();
   141      }
   142      
   143      /**
   144       * Some characters have to escaped with a backslash before
   145       * being compiled into a Regular Expression.
   146       *
   147       * @param smiley
   148       * @return
   149       */
   150      private static char[] escape_regex = new char[]
   151      {'-', '(', ')', '\\', '|', ':', '^', '$', '*', '+', '?',
   152       '{', '}', '!', '=', '<', '>', '&', '[', ']' };
   153      
   154      private String regexEscape(String smiley) {
                 /* 
    P/P           *  Method: String regexEscape(String)
                  * 
                  *  Preconditions:
                  *    smiley != null
                  *    (soft) escape_regex != null
                  *    (soft) escape_regex.length <= 232-1
                  *    (soft) init'ed(escape_regex[...])
                  * 
                  *  Presumptions:
                  *    chars.length@155 <= 232-1
                  * 
                  *  Postconditions:
                  *    java.lang.StringBuffer:toString(...)._tainted == 0
                  *    return_value == &java.lang.StringBuffer:toString(...)
                  * 
                  *  Test Vectors:
                  *    escape_regex[...]: {1..216-1}, {0}
                  */
   155          char[] chars = smiley.toCharArray();
   156          StringBuffer buf = new StringBuffer();
   157          for (int i=0; i<chars.length; i++) {
   158              for (int x=0; x<escape_regex.length; x++) {
+  159                  if (escape_regex[x] == chars[i]) {
   160                      buf.append("\\");
   161                      break;
   162                  }
   163              }
+  164              buf.append(chars[i]);
   165          }
   166          return buf.toString();
   167      }
   168      
   169  }








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