-
Notifications
You must be signed in to change notification settings - Fork 59
/
ToolsXPath.cpp
122 lines (98 loc) · 4.05 KB
/
ToolsXPath.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "StdAfx.h"
#include "XMLTools.h"
#include "Scintilla.h"
#include "nppHelpers.h"
#include "Report.h"
#include "XmlFormater.h"
#include "XPathEvalDlg.h"
using namespace QuickXml;
std::wstring currentXPath(int xpathMode) {
dbgln("currentXPath()");
XmlFormater* formater = NULL;
int currentEdit;
std::wstring nodepath(L"");
::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)¤tEdit);
HWND hCurrentEditView = getCurrentHScintilla(currentEdit);
size_t currentLength = (size_t) ::SendMessage(hCurrentEditView, SCI_GETLENGTH, 0, 0);
char* data = new char[currentLength + sizeof(char)];
if (!data) return nodepath; // allocation error, abort check
size_t size = (currentLength + sizeof(char)) * sizeof(char);
memset(data, '\0', size);
size_t currentPos = size_t(::SendMessage(hCurrentEditView, SCI_GETCURRENTPOS, 0, 0));
::SendMessage(hCurrentEditView, SCI_GETTEXT, currentLength + sizeof(char), reinterpret_cast<LPARAM>(data));
XmlFormaterParamsType params = XmlFormater::getDefaultParams();
if ((xpathMode & XPATH_MODE_KEEPIDATTRIBUTE) != 0 && xmltoolsoptions.identityAttributes.length() > 0) {
params.dumpIdAttributesName = xmltoolsoptions.dumpAttributeName;
std::wstring temp;
std::wstringstream wss(xmltoolsoptions.identityAttributes);
while (std::getline(wss, temp, L';')) {
params.identityAttribues.push_back(Report::ws2s(temp));
}
}
formater = new XmlFormater(data, currentLength, params);
nodepath = Report::utf8ToUcs2(formater->currentPath(currentPos, xpathMode)->str());
delete[] data;
delete formater;
return nodepath;
}
void printCurrentXPathInStatusbar() {
dbgln("printCurrentXPathInStatusbar()");
int currentEdit;
::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)¤tEdit);
HWND hCurrentEditView = getCurrentHScintilla(currentEdit);
int xpathmode = XPATH_MODE_WITHNAMESPACE | XPATH_MODE_KEEPIDATTRIBUTE;
if (xmltoolsoptions.printXPathIndex) xpathmode |= XPATH_MODE_WITHNODEINDEX;
std::wstring tmp = currentXPath(xpathmode);
if (tmp.length() == 0) tmp = L" "; // empty value has no effect on NPPM_SETSTATUSBAR
::SendMessage(nppData._nppHandle, NPPM_SETSTATUSBAR, STATUSBAR_DOC_TYPE, (LPARAM) tmp.c_str());
}
void getCurrentXPath(bool precise) {
dbgln("getCurrentXPath()");
int xpathmode = precise ? XPATH_MODE_WITHNAMESPACE : XPATH_MODE_BASIC;
if (xmltoolsoptions.printXPathIndex) xpathmode |= XPATH_MODE_WITHNODEINDEX;
std::wstring nodepath(currentXPath(xpathmode));
std::wstring tmpmsg(L"Current node cannot be resolved.");
if (nodepath.length() > 0) {
tmpmsg = nodepath;
tmpmsg += L"\n\n(Path has been copied into clipboard)";
::OpenClipboard(NULL);
::EmptyClipboard();
size_t size = (nodepath.length() + 1) * sizeof(wchar_t);
HGLOBAL hClipboardData = GlobalAlloc(NULL, size);
if (hClipboardData != NULL) {
wchar_t* pchData = (wchar_t*)GlobalLock(hClipboardData);
if (pchData != NULL) {
memcpy(pchData, (wchar_t*)nodepath.c_str(), size);
::GlobalUnlock(hClipboardData);
}
::SetClipboardData(CF_UNICODETEXT, hClipboardData);
}
::CloseClipboard();
}
Report::_printf_inf(tmpmsg.c_str());
}
void getCurrentXPathStd() {
dbgln("getCurrentXPathStd()");
getCurrentXPath(false);
}
void getCurrentXPathPredicate() {
dbgln("getCurrentXPathPredicate()");
getCurrentXPath(true);
}
///////////////////////////////////////////////////////////////////////////////
CXPathEvalDlg* pXPathEvalDlg = NULL;
void evaluateXPath() {
dbgln("evaluateXPath()");
#if true
if (pXPathEvalDlg == NULL) {
pXPathEvalDlg = new CXPathEvalDlg(NULL, NULL);
}
pXPathEvalDlg->DoModal();
#else
if (pXPathEvalDlg == NULL) {
pXPathEvalDlg = new CXPathEvalDlg(NULL, NULL);
pXPathEvalDlg->Create(CXPathEvalDlg::IDD, NULL);
}
pXPathEvalDlg->ShowWindow(SW_SHOW);
#endif
}