View Javadoc
1   package org.codehaus.mojo.jaxb2.schemageneration.postprocessing.javadoc;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Map;
23  
24  /**
25   * <p>Default JavaDocRenderer implementation which provides a plain JavaDocData rendering, on the form:</p>
26   * <pre>
27   *     [JavaDoc comment]
28   *     (tag1): [tag1 value]
29   *     (tag2): [tag2 value]
30   * </pre>
31   *
32   * @author <a href="mailto:lj@jguru.se">Lennart J&ouml;relid</a>, jGuru Europe AB
33   * @since 2.0
34   */
35  public class DefaultJavaDocRenderer implements JavaDocRenderer {
36  
37      /**
38       * {@inheritDoc}
39       */
40      @Override
41      public String render(final JavaDocData nonNullData, final SortableLocation location) {
42  
43          // Compile the XSD documentation string for this Node.
44          final StringBuilder builder = new StringBuilder();
45  
46          // First, render the JavaDoc comment.
47          builder.append(renderJavaDocComment(nonNullData.getComment(), location)).append("\n");
48  
49          // Then, render each JavaDoc tag.
50          for (Map.Entry<String, String> current : nonNullData.getTag2ValueMap().entrySet()) {
51  
52              final String tagXsdDoc = renderJavaDocTag(current.getKey(), current.getValue(), location);
53              if (tagXsdDoc != null && !tagXsdDoc.isEmpty()) {
54                  builder.append(tagXsdDoc);
55              }
56          }
57  
58          // All done.
59          return builder.toString();
60      }
61  
62      /**
63       * Override this method to yield another rendering of the javadoc comment.
64       *
65       * @param comment  The comment to render.
66       * @param location the SortableLocation where the JavaDocData was harvested. Never {@code null}.
67       * @return The XSD documentation for the supplied JavaDoc comment. A null or empty value will not be rendered.
68       */
69      protected String renderJavaDocComment(final String comment, final SortableLocation location) {
70          return harmonizeNewlines(comment);
71      }
72  
73      /**
74       * Override this method to yield another
75       *
76       * @param name     The name of a JavaDoc tag.
77       * @param value    The value of a JavaDoc tag.
78       * @param location the SortableLocation where the JavaDocData was harvested. Never {@code null}.
79       * @return The XSD documentation for the supplied JavaDoc tag.
80       */
81      protected String renderJavaDocTag(final String name, final String value, final SortableLocation location) {
82          final String nameKey = name != null ? name.trim() : "";
83          final String valueKey = value != null ? value.trim() : "";
84  
85          // All Done.
86          return "(" + nameKey + "): " + harmonizeNewlines(valueKey);
87      }
88  
89      /**
90       * Squashes newline characters into
91       *
92       * @param original the original string, potentially containing newline characters.
93       * @return A string where all newline characters are removed
94       */
95      protected String harmonizeNewlines(final String original) {
96  
97          final String toReturn = original.trim().replaceAll("[\r\n]+", "\n");
98          return toReturn.endsWith("\n") ? toReturn : toReturn + "\n";
99      }
100 }