Search in sources :

Example 1 with JRRuntimeException

use of net.sf.jasperreports.engine.JRRuntimeException in project opennms by OpenNMS.

the class CustomJRJdtCompiler method getNameEnvironment.

protected INameEnvironment getNameEnvironment(final JRCompilationUnit[] units) {
    final INameEnvironment env = new INameEnvironment() {

        @Override
        public NameEnvironmentAnswer findType(char[][] compoundTypeName) {
            final StringBuilder result = new StringBuilder();
            String sep = "";
            for (int i = 0; i < compoundTypeName.length; i++) {
                result.append(sep);
                result.append(compoundTypeName[i]);
                sep = ".";
            }
            return findType(result.toString());
        }

        @Override
        public NameEnvironmentAnswer findType(char[] typeName, char[][] packageName) {
            final StringBuilder result = new StringBuilder();
            String sep = "";
            for (int i = 0; i < packageName.length; i++) {
                result.append(sep);
                result.append(packageName[i]);
                sep = ".";
            }
            result.append(sep);
            result.append(typeName);
            return findType(result.toString());
        }

        private int getClassIndex(String className) {
            int classIdx;
            for (classIdx = 0; classIdx < units.length; ++classIdx) {
                if (className.equals(units[classIdx].getName())) {
                    break;
                }
            }
            if (classIdx >= units.length) {
                classIdx = -1;
            }
            return classIdx;
        }

        private NameEnvironmentAnswer findType(String className) {
            try {
                int classIdx = getClassIndex(className);
                if (classIdx >= 0) {
                    ICompilationUnit compilationUnit = new CompilationUnit(units[classIdx].getSourceCode(), className);
                    return new NameEnvironmentAnswer(compilationUnit, null);
                }
                String resourceName = className.replace('.', '/') + ".class";
                InputStream is = getResource(resourceName);
                if (is != null) {
                    try {
                        byte[] classBytes = JRLoader.loadBytes(is);
                        char[] fileName = className.toCharArray();
                        ClassFileReader classFileReader = new ClassFileReader(classBytes, fileName, true);
                        return new NameEnvironmentAnswer(classFileReader, null);
                    } finally {
                        try {
                            is.close();
                        } catch (IOException e) {
                        // ignore
                        }
                    }
                }
            } catch (JRException e) {
                LOG.error("Compilation error", e);
            } catch (org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException exc) {
                LOG.error("Compilation error", exc);
            } catch (IllegalArgumentException e) {
                throw new JRRuntimeException(EXCEPTION_MESSAGE_KEY_NAME_ENVIRONMENT_ANSWER_INSTANCE_ERROR, (Object[]) null, e);
            }
            return null;
        }

        private boolean isPackage(String result) {
            int classIdx = getClassIndex(result);
            if (classIdx >= 0) {
                return false;
            }
            String resourceName = result.replace('.', '/') + ".class";
            boolean isPackage = true;
            InputStream is = getResource(resourceName);
            if (is != null) {
                // 1.5 plugin
                try {
                    isPackage = (is.read() > 0);
                } catch (IOException e) {
                // ignore
                } finally {
                    try {
                        is.close();
                    } catch (IOException e) {
                    // ignore
                    }
                }
            }
            return isPackage;
        }

        @Override
        public boolean isPackage(char[][] parentPackageName, char[] packageName) {
            final StringBuilder result = new StringBuilder();
            String sep = "";
            if (parentPackageName != null) {
                for (int i = 0; i < parentPackageName.length; i++) {
                    result.append(sep);
                    result.append(parentPackageName[i]);
                    sep = ".";
                }
            }
            if (Character.isUpperCase(packageName[0])) {
                if (!isPackage(result.toString())) {
                    return false;
                }
            }
            result.append(sep);
            result.append(packageName);
            return isPackage(result.toString());
        }

        @Override
        public void cleanup() {
        }
    };
    return env;
}
Also used : JRCompilationUnit(net.sf.jasperreports.engine.design.JRCompilationUnit) ICompilationUnit(org.eclipse.jdt.internal.compiler.env.ICompilationUnit) ICompilationUnit(org.eclipse.jdt.internal.compiler.env.ICompilationUnit) NameEnvironmentAnswer(org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer) JRException(net.sf.jasperreports.engine.JRException) InputStream(java.io.InputStream) ClassFileReader(org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader) JRRuntimeException(net.sf.jasperreports.engine.JRRuntimeException) IOException(java.io.IOException) INameEnvironment(org.eclipse.jdt.internal.compiler.env.INameEnvironment)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 JRException (net.sf.jasperreports.engine.JRException)1 JRRuntimeException (net.sf.jasperreports.engine.JRRuntimeException)1 JRCompilationUnit (net.sf.jasperreports.engine.design.JRCompilationUnit)1 ClassFileReader (org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader)1 ICompilationUnit (org.eclipse.jdt.internal.compiler.env.ICompilationUnit)1 INameEnvironment (org.eclipse.jdt.internal.compiler.env.INameEnvironment)1 NameEnvironmentAnswer (org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer)1