summaryrefslogtreecommitdiff
path: root/src/plugins/android.codeutils/src/com/motorola/studio/android/model/resources/types/AbstractResourceNode.java
blob: f6af4721a024472aa5d8edb6aef735b3e2cea0dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.motorola.studio.android.model.resources.types;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Abstract class used to describe a node of a resource file
 */
public abstract class AbstractResourceNode
{
    /**
     * Enumeration used to describe the resource node types 
     */
    public static enum NodeType
    {
        Resources, String, Color, Dimen, Drawable, Unknown
    }

    /**
     * The known attributes of a resource node
     */
    protected final Map<String, String> attributes;

    /**
     * The unknown attributes of a resource node
     */
    protected final Map<String, String> unknownAttributes;

    /**
     * The known child nodes of a resource
     */
    protected final List<AbstractResourceNode> children;

    /**
     * The unknown child nodes of a resource
     */
    protected final List<AbstractResourceNode> unknownChildren;

    /**
     * Checks if an attribute name is valid for a resource node
     * 
     * @param attributeName The attribute name
     * 
     * @return true if the attribute is acceptable for the node and false otherwise
     */
    protected abstract boolean isAttributeValid(String attributeName);

    /**
     * Retrieves the resource node type
     * 
     * @return the resource node type
     */
    public abstract NodeType getNodeType();

    /**
     * Checks if a node can be accepted as child node
     * 
     * @param node The node to be checked
     * 
     * @return true if the node can be a child node or false otherwise
     */
    protected abstract boolean canAddChildNode(AbstractResourceNode node);

    /**
     * Retrieves the node type based on a node name
     * 
     * @param nodeName the node name
     * 
     * @return the node type related to the node name. If the name cannot be associated 
     *         to any node type, the unknown type will be returned.
     */
    public static NodeType getNodeType(String nodeName)
    {
        NodeType nodeType = NodeType.Unknown;

        for (NodeType type : NodeType.values())
        {
            if (nodeName.trim().equalsIgnoreCase(getNodeTypeName(type)))
            {
                nodeType = type;
                break;
            }
        }

        return nodeType;
    }

    /**
     * Retrieves the node name based on the node type
     * 
     * @param nodeType The node type
     * 
     * @return The node name
     */
    public static String getNodeTypeName(NodeType nodeType)
    {
        String nodeName;

        switch (nodeType)
        {
            case Resources:
                nodeName = "resources";
                break;
            case String:
                nodeName = "string";
                break;
            case Color:
                nodeName = "color";
                break;
            case Dimen:
                nodeName = "dimen";
                break;
            case Drawable:
                nodeName = "drawable";
                break;
            default:
                nodeName = "unknown";
        }

        return nodeName;
    }

    /**
     * Default constructor
     */
    public AbstractResourceNode()
    {
        attributes = new HashMap<String, String>();
        unknownAttributes = new HashMap<String, String>();
        children = new LinkedList<AbstractResourceNode>();
        unknownChildren = new LinkedList<AbstractResourceNode>();
    }

    /**
     * Retrieves an attribute value from a resource node. The attribute must be valid.
     * 
     * @param attributeName The attribute name
     * @return The attribute value
     */
    public String getAttributeValue(String attributeName)
    {
        String attrValue = null;

        if (attributeName != null)
        {
            attrValue = attributes.get(attributeName);
        }

        return attrValue;
    }

    /**
     * Retrieves an array containing all known attributes of a resource node
     * 
     * @return an array containing all known attributes of a resource node
     */
    public String[] getAttributes()
    {
        String[] attrs = new String[attributes.size()];

        attrs = attributes.keySet().toArray(attrs);

        return attrs;
    }

    /**
     * Adds an unknown attribute to a resource node
     * 
     * @param attributeName The attribute name
     * @param attributeValue The attribute value
     * @return true if the attribute has been added or false otherwise. An attribute is not
     *         added if it exists.
     */
    public boolean addUnknownAttribute(String attributeName, String attributeValue)
    {
        boolean added = false;

        if (attributeName != null)
        {
            if (!isAttributeValid(attributeName))
            {
                if (!unknownAttributes.containsKey(attributeName))
                {
                    unknownAttributes.put(attributeName, attributeValue);
                    added = true;
                }
            }
        }

        return added;
    }

    /**
     * Removes an unknown attribute value from a resource node
     * 
     * @param attributeName The attribute name
     * @return true if the attribute has been removed or false otherwise.
     */
    public boolean removeUnknownAttribute(String attributeName)
    {
        boolean removed = false;

        if (attributeName != null)
        {
            if (unknownAttributes.containsKey(attributeName))
            {
                unknownAttributes.remove(attributeName);
                removed = true;
            }
        }

        return removed;
    }

    /**
     * Retrieves the value of an unknown attribute of a resource node
     * 
     * @param attributeName The attribute name
     * @return The attribute value
     */
    public String getUnknownAttributeValue(String attributeName)
    {
        String value = null;

        if (attributeName != null)
        {
            if (unknownAttributes.containsKey(attributeName))
            {
                value = unknownAttributes.get(attributeName);
            }
        }

        return value;
    }

    /**
     * Retrieves an array containing all unknown attribute names
     * 
     * @return an array containing all unknown attribute names
     */
    public String[] getUnknownAttributes()
    {
        String attributes[] = new String[this.unknownAttributes.size()];

        attributes = this.unknownAttributes.keySet().toArray(attributes);

        return attributes;
    }

    /**
     * Removes all unknown attributes
     */
    public void clearUnknownAttributes()
    {
        unknownAttributes.clear();
    }

    /**
     * Adds a child resource node to this node
     * 
     * @param node The node to be added
     * @return true if the node has been added or false otherwise
     */
    public boolean addChildNode(AbstractResourceNode node)
    {
        boolean added = false;

        if (node != null)
        {
            if (canAddChildNode(node))
            {
                if (!children.contains(node))
                {
                    children.add(node);
                    added = true;
                }
            }
            else
            {
                if (!unknownChildren.contains(node))
                {
                    unknownChildren.add(node);
                    added = true;
                }
            }
        }

        return added;
    }

    /**
     * Removes a child node from this node
     * 
     * @param node the node to be removed
     * @return true if the node has been removed or false otherwise
     */
    public boolean removeChildNode(AbstractResourceNode node)
    {
        boolean removed = false;

        if (node != null)
        {
            if (children.contains(node))
            {
                children.remove(node);
                removed = true;
            }
            else if (unknownChildren.contains(node))
            {
                unknownChildren.remove(node);
                removed = true;
            }
        }

        return removed;
    }

    /**
     * Retrieves an array containing all child nodes of this node
     * 
     * @return an array containing all child nodes of this node
     */
    public AbstractResourceNode[] getChildNodes()
    {
        AbstractResourceNode[] childNodes = new AbstractResourceNode[children.size()];

        childNodes = children.toArray(childNodes);

        return childNodes;
    }

    /**
     * Retrieves an array containing all unknown child nodes of this node
     * 
     * @return an array containing all unknown child nodes of this node
     */
    public AbstractResourceNode[] getUnknownChildNodes()
    {
        AbstractResourceNode[] childNodes = new AbstractResourceNode[unknownChildren.size()];

        childNodes = unknownChildren.toArray(childNodes);

        return childNodes;
    }

    /**
     * Removes all child nodes of this node
     */
    public void clearChildNodes()
    {
        children.clear();
    }

    /**
     * Removes all unknown child nodes of this node
     */
    public void clearUnknownChildNodes()
    {
        unknownChildren.clear();
    }

    /**
     * Retrieves the name of this node
     * 
     * @return the name of this node
     */
    public String getNodeName()
    {
        return getNodeTypeName(getNodeType());
    }
}