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