All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.drools.compiler.kie.builder.impl.MessageImpl Maven / Gradle / Ivy

There is a newer version: 10.0.0
Show newest version
package org.drools.compiler.kie.builder.impl;

import org.drools.compiler.commons.jci.problems.CompilationProblem;
import org.kie.api.io.Resource;
import org.kie.internal.builder.KnowledgeBuilderResult;
import org.kie.api.builder.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class MessageImpl implements Message {

    protected static transient Logger logger = LoggerFactory.getLogger( MessageImpl.class );

    private final long id;
    private final Level level;
    private final String path;
    private final int line;
    private final int column;
    private final String text;

    public MessageImpl( long id,
                        Level level,
                        String path,
                        String text ) {
        this.id = id;
        this.level = level;
        this.path = path;
        this.text = text;
        this.line = 0;
        this.column = 0;
    }

    public MessageImpl( long id,
                        CompilationProblem problem ) {
        this.id = id;
        level = problem.isError() ? Level.ERROR : Level.WARNING;
        path = problem.getFileName();
        line = problem.getStartLine();
        column = problem.getStartColumn();
        text = problem.getMessage();
    }

    public MessageImpl( long id,
                        KnowledgeBuilderResult result ) {
        this.id = id;
        switch ( result.getSeverity() ) {
            case ERROR:
                level = Level.ERROR;
                break;
            case WARNING:
                level = Level.WARNING;
                break;
            default:
                level = Level.INFO;
        }
        //See JIRA DROOLS-193 (KnowledgeBuilderResult does not always contain a Resource)
        Resource resource = result.getResource();
        if ( resource == null ) {
            logger.debug( "resource is null: " + result.toString() );
            path = null;
        } else {
            path = resource.getSourcePath();
        }

        if ( result.getLines().length > 0 ) {
            line = result.getLines()[ 0 ];
        } else {
            line = -1;
        }
        column = 0;
        text = result.getMessage();
    }

    public long getId() {
        return id;
    }

    public Level getLevel() {
        return level;
    }

    public String getPath() {
        return path;
    }

    public int getLine() {
        return line;
    }

    public int getColumn() {
        return column;
    }

    public String getText() {
        return text;
    }

    public static List filterMessages( List messages,
                                                Level... levels ) {
        List filteredMsgs = new ArrayList();
        if ( levels != null && levels.length > 0 ) {
            for ( Level level : levels ) {
                for ( Message msg : messages ) {
                    if ( msg.getLevel() == level ) {
                        filteredMsgs.add( msg );
                    }
                }
            }
        }
        return filteredMsgs;
    }

    @Override
    public String toString() {
        return "Message [id=" + id + ", level=" + level + ", path=" + path + ", line=" + line + ", column=" + column + "\n   text=" + text + "]";
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy