Search in sources :

Example 1 with JavaSourceParser

use of de.java2html.javasource.JavaSourceParser in project OpenOLAT by OpenOLAT.

the class SourceViewController method showjavaSource.

/**
 * provide a class name with path and you will get an string media resource you can display
 * @param cl
 * @return
 * @throws IOException
 */
public static MediaResource showjavaSource(String cl) throws IOException {
    JavaSource jsource = null;
    cl = cl.replace('.', '/');
    String javaSourcePath = WebappHelper.getSourcePath() + "/" + cl + ".java";
    File file = new File(javaSourcePath);
    StringWriter writer = new StringWriter();
    writer.append(HTML_START);
    if (file.exists()) {
        jsource = new JavaSourceParser().parse(file);
        // Create a converter and write the JavaSource object as Html
        JavaSource2HTMLConverter converter = new JavaSource2HTMLConverter();
        converter.convert(jsource, JavaSourceConversionOptions.getDefault(), writer);
    } else {
        writer.append("<html><body><h3>The source file could not be found in the following path:<br>" + javaSourcePath + "<br>Check if configured source path in brasatoconfig.xml is correct.</h3></body></html>");
    }
    StringMediaResource mr = new StringMediaResource();
    mr.setContentType(TEXT_HTML_CHARSET_UTF_8);
    writer.append(HTML_STOP);
    mr.setData(writer.toString());
    return mr;
}
Also used : JavaSource2HTMLConverter(de.java2html.converter.JavaSource2HTMLConverter) StringWriter(java.io.StringWriter) JavaSource(de.java2html.javasource.JavaSource) StringMediaResource(org.olat.core.gui.media.StringMediaResource) File(java.io.File) JavaSourceParser(de.java2html.javasource.JavaSourceParser)

Example 2 with JavaSourceParser

use of de.java2html.javasource.JavaSourceParser in project openolat by klemens.

the class SourceViewController method showjavaSource.

/**
 * provide a class name with path and you will get an string media resource you can display
 * @param cl
 * @return
 * @throws IOException
 */
public static MediaResource showjavaSource(String cl) throws IOException {
    cl = cl.replace('.', '/');
    String javaSourcePath = WebappHelper.getSourcePath() + "/" + cl + ".java";
    File file = new File(javaSourcePath);
    StringWriter writer = new StringWriter();
    writer.append(HTML_START);
    if (file.exists()) {
        JavaSource jsource = new JavaSourceParser().parse(file);
        // Create a converter and write the JavaSource object as Html
        JavaSource2HTMLConverter converter = new JavaSource2HTMLConverter();
        converter.convert(jsource, JavaSourceConversionOptions.getDefault(), writer);
    } else {
        appendError(writer, javaSourcePath);
    }
    writer.append(HTML_STOP);
    StringMediaResource mr = new StringMediaResource();
    mr.setContentType(TEXT_HTML_CHARSET_UTF_8);
    mr.setData(writer.toString());
    return mr;
}
Also used : JavaSource2HTMLConverter(de.java2html.converter.JavaSource2HTMLConverter) StringWriter(java.io.StringWriter) JavaSource(de.java2html.javasource.JavaSource) StringMediaResource(org.olat.core.gui.media.StringMediaResource) File(java.io.File) JavaSourceParser(de.java2html.javasource.JavaSourceParser)

Example 3 with JavaSourceParser

use of de.java2html.javasource.JavaSourceParser in project OpenOLAT by OpenOLAT.

the class SourceViewController method event.

/**
 * @see org.olat.core.gui.control.DefaultController#event(org.olat.core.gui.UserRequest, org.olat.core.gui.components.Component, org.olat.core.gui.control.Event)
 */
@Override
protected void event(UserRequest ureq, Component source, Event event) {
    if (source == viewVeloctiySource) {
        String velocityTemplatePath = WebappHelper.getSourcePath() + "/" + vc.getPage();
        String vcContent = FileUtils.load(new File(velocityTemplatePath), "utf-8");
        sourceview.contextPut("content", vcContent);
        sourceview.contextPut("vcname", vc.getPage());
        view = new CloseableModalController(getWindowControl(), "close...", sourceview);
        listenTo(view);
        view.activate();
    } else if (source == viewJSource) {
        // Parse the raw text to a JavaSource object
        JavaSource jsource = null;
        try {
            String className = clazz.getCanonicalName();
            className = className.replace('.', '/');
            String sourcePath = WebappHelper.getSourcePath() + "/" + className + ".java";
            jsource = new JavaSourceParser().parse(new File(sourcePath));
        } catch (IOException e) {
            showInfo("todo");
        }
        // Create a converter and write the JavaSource object as Html
        JavaSource2HTMLConverter converter = new JavaSource2HTMLConverter();
        StringWriter writer = new StringWriter();
        writer.append(HTML_START);
        try {
            JavaSourceConversionOptions options = JavaSourceConversionOptions.getDefault();
            options.setShowLineNumbers(true);
            converter.convert(jsource, options, writer);
        } catch (IOException e) {
        // 
        }
        StringMediaResource mr = new StringMediaResource();
        mr.setContentType(TEXT_HTML_CHARSET_UTF_8);
        writer.append(HTML_STOP);
        mr.setData(writer.toString());
        ureq.getDispatchResult().setResultingMediaResource(mr);
    }
}
Also used : JavaSource2HTMLConverter(de.java2html.converter.JavaSource2HTMLConverter) JavaSourceConversionOptions(de.java2html.options.JavaSourceConversionOptions) StringWriter(java.io.StringWriter) CloseableModalController(org.olat.core.gui.control.generic.closablewrapper.CloseableModalController) JavaSource(de.java2html.javasource.JavaSource) IOException(java.io.IOException) StringMediaResource(org.olat.core.gui.media.StringMediaResource) File(java.io.File) JavaSourceParser(de.java2html.javasource.JavaSourceParser)

Example 4 with JavaSourceParser

use of de.java2html.javasource.JavaSourceParser in project evosuite by EvoSuite.

the class ClassAction method highlightSource.

public void highlightSource(VirtualChannel channel, BuildListener listener) throws InterruptedException {
    Generation latestGeneration = CUTUtil.getLatestGeneration(this.cut);
    if (latestGeneration.isFailed()) {
        StringBuilder str = new StringBuilder();
        str.append("<h3>std_err_CLIENT</h3>");
        str.append("<p>" + this.getLog(channel, latestGeneration.getStdErrCLIENT()) + "</p>");
        str.append("<h3>std_out_CLIENT</h3>");
        str.append("<p>" + this.getLog(channel, latestGeneration.getStdOutCLIENT()) + "</p>");
        str.append("<h3>std_err_MASTER</h3>");
        str.append("<p>" + this.getLog(channel, latestGeneration.getStdErrMASTER()) + "</p>");
        str.append("<h3>std_out_MASTER</h3>");
        str.append("<p>" + this.getLog(channel, latestGeneration.getStdOutMASTER()) + "</p>");
        this.testSourceCode = str.toString();
        return;
    }
    Generation latestSuccessfulGeneration = CUTUtil.getLatestSuccessfulGeneration(this.cut);
    if (latestSuccessfulGeneration == null) {
        this.testSourceCode = "<p>There was not a single successful generation " + "for this class. Likely this is an EvoSuite bug.</p>";
        return;
    }
    TestSuite suite = latestSuccessfulGeneration.getSuite();
    if (suite == null) {
        this.testSourceCode = "<p>Test suite of the latest successful generation " + "is null. Likely this is an EvoSuite bug.</p>";
        return;
    }
    try {
        String javaFile = suite.getFullPathOfTestSuite();
        listener.getLogger().println(EvoSuiteRecorder.LOG_PREFIX + "JavaFile: " + javaFile);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        new FilePath(channel, javaFile).copyTo(out);
        InputStream file = new ByteArrayInputStream(out.toByteArray());
        JavaSource source = new JavaSourceParser().parse(new InputStreamReader(file, Charset.forName("UTF-8")));
        JavaSourceConversionOptions options = JavaSourceConversionOptions.getDefault();
        options.setShowLineNumbers(true);
        options.setAddLineAnchors(true);
        JavaSource2HTMLConverter converter = new JavaSource2HTMLConverter();
        StringWriter writer = new StringWriter();
        converter.convert(source, options, writer);
        this.testSourceCode = writer.toString();
    } catch (IOException e) {
        listener.getLogger().println(EvoSuiteRecorder.LOG_PREFIX + e.getMessage());
        listener.getLogger().println(EvoSuiteRecorder.LOG_PREFIX + "Returning a empty source-code");
        this.testSourceCode = e.getMessage();
    }
}
Also used : FilePath(hudson.FilePath) JavaSourceConversionOptions(de.java2html.options.JavaSourceConversionOptions) JavaSource2HTMLConverter(de.java2html.converter.JavaSource2HTMLConverter) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) JavaSourceParser(de.java2html.javasource.JavaSourceParser) Generation(org.evosuite.xsd.Generation) TestSuite(org.evosuite.xsd.TestSuite) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) JavaSource(de.java2html.javasource.JavaSource)

Aggregations

JavaSource2HTMLConverter (de.java2html.converter.JavaSource2HTMLConverter)4 JavaSource (de.java2html.javasource.JavaSource)4 JavaSourceParser (de.java2html.javasource.JavaSourceParser)4 StringWriter (java.io.StringWriter)4 File (java.io.File)3 StringMediaResource (org.olat.core.gui.media.StringMediaResource)3 JavaSourceConversionOptions (de.java2html.options.JavaSourceConversionOptions)2 IOException (java.io.IOException)2 FilePath (hudson.FilePath)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Generation (org.evosuite.xsd.Generation)1 TestSuite (org.evosuite.xsd.TestSuite)1 CloseableModalController (org.olat.core.gui.control.generic.closablewrapper.CloseableModalController)1