CompileProperties.java revision 2571:10fc81ac75b4
1/* 2 * Copyright (c) 2012, 2014, 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.*; 29import java.net.URI; 30import java.text.MessageFormat; 31import java.util.ArrayList; 32import java.util.Collections; 33import java.util.Iterator; 34import java.util.List; 35import java.util.Properties; 36import java.util.Set; 37import java.util.HashSet; 38import java.util.Map; 39 40import com.sun.tools.sjavac.options.Options; 41import com.sun.tools.sjavac.server.JavacService; 42 43/** 44 * Compile properties transform a properties file into a Java source file. 45 * Java has built in support for reading properties from either a text file 46 * in the source or a compiled java source file. 47 * 48 * <p><b>This is NOT part of any supported API. 49 * If you write code that depends on this, you do so at your own 50 * risk. This code and its internal interfaces are subject to change 51 * or deletion without notice.</b></p> 52 */ 53public class CompileProperties implements Transformer 54{ 55 // Any extra information passed from the command line, for example if: 56 // -tr .proppp=com.sun.tools.javac.smart.CompileProperties,sun.util.resources.LocaleNamesBundle 57 // then extra will be "sun.util.resources.LocaleNamesBundle" 58 String extra; 59 60 public void setExtra(String e) { 61 extra = e; 62 } 63 64 public void setExtra(Options a) { 65 } 66 67 public boolean transform(JavacService javacService, 68 Map<String,Set<URI>> pkgSrcs, 69 Set<URI> visibleSrcs, 70 Map<URI,Set<String>> visibleClasses, 71 Map<String,Set<String>> oldPackageDependents, 72 URI destRoot, 73 Map<String,Set<URI>> packageArtifacts, 74 Map<String,Set<String>> packageDependencies, 75 Map<String,String> packagePublicApis, 76 int debugLevel, 77 boolean incremental, 78 int numCores, 79 PrintStream out, 80 PrintStream err) { 81 boolean rc = true; 82 for (String pkgName : pkgSrcs.keySet()) { 83 String pkgNameF = Util.toFileSystemPath(pkgName); 84 for (URI u : pkgSrcs.get(pkgName)) { 85 File src = new File(u); 86 boolean r = compile(pkgName, pkgNameF, src, new File(destRoot), debugLevel, 87 packageArtifacts); 88 if (r == false) { 89 rc = false; 90 } 91 } 92 } 93 return rc; 94 } 95 96 boolean compile(String pkgName, String pkgNameF, File src, File destRoot, int debugLevel, 97 Map<String,Set<URI>> packageArtifacts) 98 { 99 String superClass = "java.util.ListResourceBundle"; 100 101 if (extra != null) { 102 superClass = extra; 103 } 104 // Load the properties file. 105 Properties p = new Properties(); 106 try { 107 p.load(new FileInputStream(src)); 108 } catch (IOException e) { 109 Log.error("Error reading file "+src.getPath()); 110 return false; 111 } 112 113 // Calculate the name of the Java source file to be generated. 114 int dp = src.getName().lastIndexOf("."); 115 String classname = src.getName().substring(0,dp); 116 117 // Sort the properties in increasing key order. 118 List<String> sortedKeys = new ArrayList<>(); 119 for (Object key : p.keySet()) { 120 sortedKeys.add((String)key); 121 } 122 Collections.sort(sortedKeys); 123 Iterator<String> keys = sortedKeys.iterator(); 124 125 // Collect the properties into a string buffer. 126 StringBuilder data = new StringBuilder(); 127 while (keys.hasNext()) { 128 String key = keys.next(); 129 data.append(" { \"" + escape(key) + "\", \"" + 130 escape((String)p.get(key)) + "\" },\n"); 131 } 132 133 // Create dest file name. It is derived from the properties file name. 134 String destFilename = destRoot.getPath()+File.separator+pkgNameF+File.separator+classname+".java"; 135 File dest = new File(destFilename); 136 137 // Make sure the dest directories exist. 138 if (!dest.getParentFile().isDirectory()) { 139 if (!dest.getParentFile().mkdirs()) { 140 Log.error("Could not create the directory "+dest.getParentFile().getPath()); 141 return false; 142 } 143 } 144 145 Set<URI> as = packageArtifacts.get(pkgName); 146 if (as == null) { 147 as = new HashSet<>(); 148 packageArtifacts.put(pkgName, as); 149 } 150 as.add(dest.toURI()); 151 152 if (dest.exists() && dest.lastModified() > src.lastModified()) { 153 // A generated file exists, and its timestamp is newer than the source. 154 // Assume that we do not need to regenerate the dest file! 155 // Thus we are done. 156 return true; 157 } 158 159 String packageString = "package " + pkgNameF.replace(File.separatorChar,'.') + ";\n\n"; 160 161 Log.info("Compiling property file "+pkgNameF+File.separator+src.getName()); 162 try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dest)))) { 163 MessageFormat format = new MessageFormat(FORMAT); 164 writer.write(format.format(new Object[] { packageString, classname, superClass, data })); 165 } catch ( IOException e ) { 166 Log.error("Could not write file "+dest.getPath()); 167 return false; 168 } 169 return true; 170 } 171 172 private static final String FORMAT = 173 "{0}" + 174 "public final class {1} extends {2} '{'\n" + 175 " protected final Object[][] getContents() '{'\n" + 176 " return new Object[][] '{'\n" + 177 "{3}" + 178 " };\n" + 179 " }\n" + 180 "}\n"; 181 182 public static String escape(String theString) { 183 int len = theString.length(); 184 StringBuilder outBuffer = new StringBuilder(len*2); 185 186 for(int x=0; x<len; x++) { 187 char aChar = theString.charAt(x); 188 switch(aChar) { 189 case '\\':outBuffer.append('\\'); outBuffer.append('\\'); 190 break; 191 case '\t':outBuffer.append('\\'); outBuffer.append('t'); 192 break; 193 case '\n':outBuffer.append('\\'); outBuffer.append('n'); 194 break; 195 case '\r':outBuffer.append('\\'); outBuffer.append('r'); 196 break; 197 case '\f':outBuffer.append('\\'); outBuffer.append('f'); 198 break; 199 default: 200 if ((aChar < 0x0020) || (aChar > 0x007e)) { 201 outBuffer.append('\\'); 202 outBuffer.append('u'); 203 outBuffer.append(toHex((aChar >> 12) & 0xF)); 204 outBuffer.append(toHex((aChar >> 8) & 0xF)); 205 outBuffer.append(toHex((aChar >> 4) & 0xF)); 206 outBuffer.append(toHex( aChar & 0xF)); 207 } else { 208 if (aChar == '"') { 209 outBuffer.append('\\'); 210 } 211 outBuffer.append(aChar); 212 } 213 } 214 } 215 return outBuffer.toString(); 216 } 217 218 private static char toHex(int nibble) { 219 return hexDigit[(nibble & 0xF)]; 220 } 221 222 private static final char[] hexDigit = { 223 '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' 224 }; 225} 226