View Javadoc

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      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      {
33          _profile = profile;
34          _name = name;
35          _childPattern = newChildPattern(name);
36      }
37  
38      @Override public Profile.Section getChild(String key)
39      {
40          return _profile.get(childName(key));
41      }
42  
43      @Override public String getName()
44      {
45          return _name;
46      }
47  
48      @Override public Profile.Section getParent()
49      {
50          Profile.Section ret = null;
51          int idx = _name.lastIndexOf(_profile.getPathSeparator());
52  
53          if (idx >= 0)
54          {
55              String name = _name.substring(0, idx);
56  
57              ret = _profile.get(name);
58          }
59  
60          return ret;
61      }
62  
63      @Override public String getSimpleName()
64      {
65          int idx = _name.lastIndexOf(_profile.getPathSeparator());
66  
67          return (idx < 0) ? _name : _name.substring(idx + 1);
68      }
69  
70      @Override public Profile.Section addChild(String key)
71      {
72          String name = childName(key);
73  
74          return _profile.add(name);
75      }
76  
77      @Override public String[] childrenNames()
78      {
79          List<String> names = new ArrayList<String>();
80  
81          for (String key : _profile.keySet())
82          {
83              if (_childPattern.matcher(key).matches())
84              {
85                  names.add(key.substring(_name.length() + 1));
86              }
87          }
88  
89          return names.toArray(EMPTY_STRING_ARRAY);
90      }
91  
92      @Override public Profile.Section lookup(String... parts)
93      {
94          StringBuilder buff = new StringBuilder();
95  
96          for (String part : parts)
97          {
98              if (buff.length() != 0)
99              {
100                 buff.append(_profile.getPathSeparator());
101             }
102 
103             buff.append(part);
104         }
105 
106         return _profile.get(childName(buff.toString()));
107     }
108 
109     @Override public void removeChild(String key)
110     {
111         String name = childName(key);
112 
113         _profile.remove(name);
114     }
115 
116     @Override boolean isPropertyFirstUpper()
117     {
118         return _profile.isPropertyFirstUpper();
119     }
120 
121     @Override void resolve(StringBuilder buffer)
122     {
123         _profile.resolve(buffer, this);
124     }
125 
126     private String childName(String key)
127     {
128         StringBuilder buff = new StringBuilder(_name);
129 
130         buff.append(_profile.getPathSeparator());
131         buff.append(key);
132 
133         return buff.toString();
134     }
135 
136     private Pattern newChildPattern(String name)
137     {
138         StringBuilder buff = new StringBuilder();
139 
140         buff.append('^');
141         buff.append(Pattern.quote(name));
142         buff.append(REGEXP_ESCAPE_CHAR);
143         buff.append(_profile.getPathSeparator());
144         buff.append("[^");
145         buff.append(REGEXP_ESCAPE_CHAR);
146         buff.append(_profile.getPathSeparator());
147         buff.append("]+$");
148 
149         return Pattern.compile(buff.toString());
150     }
151 }