aboutsummaryrefslogtreecommitdiffstats
path: root/src/ooxml/java/org/apache/poi/util/OOXMLLite.java
blob: e6dc38ce78a60a7fe4236c009d69b50f50c54f7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */

package org.apache.poi.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.security.AccessController;
import java.security.CodeSource;
import java.security.PrivilegedAction;
import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.regex.Pattern;

import junit.framework.TestCase;

import org.junit.Test;
import org.junit.internal.TextListener;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;

/**
 * Build a 'lite' version of the ooxml-schemas.jar
 *
 * @author Yegor Kozlov
 */
public final class OOXMLLite {

    /**
     * Destination directory to copy filtered classes
     */
    private File _destDest;

    /**
     * Directory with the compiled ooxml tests
     */
    private File _testDir;

    /**
     * Reference to the ooxml-schemas.jar
     */
    private File _ooxmlJar;


    OOXMLLite(String dest, String test, String ooxmlJar) {
        _destDest = new File(dest);
        _testDir = new File(test);
        _ooxmlJar = new File(ooxmlJar);
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException {

        String dest = null, test = null, ooxml = null;

        for (int i = 0; i < args.length; i++) {
            if (args[i].equals("-dest")) dest = args[++i];
            else if (args[i].equals("-test")) test = args[++i];
            else if (args[i].equals("-ooxml")) ooxml = args[++i];
        }
        OOXMLLite builder = new OOXMLLite(dest, test, ooxml);
        builder.build();
    }

    void build() throws IOException, ClassNotFoundException {
        List<Class<?>> lst = new ArrayList<Class<?>>();
        //collect unit tests
        String exclude = StringUtil.join("|",
                "BaseTestXWorkbook",
                "BaseTestXSheet",
                "BaseTestXRow",
                "BaseTestXCell",
                "BaseTestXSSFPivotTable",
                "TestSXSSFWorkbook\\$\\d",
                "TestUnfixedBugs",
                "MemoryUsage",
                "TestDataProvider",
                "TestDataSamples",
                "All.+Tests",
                "ZipFileAssert",
                "AesZipFileZipEntrySource",
                "TempFileRecordingSXSSFWorkbookWithCustomZipEntrySource",
                "PkiTestUtils",
                "TestCellFormatPart\\$\\d",
                "TestSignatureInfo\\$\\d",
                "TestCertificateEncryption\\$CertData",
                "TestPOIXMLDocument\\$OPCParser",
                "TestPOIXMLDocument\\$TestFactory",
                "TestXSLFTextParagraph\\$DrawTextParagraphProxy",
                "TestXSSFExportToXML\\$\\d",
                "TestXSSFExportToXML\\$DummyEntityResolver",
                "TestFormulaEvaluatorOnXSSF\\$Result",
                "TestFormulaEvaluatorOnXSSF\\$SS",
                "TestMultiSheetFormulaEvaluatorOnXSSF\\$Result",
                "TestMultiSheetFormulaEvaluatorOnXSSF\\$SS",
                "TestXSSFBugs\\$\\d",
                "AddImageBench",
                "AddImageBench_jmhType_B\\d",
                "AddImageBench_benchCreatePicture_jmhTest",
                "TestEvilUnclosedBRFixingInputStream\\$EvilUnclosedBRFixingInputStream",
                "TempFileRecordingSXSSFWorkbookWithCustomZipEntrySource\\$TempFileRecordingSheetDataWriterWithDecorator",
                "TestXSSFBReader\\$1",
                "TestXSSFBReader\\$TestSheetHandler",
                "TestFormulaEvaluatorOnXSSF\\$1",
                "TestMultiSheetFormulaEvaluatorOnXSSF\\$1",
                "TestZipPackagePropertiesMarshaller\\$1",
                "SLCommonUtils",
                "TestPPTX2PNG\\$1"
        );
        System.out.println("Collecting unit tests from " + _testDir);
        collectTests(_testDir, _testDir, lst, ".+.class$", ".+(" + exclude + ").class");
        System.out.println("Found " + lst.size() + " classes");
        
        //run tests
        JUnitCore jUnitCore = new JUnitCore();
        jUnitCore.addListener(new TextListener(System.out));
        Result result = jUnitCore.run(lst.toArray(new Class<?>[lst.size()]));
        if (!result.wasSuccessful()) {
            throw new RuntimeException("Tests did not succeed, cannot build ooxml-lite jar");
        }

        //see what classes from the ooxml-schemas.jar are loaded
        System.out.println("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
        System.out.println("Copying .xsb resources");
        JarFile jar = new  JarFile(_ooxmlJar);
        Pattern p = Pattern.compile("schemaorg_apache_xmlbeans/(system|element)/.*\\.xsb");
        try {
            for(Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements(); ){
                JarEntry je = e.nextElement();
                if(p.matcher(je.getName()).matches()) {
                     File destFile = new File(_destDest, je.getName());
                     copyFile(jar.getInputStream(je), destFile);
                }
            }
        } finally {
            jar.close();
        }
    }

    private static boolean checkForTestAnnotation(Class<?> testclass) {
        for (Method m : testclass.getDeclaredMethods()) {
            if(m.isAnnotationPresent(Test.class)) {
                return true;
            }
        }
        
        // also check super classes
        if(testclass.getSuperclass() != null) {
            for (Method m : testclass.getSuperclass().getDeclaredMethods()) {
                if(m.isAnnotationPresent(Test.class)) {
                    return true;
                }
            }
        }
        
        System.out.println("Class " + testclass.getName() + " does not derive from TestCase and does not have a @Test annotation");

        // Should we also look at superclasses to find cases
        // where we have abstract base classes with derived tests?
        // if(checkForTestAnnotation(testclass.getSuperclass())) return true;

        return false;
    }

    /**
     * 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<Class<?>> out, String ptrn, String exclude)
    throws ClassNotFoundException {
        if (arg.isDirectory()) {
            File files[] = arg.listFiles();
            if (files != null) {
                for (File f : files) {
                    collectTests(root, f, out, ptrn, exclude);
                }
            }
        } else {
            String path = arg.getAbsolutePath();
            String prefix = root.getAbsolutePath();
            String cls = path.substring(prefix.length() + 1).replace(File.separator, ".");
            if(!cls.matches(ptrn)) return;
            if (cls.matches(exclude)) return;
            //ignore inner classes defined in tests
            if (cls.indexOf('$') != -1) {
                System.out.println("Inner class " + cls + " not included");
                return;
            }

            cls = cls.replace(".class", "");

            try {
                Class<?> testclass = Class.forName(cls);
                if (TestCase.class.isAssignableFrom(testclass)
                    || checkForTestAnnotation(testclass)) {
                    out.add(testclass);
                }
            } catch (Throwable e) { // NOSONAR
                System.out.println("Class " + cls + " is not in classpath");
            }
        }
    }

    /**
     *
     * @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<String, Class<?>> getLoadedClasses(String ptrn) {
        // make the field accessible, we defer this from static initialization to here to 
        // allow JDKs which do not have this field (e.g. IBM JDK) to at least load the class
        // without failing, see https://issues.apache.org/bugzilla/show_bug.cgi?id=56550
        final Field _classes = AccessController.doPrivileged(new PrivilegedAction<Field>() {
            @SuppressForbidden("TODO: Reflection works until Java 8 on Oracle/Sun JDKs, but breaks afterwards (different classloader types, access checks)")
            public Field run() {
                try {
                    Field fld = ClassLoader.class.getDeclaredField("classes");
                    fld.setAccessible(true);
                    return fld;
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }

            }
        });

        ClassLoader appLoader = ClassLoader.getSystemClassLoader();
        try {
            Vector<Class<?>> classes = (Vector<Class<?>>) _classes.get(appLoader);
            Map<String, Class<?>> map = new HashMap<String, Class<?>>();
            for (Class<?> cls : classes) {
                // e.g. proxy-classes, ...
                ProtectionDomain pd = cls.getProtectionDomain();
                if (pd == null) continue;
                CodeSource cs = pd.getCodeSource();
                if (cs == null) continue;
                URL loc = cs.getLocation();
                if (loc == null) continue;
                
                String jar = loc.toString();
                if (jar.contains(ptrn)) {
                    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();
        if (!(destDirectory.exists() || destDirectory.mkdirs())) {
            throw new RuntimeException("Can't create destination directory: "+destDirectory);
        }
        OutputStream destStream = new FileOutputStream(destFile);
        try {
            IOUtils.copy(srcStream, destStream);
        } finally {
            destStream.close();
        }
    }

}