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

org.drools.verifier.misc.DrlPackageParser Maven / Gradle / Ivy

There is a newer version: 9.44.0.Final
Show newest version
/*
 * Copyright 2010 JBoss Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.drools.verifier.misc;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DrlPackageParser {
    private static final String             packageExpr  = "(^\\s*#.*?\\n)?(^\\s*package.*?$)(.*?)";
    private static final Pattern            finder       = Pattern.compile( packageExpr,
                                                                            Pattern.DOTALL | Pattern.MULTILINE );

    private static final Pattern            globalFinder = Pattern.compile( "(^\\s*global.*?$)",
                                                                            Pattern.DOTALL | Pattern.MULTILINE );

    private final String                    name;
    private final List       rules;
    private final String                    description;

    private final List              metadata;

    private final Map> otherInformation;
    private final List              globals;

    public DrlPackageParser(String packageName,
                            String description,
                            List rules,
                            List globals,
                            List metadata,
                            Map> otherInformation) {
        this.name = packageName;
        this.description = description;
        this.rules = rules;
        this.globals = globals;
        this.metadata = metadata;
        this.otherInformation = otherInformation;
    }

    public static DrlPackageParser findPackageDataFromDrl(String drl) throws ParseException {

        // Remove block comments
        int start = drl.indexOf( "/*" );
        while ( start > 0 ) {
            if ( start >= 0 ) {
                drl = drl.replace( drl.substring( start,
                                                  drl.indexOf( "*/",
                                                               start ) ),
                                   "" );

                start = drl.indexOf( "/*" );
            }
        }

        final Matcher m = finder.matcher( drl );
        m.find();

        String packageNameRow;
        try {
            packageNameRow = m.group( 2 );
        } catch ( IllegalStateException e ) {
            throw new ParseException( "DRL string contained no package data.",
                                      0 );
        }

        int indexOfPackage = packageNameRow.indexOf( "package" );
        String packageName = packageNameRow.substring( indexOfPackage + "package".length() ).trim();

        Comment comment = DrlRuleParser.processComment( m.group( 1 ) );

        return new DrlPackageParser( packageName,
                                     comment.description,
                                     DrlRuleParser.findRulesDataFromDrl( drl ),
                                     findGlobals( drl ),
                                     comment.metadata,
                                     new HashMap>() );
    }

    public static List findGlobals(String drl) {
        List globals = new ArrayList();
        Matcher gm = globalFinder.matcher( drl );

        while ( gm.find() ) {
            String row = gm.group();
            globals.add( row.substring( row.indexOf( "global" ) + "global".length() ).trim() );
        }

        return globals;
    }

    static Comment processComment(String text) {
        Comment comment = new Comment();

        if ( text == null ) {
            comment.description = "";
            comment.metadata = new ArrayList();

            return comment;
        }

        comment.description = text.replaceAll( "#",
                                               "" ).trim();
        comment.metadata = findMetaData( text );

        return comment;
    }

    private static List findMetaData(String text) {
        List list = new ArrayList();

        while ( text.contains( "@" ) ) {
            int start = text.indexOf( '@' );
            text = text.substring( start + 1 );
            int end = text.indexOf( "\n" );
            list.add( text.substring( 0,
                                      end ) );
            text = text.substring( end );
        }

        return list;
    }

    public String getName() {
        return name;
    }

    public List getRules() {
        return rules;
    }

    public String getDescription() {
        return description;
    }

    public List getMetadata() {
        return metadata;
    }

    public Map> getOtherInformation() {
        return otherInformation;
    }

    public List getGlobals() {
        return globals;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy