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
28 /**
29 * @author Hervé Boutemy
30 *
31 */
32 public abstract class AbstractProjectInfoRenderer extends AbstractMavenReportRenderer {
33 /**
34 * {@link I18N}.
35 */
36 protected I18N i18n;
37
38 /**
39 * The {@link Locale}
40 */
41 protected Locale locale;
42
43 /**
44 * @param sink {@link Sink}
45 * @param i18n {@link I18N}
46 * @param locale {@link Locale}
47 */
48 public AbstractProjectInfoRenderer(Sink sink, I18N i18n, Locale locale) {
49 super(sink);
50
51 this.i18n = i18n;
52
53 this.locale = locale;
54 }
55
56 @Override
57 public String getTitle() {
58 return getI18nString("title");
59 }
60
61 /**
62 * @param key The key.
63 * @return The translated string.
64 */
65 protected String getI18nString(String key) {
66 return getI18nString(getI18Nsection(), key);
67 }
68
69 /**
70 * @param section The section.
71 * @param key The key to translate.
72 * @return the translated key.
73 */
74 protected String getI18nString(String section, String key) {
75 return i18n.getString("project-info-reports", locale, "report." + section + '.' + key);
76 }
77
78 @Override
79 protected void text(String text) {
80 if (text == null || text.isEmpty()) // Take care of spaces
81 {
82 sink.text("-");
83 } else {
84 // custombundle text with xml?
85 String regex = "(.+?)<(\"[^\"]*\"|'[^']*'|[^'\">])*>(.+?)";
86 if (Pattern.matches(regex, text)) {
87 sink.rawText(text);
88 } else {
89 sink.text(text);
90 }
91 }
92 }
93
94 /* FIXME The next two methods need to be retained until Doxia and Maven Reporting Impl properly implement
95 * the difference of a (boxed) real verbatim text and (boxed) source code.
96 */
97 /**
98 * {@inheritDoc}
99 */
100 @Override
101 protected void verbatimText(String text) {
102 sink.verbatim(null);
103
104 text(text);
105
106 sink.verbatim_();
107 }
108
109 /**
110 * {@inheritDoc}
111 */
112 @Override
113 protected void verbatimLink(String text, String href) {
114 if (href == null || href.isEmpty()) {
115 verbatimText(text);
116 } else {
117 sink.verbatim(null);
118
119 link(href, text);
120
121 sink.verbatim_();
122 }
123 }
124
125 protected abstract String getI18Nsection();
126 }