blob: aaeab9322402d2d3a64ac049b2689b4c22a54e50 [file] [log] [blame]
Simon Hunt00b369a2017-06-20 19:46:40 -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.impl.lion;
19
20import com.google.common.collect.ImmutableList;
21import org.onosproject.ui.lion.LionBundle;
22import org.onosproject.ui.lion.LionUtils;
23import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
26import java.util.ArrayList;
27import java.util.List;
28import java.util.ResourceBundle;
29import java.util.Set;
30
31/**
32 * Gathers and stitches together a localization bundle according to a
33 * "lion" configuration file.
34 */
35public class BundleStitcher {
36
37 private static final Logger log =
38 LoggerFactory.getLogger(BundleStitcher.class);
39
40 private static final String CONFIG_DIR = "_config";
41 private static final String SUFFIX = ".lioncfg";
42 private static final String SLASH = "/";
43 private static final String DOT = ".";
44
45
46 private final String base;
47
48 /**
49 * Creates a bundle stitcher, configured with the specified base resource
50 * path.
51 *
52 * @param base the base resource path
53 */
54 public BundleStitcher(String base) {
55 this.base = base;
56 }
57
58 @Override
59 public String toString() {
60 return "BundleStitcher{base=\"" + base + "\"}";
61 }
62
63 /**
64 * Stitches together a LionBundle, based on the bundle configuration data
65 * for the given bundle ID.
66 *
67 * @param id the bundle ID
68 * @return a corresponding lion bundle
69 * @throws IllegalArgumentException if the bundle config cannot be loaded
70 */
71 public LionBundle stitch(String id) {
72 String source = base + SLASH + CONFIG_DIR + SLASH + id + SUFFIX;
73 LionConfig cfg = new LionConfig().load(source);
74 LionBundle.Builder builder = new LionBundle.Builder(id);
75
76 for (LionConfig.CmdFrom from : cfg.entries()) {
77 addItemsToBuilder(builder, from);
78 }
79
80 return builder.build();
81 }
82
83 private void addItemsToBuilder(LionBundle.Builder builder,
84 LionConfig.CmdFrom from) {
85 String resBundleName = base + SLASH + from.res();
86 String resFqbn = convertToFqbn(resBundleName);
87 ResourceBundle bundle = LionUtils.getBundledResource(resFqbn);
88
89 if (from.starred()) {
90 addAllItems(builder, bundle);
91 } else {
92 addItems(builder, bundle, from.keys());
93 }
94 }
95
96 // to fully-qualified-bundle-name
97 private String convertToFqbn(String path) {
98 if (!path.startsWith(SLASH)) {
99 throw new IllegalArgumentException("path should start with '/'");
100 }
101 return path.substring(1).replaceAll(SLASH, DOT);
102 }
103
104 private void addAllItems(LionBundle.Builder builder, ResourceBundle bundle) {
105 addItems(builder, bundle, bundle.keySet());
106 }
107
108 private void addItems(LionBundle.Builder builder, ResourceBundle bundle,
109 Set<String> keys) {
110 keys.forEach(k -> builder.addItem(k, bundle.getString(k)));
111 }
112
113 /**
114 * Generates an immutable list of localization bundles, using the specified
115 * resource tree (base) and localization configuration file names (tags).
116 * <p>
117 * As an example, you might invoke:
118 * <pre>
119 * private static final String LION_BASE = "/org/onosproject/ui/lion";
120 *
121 * private static final String[] LION_TAGS = {
122 * "core.view.App",
123 * "core.view.Settings",
124 * "core.view.Cluster",
125 * "core.view.Processor",
126 * "core.view.Partition",
127 * };
128 *
129 * List&lt;LionBundle&gt; bundles =
130 * LionUtils.generateBundles(LION_BASE, LION_TAGS);
131 * </pre>
132 * It is expected that in the "LION_BASE" directory there is a subdirectory
133 * named "_config" which contains the configuration files listed in the
134 * "LION_TAGS" array, each with a ".lioncfg" suffix...
135 * <pre>
136 * /org/onosproject/ui/lion/
137 * |
138 * +-- _config
139 * |
140 * +-- core.view.App.lioncfg
141 * +-- core.view.Settings.lioncfg
142 * :
143 * </pre>
144 * These files collate a localization bundle for their particular view
145 * by referencing resource bundles and their keys.
146 *
147 * @param base the base resource directory path
148 * @param tags the list of bundles to generate
149 * @return a list of generated localization bundles
150 */
151 public static List<LionBundle> generateBundles(String base,
152 String... tags) {
153 List<LionBundle> result = new ArrayList<>(tags.length);
154 BundleStitcher stitcher = new BundleStitcher(base);
155 for (String tag : tags) {
156 try {
157 LionBundle b = stitcher.stitch(tag);
158 result.add(b);
159
160 } catch (IllegalArgumentException e) {
161 log.warn("Unable to generate bundle: {} / {}", base, tag);
162 }
163 }
164 return ImmutableList.copyOf(result);
165 }
166}