Example usage for org.apache.commons.jci.problems CompilationProblem getStartLine

List of usage examples for org.apache.commons.jci.problems CompilationProblem getStartLine

Introduction

In this page you can find the example usage for org.apache.commons.jci.problems CompilationProblem getStartLine.

Prototype

int getStartLine();

Source Link

Document

position of where the problem starts in the source code

Usage

From source file:org.xchain.framework.jsl.TemplateCompiler.java

/**
 * Compiles the source result./*from ww w  .  j  av  a  2  s  . c o  m*/
 *
 * @throws SAXException if there is a compilation error in the source file.
 */
public Class compileTemplate(SourceResult result) throws SAXException {
    // add the resource to the reader.
    try {
        resourceReader.add(result.getSourceResourceName(), result.getSource().getBytes(SOURCE_ENCODING));
    } catch (UnsupportedEncodingException uee) {
        throw new SAXException("Could not build source in the encoding '" + SOURCE_ENCODING + "'.", uee);
    }

    // create the resource store.
    ResourceStore resourceStore = new MemoryResourceStore();

    // compile the result.
    CompilationResult compilationResult = compiler.compile(new String[] { result.getSourceResourceName() },
            resourceReader, resourceStore, templateClassLoader, settings);

    // handle any errors.
    if (compilationResult.getErrors().length > 0) {
        if (log.isDebugEnabled()) {
            log.debug("Source that would not complile:\n" + result.getSource());
            for (CompilationProblem error : compilationResult.getErrors()) {
                log.debug("Error (" + error.getStartLine() + ", " + error.getStartColumn() + "):"
                        + error.getMessage());
            }
        }
        // TODO: Make this message better.
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("Could not compile template source. Errors:\n");
        for (CompilationProblem error : compilationResult.getErrors()) {
            stringBuilder.append("(" + error.getStartLine() + ", " + error.getStartColumn() + "):"
                    + error.getMessage() + "\n");
        }
        throw new SAXException(stringBuilder.toString());
    }

    // handle any errors.
    if (log.isDebugEnabled()) {
        for (CompilationProblem warning : compilationResult.getWarnings()) {
            log.debug("Compilation warning(" + warning.getStartLine() + ", " + warning.getStartColumn() + "):"
                    + warning.getMessage());
        }
    }

    byte[] compiledBytes = resourceStore.read(result.getClassResourceName());

    templateClassLoader.publicDefineClass(result.getClassName(), compiledBytes, 0, compiledBytes.length);
    Class templateClass = null;

    try {
        templateClass = templateClassLoader.loadClass(result.getClassName());
    } catch (Exception e) {
        throw new SAXException("Could not load the class '" + result.getClassName() + "'.", e);
    }

    return templateClass;
}