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