aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/tools/android/java/com/google/devtools/build/android/xml/AttrXmlResourceValue.java
blob: 52f44dbc6ec05a380d40f01f8d18fce0f6ceeeb1 (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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
// Copyright 2016 The Bazel Authors. All rights reserved.
//
// 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.google.devtools.build.android.xml;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
import com.google.devtools.build.android.FullyQualifiedName;
import com.google.devtools.build.android.XmlResourceValue;
import com.google.devtools.build.android.XmlResourceValues;

import java.io.Writer;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import javax.annotation.Nullable;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

/**
 * Represents an Android Resource custom attribute.
 *
 * <p>Attribute are the most complicated Android resource, and therefore the least documented. Most
 * of the information about them is found by reading the android compatibility library source. An
 * Attribute defines a parameter that can be passed into a view class -- as such you can think of
 * attributes as creating slots for other resources to fit into. Each slot will have at least one
 * format, and can have multiples. Simple attributes (color, boolean, reference, dimension,
 * float, integer, string, and fraction) are defined as &lt;attr name="<em>name</em>"
 * format="<em>format</em>" /&gt; while the complex ones, flag and enum, have sub tags:
 * &lt;attr name="<em>name</em>" &gt&lt;flag name="<em>name</em>" value="<em>value</em>"&gt;
 * &lt;/attr&gt;.
 *
 * <p>Attributes also have a double duty as defining validation logic for layout resources --
 * each layout attribute *must* have a corresponding attribute which will be used to validate the
 * value/resource reference defined in it.
 *
 * <p>AttrXmlValue, due to the mutiple types of attributes is actually a composite class that
 * contains multiple XmlValue instances for each resource.
 */
public class AttrXmlResourceValue implements XmlResourceValue {

  private static final String FRACTION = "fraction";
  private static final String STRING = "string";
  private static final String INTEGER = "integer";
  private static final String FLOAT = "float";
  private static final String DIMENSION = "dimension";
  private static final String BOOLEAN = "boolean";
  private static final String COLOR = "color";
  private static final String REFERENCE = "reference";
  private static final String ENUM = "enum";
  private static final String FLAG = "flag";
  private static final QName TAG_ENUM = QName.valueOf(ENUM);
  private static final QName TAG_FLAG = QName.valueOf(FLAG);
  private final Map<String, XmlResourceValue> formats;

  private AttrXmlResourceValue(Map<String, XmlResourceValue> formats) {
    this.formats = formats;
  }

  private static Map<String, String> readSubValues(XMLEventReader reader, QName subTagType)
      throws XMLStreamException {
    Map<String, String> enumValue = new HashMap<>();
    while (reader.hasNext()
        && XmlResourceValues.isTag(XmlResourceValues.peekNextTag(reader), subTagType)) {
      StartElement element = reader.nextEvent().asStartElement();
      enumValue.put(
          XmlResourceValues.getElementName(element), XmlResourceValues.getElementValue(element));
      XMLEvent endTag = reader.nextEvent();
      if (!XmlResourceValues.isEndTag(endTag, subTagType)) {
        throw new XMLStreamException(
            String.format("Unexpected [%s]; Expected %s", endTag, "</enum>"), endTag.getLocation());
      }
    }
    return enumValue;
  }

  private static void endAttrElement(XMLEventReader reader) throws XMLStreamException {
    XMLEvent endTag = reader.nextTag();
    if (!endTag.isEndElement() || !QName.valueOf("attr").equals(endTag.asEndElement().getName())) {
      throw new XMLStreamException("Unexpected Tag:" + endTag, endTag.getLocation());
    }
  }

  @VisibleForTesting
  private static final class BuilderEntry implements Map.Entry<String, XmlResourceValue> {
    private final String name;
    private final XmlResourceValue value;

    public BuilderEntry(String name, XmlResourceValue value) {
      this.name = name;
      this.value = value;
    }

    @Override
    public String getKey() {
      return name;
    }

    @Override
    public XmlResourceValue getValue() {
      return value;
    }

    @Override
    public XmlResourceValue setValue(XmlResourceValue value) {
      throw new UnsupportedOperationException();
    }
  }

  @SafeVarargs
  @VisibleForTesting
  public static XmlResourceValue fromFormatEntries(Map.Entry<String, XmlResourceValue>... entries) {
    return of(ImmutableMap.copyOf(Arrays.asList(entries)));
  }

  public static XmlResourceValue from(
      StartElement attr, @Nullable String format, XMLEventReader eventReader)
      throws XMLStreamException {
    Set<String> formatNames = new HashSet<>();
    if (format != null) {
      Collections.addAll(formatNames, format.split("\\|"));
    }
    XMLEvent nextTag = XmlResourceValues.peekNextTag(eventReader);
    if (nextTag != null && nextTag.isStartElement()) {
      formatNames.add(nextTag.asStartElement().getName().getLocalPart().toLowerCase());
    }

    Map<String, XmlResourceValue> formats = new HashMap<>();
    for (String formatName : formatNames) {
      switch (formatName) {
        case FLAG:
          Map<String, String> flags = readSubValues(eventReader, TAG_FLAG);
          endAttrElement(eventReader);
          formats.put(formatName, AttrFlagXmlValue.of(flags));
          break;
        case ENUM:
          Map<String, String> enums = readSubValues(eventReader, TAG_ENUM);
          endAttrElement(eventReader);
          formats.put(formatName, AttrEnumXmlValue.of(enums));
          break;
        case REFERENCE:
          formats.put(formatName, AttrReferenceXmlValue.of());
          break;
        case COLOR:
          formats.put(formatName, AttrColorXmlValue.of());
          break;
        case BOOLEAN:
          formats.put(formatName, AttrBooleanXmlValue.of());
          break;
        case DIMENSION:
          formats.put(formatName, AttrDimensionXmlValue.of());
          break;
        case FLOAT:
          formats.put(formatName, AttrFloatXmlValue.of());
          break;
        case INTEGER:
          formats.put(formatName, AttrIntegerXmlValue.of());
          break;
        case STRING:
          formats.put(formatName, AttrStringXmlValue.of());
          break;
        case FRACTION:
          formats.put(formatName, AttrFractionXmlValue.of());
          break;
        default:
          throw new XMLStreamException(
              String.format("Unexpected attr format: %S", formatName), attr.getLocation());
      }
    }
    return of(formats);
  }

  public static XmlResourceValue of(Map<String, XmlResourceValue> formats) {
    return new AttrXmlResourceValue(formats);
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    AttrXmlResourceValue other = (AttrXmlResourceValue) o;
    return Objects.equals(formats, other.formats);
  }

  @Override
  public int hashCode() {
    return formats.hashCode();
  }

  @Override
  public String toString() {
    return MoreObjects.toStringHelper(this).add("formats", formats).toString();
  }

  @Override
  public void write(Writer buffer, FullyQualifiedName name) {
    // TODO(corysmith): Implement write.
    throw new UnsupportedOperationException();
  }

  /** Represents an Android Enum Attribute resource. */
  @VisibleForTesting
  public static class AttrEnumXmlValue implements XmlResourceValue {

    private Map<String, String> values;

    private AttrEnumXmlValue(Map<String, String> values) {
      this.values = values;
    }

    @VisibleForTesting
    public static Map.Entry<String, XmlResourceValue> asEntryOf(String... keyThenValue) {
      Preconditions.checkArgument(keyThenValue.length > 0);
      Preconditions.checkArgument(keyThenValue.length % 2 == 0);
      Builder<String, String> builder = ImmutableMap.<String, String>builder();
      for (int i = 0; i < keyThenValue.length; i += 2) {
        builder.put(keyThenValue[i], keyThenValue[i + 1]);
      }
      return new BuilderEntry(ENUM, of(builder.build()));
    }

    public static XmlResourceValue of(Map<String, String> values) {
      return new AttrEnumXmlValue(values);
    }

    @Override
    public void write(Writer buffer, FullyQualifiedName name) {
      // TODO(corysmith): Implement write.
      throw new UnsupportedOperationException();
    }

    @Override
    public int hashCode() {
      return values.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
      if (!(obj instanceof AttrEnumXmlValue)) {
        return false;
      }
      AttrEnumXmlValue other = (AttrEnumXmlValue) obj;
      return Objects.equals(values, other.values);
    }

    @Override
    public String toString() {
      return MoreObjects.toStringHelper(getClass()).add("values", values).toString();
    }
  }

  /** Represents an Android Flag Attribute resource. */
  @VisibleForTesting
  public static class AttrFlagXmlValue implements XmlResourceValue {

    private Map<String, String> values;

    private AttrFlagXmlValue(Map<String, String> values) {
      this.values = values;
    }

    public static XmlResourceValue of(Map<String, String> values) {
      return new AttrFlagXmlValue(values);
    }

    @VisibleForTesting
    public static Map.Entry<String, XmlResourceValue> asEntryOf(String... keyThenValue) {
      Builder<String, String> builder = ImmutableMap.<String, String>builder();
      Preconditions.checkArgument(keyThenValue.length > 0);
      Preconditions.checkArgument(keyThenValue.length % 2 == 0);
      for (int i = 0; i < keyThenValue.length; i += 2) {
        builder.put(keyThenValue[i], keyThenValue[i + 1]);
      }
      return new BuilderEntry(FLAG, of(builder.build()));
    }

    @Override
    public void write(Writer buffer, FullyQualifiedName name) {
      // TODO(corysmith): Implement write.
      throw new UnsupportedOperationException();
    }

    @Override
    public int hashCode() {
      return values.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
      if (!(obj instanceof AttrFlagXmlValue)) {
        return false;
      }
      AttrFlagXmlValue other = (AttrFlagXmlValue) obj;
      return Objects.equals(values, other.values);
    }

    @Override
    public String toString() {
      return MoreObjects.toStringHelper(getClass()).add("values", values).toString();
    }
  }

  /** Represents an Android Reference Attribute resource. */
  @VisibleForTesting
  public static class AttrReferenceXmlValue implements XmlResourceValue {
    private static final AttrReferenceXmlValue INSTANCE = new AttrReferenceXmlValue();

    public static XmlResourceValue of() {
      return INSTANCE;
    }

    @VisibleForTesting
    public static BuilderEntry asEntry() {
      return new BuilderEntry(REFERENCE, of());
    }

    @Override
    public void write(Writer buffer, FullyQualifiedName name) {
      // TODO(corysmith): Implement write.
      throw new UnsupportedOperationException();
    }
  }

  /** Represents an Android Color Attribute resource. */
  @VisibleForTesting
  public static class AttrColorXmlValue implements XmlResourceValue {
    private static final AttrColorXmlValue INSTANCE = new AttrColorXmlValue();

    public static XmlResourceValue of() {
      return INSTANCE;
    }

    @VisibleForTesting
    public static BuilderEntry asEntry() {
      return new BuilderEntry(COLOR, of());
    }

    @Override
    public void write(Writer buffer, FullyQualifiedName name) {
      // TODO(corysmith): Implement write.
      throw new UnsupportedOperationException();
    }
  }

  /** Represents an Android Boolean Attribute resource. */
  @VisibleForTesting
  public static class AttrBooleanXmlValue implements XmlResourceValue {
    private static final AttrBooleanXmlValue INSTANCE = new AttrBooleanXmlValue();

    public static XmlResourceValue of() {
      return INSTANCE;
    }

    @VisibleForTesting
    public static BuilderEntry asEntry() {
      return new BuilderEntry(BOOLEAN, of());
    }

    @Override
    public void write(Writer buffer, FullyQualifiedName name) {
      // TODO(corysmith): Implement write.
      throw new UnsupportedOperationException();
    }
  }

  /** Represents an Android Float Attribute resource. */
  @VisibleForTesting
  public static class AttrFloatXmlValue implements XmlResourceValue {
    private static final AttrFloatXmlValue INSTANCE = new AttrFloatXmlValue();

    public static XmlResourceValue of() {
      return INSTANCE;
    }

    @VisibleForTesting
    public static BuilderEntry asEntry() {
      return new BuilderEntry(FLOAT, of());
    }

    @Override
    public void write(Writer buffer, FullyQualifiedName name) {
      // TODO(corysmith): Implement write.
      throw new UnsupportedOperationException();
    }
  }

  /** Represents an Android Dimension Attribute resource. */
  @VisibleForTesting
  public static class AttrDimensionXmlValue implements XmlResourceValue {
    private static final AttrDimensionXmlValue INSTANCE = new AttrDimensionXmlValue();

    @VisibleForTesting
    public static BuilderEntry asEntry() {
      return new BuilderEntry(DIMENSION, of());
    }

    public static XmlResourceValue of() {
      return INSTANCE;
    }

    @Override
    public void write(Writer buffer, FullyQualifiedName name) {
      // TODO(corysmith): Implement write.
      throw new UnsupportedOperationException();
    }
  }

  /** Represents an Android Integer Attribute resource. */
  @VisibleForTesting
  public static class AttrIntegerXmlValue implements XmlResourceValue {
    private static final AttrIntegerXmlValue INSTANCE = new AttrIntegerXmlValue();

    public static XmlResourceValue of() {
      return INSTANCE;
    }

    @VisibleForTesting
    public static BuilderEntry asEntry() {
      return new BuilderEntry(INTEGER, of());
    }

    @Override
    public void write(Writer buffer, FullyQualifiedName name) {
      // TODO(corysmith): Implement write.
      throw new UnsupportedOperationException();
    }
  }

  /** Represents an Android String Attribute resource. */
  @VisibleForTesting
  public static class AttrStringXmlValue implements XmlResourceValue {
    private static final AttrStringXmlValue INSTANCE = new AttrStringXmlValue();

    public static XmlResourceValue of() {
      return INSTANCE;
    }

    @VisibleForTesting
    public static BuilderEntry asEntry() {
      return new BuilderEntry(STRING, of());
    }

    @Override
    public void write(Writer buffer, FullyQualifiedName name) {
      // TODO(corysmith): Implement write.
      throw new UnsupportedOperationException();
    }
  }

  /** Represents an Android Fraction Attribute resource. */
  @VisibleForTesting
  public static class AttrFractionXmlValue implements XmlResourceValue {
    private static final AttrFractionXmlValue INSTANCE = new AttrFractionXmlValue();

    public static XmlResourceValue of() {
      return INSTANCE;
    }

    @VisibleForTesting
    public static BuilderEntry asEntry() {
      return new BuilderEntry(FRACTION, of());
    }

    @Override
    public void write(Writer buffer, FullyQualifiedName name) {
      // TODO(corysmith): Implement write.
      throw new UnsupportedOperationException();
    }
  }
}