01: package org.drools.brms.server.contenthandler;
02:
03: /*
04: * Copyright 2005 JBoss Inc
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: import java.io.IOException;
20: import java.io.StringReader;
21: import java.util.StringTokenizer;
22:
23: import org.drools.brms.server.builder.BRMSPackageBuilder;
24: import org.drools.brms.server.builder.ContentPackageAssembler;
25: import org.drools.compiler.DroolsParserException;
26: import org.drools.repository.AssetItem;
27:
28: public class DRLFileContentHandler extends PlainTextContentHandler
29: implements IRuleAsset {
30:
31: public void compile(BRMSPackageBuilder builder, AssetItem asset,
32: ContentPackageAssembler.ErrorLogger logger)
33: throws DroolsParserException, IOException {
34: builder.addPackageFromDrl(new StringReader(getContent(asset)));
35: }
36:
37: private String getContent(AssetItem asset) {
38: String content = asset.getContent();
39: if (isStandAloneRule(content)) {
40: content = wrapRuleDeclaration(asset, content);
41: }
42: return content;
43: }
44:
45: private String wrapRuleDeclaration(AssetItem asset, String content) {
46: return "rule '" + asset.getName() + "'\n" + content + "\nend";
47: }
48:
49: /**
50: * This will try and sniff ouf if its a stand alone rule which
51: * will use the asset name as the rule name, or if it should be treated as a package
52: * (in the latter case, the content is passed as it to the compiler).
53: */
54: static boolean isStandAloneRule(String content) {
55: if (content == null || "".equals(content.trim())) {
56: return false;
57: }
58: StringTokenizer st = new StringTokenizer(content, " ");
59: while (st.hasMoreTokens()) {
60: String tok = st.nextToken();
61: if (tok.equals("package") || tok.equals("rule")
62: || tok.equals("end") || tok.equals("function")) {
63: return false;
64: }
65: }
66: return true;
67:
68: }
69:
70: public void assembleDRL(BRMSPackageBuilder builder,
71: AssetItem asset, StringBuffer buf) {
72: String content = asset.getContent();
73: boolean standAlone = isStandAloneRule(content);
74: if (standAlone) {
75: buf.append(wrapRuleDeclaration(asset, content));
76: } else {
77: buf.append(content);
78: }
79: }
80: }
|