1/*
2 * Copyright (C) 2012 The Android Open Source Project
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
17package com.android.sdklib.internal.build;
18
19import com.android.SdkConstants;
20import com.android.sdklib.internal.build.SymbolLoader.SymbolEntry;
21import com.google.common.base.Charsets;
22import com.google.common.base.Splitter;
23import com.google.common.collect.Table;
24import com.google.common.io.Closeables;
25import com.google.common.io.Files;
26
27import java.io.BufferedWriter;
28import java.io.File;
29import java.io.IOException;
30import java.util.Map;
31
32/**
33 */
34public class SymbolWriter {
35
36    private final String mOutFolder;
37    private final String mPackageName;
38    private final SymbolLoader mSymbols;
39    private final SymbolLoader mValues;
40
41    public SymbolWriter(String outFolder, String packageName, SymbolLoader symbols,
42            SymbolLoader values) {
43        mOutFolder = outFolder;
44        mPackageName = packageName;
45        mSymbols = symbols;
46        mValues = values;
47    }
48
49    public void write() throws IOException {
50        Splitter splitter = Splitter.on('.');
51        Iterable<String> folders = splitter.split(mPackageName);
52        File file = new File(mOutFolder);
53        for (String folder : folders) {
54            file = new File(file, folder);
55        }
56        file.mkdirs();
57        file = new File(file, SdkConstants.FN_RESOURCE_CLASS);
58
59        BufferedWriter writer = null;
60        try {
61            writer = Files.newWriter(file, Charsets.UTF_8);
62
63            writer.write("/* AUTO-GENERATED FILE.  DO NOT MODIFY.\n");
64            writer.write(" *\n");
65            writer.write(" * This class was automatically generated by the\n");
66            writer.write(" * aapt tool from the resource data it found.  It\n");
67            writer.write(" * should not be modified by hand.\n");
68            writer.write(" */\n");
69
70            writer.write("package ");
71            writer.write(mPackageName);
72            writer.write(";\n\npublic final class R {\n");
73
74            Table<String, String, SymbolEntry> symbols = mSymbols.getSymbols();
75            Table<String, String, SymbolEntry> values = mValues.getSymbols();
76
77            for (String row : symbols.rowKeySet()) {
78                writer.write("\tpublic static final class ");
79                writer.write(row);
80                writer.write(" {\n");
81
82                for (Map.Entry<String, SymbolEntry> symbol : symbols.row(row).entrySet()) {
83                    // get the matching SymbolEntry from the values Table.
84                    SymbolEntry value = values.get(row, symbol.getKey());
85                    if (value != null) {
86                        writer.write("\t\tpublic static final ");
87                        writer.write(value.getType());
88                        writer.write(" ");
89                        writer.write(value.getName());
90                        writer.write(" = ");
91                        writer.write(value.getValue());
92                        writer.write(";\n");
93                    }
94                }
95
96                writer.write("\t}\n");
97            }
98
99            writer.write("}\n");
100        } finally {
101            Closeables.closeQuietly(writer);
102        }
103    }
104}