Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/com/sun/tools/sjavac/CompileProperties.java
38899 views
/*1* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.sjavac;2627import java.io.*;28import java.net.URI;29import java.text.MessageFormat;30import java.util.ArrayList;31import java.util.Collections;32import java.util.Iterator;33import java.util.List;34import java.util.Properties;35import java.util.Set;36import java.util.HashSet;37import java.util.Map;3839/**40* Compile properties transform a properties file into a Java source file.41* Java has built in support for reading properties from either a text file42* in the source or a compiled java source file.43*44* <p><b>This is NOT part of any supported API.45* If you write code that depends on this, you do so at your own46* risk. This code and its internal interfaces are subject to change47* or deletion without notice.</b></p>48*/49public class CompileProperties implements Transformer50{51// Any extra information passed from the command line, for example if:52// -tr .proppp=com.sun.tools.javac.smart.CompileProperties,sun.util.resources.LocaleNamesBundle53// then extra will be "sun.util.resources.LocaleNamesBundle"54String extra;5556public void setExtra(String e) {57extra = e;58}5960public void setExtra(String[] a) {61}6263public boolean transform(Map<String,Set<URI>> pkgSrcs,64Set<URI> visibleSrcs,65Map<URI,Set<String>> visibleClasses,66Map<String,Set<String>> oldPackageDependents,67URI destRoot,68Map<String,Set<URI>> packageArtifacts,69Map<String,Set<String>> packageDependencies,70Map<String,String> packagePublicApis,71int debugLevel,72boolean incremental,73int numCores,74PrintStream out,75PrintStream err) {76boolean rc = true;77for (String pkgName : pkgSrcs.keySet()) {78String pkgNameF = Util.toFileSystemPath(pkgName);79for (URI u : pkgSrcs.get(pkgName)) {80File src = new File(u);81boolean r = compile(pkgName, pkgNameF, src, new File(destRoot), debugLevel,82packageArtifacts);83if (r == false) {84rc = false;85}86}87}88return rc;89}9091boolean compile(String pkgName, String pkgNameF, File src, File destRoot, int debugLevel,92Map<String,Set<URI>> packageArtifacts)93{94String superClass = "java.util.ListResourceBundle";9596if (extra != null) {97superClass = extra;98}99// Load the properties file.100Properties p = new Properties();101try {102p.load(new FileInputStream(src));103} catch (IOException e) {104Log.error("Error reading file "+src.getPath());105return false;106}107108// Calculate the name of the Java source file to be generated.109int dp = src.getName().lastIndexOf(".");110String classname = src.getName().substring(0,dp);111112// Sort the properties in increasing key order.113List<String> sortedKeys = new ArrayList<String>();114for (Object key : p.keySet()) {115sortedKeys.add((String)key);116}117Collections.sort(sortedKeys);118Iterator<String> keys = sortedKeys.iterator();119120// Collect the properties into a string buffer.121StringBuilder data = new StringBuilder();122while (keys.hasNext()) {123String key = keys.next();124data.append(" { \"" + escape(key) + "\", \"" +125escape((String)p.get(key)) + "\" },\n");126}127128// Create dest file name. It is derived from the properties file name.129String destFilename = destRoot.getPath()+File.separator+pkgNameF+File.separator+classname+".java";130File dest = new File(destFilename);131132// Make sure the dest directories exist.133if (!dest.getParentFile().isDirectory()) {134if (!dest.getParentFile().mkdirs()) {135Log.error("Could not create the directory "+dest.getParentFile().getPath());136return false;137}138}139140Set<URI> as = packageArtifacts.get(pkgName);141if (as == null) {142as = new HashSet<URI>();143packageArtifacts.put(pkgName, as);144}145as.add(dest.toURI());146147if (dest.exists() && dest.lastModified() > src.lastModified()) {148// A generated file exists, and its timestamp is newer than the source.149// Assume that we do not need to regenerate the dest file!150// Thus we are done.151return true;152}153154String packageString = "package " + pkgNameF.replace(File.separatorChar,'.') + ";\n\n";155156Log.info("Compiling property file "+pkgNameF+File.separator+src.getName());157try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dest)))) {158MessageFormat format = new MessageFormat(FORMAT);159writer.write(format.format(new Object[] { packageString, classname, superClass, data }));160} catch ( IOException e ) {161Log.error("Could not write file "+dest.getPath());162return false;163}164return true;165}166167private static final String FORMAT =168"{0}" +169"public final class {1} extends {2} '{'\n" +170" protected final Object[][] getContents() '{'\n" +171" return new Object[][] '{'\n" +172"{3}" +173" };\n" +174" }\n" +175"}\n";176177public static String escape(String theString) {178int len = theString.length();179StringBuilder outBuffer = new StringBuilder(len*2);180181for(int x=0; x<len; x++) {182char aChar = theString.charAt(x);183switch(aChar) {184case '\\':outBuffer.append('\\'); outBuffer.append('\\');185break;186case '\t':outBuffer.append('\\'); outBuffer.append('t');187break;188case '\n':outBuffer.append('\\'); outBuffer.append('n');189break;190case '\r':outBuffer.append('\\'); outBuffer.append('r');191break;192case '\f':outBuffer.append('\\'); outBuffer.append('f');193break;194default:195if ((aChar < 0x0020) || (aChar > 0x007e)) {196outBuffer.append('\\');197outBuffer.append('u');198outBuffer.append(toHex((aChar >> 12) & 0xF));199outBuffer.append(toHex((aChar >> 8) & 0xF));200outBuffer.append(toHex((aChar >> 4) & 0xF));201outBuffer.append(toHex( aChar & 0xF));202} else {203if (aChar == '"') {204outBuffer.append('\\');205}206outBuffer.append(aChar);207}208}209}210return outBuffer.toString();211}212213private static char toHex(int nibble) {214return hexDigit[(nibble & 0xF)];215}216217private static final char[] hexDigit = {218'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'219};220}221222223