blob: 612edd4ef589315633531ede49b9184cc7b5f2b6 [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
28 * "lion" configuration file.
29 */
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
61 */
62 public LionBundle stitch(String id) {
63 String source = base + SLASH + CONFIG_DIR + SLASH + id + SUFFIX;
64 LionConfig cfg = new LionConfig().load(source);
65 LionBundle.Builder builder = new LionBundle.Builder(id);
66
67 for (LionConfig.CmdFrom from : cfg.entries()) {
68 addItemsToBuilder(builder, from);
69 }
70
71 return builder.build();
72 }
73
74 private void addItemsToBuilder(LionBundle.Builder builder,
75 LionConfig.CmdFrom from) {
76 String resBundleName = base + SLASH + from.res();
77 String resFqbn = convertToFqbn(resBundleName);
78 ResourceBundle bundle = LionUtils.getBundledResource(resFqbn);
79
80 if (from.starred()) {
81 addAllItems(builder, bundle);
82 } else {
83 addItems(builder, bundle, from.keys());
84 }
85 }
86
87 // to fully-qualified-bundle-name
88 private String convertToFqbn(String path) {
89 if (!path.startsWith(SLASH)) {
90 throw new IllegalArgumentException("path should start with '/'");
91 }
92 return path.substring(1).replaceAll(SLASH, DOT);
93 }
94
95 private void addAllItems(LionBundle.Builder builder, ResourceBundle bundle) {
96 addItems(builder, bundle, bundle.keySet());
97 }
98
99 private void addItems(LionBundle.Builder builder, ResourceBundle bundle,
100 Set<String> keys) {
101 keys.forEach(k -> builder.addItem(k, bundle.getString(k)));
102 }
103}