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