void build() { List<String> lst = new ArrayList<String>(); //collect unit tests Console.WriteLine("Collecting unit tests from " + _testDir); collectTests(_testDir, _testDir, lst, ".+?\\.Test.+?\\.class$"); TestSuite suite = new TestSuite(); for (String arg : lst) { //ignore inner classes defined in tests if (arg.IndexOf('$') != -1) continue; String cls = arg.Replace(".class", ""); try { Class test = Class.forName(cls); suite.AddTestSuite(test); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } //run tests TestRunner.Run(suite); //see what classes from the ooxml-schemas.jar are loaded Console.WriteLine("Copying classes to " + _destDest); Map<String, Class<?>> classes = GetLoadedClasses(_ooxmlJar.getName()); for (Class<?> cls : classes.values()) { String className = cls.GetName(); String classRef = className.Replace('.', '/') + ".class"; File destFile = new File(_destDest, classRef); copyFile(cls.GetResourceAsStream('/' + classRef), destFile); if(cls.isInterface()){ /** * Copy classes and interfaces declared as members of this class */ for(Class fc : cls.GetDeclaredClasses()){ className = fc.GetName(); classRef = className.Replace('.', '/') + ".class"; destFile = new File(_destDest, classRef); copyFile(fc.GetResourceAsStream('/' + classRef), destFile); } } } //finally copy the compiled .xsb files Console.WriteLine("Copying .xsb resources"); JarFile jar = new JarFile(_ooxmlJar); for(Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements(); ){ JarEntry je = e.nextElement(); if(je.GetName().matches("schemaorg_apache_xmlbeans/system/\\w+/\\w+\\.xsb")) { File destFile = new File(_destDest, je.GetName()); copyFile(jar.GetInputStream(je), destFile); } } jar.close(); }
/// <summary>Unpack matching files from a jar.</summary> /// <remarks> /// Unpack matching files from a jar. Entries inside the jar that do /// not match the given pattern will be skipped. /// </remarks> /// <param name="jarFile">the .jar file to unpack</param> /// <param name="toDir">the destination directory into which to unpack the jar</param> /// <param name="unpackRegex">the pattern to match jar entries against</param> /// <exception cref="System.IO.IOException"/> public static void UnJar(FilePath jarFile, FilePath toDir, Pattern unpackRegex ) { JarFile jar = new JarFile(jarFile); try { Enumeration <JarEntry> entries = ((Enumeration <JarEntry>)jar.Entries()); while (entries.MoveNext()) { JarEntry entry = entries.Current; if (!entry.IsDirectory() && unpackRegex.Matcher(entry.GetName()).Matches()) { InputStream @in = jar.GetInputStream(entry); try { FilePath file = new FilePath(toDir, entry.GetName()); EnsureDirectory(file.GetParentFile()); OutputStream @out = new FileOutputStream(file); try { IOUtils.CopyBytes(@in, @out, 8192); } finally { @out.Close(); } } finally { @in.Close(); } } } } finally { jar.Close(); } }
/** * Copy all the files in a manifest from input to output. We set * the modification times in the output to a fixed time, so as to * reduce variation in the output file and make incremental OTAs * more efficient. */ private static void copyFiles(Manifest manifest, JarFile in_, ZipOutputStream out_, DateTime timestamp) { byte[] buffer = new byte[4096]; int num; IDictionary<String, Attributes> entries = manifest.Entries; List<String> names = new List<String>(entries.Keys); names.Sort(); foreach (String name in names) { JarEntry inEntry = in_.GetJarEntry(name); JarEntry outEntry = null; if (inEntry.CompressionMethod == CompressionMethod.Stored) { // Preserve the STORED method of the input entry. outEntry = new JarEntry(inEntry); } else { // Create a new entry so that the compressed len is recomputed. outEntry = new JarEntry(name); if (inEntry.Size > -1) outEntry.Size = inEntry.Size; } outEntry.DateTime = timestamp; out_.PutNextEntry(outEntry); Stream data = in_.GetInputStream(inEntry); while ((num = data.Read(buffer, 0, buffer.Length)) > 0) { out_.Write(buffer, 0, num); } out_.Flush(); } }
/** Add the SHA1 of every file to the manifest, creating it if necessary. */ private static Manifest addDigestsToManifest(JarFile jar) { Manifest input = jar.Manifest; Manifest output = new Manifest(); Attributes main = output.MainAttributes; if (input != null) { main.AddAll(input.MainAttributes); } else { main.Add("Manifest-Version", "1.0"); main.Add("Created-By", "1.0 (Android SignApk)"); } byte[] buffer = new byte[4096]; int num; IEnumerable<JarEntry> jes; if (input == null) { jes = jar.OrderBy(j => j.Name); } else { var entries = jar.ToDictionary(j => j.Name); var sortedEntries = new List<JarEntry>(); foreach (var entry in input.Entries) sortedEntries.Add(entries[entry.Key]); jes = sortedEntries; } foreach (JarEntry entry in jes) { HashAlgorithm md = HashAlgorithm.Create("SHA1"); String name = entry.Name; if (!entry.IsDirectory && !name.Equals(JarFile.MANIFEST_NAME) && !name.Equals(CERT_SF_NAME) && !name.Equals(CERT_RSA_NAME) && !name.Equals(OTACERT_NAME) && (stripPattern == null || !stripPattern.IsMatch(name))) { Stream data = jar.GetInputStream(entry); while ((num = data.Read(buffer, 0, buffer.Length)) > 0) { md.TransformBlock(buffer, 0, num, null, 0); } md.TransformFinalBlock(buffer, 0, 0); Attributes attr = null; if (input != null) attr = input.GetAttributes(name); attr = attr != null ? new Attributes(attr) : new Attributes(); attr.Add("SHA1-Digest", Convert.ToBase64String(md.Hash)); output.Entries.Add(name, attr); } } return output; }