Coverage Report - org.ini4j.BasicProfileSection
 
Classes in this File Line Coverage Branch Coverage Complexity
BasicProfileSection
100%
49/49
100%
12/12
1.462
 
 1  
 /*
 2  
  * Copyright 2005,2009 Ivan SZKIBA
 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  
 package org.ini4j;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.List;
 20  
 import java.util.regex.Pattern;
 21  
 
 22  
 class BasicProfileSection extends BasicOptionMap implements Profile.Section
 23  
 {
 24  
     private static final long serialVersionUID = 985800697957194374L;
 25  1
     private static final String[] EMPTY_STRING_ARRAY = {};
 26  
     private static final char REGEXP_ESCAPE_CHAR = '\\';
 27  
     private final Pattern _childPattern;
 28  
     private final String _name;
 29  
     private final BasicProfile _profile;
 30  
 
 31  
     protected BasicProfileSection(BasicProfile profile, String name)
 32  602
     {
 33  602
         _profile = profile;
 34  602
         _name = name;
 35  602
         _childPattern = newChildPattern(name);
 36  602
     }
 37  
 
 38  
     @Override public Profile.Section getChild(String key)
 39  
     {
 40  19
         return _profile.get(childName(key));
 41  
     }
 42  
 
 43  
     @Override public String getName()
 44  
     {
 45  231
         return _name;
 46  
     }
 47  
 
 48  
     @Override public Profile.Section getParent()
 49  
     {
 50  10
         Profile.Section ret = null;
 51  10
         int idx = _name.lastIndexOf(_profile.getPathSeparator());
 52  
 
 53  10
         if (idx >= 0)
 54  
         {
 55  9
             String name = _name.substring(0, idx);
 56  
 
 57  9
             ret = _profile.get(name);
 58  
         }
 59  
 
 60  10
         return ret;
 61  
     }
 62  
 
 63  
     @Override public String getSimpleName()
 64  
     {
 65  15
         int idx = _name.lastIndexOf(_profile.getPathSeparator());
 66  
 
 67  15
         return (idx < 0) ? _name : _name.substring(idx + 1);
 68  
     }
 69  
 
 70  
     @Override public Profile.Section addChild(String key)
 71  
     {
 72  4
         String name = childName(key);
 73  
 
 74  4
         return _profile.add(name);
 75  
     }
 76  
 
 77  
     @Override public String[] childrenNames()
 78  
     {
 79  6
         List<String> names = new ArrayList<String>();
 80  
 
 81  6
         for (String key : _profile.keySet())
 82  
         {
 83  37
             if (_childPattern.matcher(key).matches())
 84  
             {
 85  15
                 names.add(key.substring(_name.length() + 1));
 86  
             }
 87  
         }
 88  
 
 89  6
         return names.toArray(EMPTY_STRING_ARRAY);
 90  
     }
 91  
 
 92  
     @Override public Profile.Section lookup(String... parts)
 93  
     {
 94  7
         StringBuilder buff = new StringBuilder();
 95  
 
 96  20
         for (String part : parts)
 97  
         {
 98  13
             if (buff.length() != 0)
 99  
             {
 100  6
                 buff.append(_profile.getPathSeparator());
 101  
             }
 102  
 
 103  13
             buff.append(part);
 104  
         }
 105  
 
 106  7
         return _profile.get(childName(buff.toString()));
 107  
     }
 108  
 
 109  
     @Override public void removeChild(String key)
 110  
     {
 111  1
         String name = childName(key);
 112  
 
 113  1
         _profile.remove(name);
 114  1
     }
 115  
 
 116  
     @Override boolean isPropertyFirstUpper()
 117  
     {
 118  1864
         return _profile.isPropertyFirstUpper();
 119  
     }
 120  
 
 121  
     @Override void resolve(StringBuilder buffer)
 122  
     {
 123  154
         _profile.resolve(buffer, this);
 124  154
     }
 125  
 
 126  
     private String childName(String key)
 127  
     {
 128  31
         StringBuilder buff = new StringBuilder(_name);
 129  
 
 130  31
         buff.append(_profile.getPathSeparator());
 131  31
         buff.append(key);
 132  
 
 133  31
         return buff.toString();
 134  
     }
 135  
 
 136  
     private Pattern newChildPattern(String name)
 137  
     {
 138  602
         StringBuilder buff = new StringBuilder();
 139  
 
 140  602
         buff.append('^');
 141  602
         buff.append(Pattern.quote(name));
 142  602
         buff.append(REGEXP_ESCAPE_CHAR);
 143  602
         buff.append(_profile.getPathSeparator());
 144  602
         buff.append("[^");
 145  602
         buff.append(REGEXP_ESCAPE_CHAR);
 146  602
         buff.append(_profile.getPathSeparator());
 147  602
         buff.append("]+$");
 148  
 
 149  602
         return Pattern.compile(buff.toString());
 150  
     }
 151  
 }