View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.report.projectinfo;
20  
21  import java.util.Locale;
22  import java.util.regex.Pattern;
23  
24  import org.apache.maven.doxia.sink.Sink;
25  import org.apache.maven.reporting.AbstractMavenReportRenderer;
26  import org.codehaus.plexus.i18n.I18N;
27  import org.codehaus.plexus.util.StringUtils;
28  
29  /**
30   * @author Hervé Boutemy
31   *
32   */
33  public abstract class AbstractProjectInfoRenderer extends AbstractMavenReportRenderer {
34      /**
35       * {@link I18N}.
36       */
37      protected I18N i18n;
38  
39      /**
40       * The {@link Locale}
41       */
42      protected Locale locale;
43  
44      /**
45       * @param sink {@link Sink}
46       * @param i18n {@link I18N}
47       * @param locale {@link Locale}
48       */
49      public AbstractProjectInfoRenderer(Sink sink, I18N i18n, Locale locale) {
50          super(sink);
51  
52          this.i18n = i18n;
53  
54          this.locale = locale;
55      }
56  
57      @Override
58      public String getTitle() {
59          return getI18nString("title");
60      }
61  
62      /**
63       * @param key The key.
64       * @return The translated string.
65       */
66      protected String getI18nString(String key) {
67          return getI18nString(getI18Nsection(), key);
68      }
69  
70      /**
71       * @param section The section.
72       * @param key The key to translate.
73       * @return the translated key.
74       */
75      protected String getI18nString(String section, String key) {
76          return i18n.getString("project-info-reports", locale, "report." + section + '.' + key);
77      }
78  
79      @Override
80      protected void text(String text) {
81          if (StringUtils.isEmpty(text)) // Take care of spaces
82          {
83              sink.text("-");
84          } else {
85              // custombundle text with xml?
86              String regex = "(.+?)<(\"[^\"]*\"|'[^']*'|[^'\">])*>(.+?)";
87              if (Pattern.matches(regex, text)) {
88                  sink.rawText(text);
89              } else {
90                  sink.text(text);
91              }
92          }
93      }
94  
95      /* FIXME The next two methods need to be retained until Doxia and Maven Reporting Impl properly implement
96       * the difference of a (boxed) real verbatim text and (boxed) source code.
97       */
98      /**
99       * {@inheritDoc}
100      */
101     @Override
102     protected void verbatimText(String text) {
103         sink.verbatim(null);
104 
105         text(text);
106 
107         sink.verbatim_();
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     @Override
114     protected void verbatimLink(String text, String href) {
115         if (StringUtils.isEmpty(href)) {
116             verbatimText(text);
117         } else {
118             sink.verbatim(null);
119 
120             link(href, text);
121 
122             sink.verbatim_();
123         }
124     }
125 
126     protected abstract String getI18Nsection();
127 }