Coverage Report - com.sdmetrics.metrics.Description - www.sdmetrics.com
 
Classes in this File Line Coverage Branch Coverage Complexity
Description
100%
69/69
97%
33/34
3,571
 
 1  
 /*
 2  
  * SDMetrics Open Core for UML design measurement
 3  
  * Copyright (c) Juergen Wuest
 4  
  * To contact the author, see <http://www.sdmetrics.com/Contact.html>.
 5  
  * 
 6  
  * This file is part of the SDMetrics Open Core.
 7  
  * 
 8  
  * SDMetrics Open Core is free software: you can redistribute it and/or modify
 9  
  * it under the terms of the GNU Affero General Public License as
 10  
  * published by the Free Software Foundation, either version 3 of the
 11  
  * License, or (at your option) any later version.
 12  
     
 13  
  * SDMetrics Open Core is distributed in the hope that it will be useful,
 14  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16  
  * GNU Affero General Public License for more details.
 17  
  *
 18  
  * You should have received a copy of the GNU Affero General Public License
 19  
  * along with SDMetrics Open Core.  If not, see <http://www.gnu.org/licenses/>.
 20  
  *
 21  
  */
 22  
 package com.sdmetrics.metrics;
 23  
 
 24  
 /**
 25  
  * Represents the description of an entry in the metric definition file.
 26  
  * <p>
 27  
  * Similar to Java docs, the first sentence of the description provides a brief
 28  
  * description of the entry. This brief description is usually terminated by a
 29  
  * period. Also like Java docs, descriptions contain HTML markup for formatting
 30  
  * of paragraphs, bulleted lists etc.
 31  
  */
 32  17956
 class Description {
 33  
 
 34  
         /** Builder for the raw description string during XML parsing. */
 35  8978
         private StringBuilder text = new StringBuilder();
 36  
         /** The final description as HTML fragment. */
 37  8978
         private String description = null;
 38  
 
 39  
         /**
 40  
          * The separator string that indicates the end of the brief description.
 41  
          */
 42  8978
         private String descriptionSeparator = ".";
 43  
 
 44  
         /**
 45  
          * Appends text to the description.
 46  
          * 
 47  
          * @param str Character array that contains the text
 48  
          * @param offset Index of the first character to use
 49  
          * @param len length of the text to use
 50  
          */
 51  
         void add(char[] str, int offset, int len) {
 52  1228
                 int startIndex = offset;
 53  1228
                 int charsToCopy = len;
 54  1228
                 if (text.length() == 0) {
 55  
                         // First text fragment, skip any leading whitespace
 56  2027
                         while (charsToCopy > 0 && Character.isWhitespace(str[startIndex])) {
 57  43
                                 startIndex++;
 58  43
                                 charsToCopy--;
 59  
                         }
 60  
                 }
 61  1228
                 if (charsToCopy > 0)
 62  1226
                         text.append(str, startIndex, charsToCopy);
 63  1228
         }
 64  
 
 65  
         /**
 66  
          * Sets the separator string to indicate the end of the brief description.
 67  
          * By default, this is the period (".")
 68  
          * 
 69  
          * @param sep The new separator string.
 70  
          */
 71  
         void setSeparator(String sep) {
 72  69
                 descriptionSeparator = sep;
 73  69
         }
 74  
 
 75  
         /**
 76  
          * Obtains the short version of the description. This is the description
 77  
          * text up to the first occurrence of the separator string, or the entire
 78  
          * description text if the text does not contain the separator string.
 79  
          * 
 80  
          * @return Description summary.
 81  
          */
 82  
         String getBriefDescription() {
 83  15
                 getDescription();
 84  15
                 int firstSeparatorIndex = description.indexOf(descriptionSeparator, 0);
 85  15
                 if (firstSeparatorIndex < 0) {
 86  2
                         return description;
 87  
                 }
 88  26
                 return description.substring(0, firstSeparatorIndex
 89  13
                                 + descriptionSeparator.length());
 90  
         }
 91  
 
 92  
         /**
 93  
          * Obtains the full description text. This includes the brief description
 94  
          * and all additional description that follows.
 95  
          * 
 96  
          * @return the full description text
 97  
          */
 98  
         String getDescription() {
 99  27
                 if (description == null) {
 100  15
                         processHTML(text);
 101  15
                         description = text.toString();
 102  15
                         text = null; // string buffer no longer needed.
 103  
                 }
 104  27
                 return description;
 105  
         }
 106  
 
 107  
         /** Fixes the HTML tags and locator references in the description text. */
 108  
         private static void processHTML(StringBuilder buf) {
 109  
                 // replace (( with < and )) with >, and newlines with spaces
 110  15
                 replaceAll(buf, "((", "<");
 111  15
                 replaceAll(buf, "))", ">");
 112  15
                 replaceAll(buf, "\n", " ");
 113  
 
 114  
                 // turn metric locators etc. into hyperlinks
 115  90
                 for (DescriptionLocator loc : DescriptionLocator.values())
 116  75
                         replaceLinks(buf, loc);
 117  15
         }
 118  
 
 119  
         /**
 120  
          * Replaces all occurrences of a string in a string builder.
 121  
          * 
 122  
          * @param buf Text to operate on.
 123  
          * @param what The string in the buffer to be replaced.
 124  
          * @param with The replacement string.
 125  
          */
 126  
         private static void replaceAll(StringBuilder buf, String what, String with) {
 127  45
                 int index = 0;
 128  125
                 while ((index = buf.indexOf(what, index)) >= 0) {
 129  35
                         buf.replace(index, index + what.length(), with);
 130  35
                         index += with.length();
 131  
                 }
 132  45
         }
 133  
 
 134  
         /**
 135  
          * Replaces all occurrences of a locator with HTML hyperlinks.
 136  
          * <p>
 137  
          * The hyperlink has the locator as its target, and the last parameter of
 138  
          * the link as its text. For example the text
 139  
          * <code>metric://class/NumOps/</code> will be replaced by the hyperlink
 140  
          * <code>&lt;a href="metric://class/NumOps/"&gt;NumOps&lt;/a&gt;</code>. In
 141  
          * addition, literature references are set in square brackets so the link
 142  
          * looks likes this: [<a href="ref://BW02/">BW02</a>].
 143  
          * 
 144  
          * @param buf The text to operate on.
 145  
          * @param loc The locator to replace.
 146  
          */
 147  
         private static void replaceLinks(StringBuilder buf,
 148  
                         DescriptionLocator locator) {
 149  75
                 int prefixLength = locator.getPrefix().length();
 150  75
                 int index = 0;
 151  161
                 while ((index = buf.indexOf(locator.getPrefix(), index)) >= 0) {
 152  
                         // extract the start and end index of the last parameter
 153  11
                         int lastParamStart = index + prefixLength;
 154  17
                         for (int i = 0; i < locator.getParameterCount() - 1; i++) {
 155  6
                                 if (lastParamStart > 0)
 156  6
                                         lastParamStart = buf.indexOf("/", lastParamStart) + 1;
 157  
                         }
 158  32
                         int lastParamEnd = lastParamStart > 0 ? buf.indexOf("/",
 159  20
                                         lastParamStart) : -1;
 160  
 
 161  
                         // replace the HTML for a hyperlink
 162  11
                         if (lastParamEnd >= 0) {
 163  10
                                 StringBuilder link = new StringBuilder(); // the hyperlink text
 164  10
                                 if (locator == DescriptionLocator.REFERENCE)
 165  4
                                         link.append('[');
 166  10
                                 link.append("<a href=\"");
 167  10
                                 link.append(buf.substring(index, lastParamEnd + 1));
 168  10
                                 link.append("\">");
 169  
                                 String linkText;
 170  10
                                 if (lastParamStart == lastParamEnd
 171  2
                                                 && locator == DescriptionLocator.GLOSSARY) {
 172  
                                         // last parameter of glossary reference is empty => use
 173  
                                         // first parameter as text.
 174  1
                                         int firstParEndInd = buf.indexOf("/", index + prefixLength);
 175  2
                                         linkText = buf.substring(index + prefixLength,
 176  1
                                                         firstParEndInd);
 177  1
                                 } else
 178  9
                                         linkText = buf.substring(lastParamStart, lastParamEnd);
 179  10
                                 link.append(linkText);
 180  10
                                 link.append("</a>");
 181  10
                                 if (locator == DescriptionLocator.REFERENCE)
 182  4
                                         link.append(']');
 183  
 
 184  
                                 // replace the locator, keep searching the remaining text
 185  10
                                 buf.replace(index, lastParamEnd + 1, link.toString());
 186  10
                                 index += link.length();
 187  10
                         } else {
 188  
                                 // no parameters found, leave as is and continue
 189  1
                                 index += prefixLength;
 190  
                         }
 191  
                 }
 192  75
         }
 193  
 }