Skip to content

Commit

Permalink
Merge pull request #79 from zawiasam/feature/allowScalarResult
Browse files Browse the repository at this point in the history
handle scalar values
  • Loading branch information
DotJoshJohnson authored Dec 20, 2016
2 parents 77a9ae0 + 0b737ee commit 4bddc76
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 16 deletions.
18 changes: 10 additions & 8 deletions src/providers/XPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import * as vsc from 'vscode';
import * as ext from '../Extension';
import { XPathEvaluator } from '../services/XPathEvaluator';
import { XPathEvaluator, EvaluatorResult, EvaluatorResultType } from '../services/XPathEvaluator';

const CFG_SECTION: string = 'xmlTools';
const CFG_PERSIST_QUERY: string = 'persistXPathQuery';
Expand Down Expand Up @@ -59,12 +59,11 @@ export class XPathFeatureProvider {

// run the query
let xml: string = editor.document.getText();
let nodes: Node[];
let evalResult: EvaluatorResult;

try {
nodes = XPathEvaluator.evaluate(query, xml, ignoreDefaultNamespace);
evalResult = XPathEvaluator.evaluate(query, xml, ignoreDefaultNamespace);
}

catch (error) {
console.error(error);
vsc.window.showErrorMessage(`Something went wrong while evaluating the XPath: ${error}`);
Expand All @@ -78,10 +77,13 @@ export class XPathFeatureProvider {
outputChannel.appendLine(`XPath Query: ${query}`);
outputChannel.append('\n');

nodes.forEach((node: XmlNode) => {
outputChannel.appendLine(`[Line ${node.lineNumber}] ${node.localName}: ${node.textContent}`);
});

if (evalResult.type === EvaluatorResultType.NODE_COLLECTION) {
(evalResult.result as Node[]).forEach((node: XmlNode) => {
outputChannel.appendLine(`[Line ${node.lineNumber}] ${node.localName}: ${node.textContent}`);
});
} else {
outputChannel.appendLine(`[Result]: ${evalResult.result}`);
}
outputChannel.show(vsc.ViewColumn.Three);

// if persistence is enabled, save the query for later
Expand Down
51 changes: 43 additions & 8 deletions src/services/XPathEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,18 @@ import * as xpath from 'xpath';

let DOMParser = require('xmldom').DOMParser;

export class EvaluatorResult {
type: EvaluatorResultType;
result: Node[]|number|string|boolean;
}

export class EvaluatorResultType {
static SCALAR_TYPE: number = 0;
static NODE_COLLECTION: number = 1;
}

export class XPathEvaluator {
static evaluate(query: string, xml: string, ignoreDefaultNamespace: boolean): Node[] {
static evaluate(query: string, xml: string, ignoreDefaultNamespace: boolean): EvaluatorResult {
if (ignoreDefaultNamespace) {
xml = xml.replace(/xmlns=".+"/g, (match: string) => {
return match.replace(/xmlns/g, 'xmlns:default');
Expand All @@ -17,14 +27,39 @@ export class XPathEvaluator {
let xdoc: Document = new DOMParser().parseFromString(xml, 'text/xml');

let resolver: xpath.XPathNSResolver = xpath.createNSResolver(xdoc);
let expression: xpath.XPathExpression = xpath.createExpression(query, resolver);
let result: xpath.XPathResult = expression.evaluate(xdoc, xpath.XPathResult.ORDERED_NODE_ITERATOR_TYPE);
let result: xpath.XPathResult = xpath.evaluate(
query, // xpathExpression
xdoc, // contextNode
resolver, // namespaceResolver
xpath.XPathResult.ANY_TYPE, // resultType
null // result
)

let evalResult = new EvaluatorResult();
evalResult.type = EvaluatorResultType.SCALAR_TYPE;

let node: Node;
while (node = result.iterateNext()) {
nodes.push(node);
switch(result.resultType) {
case xpath.XPathResult.NUMBER_TYPE:
evalResult.result = result.numberValue;
break;
case xpath.XPathResult.STRING_TYPE:
evalResult.result = result.stringValue;
break;
case xpath.XPathResult.BOOLEAN_TYPE:
evalResult.result = result.booleanValue;
break;
case xpath.XPathResult.UNORDERED_NODE_ITERATOR_TYPE:
case xpath.XPathResult.ORDERED_NODE_ITERATOR_TYPE:
evalResult.result = result.booleanValue;
let node: Node;
while (node = result.iterateNext()) {
nodes.push(node);
}
evalResult.result = nodes;
evalResult.type = EvaluatorResultType.NODE_COLLECTION;
break;
}
return nodes;

return evalResult;
}
}

0 comments on commit 4bddc76

Please sign in to comment.