⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 templatefactory.java

📁 jboss规则引擎
💻 JAVA
字号:
package org.drools.lang.dsl.template;

/*
 * 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.
 */

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

/**
 * This takes a natural grammar template (the left hand side of a grammar mapping table)
 * and builds a TemplateContext for it.
 * 
 * Uses a built in lexer.
 * 
 * @author <a href="mailto:michael.neale@gmail.com"> Michael Neale</a>
 */
class TemplateFactory {

    /**
     * This will lex the template string into chunks.
     * @param naturalTemplate From the grammar. eg "{0} likes cheese" is a template.
     * @return A template context ready to apply to a nl expression.
     */
    public Template getTemplate(final String naturalTemplate) {

        final Template template = new Template();

        final List chunkList = lexChunks( naturalTemplate );
        for ( final Iterator iter = chunkList.iterator(); iter.hasNext(); ) {
            template.addChunk( (String) iter.next() );

        }
        return template;
    }

    List lexChunks(final String grammarTemplate) {
        final ChunkLexer lexer = new ChunkLexer();
        return lexer.lex( grammarTemplate );
    }

    /**
     * Lex out chunks. 
     * @author <a href="mailto:michael.neale@gmail.com"> Michael Neale</a>
     */
    static class ChunkLexer {

        private final List         chunks = new ArrayList();

        private StringBuffer buffer = new StringBuffer();

        public List lex(final String grammarTemplate) {

            final char[] chars = grammarTemplate.toCharArray();

            for ( int i = 0; i < chars.length; i++ ) {
                switch ( chars[i] ) {
                    case '{' :
                        startHole();
                        break;
                    case '}' :
                        endHole();
                        break;
                    default :
                        this.buffer.append( chars[i] );
                        break;
                }
            }
            final String buf = this.buffer.toString();
            if ( !buf.equals( "" ) ) {
                addChunk( buf );
            }
            return this.chunks;

        }

        private boolean addChunk(final String buf) {
            return this.chunks.add( buf.trim() );
        }

        private void endHole() {
            final String buf = this.buffer.toString();
            this.chunks.add( "{" + buf + "}" );
            this.buffer = new StringBuffer();
        }

        private void startHole() {
            final String buf = this.buffer.toString();
            if ( !buf.equals( "" ) ) {
                addChunk( buf );
            }
            this.buffer = new StringBuffer();
        }

    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -