blob: dc6e05f2fa19ffd02d2affc97f85d0606787411f [file] [log] [blame]
Simon Hunte556e942017-06-19 15:35:44 -07001/*
2 * Copyright 2017-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.onosproject.ui.lion.stitch;
19
20import org.onosproject.ui.lion.LionBundle;
21import org.onosproject.ui.lion.LionUtils;
22
23import java.util.ResourceBundle;
24import java.util.Set;
25
26/**
27 * Gathers and stitches together a localization bundle according to a
Simon Hunt7379a3d2017-06-20 16:50:39 -070028 * "lion" configuration file.
Simon Hunte556e942017-06-19 15:35:44 -070029 */
30public class BundleStitcher {
31
32 private static final String CONFIG_DIR = "_config";
33 private static final String SUFFIX = ".lioncfg";
34 private static final String SLASH = "/";
35 private static final String DOT = ".";
36
37
38 private final String base;
39
40 /**
41 * Creates a bundle stitcher, configured with the specified base resource
42 * path.
43 *
44 * @param base the base resource path
45 */
46 public BundleStitcher(String base) {
47 this.base = base;
48 }
49
50 @Override
51 public String toString() {
52 return "BundleStitcher{base=\"" + base + "\"}";
53 }
54
55 /**
56 * Stitches together a LionBundle, based on the bundle configuration data
57 * for the given bundle ID.
58 *
59 * @param id the bundle ID
60 * @return a corresponding lion bundle
Simon Hunt7379a3d2017-06-20 16:50:39 -070061 * @throws IllegalArgumentException if the bundle config cannot be loaded
Simon Hunte556e942017-06-19 15:35:44 -070062 */
63 public LionBundle stitch(String id) {
64 String source = base + SLASH + CONFIG_DIR + SLASH + id + SUFFIX;
65 LionConfig cfg = new LionConfig().load(source);
66 LionBundle.Builder builder = new LionBundle.Builder(id);
67
68 for (LionConfig.CmdFrom from : cfg.entries()) {
69 addItemsToBuilder(builder, from);
70 }
71
72 return builder.build();
73 }
74
75 private void addItemsToBuilder(LionBundle.Builder builder,
76 LionConfig.CmdFrom from) {
77 String resBundleName = base + SLASH + from.res();
78 String resFqbn = convertToFqbn(resBundleName);
79 ResourceBundle bundle = LionUtils.getBundledResource(resFqbn);
80
81 if (from.starred()) {
82 addAllItems(builder, bundle);
83 } else {
84 addItems(builder, bundle, from.keys());
85 }
86 }
87
88 // to fully-qualified-bundle-name
89 private String convertToFqbn(String path) {
90 if (!path.startsWith(SLASH)) {
91 throw new IllegalArgumentException("path should start with '/'");
92 }
93 return path.substring(1).replaceAll(SLASH, DOT);
94 }
95
96 private void addAllItems(LionBundle.Builder builder, ResourceBundle bundle) {
97 addItems(builder, bundle, bundle.keySet());
98 }
99
100 private void addItems(LionBundle.Builder builder, ResourceBundle bundle,
101 Set<String> keys) {
102 keys.forEach(k -> builder.addItem(k, bundle.getString(k)));
103 }
104}