/* * 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.android.sdklib.internal.build; import com.android.SdkConstants; import com.android.sdklib.internal.build.SymbolLoader.SymbolEntry; import com.google.common.base.Charsets; import com.google.common.base.Splitter; import com.google.common.collect.Table; import com.google.common.io.Closeables; import com.google.common.io.Files; import java.io.BufferedWriter; import java.io.File; import java.io.IOException; import java.util.Map; /** */ public class SymbolWriter { private final String mOutFolder; private final String mPackageName; private final SymbolLoader mSymbols; private final SymbolLoader mValues; public SymbolWriter(String outFolder, String packageName, SymbolLoader symbols, SymbolLoader values) { mOutFolder = outFolder; mPackageName = packageName; mSymbols = symbols; mValues = values; } @SuppressWarnings("resource") // Eclipse does handle Closeables.closeQuietly; see E#381445 public void write() throws IOException { Splitter splitter = Splitter.on('.'); Iterable<String> folders = splitter.split(mPackageName); File file = new File(mOutFolder); for (String folder : folders) { file = new File(file, folder); } file.mkdirs(); file = new File(file, SdkConstants.FN_RESOURCE_CLASS); BufferedWriter writer = null; try { writer = Files.newWriter(file, Charsets.UTF_8); writer.write("/* AUTO-GENERATED FILE. DO NOT MODIFY.\n"); writer.write(" *\n"); writer.write(" * This class was automatically generated by the\n"); writer.write(" * aapt tool from the resource data it found. It\n"); writer.write(" * should not be modified by hand.\n"); writer.write(" */\n"); writer.write("package "); writer.write(mPackageName); writer.write(";\n\npublic final class R {\n"); Table<String, String, SymbolEntry> symbols = mSymbols.getSymbols(); Table<String, String, SymbolEntry> values = mValues.getSymbols(); for (String row : symbols.rowKeySet()) { writer.write("\tpublic static final class "); writer.write(row); writer.write(" {\n"); for (Map.Entry<String, SymbolEntry> symbol : symbols.row(row).entrySet()) { // get the matching SymbolEntry from the values Table. SymbolEntry value = values.get(row, symbol.getKey()); if (value != null) { writer.write("\t\tpublic static final "); writer.write(value.getType()); writer.write(" "); writer.write(value.getName()); writer.write(" = "); writer.write(value.getValue()); writer.write(";\n"); } } writer.write("\t}\n"); } writer.write("}\n"); } finally { Closeables.closeQuietly(writer); } } }