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  
                 }
 64  1228
         }
 65  
 
 66  
         /**
 67  
          * Sets the separator string to indicate the end of the brief description.
 68  
          * By default, this is the period (".")
 69  
          * 
 70  
          * @param sep The new separator string.
 71  
          */
 72  
         void setSeparator(String sep) {
 73  69
                 descriptionSeparator = sep;
 74  69
         }
 75  
 
 76  
         /**
 77  
          * Obtains the short version of the description. This is the description
 78  
          * text up to the first occurrence of the separator string, or the entire
 79  
          * description text if the text does not contain the separator string.
 80  
          * 
 81  
          * @return Description summary.
 82  
          */
 83  
         String getBriefDescription() {
 84  15
                 getDescription();
 85  15
                 int firstSeparatorIndex = description.indexOf(descriptionSeparator, 0);
 86  15
                 if (firstSeparatorIndex < 0) {
 87  2
                         return description;
 88  
                 }
 89  26
                 return description.substring(0, firstSeparatorIndex
 90  13
                                 + descriptionSeparator.length());
 91  
         }
 92  
 
 93  
         /**
 94  
          * Obtains the full description text. This includes the brief description
 95  
          * and all additional description that follows.
 96  
          * 
 97  
          * @return the full description text
 98  
          */
 99  
         String getDescription() {
 100  27
                 if (description == null) {
 101  15
                         processHTML(text);
 102  15
                         description = text.toString();
 103  15
                         text = null; // string buffer no longer needed.
 104  
                 }
 105  27
                 return description;
 106  
         }
 107  
 
 108  
         /** Fixes the HTML tags and locator references in the description text. */
 109  
         private static void processHTML(StringBuilder buf) {
 110  
                 // replace (( with < and )) with >, and newlines with spaces
 111  15
                 replaceAll(buf, "((", "<");
 112  15
                 replaceAll(buf, "))", ">");
 113  15
                 replaceAll(buf, "\n", " ");
 114  
 
 115  
                 // turn metric locators etc. into hyperlinks
 116  90
                 for (DescriptionLocator loc : DescriptionLocator.values()) {
 117  75
                         replaceLinks(buf, loc);
 118  
                 }
 119  15
         }
 120  
 
 121  
         /**
 122  
          * Replaces all occurrences of a string in a string builder.
 123  
          * 
 124  
          * @param buf Text to operate on.
 125  
          * @param what The string in the buffer to be replaced.
 126  
          * @param with The replacement string.
 127  
          */
 128  
         private static void replaceAll(StringBuilder buf, String what, String with) {
 129  45
                 int index = 0;
 130  125
                 while ((index = buf.indexOf(what, index)) >= 0) {
 131  35
                         buf.replace(index, index + what.length(), with);
 132  35
                         index += with.length();
 133  
                 }
 134  45
         }
 135  
 
 136  
         /**
 137  
          * Replaces all occurrences of a locator with HTML hyperlinks.
 138  
          * <p>
 139  
          * The hyperlink has the locator as its target, and the last parameter of
 140  
          * the link as its text. For example the text
 141  
          * <code>metric://class/NumOps/</code> will be replaced by the hyperlink
 142  
          * <code>&lt;a href="metric://class/NumOps/"&gt;NumOps&lt;/a&gt;</code>. In
 143  
          * addition, literature references are set in square brackets so the link
 144  
          * looks likes this: [<a href="ref://BW02/">BW02</a>].
 145  
          * 
 146  
          * @param buf The text to operate on.
 147  
          * @param loc The locator to replace.
 148  
          */
 149  
         private static void replaceLinks(StringBuilder buf,
 150  
                         DescriptionLocator locator) {
 151  75
                 int prefixLength = locator.getPrefix().length();
 152  75
                 int index = 0;
 153  161
                 while ((index = buf.indexOf(locator.getPrefix(), index)) >= 0) {
 154  
                         // extract the start and end index of the last parameter
 155  11
                         int lastParamStart = index + prefixLength;
 156  17
                         for (int i = 0; i < locator.getParameterCount() - 1; i++) {
 157  6
                                 if (lastParamStart > 0) {
 158  6
                                         lastParamStart = buf.indexOf("/", lastParamStart) + 1;
 159  
                                 }
 160  
                         }
 161  32
                         int lastParamEnd = lastParamStart > 0 ? buf.indexOf("/",
 162  20
                                         lastParamStart) : -1;
 163  
 
 164  
                         // replace the HTML for a hyperlink
 165  11
                         if (lastParamEnd >= 0) {
 166  10
                                 StringBuilder link = new StringBuilder(); // the hyperlink text
 167  10
                                 if (locator == DescriptionLocator.REFERENCE) {
 168  4
                                         link.append('[');
 169  
                                 }
 170  10
                                 link.append("<a href=\"");
 171  10
                                 link.append(buf.substring(index, lastParamEnd + 1));
 172  10
                                 link.append("\">");
 173  
                                 String linkText;
 174  10
                                 if (lastParamStart == lastParamEnd
 175  2
                                                 && locator == DescriptionLocator.GLOSSARY) {
 176  
                                         // last parameter of glossary reference is empty => use
 177  
                                         // first parameter as text.
 178  1
                                         int firstParEndInd = buf.indexOf("/", index + prefixLength);
 179  2
                                         linkText = buf.substring(index + prefixLength,
 180  1
                                                         firstParEndInd);
 181  1
                                 } else {
 182  9
                                         linkText = buf.substring(lastParamStart, lastParamEnd);
 183  
                                 }
 184  10
                                 link.append(linkText);
 185  10
                                 link.append("</a>");
 186  10
                                 if (locator == DescriptionLocator.REFERENCE) {
 187  4
                                         link.append(']');
 188  
                                 }
 189  
 
 190  
                                 // replace the locator, keep searching the remaining text
 191  10
                                 buf.replace(index, lastParamEnd + 1, link.toString());
 192  10
                                 index += link.length();
 193  10
                         } else {
 194  
                                 // no parameters found, leave as is and continue
 195  1
                                 index += prefixLength;
 196  
                         }
 197  
                 }
 198  75
         }
 199  
 }