24
24
import org .apache .commons .io .IOUtils ;
25
25
import org .apache .commons .lang .StringEscapeUtils ;
26
26
import org .apache .commons .lang .StringUtils ;
27
+ import org .eclipse .jdt .annotation .NonNullByDefault ;
28
+ import org .eclipse .jdt .annotation .Nullable ;
27
29
import org .openhab .core .i18n .I18nUtil ;
28
30
import org .openhab .core .i18n .LocaleProvider ;
29
31
import org .openhab .core .i18n .TranslationProvider ;
30
32
import org .openhab .core .library .types .QuantityType ;
31
33
import org .openhab .core .model .sitemap .sitemap .Widget ;
32
34
import org .openhab .core .types .State ;
33
35
import org .openhab .core .ui .items .ItemUIRegistry ;
34
- import org .openhab .ui .basic .internal .WebAppActivator ;
35
36
import org .openhab .ui .basic .internal .WebAppConfig ;
36
37
import org .openhab .ui .basic .render .RenderException ;
37
38
import org .openhab .ui .basic .render .WidgetRenderer ;
38
39
import org .osgi .framework .BundleContext ;
40
+ import org .osgi .service .component .annotations .Activate ;
41
+ import org .osgi .service .component .annotations .Reference ;
39
42
import org .slf4j .Logger ;
40
43
import org .slf4j .LoggerFactory ;
41
44
46
49
*
47
50
* @author Kai Kreuzer - Initial contribution and API
48
51
* @author Vlad Ivanov - BasicUI changes
49
- *
50
52
*/
53
+ @ NonNullByDefault
51
54
public abstract class AbstractWidgetRenderer implements WidgetRenderer {
52
55
53
56
private final Logger logger = LoggerFactory .getLogger (AbstractWidgetRenderer .class );
54
57
55
58
public static final String ICON_TYPE = "svg" ;
56
59
57
- protected ItemUIRegistry itemUIRegistry ;
58
- protected TranslationProvider i18nProvider ;
59
- protected LocaleProvider localeProvider ;
60
+ private final BundleContext bundleContext ;
61
+ protected final TranslationProvider i18nProvider ;
62
+ protected final ItemUIRegistry itemUIRegistry ;
63
+ protected final LocaleProvider localeProvider ;
60
64
61
- protected WebAppConfig config ;
65
+ protected WebAppConfig config = new WebAppConfig () ;
62
66
63
- private BundleContext bundleContext ;
67
+ @ Activate
68
+ public AbstractWidgetRenderer (final BundleContext bundleContext , final @ Reference TranslationProvider i18nProvider ,
69
+ final @ Reference ItemUIRegistry itemUIRegistry , final @ Reference LocaleProvider localeProvider ) {
70
+ this .bundleContext = bundleContext ;
71
+ this .i18nProvider = i18nProvider ;
72
+ this .itemUIRegistry = itemUIRegistry ;
73
+ this .localeProvider = localeProvider ;
74
+ }
64
75
65
76
/* the file extension of the snippets */
66
77
protected static final String SNIPPET_EXT = ".html" ;
@@ -69,44 +80,12 @@ public abstract class AbstractWidgetRenderer implements WidgetRenderer {
69
80
protected static final String SNIPPET_LOCATION = "snippets/" ;
70
81
71
82
/* a local cache so we do not have to read the snippets over and over again from the bundle */
72
- protected static final Map <String , String > SNIPPET_CACHE = new HashMap <String , String >();
73
-
74
- protected void setItemUIRegistry (ItemUIRegistry itemUIRegistry ) {
75
- this .itemUIRegistry = itemUIRegistry ;
76
- }
77
-
78
- protected void unsetItemUIRegistry (ItemUIRegistry itemUIRegistry ) {
79
- this .itemUIRegistry = null ;
80
- }
83
+ protected static final Map <String , String > SNIPPET_CACHE = new HashMap <>(30 );
81
84
82
85
public ItemUIRegistry getItemUIRegistry () {
83
86
return itemUIRegistry ;
84
87
}
85
88
86
- protected void setLocaleProvider (LocaleProvider localeProvider ) {
87
- this .localeProvider = localeProvider ;
88
- }
89
-
90
- protected void unsetLocaleProvider (final LocaleProvider localeProvider ) {
91
- this .localeProvider = null ;
92
- }
93
-
94
- protected void setTranslationProvider (TranslationProvider i18nProvider ) {
95
- this .i18nProvider = i18nProvider ;
96
- }
97
-
98
- protected void unsetTranslationProvider (TranslationProvider i18nProvider ) {
99
- this .i18nProvider = null ;
100
- }
101
-
102
- protected void activate (BundleContext context ) {
103
- this .bundleContext = context ;
104
- }
105
-
106
- protected void deactivate (BundleContext context ) {
107
- this .bundleContext = null ;
108
- }
109
-
110
89
/**
111
90
* Replace some common values in the widget template
112
91
*
@@ -123,12 +102,12 @@ protected String preprocessSnippet(String originalSnippet, Widget w) {
123
102
String text = itemUIRegistry .getLabel (w );
124
103
snippet = StringUtils .replace (snippet , "%label%" , getLabel (text ));
125
104
snippet = StringUtils .replace (snippet , "%value%" , getValue (text ));
126
- snippet = StringUtils .replace (snippet , "%has_value%" , new Boolean (hasValue (text )).toString ());
105
+ snippet = StringUtils .replace (snippet , "%has_value%" , Boolean . valueOf (hasValue (text )).toString ());
127
106
snippet = StringUtils .replace (snippet , "%visibility_class%" ,
128
107
itemUIRegistry .getVisiblity (w ) ? "" : "mdl-form__row--hidden" );
129
108
130
109
String state = getState (w );
131
- snippet = StringUtils .replace (snippet , "%state%" , state == null ? "" : escapeURL (state ));
110
+ snippet = StringUtils .replace (snippet , "%state%" , escapeURL (state ));
132
111
133
112
String category = getCategory (w );
134
113
snippet = StringUtils .replace (snippet , "%category%" , escapeURL (category ));
@@ -148,7 +127,7 @@ protected synchronized String getSnippet(String elementType) throws RenderExcept
148
127
String snippet = SNIPPET_CACHE .get (lowerTypeElementType );
149
128
if (snippet == null ) {
150
129
String snippetLocation = SNIPPET_LOCATION + lowerTypeElementType + SNIPPET_EXT ;
151
- URL entry = WebAppActivator . getContext () .getBundle ().getEntry (snippetLocation );
130
+ URL entry = bundleContext .getBundle ().getEntry (snippetLocation );
152
131
if (entry != null ) {
153
132
try {
154
133
snippet = IOUtils .toString (entry .openStream ());
@@ -179,7 +158,11 @@ public String getLabel(Widget w) {
179
158
* @param text the text containing the label and an optional value around []
180
159
* @return the label extracted from the text
181
160
*/
182
- protected String getLabel (String text ) {
161
+ protected String getLabel (@ Nullable String text ) {
162
+ if (text == null ) {
163
+ return "" ;
164
+ }
165
+
183
166
int index = text .indexOf ('[' );
184
167
185
168
if (index != -1 ) {
@@ -205,7 +188,11 @@ public String getValue(Widget w) {
205
188
* @param text the text containing the label and an optional value around []
206
189
* @return the value extracted from the text or "" if not present
207
190
*/
208
- protected String getValue (String text ) {
191
+ protected String getValue (@ Nullable String text ) {
192
+ if (text == null ) {
193
+ return "" ;
194
+ }
195
+
209
196
int index = text .indexOf ('[' );
210
197
211
198
if (index != -1 ) {
@@ -231,8 +218,8 @@ public boolean hasValue(Widget w) {
231
218
* @param text the text containing the label and an optional value around []
232
219
* @return true if the text contains a value
233
220
*/
234
- protected boolean hasValue (String text ) {
235
- return ( text .indexOf ('[' ) != -1 ) ;
221
+ protected boolean hasValue (@ Nullable String text ) {
222
+ return text != null && text .indexOf ('[' ) != -1 ;
236
223
}
237
224
238
225
/**
@@ -242,9 +229,9 @@ protected boolean hasValue(String text) {
242
229
* @param string The string that has to be escaped
243
230
* @return The escaped string
244
231
*/
245
- protected String escapeURL (String string ) {
232
+ protected String escapeURL (@ Nullable String string ) {
246
233
if (string == null ) {
247
- return null ;
234
+ return "" ;
248
235
}
249
236
250
237
try {
@@ -287,7 +274,7 @@ protected String processColor(Widget w, String originalSnippet) {
287
274
return snippet ;
288
275
}
289
276
290
- protected String getCategory (Widget w ) {
277
+ protected @ Nullable String getCategory (Widget w ) {
291
278
return itemUIRegistry .getCategory (w );
292
279
}
293
280
@@ -300,7 +287,7 @@ protected String getState(Widget w) {
300
287
}
301
288
}
302
289
303
- protected String escapeHtml (String s ) {
290
+ protected String escapeHtml (@ Nullable String s ) {
304
291
return StringEscapeUtils .escapeHtml (s );
305
292
}
306
293
@@ -309,24 +296,24 @@ public void setConfig(WebAppConfig config) {
309
296
this .config = config ;
310
297
}
311
298
312
- protected String localizeText (String key ) {
299
+ protected @ Nullable String localizeText (String key ) {
313
300
String result = "" ;
314
301
if (I18nUtil .isConstant (key )) {
315
- result = this . i18nProvider .getText (this . bundleContext .getBundle (), I18nUtil .stripConstant (key ), "" ,
316
- this . localeProvider .getLocale ());
302
+ result = i18nProvider .getText (bundleContext .getBundle (), I18nUtil .stripConstant (key ), "" ,
303
+ localeProvider .getLocale ());
317
304
}
318
305
return result ;
319
306
}
320
307
321
- protected String getUnitForWidget (Widget widget ) {
308
+ protected @ Nullable String getUnitForWidget (Widget widget ) {
322
309
return itemUIRegistry .getUnitForWidget (widget );
323
310
}
324
311
325
- protected State convertStateToLabelUnit (QuantityType <?> state , String label ) {
312
+ protected @ Nullable State convertStateToLabelUnit (QuantityType <?> state , String label ) {
326
313
return itemUIRegistry .convertStateToLabelUnit (state , label );
327
314
}
328
315
329
- protected boolean isValidURL (String url ) {
316
+ protected boolean isValidURL (@ Nullable String url ) {
330
317
if (url != null && !url .isEmpty ()) {
331
318
try {
332
319
return new URL (url ).toURI () != null ? true : false ;
0 commit comments