Search in sources :

Example 1 with Data

use of com.ramussoft.report.data.Data in project ramus by Vitaliy-Yakovchuk.

the class XMLReportEngine method execute.

@Override
public void execute(String path, OutputStream stream, Map<String, Object> parameters) throws IOException {
    createOut(stream);
    Query query = (Query) parameters.get("query");
    this.data = new Data(engine, query);
    try {
        final SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser parser;
        parser = factory.newSAXParser();
        byte[] bs = engine.getStream(path);
        if (bs == null)
            throw new DataException("Error.reportEmpty", "Report's form is empty");
        do {
            try {
                parser.parse(new ByteArrayInputStream(bs), new ReportHandler());
            } catch (NoRowsException e) {
                break;
            }
            if (null == data.getBaseRows())
                break;
            connectedLabels.clear();
            if (data.isSameBaseQualifier())
                break;
        } while (data.getBaseRows().next() != null);
    } catch (ParserConfigurationException e) {
        throw new DataException(e);
    } catch (SAXException e) {
        throw new DataException(e);
    }
    if (out.checkError())
        throw new IOException();
    out.flush();
    out.realWrite();
}
Also used : DataException(com.ramussoft.report.data.DataException) ByteArrayInputStream(java.io.ByteArrayInputStream) NoRowsException(com.ramussoft.report.xml.NoRowsException) SAXParser(javax.xml.parsers.SAXParser) Data(com.ramussoft.report.data.Data) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Example 2 with Data

use of com.ramussoft.report.data.Data in project ramus by Vitaliy-Yakovchuk.

the class JSSPDocBookReportEngine method execute.

@SuppressWarnings("deprecation")
public void execute(String scriptPath, OutputStream stream, Map<String, Object> parameters) throws IOException {
    byte[] bytes = engine.getStream(scriptPath);
    if (bytes == null)
        bytes = new byte[] {};
    String script = new String(bytes, "UTF-8");
    JSSPToJsConverter converter = new JSSPToJsConverter(script);
    final ScriptEngine engine = manager.getEngineByName("JavaScript");
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    Out out = createOut(outputStream);
    try {
        SimpleOut simpleOut = new SimpleOut(out);
        engine.put("doc", simpleOut);
        engine.put("document", simpleOut);
        engine.put("out", simpleOut);
        Query query = (Query) parameters.get("query");
        engine.put("data", new Data(this.engine, query, reportQuery));
        for (Entry<String, Object> entry : parameters.entrySet()) engine.put(entry.getKey(), entry.getValue());
        final String convert = converter.convert();
        final Object w = new Object();
        final ExceptionHolder exceptionHolder = new ExceptionHolder();
        Thread thread = new Thread() {

            @Override
            public void run() {
                try {
                    engine.eval(convert);
                } catch (ScriptException e) {
                    exceptionHolder.scriptException = e;
                } catch (RuntimeException e) {
                    exceptionHolder.exception = e;
                }
                synchronized (w) {
                    exceptionHolder.finished = true;
                    w.notify();
                }
            }
        };
        thread.start();
        try {
            synchronized (w) {
                if (Metadata.CORPORATE) {
                    if (!exceptionHolder.finished)
                        w.wait(300000);
                } else {
                    if (!exceptionHolder.finished)
                        w.wait(50000);
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (!exceptionHolder.finished) {
            thread.stop();
            throw new ScriptException(SCRIPT_WORKED_TOO_LONG);
        }
        if (exceptionHolder.scriptException != null) {
            /*
                 * if(exceptionHolder.scriptException.getCause() instanceof
				 * Exception)
				 * ((Exception)exceptionHolder.scriptException.getCause
				 * ()).printStackTrace();
				 */
            throw exceptionHolder.scriptException;
        }
        if (exceptionHolder.exception != null)
            throw exceptionHolder.exception;
        out.flush();
        out.realWrite(false);
        stream.write(outputStream.toByteArray());
    } catch (ScriptException e) {
        e.printStackTrace();
        out = new Out(stream);
        String message = e.getLocalizedMessage();
        String moz = "sun.org.mozilla.javascript.internal.EcmaError:";
        if (message.startsWith(moz))
            message = message.substring(moz.length() + 1);
        Pattern pattern = Pattern.compile("(\\d+)");
        Matcher matcher = pattern.matcher(message);
        int minus = 5;
        int plus = 5;
        if (matcher.find()) {
            String group = matcher.group();
            while (matcher.find()) group = matcher.group();
            int number = Integer.parseInt(group);
            int from = 0;
            if (number > minus)
                from = number - minus;
            BufferedReader br = new BufferedReader(new StringReader(script));
            int i = 0;
            while (i < from) {
                try {
                    br.readLine();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                i++;
            }
            out.println("<html>");
            out.println("<body>");
            out.println(message);
            out.println("<br><br><table width=\"100%\">");
            i = from;
            while (true) {
                String line = null;
                line = br.readLine();
                if (line == null)
                    break;
                if (i - plus >= number)
                    break;
                out.println("<tr>");
                out.println("<td width=\"1%\"><font color=green>" + (i + 1) + "</font></td>");
                String string = line.replace("<", "&lt;").replace(">", "&gt;");
                if (i + 1 == number)
                    string = "<font color=\"#FF0000\">" + string + "</font>";
                out.println("<td width=\"99%\"><pre>" + string + "</pre></td>");
                out.println("</tr>");
                i++;
            }
            out.println("</table>");
            out.println("</body>");
            out.println("</html>");
        } else {
            out.println("<html>");
            out.println("<body>");
            out.println("<pre>");
            if (e.getLocalizedMessage().equals(SCRIPT_WORKED_TOO_LONG))
                out.print(e.getLocalizedMessage());
            else
                e.printStackTrace(out);
            out.println("</pre>");
            out.println("</body>");
            out.println("</html>");
        }
        out.flush();
        out.realWrite();
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Data(com.ramussoft.report.data.Data) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ScriptEngine(javax.script.ScriptEngine) Out(com.ramussoft.report.data.Out) ScriptException(javax.script.ScriptException) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader)

Example 3 with Data

use of com.ramussoft.report.data.Data in project ramus by Vitaliy-Yakovchuk.

the class JSSPReportEngine method execute.

@SuppressWarnings("deprecation")
public void execute(String scriptPath, OutputStream stream, Map<String, Object> parameters) throws IOException {
    byte[] bytes = engine.getStream(scriptPath);
    if (bytes == null)
        bytes = new byte[] {};
    String script = new String(bytes, "UTF-8");
    JSSPToJsConverter converter = new JSSPToJsConverter(script);
    final ScriptEngine engine = manager.getEngineByName("JavaScript");
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    Out out = createOut(outputStream);
    try {
        SimpleOut simpleOut = new SimpleOut(out);
        engine.put("doc", simpleOut);
        engine.put("document", simpleOut);
        engine.put("out", simpleOut);
        Query query = (Query) parameters.get("query");
        engine.put("data", new Data(this.engine, query, reportQuery));
        for (Entry<String, Object> entry : parameters.entrySet()) engine.put(entry.getKey(), entry.getValue());
        final String convert = converter.convert();
        final Object w = new Object();
        final ExceptionHolder exceptionHolder = new ExceptionHolder();
        Thread thread = new Thread() {

            @Override
            public void run() {
                try {
                    engine.eval(convert);
                } catch (ScriptException e) {
                    exceptionHolder.scriptException = e;
                } catch (RuntimeException e) {
                    exceptionHolder.exception = e;
                }
                synchronized (w) {
                    exceptionHolder.finished = true;
                    w.notify();
                }
            }
        };
        thread.start();
        try {
            synchronized (w) {
                if (Metadata.CORPORATE) {
                    if (!exceptionHolder.finished)
                        w.wait(300000);
                } else {
                    if (!exceptionHolder.finished)
                        w.wait(50000);
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (!exceptionHolder.finished) {
            thread.stop();
            throw new ScriptException(SCRIPT_WORKED_TOO_LONG);
        }
        if (exceptionHolder.scriptException != null) {
            /*
                 * if(exceptionHolder.scriptException.getCause() instanceof
				 * Exception)
				 * ((Exception)exceptionHolder.scriptException.getCause
				 * ()).printStackTrace();
				 */
            throw exceptionHolder.scriptException;
        }
        if (exceptionHolder.exception != null)
            throw exceptionHolder.exception;
        out.flush();
        out.realWriteWithHTMLUpdate();
        stream.write(outputStream.toByteArray());
    } catch (ScriptException e) {
        e.printStackTrace();
        out = new Out(stream);
        String message = e.getLocalizedMessage();
        String moz = "sun.org.mozilla.javascript.internal.EcmaError:";
        if (message.startsWith(moz))
            message = message.substring(moz.length() + 1);
        Pattern pattern = Pattern.compile("(\\d+)");
        Matcher matcher = pattern.matcher(message);
        int minus = 5;
        int plus = 5;
        if (matcher.find()) {
            String group = matcher.group();
            while (matcher.find()) group = matcher.group();
            int number = Integer.parseInt(group);
            int from = 0;
            if (number > minus)
                from = number - minus;
            BufferedReader br = new BufferedReader(new StringReader(script));
            int i = 0;
            while (i < from) {
                try {
                    br.readLine();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                i++;
            }
            out.println("<html>");
            out.println("<body>");
            out.println(message);
            out.println("<br><br><table width=\"100%\">");
            i = from;
            while (true) {
                String line = null;
                line = br.readLine();
                if (line == null)
                    break;
                if (i - plus >= number)
                    break;
                out.println("<tr>");
                out.println("<td width=\"1%\"><font color=green>" + (i + 1) + "</font></td>");
                String string = line.replace("<", "&lt;").replace(">", "&gt;");
                if (i + 1 == number)
                    string = "<font color=\"#FF0000\">" + string + "</font>";
                out.println("<td width=\"99%\"><pre>" + string + "</pre></td>");
                out.println("</tr>");
                i++;
            }
            out.println("</table>");
            out.println("</body>");
            out.println("</html>");
        } else {
            out.println("<html>");
            out.println("<body>");
            out.println("<pre>");
            if (e.getLocalizedMessage().equals(SCRIPT_WORKED_TOO_LONG))
                out.print(e.getLocalizedMessage());
            else
                e.printStackTrace(out);
            out.println("</pre>");
            out.println("</body>");
            out.println("</html>");
        }
        out.flush();
        out.realWrite();
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Data(com.ramussoft.report.data.Data) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ScriptEngine(javax.script.ScriptEngine) Out(com.ramussoft.report.data.Out) ScriptException(javax.script.ScriptException) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader)

Aggregations

Data (com.ramussoft.report.data.Data)3 IOException (java.io.IOException)3 Out (com.ramussoft.report.data.Out)2 BufferedReader (java.io.BufferedReader)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 StringReader (java.io.StringReader)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 ScriptEngine (javax.script.ScriptEngine)2 ScriptException (javax.script.ScriptException)2 DataException (com.ramussoft.report.data.DataException)1 NoRowsException (com.ramussoft.report.xml.NoRowsException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 SAXParser (javax.xml.parsers.SAXParser)1 SAXParserFactory (javax.xml.parsers.SAXParserFactory)1 SAXException (org.xml.sax.SAXException)1