1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package com.puppycrawl.tools.checkstyle;
20
21 import com.google.common.collect.ImmutableMap;
22
23 import com.google.common.collect.Lists;
24 import com.google.common.collect.Maps;
25 import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
26 import com.puppycrawl.tools.checkstyle.api.Configuration;
27
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Set;
31
32
33
34
35
36 public final class DefaultConfiguration implements Configuration
37 {
38
39 private static final long serialVersionUID = 1157875385356127169L;
40
41
42 private final String name;
43
44
45 private final List<Configuration> children = Lists.newArrayList();
46
47
48 private final Map<String, String> attributeMap = Maps.newHashMap();
49
50
51 private final Map<String, String> messages = Maps.newHashMap();
52
53
54
55
56
57 public DefaultConfiguration(String name)
58 {
59 this.name = name;
60 }
61
62
63 @Override
64 public String[] getAttributeNames()
65 {
66 final Set<String> keySet = attributeMap.keySet();
67 return keySet.toArray(new String[keySet.size()]);
68 }
69
70
71 @Override
72 public String getAttribute(String name) throws CheckstyleException
73 {
74 if (!attributeMap.containsKey(name)) {
75
76 throw new CheckstyleException(
77 "missing key '" + name + "' in " + getName());
78 }
79 return attributeMap.get(name);
80 }
81
82
83 @Override
84 public Configuration[] getChildren()
85 {
86 return children.toArray(
87 new Configuration[children.size()]);
88 }
89
90
91 @Override
92 public String getName()
93 {
94 return name;
95 }
96
97
98
99
100
101 public void addChild(Configuration configuration)
102 {
103 children.add(configuration);
104 }
105
106
107
108
109
110 public void removeChild(final Configuration configuration)
111 {
112 children.remove(configuration);
113 }
114
115
116
117
118
119
120 public void addAttribute(String name, String value)
121 {
122 final String current = attributeMap.put(name, value);
123 if (null == current) {
124 attributeMap.put(name, value);
125 }
126 else {
127 attributeMap.put(name, current + "," + value);
128 }
129 }
130
131
132
133
134
135
136 public void addMessage(String key, String value)
137 {
138 messages.put(key, value);
139 }
140
141
142
143
144
145
146 @Override
147 public ImmutableMap<String, String> getMessages()
148 {
149 return ImmutableMap.copyOf(messages);
150 }
151 }