Search in sources :

Example 1 with Out

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

the class HTTPParser method getReportHTMLText.

protected Source getReportHTMLText(Engine engine, Element report, Query query) {
    String page = null;
    try {
        HashMap<String, Object> map = new HashMap<String, Object>();
        ReportQuery impl;
        if (dataPlugin.getEngine().getDeligate() instanceof IEngineImpl)
            impl = new ReportQueryImpl(engine) {

                @Override
                protected Out createOut(OutputStream stream) {
                    try {
                        return new Out(stream) {

                            @Override
                            public void print(Object object) {
                                if (!printVersion) {
                                    if (object instanceof Qualifier) {
                                        Engine engine = dataPlugin.getEngine();
                                        Element element = StandardAttributesPlugin.getElement(engine, ((Qualifier) object).getId());
                                        if (element == null) {
                                            print(object.toString());
                                        } else {
                                            String href = "rows/index.html?id=" + element.getId();
                                            print(getStartATeg(href, false, true));
                                            print(object.toString());
                                            print(getEndATeg());
                                        }
                                    } else if (object instanceof Code) {
                                        String href = "rows/index.html?id=" + ((Code) object).getElement().getId();
                                        print(getStartATeg(href, false, true));
                                        print(object.toString());
                                        print(getEndATeg());
                                    } else if (object instanceof com.ramussoft.database.common.Row) {
                                        String href = "rows/index.html?id=" + ((com.ramussoft.database.common.Row) object).getElementId();
                                        print(getStartATeg(href, false, true));
                                        print(object.toString());
                                        print(getEndATeg());
                                    } else
                                        super.print(object);
                                } else
                                    super.print(object);
                            }
                        };
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                        throw new RuntimeException(e);
                    }
                }
            };
        else
            impl = (ReportQuery) dataPlugin.getEngine();
        if (query != null)
            map.put("query", query);
        page = impl.getHTMLReport(report, map);
    } catch (Exception e1) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        java.io.PrintStream s = null;
        try {
            s = new java.io.PrintStream(stream, true, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if (e1 instanceof DataException)
            s.println(((DataException) e1).getMessage(new MessageFormatter() {

                @Override
                public String getString(String key, Object[] arguments) {
                    return MessageFormat.format(ReportResourceManager.getString(key), arguments);
                }
            }));
        else {
            s.println("<pre>");
            e1.printStackTrace(s);
            s.println("</pre>");
        }
        s.flush();
        try {
            page = new String(stream.toByteArray(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        htmlStream.println(page);
        return null;
    }
    if (!printVersion) {
        htmlStream.println("<H4>" + report.getName() + "</H4>");
    }
    Source source = new Source(page);
    source.fullSequentialParse();
    htmlStream.println(source);
    return source;
}
Also used : HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) Element(com.ramussoft.common.Element) MessageFormatter(com.ramussoft.report.data.MessageFormatter) Source(net.htmlparser.jericho.Source) Qualifier(com.ramussoft.common.Qualifier) IEngine(com.ramussoft.common.IEngine) Engine(com.ramussoft.common.Engine) ReportQuery(com.ramussoft.report.ReportQuery) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Code(com.ramussoft.report.Code) DataException(com.ramussoft.report.data.DataException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SQLException(java.sql.SQLException) IOException(java.io.IOException) ReportQueryImpl(com.ramussoft.report.ReportQueryImpl) Out(com.ramussoft.report.data.Out) DataException(com.ramussoft.report.data.DataException) IEngineImpl(com.ramussoft.core.impl.IEngineImpl) Row(com.ramussoft.pb.Row) NRow(com.ramussoft.pb.data.negine.NRow)

Example 2 with Out

use of com.ramussoft.report.data.Out 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 Out

use of com.ramussoft.report.data.Out 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

Out (com.ramussoft.report.data.Out)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 IOException (java.io.IOException)3 Data (com.ramussoft.report.data.Data)2 BufferedReader (java.io.BufferedReader)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 Element (com.ramussoft.common.Element)1 Engine (com.ramussoft.common.Engine)1 IEngine (com.ramussoft.common.IEngine)1 Qualifier (com.ramussoft.common.Qualifier)1 IEngineImpl (com.ramussoft.core.impl.IEngineImpl)1 Row (com.ramussoft.pb.Row)1 NRow (com.ramussoft.pb.data.negine.NRow)1 Code (com.ramussoft.report.Code)1 ReportQuery (com.ramussoft.report.ReportQuery)1 ReportQueryImpl (com.ramussoft.report.ReportQueryImpl)1