##### Coverage Report - org.webmacro.util.SubSettings

Classes in this File | Line Coverage | Branch Coverage | Complexity
SubSettings |
100% | 14/14
83% | 5/6
| 1.6;1.6

1 |  |
     /*
2 |  |
      * Copyright (C) 1998-2000 Semiotek Inc.  All Rights Reserved.
3 |  |
      *
4 |  |
      * Redistribution and use in source and binary forms, with or without
5 |  |
      * modification, are permitted under the terms of either of the following
6 |  |
      * Open Source licenses:
7 |  |
      *
8 |  |
      * The GNU General Public License, version 2, or any later version, as
9 |  |
      * published by the Free Software Foundation
10 |  |
      * (http://www.fsf.org/copyleft/gpl.html);
11 |  |
      *
12 |  |
      *  or
13 |  |
      *
14 |  |
      * The Semiotek Public License (http://webmacro.org/LICENSE.)
15 |  |
      *
16 |  |
      * This software is provided "as is", with NO WARRANTY, not even the
17 |  |
      * implied warranties of fitness to purpose, or merchantability. You
18 |  |
      * assume all risks and liabilities associated with its use.
19 |  |
      *
20 |  |
      * See www.webmacro.org for more information on the WebMacro project.
21 |  |
      */
22 |  |
23 |  |
24 |  |
     package org.webmacro.util;
25 |  |
26 |  |
     import java.util.ArrayList;
27 |  |
28 |  |
     /**
29 |  |
      * Models the subsettings of a setting.
30 |  |
      */
31 |  |
     public class SubSettings extends Settings
32 |  |
     {
33 |  |
34 |  |
         private final Settings _settings;
35 |  |
         private final String _prefix;
36 |  |
37 |  |
         /**
38 |  |
          * Get a subset of the settings in this Settings. The
39 |  |
          * returned Settings object will be just those settings
40 |  |
          * beginning with the supplied prefix, with the
41 |  |
          * prefix chopped off. So if the Settings had
42 |  |
          * a setting "LogLevel.foo" then the settings file
43 |  |
          * returned by getSubSettings("LogLevel") would contain
44 |  |
          * the key "foo".
45 |  |
          */
46 |  |
         public SubSettings (Settings settings, String prefix)
47 |  |
         {
48 | 84 |
             super();
49 | 84 |
             _settings = settings;
50 | 84 |
             _prefix = (prefix == null) ? "" : prefix + ".";
51 | 84 |
         }
52 |  |
53 |  |
         public String[] getKeys ()
54 |  |
         {
55 | 60 |
             String[] underlyingKeys = _settings.getKeys();
56 | 60 |
             ArrayList al = new ArrayList(underlyingKeys.length);
57 | 6060 |
             for (int i = 0; i < underlyingKeys.length; i++)
58 |  |
             {
59 | 6000 |
                 String key = underlyingKeys[i];
60 | 6000 |
                 if (key.startsWith(_prefix))
61 | 372 |
                     al.add(key.substring(_prefix.length()));
62 |  |
             }
63 | 60 |
             return (String[]) al.toArray(stringArray);
64 |  |
         }
65 |  |
66 |  |
         /**
67 |  |
          * Prefix the key for use with the underlying Settings
68 |  |
          */
69 |  |
         private String prefix (String key)
70 |  |
         {
71 | 504 |
             return _prefix + key;
72 |  |
         }
73 |  |
74 |  |
         /**
75 |  |
          * Find out if a setting is defined
76 |  |
          */
77 |  |
         public boolean containsKey (String key)
78 |  |
         {
79 | 96 |
             return _settings.containsKey(prefix(key));
80 |  |
         }
81 |  |
82 |  |
         /**
83 |  |
          * Get a setting
84 |  |
          */
85 |  |
         public String getSetting (String key)
86 |  |
         {
87 | 408 |
             return _settings.getSetting(prefix(key));
88 |  |
         }
89 |  |
     }
90 |  |