+ def String wrapToDocumentation(String text) {
+ if (text.empty)
+ return ""
+
+ val StringBuilder sb = new StringBuilder("/**")
+ sb.append(NEW_LINE)
+
+ for (String t : NL_SPLITTER.split(text)) {
+ sb.append(" *")
+ if (!t.isEmpty()) {
+ sb.append(' ');
+ sb.append(t)
+ }
+ sb.append(NEW_LINE)
+ }
+ sb.append(" */")
+
+ return sb.toString
+ }
+
+ def protected String formatDataForJavaDoc(GeneratedType type) {
+ val typeDescription = type.getDescription().encodeJavadocSymbols;
+
+ return '''
+ «IF !typeDescription.nullOrEmpty»
+ «typeDescription»
+ «ENDIF»
+ '''.toString
+ }
+
+ private static final CharMatcher AMP_MATCHER = CharMatcher.is('&');
+ private static final CharMatcher GT_MATCHER = CharMatcher.is('>');
+ private static final CharMatcher LT_MATCHER = CharMatcher.is('<');
+
+ def encodeJavadocSymbols(String description) {
+ if (description.nullOrEmpty) {
+ return description;
+ }
+
+ var ret = description.replace("*/", "*/")
+
+ // FIXME: Use Guava's HtmlEscapers once we have it available
+ ret = AMP_MATCHER.replaceFrom(ret, "&");
+ ret = GT_MATCHER.replaceFrom(ret, ">");
+ ret = LT_MATCHER.replaceFrom(ret, "<");
+ return ret;
+ }
+
+ def protected String formatDataForJavaDoc(GeneratedType type, String additionalComment) {
+ val StringBuilder typeDescription = new StringBuilder();
+ if (!type.description.nullOrEmpty) {
+ typeDescription.append(type.description)
+ typeDescription.append(NEW_LINE)
+ typeDescription.append(NEW_LINE)
+ typeDescription.append(NEW_LINE)
+ typeDescription.append(additionalComment)
+ } else {
+ typeDescription.append(additionalComment)
+ }
+
+ return '''
+ «typeDescription.toString»
+ '''.toString
+ }
+
+ def asLink(String text) {
+ val StringBuilder sb = new StringBuilder()
+ var tempText = text
+ var char lastChar = ' '
+ var boolean badEnding = false
+
+ if (text.endsWith('.') || text.endsWith(':') || text.endsWith(',')) {
+ tempText = text.substring(0, text.length - 1)
+ lastChar = text.charAt(text.length - 1)
+ badEnding = true
+ }
+ sb.append("<a href = \"")
+ sb.append(tempText)
+ sb.append("\">")
+ sb.append(tempText)
+ sb.append("</a>")
+
+ if(badEnding)
+ sb.append(lastChar)
+
+ return sb.toString
+ }
+
+ protected def formatToParagraph(String text) {
+ if(text == null || text.isEmpty)
+ return text
+
+ var formattedText = text
+ val StringBuilder sb = new StringBuilder();
+ var StringBuilder lineBuilder = new StringBuilder();
+ var boolean isFirstElementOnNewLineEmptyChar = false;
+
+ formattedText = formattedText.encodeJavadocSymbols
+ formattedText = NL_MATCHER.removeFrom(formattedText)
+ formattedText = TAB_MATCHER.removeFrom(formattedText)
+ formattedText = SPACES_PATTERN.matcher(formattedText).replaceAll(" ")
+
+ val StringTokenizer tokenizer = new StringTokenizer(formattedText, " ", true);
+
+ while(tokenizer.hasMoreElements) {
+ val nextElement = tokenizer.nextElement.toString
+
+ if(lineBuilder.length + nextElement.length > 80) {
+ if (lineBuilder.charAt(lineBuilder.length - 1) == ' ') {
+ lineBuilder.setLength(0)
+ lineBuilder.append(lineBuilder.substring(0, lineBuilder.length - 1))
+ }
+ if (lineBuilder.charAt(0) == ' ') {
+ lineBuilder.setLength(0)
+ lineBuilder.append(lineBuilder.substring(1))
+ }
+
+ sb.append(lineBuilder);
+ lineBuilder.setLength(0)
+ sb.append(NEW_LINE)
+
+ if(nextElement.toString == ' ') {
+ isFirstElementOnNewLineEmptyChar = !isFirstElementOnNewLineEmptyChar;
+ }
+ }
+
+ if(isFirstElementOnNewLineEmptyChar) {
+ isFirstElementOnNewLineEmptyChar = !isFirstElementOnNewLineEmptyChar
+ }
+
+ else {
+ lineBuilder.append(nextElement)
+ }
+ }
+ sb.append(lineBuilder)
+ sb.append(NEW_LINE)
+
+ return sb.toString
+ }
+