From aead99342ff0845db40ca10da97634cb093395f1 Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Mon, 16 Nov 2009 15:32:05 +0000 Subject: [PATCH] org.apache.poi.util.OOXMLLite - an utility to build a 'lite' version of the ooxml-schemas.jar git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@880798 13f79535-47bb-0310-9956-ffa450edef68 --- build.xml | 53 ++++- .../java/org/apache/poi/util/OOXMLLite.java | 200 ++++++++++++++++++ 2 files changed, 250 insertions(+), 3 deletions(-) create mode 100755 src/ooxml/java/org/apache/poi/util/OOXMLLite.java diff --git a/build.xml b/build.xml index 3f7ff7bc12..3cfb9aed7e 100644 --- a/build.xml +++ b/build.xml @@ -184,8 +184,8 @@ under the License. - - + + @@ -408,7 +408,54 @@ under the License. basedir="${ooxml.xsds.src.dir}" destfile="${ooxml.xsds.src.jar}" /> - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + lst = new ArrayList(); + //collect unit tests + System.out.println("Collecting unit tests"); + 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 + System.out.println("Running tests"); + TestRunner.run(suite); + + //see what classes from the ooxml-schemas.jar are loaded + System.out.println("Copying classes"); + Map> classes = getLoadedClasses(_ooxmlJar.getName()); + for (Class cls : classes.values()) { + String className = cls.getName(); + String classRef = className.replace('.', '/') + ".class"; + File destFile = new File(_destDest, classRef); + //System.out.println(classRef + " --> " + destFile); + copyFile(cls.getResourceAsStream('/' + classRef), destFile); + + if(cls.isInterface()){ + //always copy Factory that accompanies every ooxml schema object + String factoryClass = className + "$Factory"; + if(!classes.containsKey(factoryClass)){ + try { + Class fc = Class.forName(factoryClass); + className = fc.getName(); + classRef = className.replace('.', '/') + ".class"; + destFile = new File(_destDest, classRef); + //System.out.println(classRef + " --> " + destFile); + copyFile(fc.getResourceAsStream('/' + classRef), destFile); + } catch(ClassNotFoundException e) { + e.printStackTrace(); + } + } + } + } + + //finally copy the compiled .xsb files + System.out.println("Copying .xsb resources"); + JarFile jar = new JarFile(_ooxmlJar); + for(Enumeration 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()); + //System.out.println(je.getName() + " --> " + destFile); + copyFile(jar.getInputStream(je), destFile); + } + } + jar.close(); + } + + /** + * Recursively collect classes from the supplied directory + * + * @param arg the directory to search in + * @param out output + * @param ptrn the pattern (regexp) to filter found files + */ + private static void collectTests(File root, File arg, List out, String ptrn) { + if (arg.isDirectory()) { + for (File f : arg.listFiles()) { + collectTests(root, f, out, ptrn); + } + } else { + String path = arg.getAbsolutePath(); + String prefix = root.getAbsolutePath(); + String cls = path.substring(prefix.length() + 1).replace(File.separator, "."); + if(cls.matches(ptrn)) out.add(cls); + } + } + + /** + * + * @param ptrn the pattern to filter output + * @return the classes loaded by the system class loader keyed by class name + */ + @SuppressWarnings("unchecked") + private static Map> getLoadedClasses(String ptrn) { + ClassLoader appLoader = ClassLoader.getSystemClassLoader(); + try { + Vector> classes = (Vector>) _classes.get(appLoader); + Map> map = new HashMap>(); + for (Class cls : classes) { + String jar = cls.getProtectionDomain().getCodeSource().getLocation().toString(); + if(jar.indexOf(ptrn) != -1) map.put(cls.getName(), cls); + } + return map; + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } + + private static void copyFile(InputStream srcStream, File destFile) throws IOException { + File destDirectory = destFile.getParentFile(); + destDirectory.mkdirs(); + OutputStream destStream = new FileOutputStream(destFile); + try { + IOUtils.copy(srcStream, destStream); + } finally { + destStream.close(); + } + } + +} \ No newline at end of file