Add XPath Engine
This commit is contained in:
		
							parent
							
								
									1cb72f0d69
								
							
						
					
					
						commit
						eb46dd3cf1
					
				
					 3 changed files with 52 additions and 1 deletions
				
			
		| 
						 | 
				
			
			@ -38,6 +38,10 @@
 | 
			
		|||
			{
 | 
			
		||||
				"command": "xmltools.linearizeXml",
 | 
			
		||||
				"title": "XML Tools: Linearize XML"
 | 
			
		||||
			},
 | 
			
		||||
			{
 | 
			
		||||
				"command": "xmltools.evaluateXPath",
 | 
			
		||||
				"title": "XML Tools: Evaluate XPath"
 | 
			
		||||
			}
 | 
			
		||||
		]
 | 
			
		||||
	},
 | 
			
		||||
| 
						 | 
				
			
			@ -51,7 +55,9 @@
 | 
			
		|||
	"dependencies": {
 | 
			
		||||
		"semver": "^5.1.0",
 | 
			
		||||
		"request": "^2.67.0",
 | 
			
		||||
		"pretty-data": "^0.40.0"
 | 
			
		||||
		"pretty-data": "^0.40.0",
 | 
			
		||||
		"xmldom": "^0.1.19",
 | 
			
		||||
		"xpath": "^0.0.9"
 | 
			
		||||
	},
 | 
			
		||||
	"scripts": {
 | 
			
		||||
		"compile": "node ./node_modules/vscode/bin/compile -watch -p ./"
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										43
									
								
								src/features/xmlXPathEngine.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								src/features/xmlXPathEngine.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,43 @@
 | 
			
		|||
'use strict';
 | 
			
		||||
 | 
			
		||||
import { window, TextEditor, TextEditorEdit, OutputChannel, ViewColumn } from 'vscode';
 | 
			
		||||
 | 
			
		||||
let xpath = require('xpath');
 | 
			
		||||
let dom = require('xmldom').DOMParser;
 | 
			
		||||
let resultChannel: OutputChannel = null;
 | 
			
		||||
 | 
			
		||||
export function evaluateXPath(editor: TextEditor, edit: TextEditorEdit): void {
 | 
			
		||||
	window.showInputBox({
 | 
			
		||||
		placeHolder: 'XPath Query',
 | 
			
		||||
		prompt: 'Please enter an XPath query to evaluate.'
 | 
			
		||||
		
 | 
			
		||||
	}).then((query) => {
 | 
			
		||||
		if (query === undefined) return;
 | 
			
		||||
		
 | 
			
		||||
		let xml = editor.document.getText();
 | 
			
		||||
		let doc = new dom().parseFromString(xml);
 | 
			
		||||
		
 | 
			
		||||
		try {
 | 
			
		||||
			var nodes = xpath.select(query, doc);
 | 
			
		||||
		}
 | 
			
		||||
		
 | 
			
		||||
		catch (ex) {
 | 
			
		||||
			window.showErrorMessage(ex);
 | 
			
		||||
			return;
 | 
			
		||||
		}
 | 
			
		||||
		
 | 
			
		||||
		if (nodes === null || nodes === undefined || nodes.length == 0) {
 | 
			
		||||
			window.showInformationMessage('Your XPath query returned no results.');
 | 
			
		||||
			return;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if (resultChannel === null) resultChannel = window.createOutputChannel('XPath Evaluation Results');
 | 
			
		||||
		resultChannel.clear();
 | 
			
		||||
		
 | 
			
		||||
		nodes.forEach((node) => {
 | 
			
		||||
			resultChannel.appendLine(`${node.localName}: ${node.firstChild.data}`);
 | 
			
		||||
		});
 | 
			
		||||
		
 | 
			
		||||
		resultChannel.show(ViewColumn.Three);
 | 
			
		||||
	});
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -2,6 +2,7 @@
 | 
			
		|||
 | 
			
		||||
import { commands, ExtensionContext } from 'vscode';
 | 
			
		||||
import { formatXml, linearizeXml } from './features/xmlFormatting';
 | 
			
		||||
import { evaluateXPath } from './features/xmlXPathEngine';
 | 
			
		||||
 | 
			
		||||
export function activate(ctx: ExtensionContext) {
 | 
			
		||||
	// check for update
 | 
			
		||||
| 
						 | 
				
			
			@ -10,4 +11,5 @@ export function activate(ctx: ExtensionContext) {
 | 
			
		|||
	// register pallete commands
 | 
			
		||||
	ctx.subscriptions.push(commands.registerTextEditorCommand('xmltools.formatXml', formatXml));
 | 
			
		||||
	ctx.subscriptions.push(commands.registerTextEditorCommand('xmltools.linearizeXml', linearizeXml));
 | 
			
		||||
	ctx.subscriptions.push(commands.registerTextEditorCommand('xmltools.evaluateXPath', evaluateXPath));
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		
		Reference in a new issue