Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.drools.guvnor.server.contenthandler;
  
  /*
   * Copyright 2005 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.
  */
 
 
 
     implements
     IRuleAsset {
 
     public void compile(BRMSPackageBuilder builder,
                         AssetItem asset,
                         ContentPackageAssembler.ErrorLogger loggerthrows DroolsParserException,
                                                                    IOException {
         builder.addPackageFromDrlnew StringReadergetContentasset ) ) );
     }
 
     private String getContent(AssetItem asset) {
         String content = asset.getContent();
         if ( isStandAloneRulecontent ) ) {
 
             String parentName = this.parentNameFromCategoryasset,
                                                              "" );
             content = wrapRuleDeclarationasset.getName(),
                                            parentName,
                                            content );
         }
         return content;
     }
 
                                String parentName,
                                String content) {
         if ( parentName == null || "".equalsparentName ) ) {
             return "rule '" + name + "'\n" + getContentcontent ) + "\nend";
         } else {
             return "rule '" + name + "' extends " + parentName + "\n" + getContentcontent ) + "\nend";
         }
     }
 
     String getContent(String content) {
         if ( content != null && content.indexOf"dialect" ) == -1 ) {
             return "dialect 'mvel'\n" + content;
         }
         return content;
     }

    
This will try and sniff ouf if its a stand alone rule which will use the asset name as the rule name, or if it should be treated as a package (in the latter case, the content is passed as it to the compiler).
 
     public static boolean isStandAloneRule(String content) {
         if ( content == null || "".equalscontent.trim() ) ) {
             return false;
         }
         StringTokenizer st = new StringTokenizercontent,
                                                   "\n\r" );
         while ( st.hasMoreTokens() ) {
             String tok = st.nextToken().trim();
             if ( tok.startsWith"when" ) ) {
                 //well obviously it is stand alone...
                 return true;
             }
             //otherwise sniff for a suitable keyword at the start of a line
             if ( startsWithWord"package",
                                  tok ) || startsWithWord"rule",
                                                           tok ) || startsWithWord"end",
                                                                                    tok ) || startsWithWord"function",
                                                                                                             tok ) || startsWithWord"query",
                                                                                                                                      tok ) ) {
                 return false;
             }
         }
         return true;
 
     }
 
    static boolean startsWithWord(String word,
                                  String sentence) {
        String[] words = sentence.trim().split"\\s" );
        if ( words.length > 0 ) {
            return words[0].equalsword );
        } else {
            return false;
        }
    }
    public void assembleDRL(BRMSPackageBuilder builder,
                            AssetItem asset,
                            StringBuffer buf) {
        String content = asset.getContent();
        boolean standAlone = isStandAloneRulecontent );
        if ( standAlone ) {
            String parentName = this.parentNameFromCategoryasset,
                                                             "" );
            buf.appendwrapRuleDeclarationasset.getName(),
                                             parentName,
                                             content ) );
        } else {
            buf.appendcontent );
        }
    }
New to GrepCode? Check out our FAQ X