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
|
// 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.base.Function;
import com.google.common.base.MoreObjects;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.android.AndroidDataWritingVisitor;
import com.google.devtools.build.android.FullyQualifiedName;
import com.google.devtools.build.android.XmlResourceValue;
import com.google.devtools.build.android.XmlResourceValues;
import com.google.devtools.build.android.proto.SerializeFormat;
import com.google.devtools.build.android.proto.SerializeFormat.DataValue.Builder;
import com.google.devtools.build.android.proto.SerializeFormat.DataValueXml.XmlType;
import com.google.protobuf.CodedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Path;
import java.util.Map.Entry;
import java.util.Objects;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
/**
* Represents an Android Plural Resource.
*
* <p>
* Plurals are a localization construct (http://developer.android.com/guide/topics/resources/
* string-resource.html#Plurals) that are basically a map of key to value. They are defined in xml
* as: <code>
* <plurals name="plural_name">
* <item quantity=["zero" | "one" | "two" | "few" | "many" | "other"]>
* text_string
* </item>
* </plurals>
* </code>
*/
@Immutable
public class PluralXmlResourceValue implements XmlResourceValue {
public static final Function<Entry<String, String>, String> ENTRY_TO_PLURAL =
new Function<Entry<String, String>, String>() {
@Nullable
@Override
public String apply(Entry<String, String> input) {
return String.format("<item quantity='%s'>%s</item>", input.getKey(), input.getValue());
}
};
private final ImmutableMap<String, String> values;
private PluralXmlResourceValue(ImmutableMap<String, String> values) {
this.values = values;
}
public static XmlResourceValue of(ImmutableMap<String, String> values) {
return new PluralXmlResourceValue(values);
}
@Override
public void write(
FullyQualifiedName key, Path source, AndroidDataWritingVisitor mergedDataWriter) {
mergedDataWriter.writeToValuesXml(
key,
FluentIterable.from(
ImmutableList.of(
String.format("<!-- %s -->", source),
String.format("<plurals name='%s'>", key.name())))
.append(FluentIterable.from(values.entrySet()).transform(ENTRY_TO_PLURAL))
.append("</plurals>"));
}
@Override
public int hashCode() {
return values.hashCode();
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof PluralXmlResourceValue)) {
return false;
}
PluralXmlResourceValue other = (PluralXmlResourceValue) obj;
return Objects.equals(values, other.values);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass()).add("values", values).toString();
}
public static XmlResourceValue from(SerializeFormat.DataValueXml proto) {
return of(ImmutableMap.copyOf(proto.getMappedStringValue()));
}
@Override
public int serializeTo(Path source, OutputStream output) throws IOException {
Builder builder = XmlResourceValues.newProtoDataBuilder(source);
SerializeFormat.DataValue value =
builder
.setXmlValue(
builder
.getXmlValueBuilder()
.setType(XmlType.PLURAL)
.putAllMappedStringValue(values))
.build();
value.writeDelimitedTo(output);
return CodedOutputStream.computeUInt32SizeNoTag(value.getSerializedSize())
+ value.getSerializedSize();
}
}
|