Coverage Report - de.smartics.properties.api.core.util.JavadocCommentHelper
 
Classes in this File Line Coverage Branch Coverage Complexity
JavadocCommentHelper
0%
0/27
0%
0/10
2,5
 
 1  
 /*
 2  
  * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not 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.
 15  
  */
 16  
 package de.smartics.properties.api.core.util;
 17  
 
 18  
 import java.util.regex.Matcher;
 19  
 import java.util.regex.Pattern;
 20  
 
 21  
 import org.apache.commons.lang.StringUtils;
 22  
 
 23  
 /**
 24  
  * Utilities to normalize Javadoc comments.
 25  
  */
 26  
 public final class JavadocCommentHelper
 27  
 {
 28  
   // ********************************* Fields *********************************
 29  
 
 30  
   // --- constants ------------------------------------------------------------
 31  
 
 32  
   /**
 33  
    * The pattern matches inline Javadoc tags. In group 1 is the text that is to
 34  
    * be used and surrounded with {@link #prefix1} and {@link #suffix1}, if there
 35  
    * is no match for group 2. If group 2 is present, the text is to be used
 36  
    * without prefix/suffix 2.
 37  
    */
 38  0
   private static final Pattern INLINE_PATTERN = Pattern.compile(
 39  
       "\\{@[\\w]+\\s*([#\\.\\w]+(\\([^)]*\\))?)\\s*([^}]+)?\\}", Pattern.MULTILINE);
 40  
 
 41  
   // --- members --------------------------------------------------------------
 42  
 
 43  
   /**
 44  
    * The prefix for group 1 match.
 45  
    */
 46  
   private final String prefix1;
 47  
 
 48  
   /**
 49  
    * The suffix for group 1 match.
 50  
    */
 51  
   private final String suffix1;
 52  
 
 53  
   /**
 54  
    * The prefix for group 2 match.
 55  
    */
 56  
   private final String prefix2;
 57  
 
 58  
   /**
 59  
    * The suffix for group 2 match.
 60  
    */
 61  
   private final String suffix2;
 62  
 
 63  
   // ****************************** Initializer *******************************
 64  
 
 65  
   // ****************************** Constructors ******************************
 66  
 
 67  
   /**
 68  
    * Default constructor.
 69  
    *
 70  
    * @param prefix1 the prefix for group 1 match.
 71  
    * @param suffix1 the suffix for group 1 match.
 72  
    * @param prefix2 the prefix for group 2 match.
 73  
    * @param suffix2 the suffix for group 2 match.
 74  
    */
 75  
   public JavadocCommentHelper(final String prefix1, final String suffix1,
 76  
       final String prefix2, final String suffix2)
 77  0
   {
 78  0
     this.prefix1 = prefix1;
 79  0
     this.suffix1 = suffix1;
 80  0
     this.prefix2 = prefix2;
 81  0
     this.suffix2 = suffix2;
 82  0
   }
 83  
 
 84  
   // ****************************** Inner Classes *****************************
 85  
 
 86  
   // ********************************* Methods ********************************
 87  
 
 88  
   // --- init -----------------------------------------------------------------
 89  
 
 90  
   // --- create ---------------------------------------------------------------
 91  
 
 92  
   /**
 93  
    * Creates a default helper for normalizing in an HTML context.
 94  
    *
 95  
    * @return the HTML version of the helper.
 96  
    */
 97  
   public static JavadocCommentHelper createHtml()
 98  
   {
 99  0
     return new JavadocCommentHelper("<tt>", "</tt>", "<i>", "</i>");
 100  
   }
 101  
 
 102  
   /**
 103  
    * Creates a default helper for normalizing in text context.
 104  
    *
 105  
    * @return the text version of the helper.
 106  
    */
 107  
   public static JavadocCommentHelper createText()
 108  
   {
 109  0
     return new JavadocCommentHelper("'", "'", "", "");
 110  
   }
 111  
 
 112  
   // --- get&set --------------------------------------------------------------
 113  
 
 114  
   // --- business -------------------------------------------------------------
 115  
 
 116  
   /**
 117  
    * Replaces Javadoc inline tags.
 118  
    *
 119  
    * @param fragment the fragment with inline tags.
 120  
    * @return the normalized fragment.
 121  
    */
 122  
   public String replaceJavadocInlines(final String fragment)
 123  
   {
 124  0
     if (StringUtils.isBlank(fragment))
 125  
     {
 126  0
       return fragment;
 127  
     }
 128  
 
 129  0
     final StringBuffer buffer = new StringBuffer(fragment.length());
 130  0
     final Matcher matcher = INLINE_PATTERN.matcher(fragment);
 131  
 
 132  0
     while (matcher.find())
 133  
     {
 134  0
       final String group2 = matcher.group(3);
 135  0
       if (group2 != null)
 136  
       {
 137  0
         final String replacement = prefix2 + group2 + suffix2;
 138  0
         matcher.appendReplacement(buffer, replacement);
 139  0
       }
 140  
       else
 141  
       {
 142  0
         String group1 = matcher.group(1);
 143  0
         if (group1.length() > 1 && group1.charAt(0) == '#')
 144  
         {
 145  0
           group1 = group1.substring(1);
 146  
         }
 147  0
         final String replacement = prefix1 + group1 + suffix1;
 148  0
         matcher.appendReplacement(buffer, replacement);
 149  
       }
 150  0
     }
 151  0
     matcher.appendTail(buffer);
 152  
 
 153  0
     return buffer.toString();
 154  
   }
 155  
 
 156  
   // --- object basics --------------------------------------------------------
 157  
 
 158  
 }