OLD | NEW |
(Empty) | |
| 1 /* Copyright (c) 2002-2013 Sun Microsystems, Inc. All rights reserved |
| 2 * |
| 3 * This program is distributed under the terms of |
| 4 * the GNU General Public License Version 2. See the LICENSE file |
| 5 * at the top of the source tree. |
| 6 */ |
| 7 package org.pantsbuild.jmake; |
| 8 |
| 9 import java.io.BufferedReader; |
| 10 import java.io.ByteArrayInputStream; |
| 11 import java.io.File; |
| 12 import java.io.FileInputStream; |
| 13 import java.io.FileNotFoundException; |
| 14 import java.io.IOException; |
| 15 import java.io.InputStreamReader; |
| 16 import java.io.ObjectInputStream; |
| 17 import java.io.UnsupportedEncodingException; |
| 18 import java.util.LinkedHashMap; |
| 19 import java.util.Map; |
| 20 import java.util.regex.Matcher; |
| 21 import java.util.regex.Pattern; |
| 22 |
| 23 |
| 24 /** |
| 25 * This class creates the internal representation of the project database from a
text buffer. |
| 26 * |
| 27 * The Pants build tool manipulates this data in various ways, and it's easiest
for it |
| 28 * to do so by parsing text files directly. This brings JMake into line with Zi
nc (the |
| 29 * Scala incremental compiler) and allows Pants to handle both uniformly. |
| 30 * |
| 31 * @author Benjy Weinberger |
| 32 * 13 January 2013 |
| 33 */ |
| 34 public class TextProjectDatabaseReader { |
| 35 public Map<String,PCDEntry> readProjectDatabaseFromFile(File infile) { |
| 36 try { |
| 37 BufferedReader in = |
| 38 new BufferedReader(new InputStreamReader(new FileInputStream(inf
ile), "UTF-8")); |
| 39 try { |
| 40 return readProjectDatabase(in); |
| 41 } finally { |
| 42 in.close(); |
| 43 } |
| 44 } catch (FileNotFoundException e) { |
| 45 throw new PrivateException(e); |
| 46 } catch (UnsupportedEncodingException e) { |
| 47 throw new PrivateException(e); |
| 48 } catch (IOException e) { |
| 49 throw new PrivateException(e); |
| 50 } |
| 51 } |
| 52 |
| 53 public Map<String,PCDEntry> readProjectDatabase(BufferedReader in) { |
| 54 Map<String,PCDEntry> pcd; |
| 55 try { |
| 56 String line = in.readLine(); |
| 57 if (!"pcd entries:".equals(line)) |
| 58 throw error("Expected: 'pcd entries:', got: " + line); |
| 59 line = in.readLine(); |
| 60 Matcher m = Pattern.compile("^(\\d+) items$").matcher(line); |
| 61 if (!m.matches()) |
| 62 throw error("Expected: '<n> items', got: " + line); |
| 63 int numEntries = Integer.parseInt(m.group(1)); |
| 64 pcd = new LinkedHashMap<String, PCDEntry>(numEntries); |
| 65 for (int i = 0; i < numEntries; i++) { |
| 66 line = in.readLine(); |
| 67 if (line == null) |
| 68 throw error("Unexpected EOF"); |
| 69 String[] parts = line.split("\t"); |
| 70 if (parts.length != 5) { |
| 71 throw error("Invalid line: " + line); |
| 72 } |
| 73 String className = parts[0]; |
| 74 String javaFullFilePath = parts[1]; |
| 75 long oldClassFileLastModified = Long.parseLong(parts[2]); |
| 76 long oldClassFileFingerprint = Long.parseLong(parts[3]); |
| 77 ClassInfo ci = classInfoFromBase64(parts[4]); |
| 78 PCDEntry entry = new PCDEntry(className, javaFullFilePath, oldCl
assFileLastModified, |
| 79 oldClassFileFingerprint, ci); |
| 80 pcd.put(entry.className, entry); |
| 81 } |
| 82 // We're done: We have detailed dep information in the PCD entries,
so we don't |
| 83 // need to read the dep information lines from the file. |
| 84 } catch (IOException e) { |
| 85 throw new PrivateException(e); |
| 86 } |
| 87 return pcd; |
| 88 } |
| 89 |
| 90 private PrivateException error(String msg) { |
| 91 return new PrivateException(new IllegalArgumentException(msg)); |
| 92 } |
| 93 |
| 94 private ClassInfo classInfoFromBase64(String s) { |
| 95 try { |
| 96 byte[] bytes = Base64.decode(s.toCharArray()); |
| 97 ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStre
am(bytes)); |
| 98 ClassInfo ret = (ClassInfo)ois.readObject(); |
| 99 ret.initializeImmediateTransientFields(); |
| 100 return ret; |
| 101 } catch (IOException e) { |
| 102 throw new PrivateException(e); |
| 103 } catch (ClassNotFoundException e) { |
| 104 throw new PrivateException(e); |
| 105 } |
| 106 } |
| 107 } |
OLD | NEW |