From 60870497efc7e1e052fc2eae1457c420a95ec6ae Mon Sep 17 00:00:00 2001 From: Craig Baker Date: Thu, 19 Jan 2012 15:25:20 -0500 Subject: [PATCH] Removed commented out code and some left over console.log's. --- lib/filters/index.js | 234 +++++++------------------------------------ 1 file changed, 37 insertions(+), 197 deletions(-) diff --git a/lib/filters/index.js b/lib/filters/index.js index f2653f6..6a7d07d 100644 --- a/lib/filters/index.js +++ b/lib/filters/index.js @@ -28,74 +28,6 @@ var BerReader = asn1.BerReader; ///--- Internal Parsers - -/* - * This is a pretty naive approach to parsing, but it's relatively short amount - * of code. Basically, we just build a stack as we go. - */ -function _filterStringToStack(str) { - assert.ok(str); - - var tmp = ''; - var esc = false; - var stack = []; - var depth = -1; - var open = false; - for (var i = 0; i < str.length; i++) { - var c = str[i]; - - if (esc) { - esc = false; - tmp += c; - continue; - } - - switch (c) { - case '(': - open = true; - tmp = ''; - stack[++depth] = ''; - break; - case ')': - if (open) { - stack[depth].value = tmp; - tmp = ''; - } - open = false; - break; - case '&': - case '|': - case '!': - stack[depth] = c; - break; - case '=': - stack[depth] = { attribute: tmp, op: c }; - tmp = ''; - break; - case '>': - case '<': - case '~': - if (!(str[++i] === '=')) - throw new Error('Invalid filter: ' + tmp + c + str[i]); - - stack[depth] = {attribute: tmp, op: c}; - tmp = ''; - break; - case '\\': - esc = true; - default: - tmp += c; - break; - } - } - - if (open) - throw new Error('Invalid filter: ' + str); - - return stack; -} - - //expression parsing //returns the index of the closing parenthesis matching the open paren specified by openParenIndex function matchParens(str, openParenIndex){ @@ -115,16 +47,17 @@ function matchParens(str, openParenIndex){ //console.log('[_findMatchingParenthesis]: returning ', str.length-1); return str.length-1; }; + + //recursive function that builds a filter tree from a string expression -//the filter tree is an intermediary step between the incoming expression and the outgoing Dsml. -//see the comments of store.fetch concerning the filter argument for more info +//the filter tree is an intermediary step between the incoming expression and the outgoing Filter Class structure. function _buildFilterTree(expr){ - console.log('[buildFilterTree]: expression: ',expr); + //console.log('[buildFilterTree]: expression: ',expr); var tree = {}; if(expr.length === 0){ return tree; } - console.log(expr); + //console.log(expr); //remove leading and trailing parenthesis if they are there if (expr.charAt(0) == '('){ @@ -132,7 +65,7 @@ function _buildFilterTree(expr){ console.log('substring: '+expr); } - //store prefix op + //store prefix operator if(expr.charAt(0) == '&'){ tree.op = 'and'; expr = expr.substring(1); @@ -166,7 +99,11 @@ function _buildFilterTree(expr){ i++; } }else{ - //else its equality expression, parse and return as such + //else its some sort of non-logical expression, parse and return as such + + //tag represents the name of the operator, initially this library was used to + //encode a filter string into DSML where the tag name would be + //so thats why its named tag, if you were wondering... var operatorStr = ""; var valueOffset=0; tree.name = ""; @@ -201,14 +138,24 @@ function _buildFilterTree(expr){ tree.name =splitAry[0]; tree.value = splitAry[1]; + //substrings fall into the equality bin in the switch above + //so we need another check here to look for * if (tree.value.indexOf('*') != -1 && tree.tag == 'equalityMatch'){ tree.tag = 'substrings'; + var split = tree.value.split("*"); + + //if the value string doesn't start with a * then theres no initial value + //else split will have an empty string in its first array index... + //we need to remove that empty string if(tree.value.indexOf("*") != 0){ tree.initial = split.shift(); }else{ split.shift(); } + + //if the value string doesn't end with a * then theres no final value + //also same split stuff as the initial stuff above if(tree.value.lastIndexOf("*") != tree.value.length-1){ tree['final'] = split.pop(); }else{ @@ -226,7 +173,7 @@ function _buildFilterTree(expr){ }; var treeToObjs = function(tree,filterObj){ - console.log("in treeToObjs"); + //console.log("in treeToObjs"); if(tree === undefined){ return filterObj; } @@ -235,6 +182,7 @@ var treeToObjs = function(tree,filterObj){ return filterObj; } + //if the current tree object is not an expression then its a logical operator (ie an internal node in the tree) var currentFilter = filterObj; if(tree.op != "expr"){ console.log("adding "+tree.op+" to filters"); @@ -254,36 +202,38 @@ var treeToObjs = function(tree,filterObj){ treeToObjs(child,currentFilter); } }else{ + //else its a leaf node in the tree, and represents some type of non-logical expression var tempFilter; - //console.log("adding "+tree.tag+" to filters"); + + //convert the tag name to a filter class type switch(tree.tag){ case "approxMatch": tempFilter = new ApproximateFilter({ attribute: tree.name, value: tree.value }); - console.log("adding "+tree.tag+"; attr: "+tree.name+"; value: "+tree.value); + //console.log("adding "+tree.tag+"; attr: "+tree.name+"; value: "+tree.value); break; case "greaterOrEqual": tempFilter = new GreaterThanEqualsFilter({ attribute: tree.name, value: tree.value }); - console.log("adding "+tree.tag+"; attr: "+tree.name+"; value: "+tree.value); + //console.log("adding "+tree.tag+"; attr: "+tree.name+"; value: "+tree.value); break; case "lessOrEqual": tempFilter = new LessThanEqualsFilter({ attribute: tree.name, value: tree.value }); - console.log("adding "+tree.tag+"; attr: "+tree.name+"; value: "+tree.value); + //console.log("adding "+tree.tag+"; attr: "+tree.name+"; value: "+tree.value); break; case "equalityMatch": tempFilter = new EqualityFilter({ attribute: tree.name, value: tree.value }); - console.log("adding "+tree.tag+"; attr: "+tree.name+"; value: "+tree.value); + //console.log("adding "+tree.tag+"; attr: "+tree.name+"; value: "+tree.value); break; case "substrings": tempFilter = new SubstringFilter({ @@ -292,13 +242,13 @@ var treeToObjs = function(tree,filterObj){ any: tree.any, "final": tree["final"] }); - console.log("adding "+tree.tag+"; attr: "+tree.name+"; initial: "+tree.initial+"; any: "+JSON.stringify(tree.any) + "; final: "+tree['final']); + //console.log("adding "+tree.tag+"; attr: "+tree.name+"; initial: "+tree.initial+"; any: "+JSON.stringify(tree.any) + "; final: "+tree['final']); break; case "present": tempFilter = new PresenceFilter({ attribute: tree.name }); - console.log("adding "+tree.tag+"; attr: "+tree.name); + //console.log("adding "+tree.tag+"; attr: "+tree.name); break; } filterObj.addFilter(tempFilter); @@ -308,130 +258,20 @@ var treeToObjs = function(tree,filterObj){ function _parseString(str){ assert.ok(str); + //create a blank object to pass into treeToObjs + //since its recursive we have to prime it ourselves. + //this gets stripped off before the filter structure is returned + //at the bottom of this function. var filterObj = new AndFilter({ filters:[] }); var tree = _buildFilterTree(str); - console.log("tree built: ",JSON.stringify(tree)); + //console.log("tree built: ",JSON.stringify(tree)); treeToObjs(tree,filterObj); return filterObj.filters[0]; }; -/* -function _parseString(str) { - assert.ok(str); - - var stack = _filterStringToStack(str); - - if (!stack || !stack.length) - throw new Error('Invalid filter: ' + str); - - debugger; - var f; - var filters = []; - for (var i = stack.length - 1; i >= 0; i--) { - if (stack[i] === '&') { - filters.unshift(new AndFilter({ - filters: filters - })); - filters.length = 1; - } else if (stack[i] === '|') { - filters.unshift(new OrFilter({ - filters: filters - })); - filters.length = 1; - } else if (stack[i] === '!') { - filters.push(new NotFilter({ - filter: filters.pop() - })); - } else { - switch (stack[i].op) { - case '=': // could be presence, equality, substr or ext - if (stack[i].value === '*') { - filters.push(new PresenceFilter(stack[i])); - } else { - var vals = ['']; - var ndx = 0; - var esc = false; - for (var j = 0; j < stack[i].value.length; j++) { - var c = stack[i].value[j]; - if (c === '\\') { - if (esc) { - esc = true; - } else { - vals[ndx] += c; - esc = false; - } - } else if (c === '*') { - if (esc) { - vals[ndx] = c; - } else { - vals[++ndx] = ''; - } - } else { - vals[ndx] += c; - } - } - if (vals.length === 1) { - if (stack[i].attribute.indexOf(':') !== -1) { - var extTmp = stack[i].attribute.split(':'); - var extOpts = {}; - extOpts.matchType = extTmp[0]; - switch (extTmp.length) { - case 2: - break; - case 3: - if (extTmp[1].toLowerCase() === 'dn') { - extOpts.dnAttributes = true; - } else { - extOpts.rule = extTmp[1]; - } - break; - case 4: - extOpts.dnAttributes = true; - extOpts.rule = extTmp[2]; - break; - default: - throw new Error('Invalid extensible filter'); - } - extOpts.value = vals[0]; - filters.push(new ExtensibleFilter(extOpts)); - } else { - filters.push(new EqualityFilter(stack[i])); - } - } else { - filters.push(new SubstringFilter({ - attribute: stack[i].attribute, - initial: vals.shift() || null, - 'final': vals.pop() || null, - any: vals || null - })); - } - } - break; - case '~': - filters.push(new ApproximateFilter(stack[i])); - break; - case '>': - filters.push(new GreaterThanEqualsFilter(stack[i])); - break; - case '<': - filters.push(new LessThanEqualsFilter(stack[i])); - break; - default: - throw new Error('Invalid filter (op=' + stack[i].op + '): ' + str); - } - } - } - - if (filters.length !== 1) - throw new Error('Invalid filter: ' + str); - - return filters.pop(); -} -*/ - /* * A filter looks like this coming in: * Filter ::= CHOICE {