001: package org.drools.eclipse.editors;
002:
003: import java.util.Iterator;
004:
005: import org.drools.lang.descr.AttributeDescr;
006: import org.drools.lang.descr.BaseDescr;
007: import org.drools.lang.descr.FactTemplateDescr;
008: import org.drools.lang.descr.FunctionDescr;
009: import org.drools.lang.descr.FunctionImportDescr;
010: import org.drools.lang.descr.GlobalDescr;
011: import org.drools.lang.descr.ImportDescr;
012: import org.drools.lang.descr.PackageDescr;
013: import org.drools.lang.descr.RuleDescr;
014:
015: /**
016: * Util class for searching Descr tree.
017: *
018: * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
019: */
020: public final class DescrUtil {
021:
022: private DescrUtil() {
023: }
024:
025: public static BaseDescr getDescr(BaseDescr descr, int offset) {
026: if (descr instanceof PackageDescr) {
027: PackageDescr packageDescr = (PackageDescr) descr;
028: // rules
029: for (Iterator iterator = packageDescr.getRules().iterator(); iterator
030: .hasNext();) {
031: RuleDescr ruleDescr = (RuleDescr) iterator.next();
032: if (ruleDescr != null) {
033: BaseDescr result = getDescr(ruleDescr, offset);
034: if (result != null) {
035: return result;
036: }
037: }
038: }
039: // imports
040: for (Iterator iterator = packageDescr.getImports()
041: .iterator(); iterator.hasNext();) {
042: ImportDescr importDescr = (ImportDescr) iterator.next();
043: if (importDescr != null) {
044: BaseDescr result = getDescr(importDescr, offset);
045: if (result != null) {
046: return result;
047: }
048: }
049: }
050: // function imports
051: for (Iterator iterator = packageDescr.getFunctionImports()
052: .iterator(); iterator.hasNext();) {
053: FunctionImportDescr functionImportDescr = (FunctionImportDescr) iterator
054: .next();
055: if (functionImportDescr != null) {
056: BaseDescr result = getDescr(functionImportDescr,
057: offset);
058: if (result != null) {
059: return result;
060: }
061: }
062: }
063: // functions
064: for (Iterator iterator = packageDescr.getFunctions()
065: .iterator(); iterator.hasNext();) {
066: FunctionDescr functionDescr = (FunctionDescr) iterator
067: .next();
068: if (functionDescr != null) {
069: BaseDescr result = getDescr(functionDescr, offset);
070: if (result != null) {
071: return result;
072: }
073: }
074: }
075: // attributes
076: for (Iterator iterator = packageDescr.getAttributes()
077: .iterator(); iterator.hasNext();) {
078: AttributeDescr attributeDescr = (AttributeDescr) iterator
079: .next();
080: if (attributeDescr != null) {
081: BaseDescr result = getDescr(attributeDescr, offset);
082: if (result != null) {
083: return result;
084: }
085: }
086: }
087: // globals
088: for (Iterator iterator = packageDescr.getGlobals()
089: .iterator(); iterator.hasNext();) {
090: GlobalDescr globalDescr = (GlobalDescr) iterator.next();
091: if (globalDescr != null) {
092: BaseDescr result = getDescr(globalDescr, offset);
093: if (result != null) {
094: return result;
095: }
096: }
097: }
098: // fact templates
099: for (Iterator iterator = packageDescr.getFactTemplates()
100: .iterator(); iterator.hasNext();) {
101: FactTemplateDescr factTemplDescr = (FactTemplateDescr) iterator
102: .next();
103: if (factTemplDescr != null) {
104: BaseDescr result = getDescr(factTemplDescr, offset);
105: if (result != null) {
106: return result;
107: }
108: }
109: }
110: return null;
111: } else {
112: if (offset < descr.getStartCharacter()
113: || offset > descr.getEndCharacter()) {
114: return null;
115: }
116: // TODO: select subDescr if possible
117: return descr;
118: }
119: }
120:
121: }
|