Simon Hunt | e556e94 | 2017-06-19 15:35:44 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Simon Hunt | e556e94 | 2017-06-19 15:35:44 -0700 | [diff] [blame] | 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 | |
Simon Hunt | 00b369a | 2017-06-20 19:46:40 -0700 | [diff] [blame] | 18 | package org.onosproject.ui.impl.lion; |
Simon Hunt | e556e94 | 2017-06-19 15:35:44 -0700 | [diff] [blame] | 19 | |
| 20 | import com.google.common.collect.ImmutableList; |
| 21 | import com.google.common.collect.ImmutableSortedSet; |
Simon Hunt | 7d1c081 | 2017-06-21 13:48:30 -0700 | [diff] [blame] | 22 | import com.google.common.io.CharStreams; |
Simon Hunt | e556e94 | 2017-06-19 15:35:44 -0700 | [diff] [blame] | 23 | |
| 24 | import java.io.IOException; |
Simon Hunt | 7d1c081 | 2017-06-21 13:48:30 -0700 | [diff] [blame] | 25 | import java.io.InputStreamReader; |
| 26 | import java.io.Reader; |
Simon Hunt | e556e94 | 2017-06-19 15:35:44 -0700 | [diff] [blame] | 27 | import java.util.ArrayList; |
| 28 | import java.util.Collections; |
| 29 | import java.util.HashMap; |
| 30 | import java.util.HashSet; |
| 31 | import java.util.List; |
| 32 | import java.util.Map; |
| 33 | import java.util.Set; |
| 34 | import java.util.TreeSet; |
| 35 | import java.util.regex.Matcher; |
| 36 | import java.util.regex.Pattern; |
| 37 | |
| 38 | import static java.nio.charset.StandardCharsets.UTF_8; |
| 39 | |
| 40 | /** |
| 41 | * A Java representation of a lion configuration file. You can create one with |
| 42 | * something like the following: |
| 43 | * <pre> |
| 44 | * String filepath = "/path/to/some/file.lioncfg"; |
| 45 | * LionConfig cfg = new LionConfig().load(filepath); |
| 46 | * </pre> |
| 47 | */ |
| 48 | public class LionConfig { |
| 49 | private static final Pattern RE_COMMENT = Pattern.compile("^\\s*#.*"); |
| 50 | private static final Pattern RE_BLANK = Pattern.compile("^\\s*$"); |
| 51 | |
| 52 | static final Pattern RE_IMPORT = |
| 53 | Pattern.compile("^(\\S+)\\s+import\\s+(.*)$"); |
| 54 | |
| 55 | private static final String BUNDLE = "bundle"; |
| 56 | private static final String ALIAS = "alias"; |
| 57 | private static final String FROM = "from"; |
| 58 | private static final String STAR = "*"; |
| 59 | private static final char SPC = ' '; |
| 60 | private static final char DOT = '.'; |
| 61 | |
| 62 | private List<String> lines; |
| 63 | private List<String> badLines; |
| 64 | |
| 65 | private CmdBundle bundle; |
| 66 | private final Set<CmdAlias> aliases = new TreeSet<>(); |
| 67 | private final Set<CmdFrom> froms = new TreeSet<>(); |
| 68 | |
| 69 | private Map<String, String> aliasMap; |
| 70 | private Map<String, Set<String>> fromMap; |
| 71 | |
| 72 | /** |
| 73 | * Loads in the specified file and attempts to parse it as a |
| 74 | * {@code .lioncfg} format file. |
| 75 | * |
| 76 | * @param source path to .lioncfg file |
| 77 | * @return the instance |
| 78 | * @throws IllegalArgumentException if there is a problem reading the file |
| 79 | */ |
| 80 | public LionConfig load(String source) { |
Simon Hunt | 7d1c081 | 2017-06-21 13:48:30 -0700 | [diff] [blame] | 81 | try (Reader r = new InputStreamReader(getClass().getResourceAsStream(source), |
| 82 | UTF_8)) { |
| 83 | lines = CharStreams.readLines(r); |
Yuta HIGUCHI | a5323ce | 2017-06-21 09:54:20 -0700 | [diff] [blame] | 84 | } catch (IOException e) { |
Simon Hunt | e556e94 | 2017-06-19 15:35:44 -0700 | [diff] [blame] | 85 | throw new IllegalArgumentException("Failed to read: " + source, e); |
| 86 | } |
| 87 | |
| 88 | stripCommentsAndWhitespace(); |
| 89 | parse(); |
| 90 | processAliases(); |
| 91 | processFroms(); |
| 92 | |
| 93 | return this; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | private boolean isCommentOrBlank(String s) { |
| 98 | return RE_COMMENT.matcher(s).matches() || RE_BLANK.matcher(s).matches(); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | private void stripCommentsAndWhitespace() { |
| 103 | if (lines != null) { |
| 104 | lines.removeIf(this::isCommentOrBlank); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | private void parse() { |
| 109 | badLines = new ArrayList<>(); |
| 110 | |
| 111 | lines.forEach(l -> { |
| 112 | int i = l.indexOf(SPC); |
| 113 | if (i < 1) { |
| 114 | badLines.add(l); |
| 115 | return; |
| 116 | } |
| 117 | String keyword = l.substring(0, i); |
| 118 | String params = l.substring(i + 1); |
| 119 | |
| 120 | switch (keyword) { |
| 121 | case BUNDLE: |
| 122 | CmdBundle cb = new CmdBundle(l, params); |
| 123 | |
| 124 | if (bundle != null) { |
| 125 | // we can only declare the bundle once |
| 126 | badLines.add(l); |
| 127 | } else { |
| 128 | bundle = cb; |
| 129 | } |
| 130 | break; |
| 131 | |
| 132 | case ALIAS: |
| 133 | CmdAlias ca = new CmdAlias(l, params); |
| 134 | if (ca.malformed) { |
| 135 | badLines.add(l); |
| 136 | } else { |
| 137 | aliases.add(ca); |
| 138 | } |
| 139 | break; |
| 140 | |
| 141 | case FROM: |
| 142 | CmdFrom cf = new CmdFrom(l, params); |
| 143 | if (cf.malformed) { |
| 144 | badLines.add(l); |
| 145 | } else { |
| 146 | froms.add(cf); |
| 147 | } |
| 148 | break; |
| 149 | |
| 150 | default: |
| 151 | badLines.add(l); |
| 152 | break; |
| 153 | } |
| 154 | }); |
| 155 | } |
| 156 | |
| 157 | private void processAliases() { |
| 158 | aliasMap = new HashMap<>(aliasCount()); |
| 159 | aliases.forEach(a -> aliasMap.put(a.alias, a.subst)); |
| 160 | } |
| 161 | |
| 162 | private void processFroms() { |
| 163 | fromMap = new HashMap<>(fromCount()); |
| 164 | froms.forEach(f -> { |
| 165 | f.expandAliasIfAny(aliasMap); |
| 166 | if (singleStarCheck(f)) { |
| 167 | fromMap.put(f.expandedRes, f.keys); |
| 168 | } else { |
| 169 | badLines.add(f.orig); |
| 170 | } |
| 171 | }); |
| 172 | } |
| 173 | |
| 174 | private boolean singleStarCheck(CmdFrom from) { |
| 175 | from.starred = false; |
| 176 | Set<String> keys = from.keys(); |
Simon Hunt | 7379a3d | 2017-06-20 16:50:39 -0700 | [diff] [blame] | 177 | for (String k : keys) { |
Simon Hunt | e556e94 | 2017-06-19 15:35:44 -0700 | [diff] [blame] | 178 | if (STAR.equals(k)) { |
| 179 | from.starred = true; |
| 180 | } |
| 181 | } |
| 182 | return !from.starred || keys.size() == 1; |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public String toString() { |
| 187 | int nlines = lines == null ? 0 : lines.size(); |
| 188 | return String.format("LionConfig{#lines=%d}", nlines); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Returns the configured bundle ID for this config. |
| 193 | * |
| 194 | * @return the bundle ID |
| 195 | */ |
| 196 | String id() { |
| 197 | return bundle == null ? null : bundle.id; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Returns the number of aliases configured in this config. |
| 202 | * |
| 203 | * @return the alias count |
| 204 | */ |
| 205 | int aliasCount() { |
| 206 | return aliases.size(); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Returns the number of from...import lines configured in this config. |
| 211 | * |
| 212 | * @return the number of from...import lines |
| 213 | */ |
| 214 | int fromCount() { |
| 215 | return froms.size(); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Returns the substitution string for the given alias. |
| 220 | * |
| 221 | * @param a the alias |
| 222 | * @return the substitution |
| 223 | */ |
| 224 | String alias(String a) { |
| 225 | return aliasMap.get(a); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Returns the number of keys imported from the specified resource. |
| 230 | * |
| 231 | * @param res the resource |
| 232 | * @return number of keys imported from that resource |
| 233 | */ |
| 234 | int fromKeyCount(String res) { |
| 235 | Set<String> keys = fromMap.get(res); |
| 236 | return keys == null ? 0 : keys.size(); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Returns true if the specified resource exists and contains the |
| 241 | * given key. |
| 242 | * |
| 243 | * @param res the resource |
| 244 | * @param key the key |
| 245 | * @return true, if resource exists and contains the key; false otherwise |
| 246 | */ |
| 247 | boolean fromContains(String res, String key) { |
| 248 | Set<String> keys = fromMap.get(res); |
| 249 | return keys != null && keys.contains(key); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Returns the set of (expanded) "from" entries in this configuration. |
| 254 | * |
| 255 | * @return the entries |
| 256 | */ |
| 257 | public Set<CmdFrom> entries() { |
| 258 | return froms; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Returns the number of parse errors detected. |
| 263 | * |
| 264 | * @return number of bad lines |
| 265 | */ |
| 266 | public int errorCount() { |
| 267 | return badLines.size(); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Returns the lines that failed the parser. |
| 272 | * |
| 273 | * @return the erroneous lines in the config |
| 274 | */ |
| 275 | public List<String> errorLines() { |
| 276 | return ImmutableList.copyOf(badLines); |
| 277 | } |
| 278 | |
| 279 | // ==== Mini class hierarchy of command types |
| 280 | |
| 281 | private abstract static class Cmd { |
| 282 | final String orig; |
| 283 | boolean malformed = false; |
| 284 | |
| 285 | Cmd(String orig) { |
| 286 | this.orig = orig; |
| 287 | } |
Simon Hunt | d875465 | 2017-06-21 11:45:22 -0700 | [diff] [blame] | 288 | |
| 289 | /** |
| 290 | * Returns the original string from the configuration file. |
| 291 | * |
| 292 | * @return original from string |
| 293 | */ |
| 294 | public String orig() { |
| 295 | return orig; |
| 296 | } |
Simon Hunt | e556e94 | 2017-06-19 15:35:44 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | private static final class CmdBundle extends Cmd { |
| 300 | private final String id; |
| 301 | |
| 302 | private CmdBundle(String orig, String params) { |
| 303 | super(orig); |
| 304 | id = params; |
| 305 | } |
| 306 | |
| 307 | @Override |
| 308 | public String toString() { |
| 309 | return "CmdBundle{id=\"" + id + "\"}"; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | private static final class CmdAlias extends Cmd |
| 314 | implements Comparable<CmdAlias> { |
| 315 | private final String alias; |
| 316 | private final String subst; |
| 317 | |
| 318 | private CmdAlias(String orig, String params) { |
| 319 | super(orig); |
| 320 | int i = params.indexOf(SPC); |
| 321 | if (i < 1) { |
| 322 | malformed = true; |
| 323 | alias = null; |
| 324 | subst = null; |
| 325 | } else { |
| 326 | alias = params.substring(0, i); |
| 327 | subst = params.substring(i + 1); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | @Override |
| 332 | public String toString() { |
| 333 | return "CmdAlias{alias=\"" + alias + "\", subst=\"" + subst + "\"}"; |
| 334 | } |
| 335 | |
| 336 | @Override |
| 337 | public int compareTo(CmdAlias o) { |
| 338 | return alias.compareTo(o.alias); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Represents a "from {res} import {stuff}" command in the configuration. |
| 344 | */ |
| 345 | public static final class CmdFrom extends Cmd |
| 346 | implements Comparable<CmdFrom> { |
| 347 | private final String rawRes; |
| 348 | private final Set<String> keys; |
| 349 | private String expandedRes; |
| 350 | private boolean starred = false; |
| 351 | |
| 352 | private CmdFrom(String orig, String params) { |
| 353 | super(orig); |
| 354 | Matcher m = RE_IMPORT.matcher(params); |
| 355 | if (!m.matches()) { |
| 356 | malformed = true; |
| 357 | rawRes = null; |
| 358 | keys = null; |
| 359 | } else { |
| 360 | rawRes = m.group(1); |
| 361 | keys = genKeys(m.group(2)); |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | private Set<String> genKeys(String keys) { |
| 366 | String[] k = keys.split("\\s*,\\s*"); |
| 367 | Set<String> allKeys = new HashSet<>(); |
| 368 | Collections.addAll(allKeys, k); |
| 369 | return ImmutableSortedSet.copyOf(allKeys); |
| 370 | } |
| 371 | |
| 372 | private void expandAliasIfAny(Map<String, String> aliases) { |
| 373 | String expanded = rawRes; |
| 374 | int i = rawRes.indexOf(DOT); |
| 375 | if (i > 0) { |
| 376 | String alias = rawRes.substring(0, i); |
| 377 | String sub = aliases.get(alias); |
| 378 | if (sub != null) { |
| 379 | expanded = sub + rawRes.substring(i); |
| 380 | } |
| 381 | } |
| 382 | expandedRes = expanded; |
| 383 | } |
| 384 | |
| 385 | @Override |
| 386 | public String toString() { |
| 387 | return "CmdFrom{res=\"" + rawRes + "\", keys=" + keys + "}"; |
| 388 | } |
| 389 | |
| 390 | @Override |
| 391 | public int compareTo(CmdFrom o) { |
| 392 | return rawRes.compareTo(o.rawRes); |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * Returns the resource bundle name from which to import things. |
| 397 | * |
| 398 | * @return the resource bundle name |
| 399 | */ |
| 400 | public String res() { |
| 401 | return expandedRes; |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * Returns the set of keys which should be imported. |
| 406 | * |
| 407 | * @return the keys to import |
| 408 | */ |
| 409 | public Set<String> keys() { |
| 410 | return keys; |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * Returns true if this "from" command is importing ALL keys from |
| 415 | * the specified resource; false otherwise. |
| 416 | * |
| 417 | * @return true, if importing ALL keys; false otherwise |
| 418 | */ |
| 419 | public boolean starred() { |
| 420 | return starred; |
| 421 | } |
| 422 | } |
| 423 | } |