CompileProperties.java revision 3012:adba44f6b471
1/* 2 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package com.sun.tools.sjavac; 27 28import java.io.BufferedWriter; 29import java.io.File; 30import java.io.FileInputStream; 31import java.io.FileOutputStream; 32import java.io.IOException; 33import java.io.OutputStreamWriter; 34import java.io.PrintStream; 35import java.io.Writer; 36import java.net.URI; 37import java.text.MessageFormat; 38import java.util.ArrayList; 39import java.util.Collections; 40import java.util.HashSet; 41import java.util.Iterator; 42import java.util.List; 43import java.util.Map; 44import java.util.Properties; 45import java.util.Set; 46 47import com.sun.tools.sjavac.comp.CompilationService; 48import com.sun.tools.sjavac.options.Options; 49import com.sun.tools.sjavac.pubapi.PubApi; 50 51/** 52 * Compile properties transform a properties file into a Java source file. 53 * Java has built in support for reading properties from either a text file 54 * in the source or a compiled java source file. 55 * 56 * <p><b>This is NOT part of any supported API. 57 * If you write code that depends on this, you do so at your own risk. 58 * This code and its internal interfaces are subject to change or 59 * deletion without notice.</b> 60 */ 61public class CompileProperties implements Transformer { 62 // Any extra information passed from the command line, for example if: 63 // -tr .proppp=com.sun.tools.javac.smart.CompileProperties,sun.util.resources.LocaleNamesBundle 64 // then extra will be "sun.util.resources.LocaleNamesBundle" 65 String extra; 66 67 public void setExtra(String e) { 68 extra = e; 69 } 70 71 public void setExtra(Options a) { 72 } 73 74 public boolean transform(CompilationService compilationService, 75 Map<String,Set<URI>> pkgSrcs, 76 Set<URI> visibleSrcs, 77 Map<URI,Set<String>> visibleClasses, 78 Map<String,Set<String>> oldPackageDependents, 79 URI destRoot, 80 Map<String,Set<URI>> packageArtifacts, 81 Map<String,Map<String, Set<String>>> packageDependencies, 82 Map<String,Map<String, Set<String>>> packageCpDependencies, 83 Map<String, PubApi> packagePublicApis, 84 Map<String, PubApi> dependencyPublicApis, 85 int debugLevel, 86 boolean incremental, 87 int numCores, 88 PrintStream out, 89 PrintStream err) { 90 boolean rc = true; 91 for (String pkgName : pkgSrcs.keySet()) { 92 String pkgNameF = Util.toFileSystemPath(pkgName); 93 for (URI u : pkgSrcs.get(pkgName)) { 94 File src = new File(u); 95 boolean r = compile(pkgName, pkgNameF, src, new File(destRoot), debugLevel, 96 packageArtifacts); 97 if (r == false) { 98 rc = false; 99 } 100 } 101 } 102 return rc; 103 } 104 105 boolean compile(String pkgName, String pkgNameF, File src, File destRoot, int debugLevel, 106 Map<String,Set<URI>> packageArtifacts) 107 { 108 String superClass = "java.util.ListResourceBundle"; 109 110 if (extra != null) { 111 superClass = extra; 112 } 113 // Load the properties file. 114 Properties p = new Properties(); 115 try { 116 p.load(new FileInputStream(src)); 117 } catch (IOException e) { 118 Log.error("Error reading file "+src.getPath()); 119 return false; 120 } 121 122 // Calculate the name of the Java source file to be generated. 123 int dp = src.getName().lastIndexOf("."); 124 String classname = src.getName().substring(0,dp); 125 126 // Sort the properties in increasing key order. 127 List<String> sortedKeys = new ArrayList<>(); 128 for (Object key : p.keySet()) { 129 sortedKeys.add((String)key); 130 } 131 Collections.sort(sortedKeys); 132 Iterator<String> keys = sortedKeys.iterator(); 133 134 // Collect the properties into a string buffer. 135 StringBuilder data = new StringBuilder(); 136 while (keys.hasNext()) { 137 String key = keys.next(); 138 data.append(" { \"" + escape(key) + "\", \"" + 139 escape((String)p.get(key)) + "\" },\n"); 140 } 141 142 // Create dest file name. It is derived from the properties file name. 143 String destFilename = destRoot.getPath()+File.separator+pkgNameF+File.separator+classname+".java"; 144 File dest = new File(destFilename); 145 146 // Make sure the dest directories exist. 147 if (!dest.getParentFile().isDirectory()) { 148 if (!dest.getParentFile().mkdirs()) { 149 Log.error("Could not create the directory "+dest.getParentFile().getPath()); 150 return false; 151 } 152 } 153 154 Set<URI> as = packageArtifacts.get(pkgName); 155 if (as == null) { 156 as = new HashSet<>(); 157 packageArtifacts.put(pkgName, as); 158 } 159 as.add(dest.toURI()); 160 161 if (dest.exists() && dest.lastModified() > src.lastModified()) { 162 // A generated file exists, and its timestamp is newer than the source. 163 // Assume that we do not need to regenerate the dest file! 164 // Thus we are done. 165 return true; 166 } 167 168 String packageString = "package " + pkgNameF.replace(File.separatorChar,'.') + ";\n\n"; 169 170 Log.info("Compiling property file "+pkgNameF+File.separator+src.getName()); 171 try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dest)))) { 172 MessageFormat format = new MessageFormat(FORMAT); 173 writer.write(format.format(new Object[] { packageString, classname, superClass, data })); 174 } catch ( IOException e ) { 175 Log.error("Could not write file "+dest.getPath()); 176 return false; 177 } 178 return true; 179 } 180 181 private static final String FORMAT = 182 "{0}" + 183 "public final class {1} extends {2} '{'\n" + 184 " protected final Object[][] getContents() '{'\n" + 185 " return new Object[][] '{'\n" + 186 "{3}" + 187 " };\n" + 188 " }\n" + 189 "}\n"; 190 191 public static String escape(String theString) { 192 int len = theString.length(); 193 StringBuilder outBuffer = new StringBuilder(len*2); 194 195 for(int x=0; x<len; x++) { 196 char aChar = theString.charAt(x); 197 switch(aChar) { 198 case '\\':outBuffer.append('\\'); outBuffer.append('\\'); 199 break; 200 case '\t':outBuffer.append('\\'); outBuffer.append('t'); 201 break; 202 case '\n':outBuffer.append('\\'); outBuffer.append('n'); 203 break; 204 case '\r':outBuffer.append('\\'); outBuffer.append('r'); 205 break; 206 case '\f':outBuffer.append('\\'); outBuffer.append('f'); 207 break; 208 default: 209 if ((aChar < 0x0020) || (aChar > 0x007e)) { 210 outBuffer.append('\\'); 211 outBuffer.append('u'); 212 outBuffer.append(toHex((aChar >> 12) & 0xF)); 213 outBuffer.append(toHex((aChar >> 8) & 0xF)); 214 outBuffer.append(toHex((aChar >> 4) & 0xF)); 215 outBuffer.append(toHex( aChar & 0xF)); 216 } else { 217 if (aChar == '"') { 218 outBuffer.append('\\'); 219 } 220 outBuffer.append(aChar); 221 } 222 } 223 } 224 return outBuffer.toString(); 225 } 226 227 private static char toHex(int nibble) { 228 return hexDigit[(nibble & 0xF)]; 229 } 230 231 private static final char[] hexDigit = { 232 '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' 233 }; 234} 235