1 /* 2 * Copyright (c) 2001, 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 26 package com.sun.tools.sjavac; 27 28 import java.io.*; 29 import java.net.URI; 30 import java.util.ArrayList; 31 import java.util.Collections; 32 import java.util.List; 33 import java.util.Set; 34 import java.util.HashSet; 35 import java.util.Map; 36 import java.util.Properties; 37 38 import com.sun.tools.sjavac.options.Options; 39 import com.sun.tools.sjavac.server.JavacService; 40 41 /** 42 * The clean properties transform should not be necessary. 43 * Eventually we will cleanup the property file sources in the OpenJDK instead. 44 * 45 * <p><b>This is NOT part of any supported API. 46 * If you write code that depends on this, you do so at your own 47 * risk. This code and its internal interfaces are subject to change 48 * or deletion without notice.</b></p> 49 */ 50 public class CleanProperties implements Transformer 51 { 52 public void setExtra(String e) { 53 // Any extra information is ignored for clean properties. 54 } 55 56 public void setExtra(Options a) { 57 // Any extra information is ignored for clean properties. 58 } 59 60 public boolean transform(JavacService javacService, 61 Map<String,Set<URI>> pkgSrcs, 62 Set<URI> visibleSrcs, 63 Map<URI,Set<String>> visibleClasses, 64 Map<String,Set<String>> oldPackageDependencies, 65 URI destRoot, 66 Map<String,Set<URI>> packageArtifacts, 67 Map<String,Set<String>> packageDependencies, 68 Map<String,String> packagePublicApis, 69 int debugLevel, 70 boolean incremental, 71 int numCores, 72 PrintStream out, 73 PrintStream err) 74 { 75 boolean rc = true; 76 for (String pkgName : pkgSrcs.keySet()) { 77 String pkgNameF = pkgName.replace('.',File.separatorChar); 78 for (URI u : pkgSrcs.get(pkgName)) { 79 File src = new File(u); 80 boolean r = clean(pkgName, pkgNameF, src, new File(destRoot), debugLevel, 81 packageArtifacts); 82 if (r == false) { 83 rc = false; 84 } 85 } 86 } 87 return rc; 88 } 89 90 boolean clean(String pkgName, String pkgNameF, File src, File destRoot, int debugLevel, 91 Map<String,Set<URI>> packageArtifacts) 92 { 93 // Load the properties file. 94 Properties p = new Properties(); 95 try { 96 p.load(new FileInputStream(src)); 97 } catch (IOException e) { 98 Log.error("Error reading file "+src.getPath()); 99 return false; 100 } 101 102 // Sort the properties in increasing key order. 103 List<String> sortedKeys = new ArrayList<>(); 104 for (Object key : p.keySet()) { 105 sortedKeys.add((String)key); 106 } 107 Collections.sort(sortedKeys); 108 109 // Collect the properties into a string buffer. 110 StringBuilder data = new StringBuilder(); 111 for (String key : sortedKeys) { 112 data.append(CompileProperties.escape(key)) 113 .append(":") 114 .append(CompileProperties.escape((String) p.get(key))) 115 .append("\n"); 116 } 117 118 String destFilename = destRoot.getPath()+File.separator+pkgNameF+File.separator+src.getName(); 119 File dest = new File(destFilename); 120 121 // Make sure the dest directories exist. 122 if (!dest.getParentFile().isDirectory()) { 123 if (!dest.getParentFile().mkdirs()) { 124 Log.error("Could not create the directory "+dest.getParentFile().getPath()); 125 return false; 126 } 127 } 128 129 Set<URI> as = packageArtifacts.get(pkgName); 130 if (as == null) { 131 as = new HashSet<>(); 132 packageArtifacts.put(pkgName, as); 133 } 134 as.add(dest.toURI()); 135 136 if (dest.exists() && dest.lastModified() > src.lastModified()) { 137 // A cleaned property file exists, and its timestamp is newer than the source. 138 // Assume that we do not need to clean! 139 // Thus we are done. 140 return true; 141 } 142 143 Log.info("Cleaning property file "+pkgNameF+File.separator+src.getName()); 144 try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dest)))) { 145 writer.write(data.toString()); 146 } catch ( IOException e ) { 147 Log.error("Could not write file "+dest.getPath()); 148 return false; 149 } 150 return true; 151 } 152 }