Removed commented out code and some left over console.log's.
This commit is contained in:
parent
d09a2ee711
commit
60870497ef
|
@ -28,74 +28,6 @@ var BerReader = asn1.BerReader;
|
||||||
|
|
||||||
///--- Internal Parsers
|
///--- 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
|
//expression parsing
|
||||||
//returns the index of the closing parenthesis matching the open paren specified by openParenIndex
|
//returns the index of the closing parenthesis matching the open paren specified by openParenIndex
|
||||||
function matchParens(str, openParenIndex){
|
function matchParens(str, openParenIndex){
|
||||||
|
@ -115,16 +47,17 @@ function matchParens(str, openParenIndex){
|
||||||
//console.log('[_findMatchingParenthesis]: returning ', str.length-1);
|
//console.log('[_findMatchingParenthesis]: returning ', str.length-1);
|
||||||
return str.length-1;
|
return str.length-1;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
//recursive function that builds a filter tree from a string expression
|
//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.
|
//the filter tree is an intermediary step between the incoming expression and the outgoing Filter Class structure.
|
||||||
//see the comments of store.fetch concerning the filter argument for more info
|
|
||||||
function _buildFilterTree(expr){
|
function _buildFilterTree(expr){
|
||||||
console.log('[buildFilterTree]: expression: ',expr);
|
//console.log('[buildFilterTree]: expression: ',expr);
|
||||||
var tree = {};
|
var tree = {};
|
||||||
if(expr.length === 0){
|
if(expr.length === 0){
|
||||||
return tree;
|
return tree;
|
||||||
}
|
}
|
||||||
console.log(expr);
|
//console.log(expr);
|
||||||
|
|
||||||
//remove leading and trailing parenthesis if they are there
|
//remove leading and trailing parenthesis if they are there
|
||||||
if (expr.charAt(0) == '('){
|
if (expr.charAt(0) == '('){
|
||||||
|
@ -132,7 +65,7 @@ function _buildFilterTree(expr){
|
||||||
console.log('substring: '+expr);
|
console.log('substring: '+expr);
|
||||||
}
|
}
|
||||||
|
|
||||||
//store prefix op
|
//store prefix operator
|
||||||
if(expr.charAt(0) == '&'){
|
if(expr.charAt(0) == '&'){
|
||||||
tree.op = 'and';
|
tree.op = 'and';
|
||||||
expr = expr.substring(1);
|
expr = expr.substring(1);
|
||||||
|
@ -166,7 +99,11 @@ function _buildFilterTree(expr){
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}else{
|
}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 <DSML:equalityMatch ...>
|
||||||
|
//so thats why its named tag, if you were wondering...
|
||||||
var operatorStr = "";
|
var operatorStr = "";
|
||||||
var valueOffset=0;
|
var valueOffset=0;
|
||||||
tree.name = "";
|
tree.name = "";
|
||||||
|
@ -201,14 +138,24 @@ function _buildFilterTree(expr){
|
||||||
tree.name =splitAry[0];
|
tree.name =splitAry[0];
|
||||||
tree.value = splitAry[1];
|
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'){
|
if (tree.value.indexOf('*') != -1 && tree.tag == 'equalityMatch'){
|
||||||
tree.tag = 'substrings';
|
tree.tag = 'substrings';
|
||||||
|
|
||||||
var split = tree.value.split("*");
|
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){
|
if(tree.value.indexOf("*") != 0){
|
||||||
tree.initial = split.shift();
|
tree.initial = split.shift();
|
||||||
}else{
|
}else{
|
||||||
split.shift();
|
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){
|
if(tree.value.lastIndexOf("*") != tree.value.length-1){
|
||||||
tree['final'] = split.pop();
|
tree['final'] = split.pop();
|
||||||
}else{
|
}else{
|
||||||
|
@ -226,7 +173,7 @@ function _buildFilterTree(expr){
|
||||||
};
|
};
|
||||||
|
|
||||||
var treeToObjs = function(tree,filterObj){
|
var treeToObjs = function(tree,filterObj){
|
||||||
console.log("in treeToObjs");
|
//console.log("in treeToObjs");
|
||||||
if(tree === undefined){
|
if(tree === undefined){
|
||||||
return filterObj;
|
return filterObj;
|
||||||
}
|
}
|
||||||
|
@ -235,6 +182,7 @@ var treeToObjs = function(tree,filterObj){
|
||||||
return 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;
|
var currentFilter = filterObj;
|
||||||
if(tree.op != "expr"){
|
if(tree.op != "expr"){
|
||||||
console.log("adding "+tree.op+" to filters");
|
console.log("adding "+tree.op+" to filters");
|
||||||
|
@ -254,36 +202,38 @@ var treeToObjs = function(tree,filterObj){
|
||||||
treeToObjs(child,currentFilter);
|
treeToObjs(child,currentFilter);
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
|
//else its a leaf node in the tree, and represents some type of non-logical expression
|
||||||
var tempFilter;
|
var tempFilter;
|
||||||
//console.log("adding "+tree.tag+" to filters");
|
|
||||||
|
//convert the tag name to a filter class type
|
||||||
switch(tree.tag){
|
switch(tree.tag){
|
||||||
case "approxMatch":
|
case "approxMatch":
|
||||||
tempFilter = new ApproximateFilter({
|
tempFilter = new ApproximateFilter({
|
||||||
attribute: tree.name,
|
attribute: tree.name,
|
||||||
value: tree.value
|
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;
|
break;
|
||||||
case "greaterOrEqual":
|
case "greaterOrEqual":
|
||||||
tempFilter = new GreaterThanEqualsFilter({
|
tempFilter = new GreaterThanEqualsFilter({
|
||||||
attribute: tree.name,
|
attribute: tree.name,
|
||||||
value: tree.value
|
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;
|
break;
|
||||||
case "lessOrEqual":
|
case "lessOrEqual":
|
||||||
tempFilter = new LessThanEqualsFilter({
|
tempFilter = new LessThanEqualsFilter({
|
||||||
attribute: tree.name,
|
attribute: tree.name,
|
||||||
value: tree.value
|
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;
|
break;
|
||||||
case "equalityMatch":
|
case "equalityMatch":
|
||||||
tempFilter = new EqualityFilter({
|
tempFilter = new EqualityFilter({
|
||||||
attribute: tree.name,
|
attribute: tree.name,
|
||||||
value: tree.value
|
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;
|
break;
|
||||||
case "substrings":
|
case "substrings":
|
||||||
tempFilter = new SubstringFilter({
|
tempFilter = new SubstringFilter({
|
||||||
|
@ -292,13 +242,13 @@ var treeToObjs = function(tree,filterObj){
|
||||||
any: tree.any,
|
any: tree.any,
|
||||||
"final": tree["final"]
|
"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;
|
break;
|
||||||
case "present":
|
case "present":
|
||||||
tempFilter = new PresenceFilter({
|
tempFilter = new PresenceFilter({
|
||||||
attribute: tree.name
|
attribute: tree.name
|
||||||
});
|
});
|
||||||
console.log("adding "+tree.tag+"; attr: "+tree.name);
|
//console.log("adding "+tree.tag+"; attr: "+tree.name);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
filterObj.addFilter(tempFilter);
|
filterObj.addFilter(tempFilter);
|
||||||
|
@ -308,130 +258,20 @@ var treeToObjs = function(tree,filterObj){
|
||||||
|
|
||||||
function _parseString(str){
|
function _parseString(str){
|
||||||
assert.ok(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({
|
var filterObj = new AndFilter({
|
||||||
filters:[]
|
filters:[]
|
||||||
});
|
});
|
||||||
|
|
||||||
var tree = _buildFilterTree(str);
|
var tree = _buildFilterTree(str);
|
||||||
console.log("tree built: ",JSON.stringify(tree));
|
//console.log("tree built: ",JSON.stringify(tree));
|
||||||
treeToObjs(tree,filterObj);
|
treeToObjs(tree,filterObj);
|
||||||
return filterObj.filters[0];
|
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:
|
* A filter looks like this coming in:
|
||||||
* Filter ::= CHOICE {
|
* Filter ::= CHOICE {
|
||||||
|
|
Loading…
Reference in New Issue