001: package org.drools.brms.server.contenthandler;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.io.IOException;
020: import java.io.StringReader;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import org.drools.brms.client.rpc.RuleAsset;
025: import org.drools.brms.client.rpc.RuleContentText;
026: import org.drools.brms.server.builder.BRMSPackageBuilder;
027: import org.drools.brms.server.builder.ContentAssemblyError;
028: import org.drools.brms.server.builder.ContentPackageAssembler;
029: import org.drools.compiler.DroolsParserException;
030: import org.drools.lang.ExpanderException;
031: import org.drools.lang.dsl.DSLMappingFile;
032: import org.drools.lang.dsl.DefaultExpander;
033: import org.drools.repository.AssetItem;
034: import org.drools.repository.PackageItem;
035:
036: import com.google.gwt.user.client.rpc.SerializableException;
037:
038: public class DSLRuleContentHandler extends ContentHandler implements
039: IRuleAsset {
040:
041: public void retrieveAssetContent(RuleAsset asset, PackageItem pkg,
042: AssetItem item) throws SerializableException {
043: RuleContentText text = new RuleContentText();
044: text.content = item.getContent();
045:
046: asset.content = text;
047:
048: }
049:
050: public void storeAssetContent(RuleAsset asset, AssetItem repoAsset)
051: throws SerializableException {
052:
053: RuleContentText text = (RuleContentText) asset.content;
054: repoAsset.updateContent(text.content);
055:
056: }
057:
058: public void compile(BRMSPackageBuilder builder, AssetItem asset,
059: ContentPackageAssembler.ErrorLogger logger)
060: throws DroolsParserException, IOException {
061: DefaultExpander expander = getExpander(builder, asset, logger);
062:
063: //add the rule keyword if its 'stand alone'
064: String source = asset.getContent();
065: if (DRLFileContentHandler.isStandAloneRule(source)) {
066: source = wrapRule(asset, source);
067: }
068:
069: //expand and check for errors
070: String drl = expander.expand(source);
071: if (expander.hasErrors()) {
072: List exErrs = expander.getErrors();
073: for (Iterator iter = exErrs.iterator(); iter.hasNext();) {
074: ExpanderException ex = (ExpanderException) iter.next();
075: logger.logError(new ContentAssemblyError(asset, ex
076: .getMessage()));
077: }
078: return;
079: }
080:
081: builder.addPackageFromDrl(new StringReader(drl));
082: }
083:
084: private DefaultExpander getExpander(BRMSPackageBuilder builder,
085: AssetItem asset, ContentPackageAssembler.ErrorLogger logger) {
086:
087: if (!builder.hasDSL()) {
088: logger
089: .logError(new ContentAssemblyError(asset,
090: "This rule asset requires a DSL, yet none were configured in the package."));
091: }
092:
093: return builder.getDSLExpander();
094: }
095:
096: public void assembleDRL(BRMSPackageBuilder builder,
097: AssetItem asset, StringBuffer buf) {
098: //add the rule keyword if its 'stand alone'
099: String source = asset.getContent();
100: if (DRLFileContentHandler.isStandAloneRule(source)) {
101: source = wrapRule(asset, source);
102: }
103:
104: DefaultExpander expander = builder.getDSLExpander();
105: buf.append(expander.expand(source));
106:
107: }
108:
109: private String wrapRule(AssetItem asset, String source) {
110: return "rule '" + asset.getName() + "' \n" + source + "\nend";
111: }
112:
113: }
|