-
Notifications
You must be signed in to change notification settings - Fork 59
/
XPathEvalDlg.cpp
283 lines (233 loc) · 9.77 KB
/
XPathEvalDlg.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// XpathEvalDlg.cpp : implementation file
//
#include "StdAfx.h"
#include "Scintilla.h"
#include "XMLTools.h"
#include "XpathEvalDlg.h"
#include "Report.h"
#include "MSXMLHelper.h"
#include "XmlWrapperInterface.h"
#include "MSXMLWrapper.h"
#include <assert.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//extern void updateProxyConfig();
/////////////////////////////////////////////////////////////////////////////
// CXPathEvalDlg dialog
CXPathEvalDlg::CXPathEvalDlg(CWnd* pParent /*=NULL*/, unsigned long flags /*= 0*/)
: CDialog(CXPathEvalDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CXPathEvalDlg)
m_sExpression = _T("");
m_sResult = _T("");
//}}AFX_DATA_INIT
this->m_iFlags = flags;
}
void CXPathEvalDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CXPathEvalDlg)
DDX_Text(pDX, IDC_EDIT_EXPRESSION, m_sExpression);
DDX_Text(pDX, IDC_EDIT_NAMESPACE, m_sNamespace);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CXPathEvalDlg, CDialog)
//{{AFX_MSG_MAP(CXPathEvalDlg)
ON_BN_CLICKED(IDC_BTN_EVALUATE, OnBtnEvaluate)
ON_BN_CLICKED(IDC_BTN_COPY2CLIPBOARD, OnBnClickedBtnCopy2clipboard)
ON_WM_SIZE()
//}}AFX_MSG_MAP
// ON_WM_DESTROY()
//ON_WM_CLOSE()
ON_BN_CLICKED(IDC_BTN_CLEARLIST, &CXPathEvalDlg::OnBnClickedBtnClearlist)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CXPathEvalDlg message handlers
HWND CXPathEvalDlg::getCurrentHScintilla(int which) {
return (which == 0)?nppData._scintillaMainHandle:nppData._scintillaSecondHandle;
}
void CXPathEvalDlg::OnBtnEvaluate() {
this->UpdateData();
this->m_sResult = _T("");
if (!m_sExpression.GetLength()) {
Report::_printf_err(L"Empty expression; evaluation aborted.");
}
else {
execute_xpath_expression(m_sExpression);
}
}
/**
* execute_xpath_expression:
* @xpathExpr: the xpath expression for evaluation.
* @nsList: the optional list of known namespaces in
* "<prefix1>=<href1> <prefix2>=href2> ..." format.
*
* Parses input XML file, evaluates XPath expression and prints results.
*
* Returns 0 on success and a negative value otherwise.
*/
int CXPathEvalDlg::execute_xpath_expression(CStringW xpathExpr) {
int currentEdit;
::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(-1); // allocation error, abort check
memset(data, '\0', currentLength + sizeof(char));
::SendMessage(hCurrentEditView, SCI_GETTEXT, currentLength + sizeof(char), reinterpret_cast<LPARAM>(data));
XmlWrapperInterface* wrapper = new MSXMLWrapper(data, currentLength);
delete[] data; data = NULL;
std::vector<XPathResultEntryType> nodes = wrapper->xpathEvaluate(xpathExpr.GetString(), m_sNamespace.GetString());
std::vector<ErrorEntryType> errors = wrapper->getLastErrors();
if (errors.empty()) {
print_xpath_nodes(nodes);
}
else {
displayXMLErrors(errors, hCurrentEditView, L"Error: unable to parse XML", ERRORS_DISPLAY_MODE_ALERT);
}
delete wrapper;
return 0;
}
void CXPathEvalDlg::AddToList(CListCtrl *list, CStringW type, CStringW name, CStringW value) {
int idx = list->GetItemCount();
list->InsertItem(idx, type);
list->SetItemText(idx, 1, name);
list->SetItemText(idx, 2, value);
this->m_sResult.AppendFormat(L"%s\t%s\t%s\n", type.GetString(), name.GetString(), value.GetString());
}
void CXPathEvalDlg::print_xpath_nodes(std::vector<XPathResultEntryType> nodes) {
CListCtrl* listresults = (CListCtrl*)this->GetDlgItem(IDC_LIST_XPATHRESULTS);
listresults->DeleteAllItems();
if (nodes.size() == 0) {
AddToList(listresults, "", "No result", "");
}
else {
for (std::vector<XPathResultEntryType>::iterator it = nodes.begin(); it != nodes.end(); ++it) {
AddToList(listresults, Report::cstring((*it).type.c_str()),
Report::cstring((*it).name.c_str()),
Report::cstring((*it).value.c_str()));
}
}
}
BOOL CXPathEvalDlg::OnInitDialog() {
CDialog::OnInitDialog();
CListCtrl *listresults = (CListCtrl*) this->GetDlgItem(IDC_LIST_XPATHRESULTS);
// Initialize the destination list control
listresults->InsertColumn(0, L"Type", LVCFMT_LEFT, 100);
listresults->InsertColumn(1, L"Name", LVCFMT_LEFT, 150);
listresults->InsertColumn(2, L"Value", LVCFMT_LEFT, 400);
listresults->DeleteAllItems();
CString nfo = "If your expression requires namespaces, please declare them in \"Namespace definition\" field the same way you declare namespaces in xml, for instance:\nxmlns:npp='http://notepad-plus-plus.org' xmlns:a='another-namespace'";
SetDlgItemTextW(IDC_STATIC_INFOS, nfo);
CRect myRect;
GetClientRect(&myRect);
ClientToScreen(myRect);
MoveWindow(myRect.left+100, myRect.top+100, myRect.Width(), myRect.Height());
GetDlgItem(IDC_EDIT_EXPRESSION)->SetFocus();
return FALSE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
//void CXPathEvalDlg::OnClose() {
// CListCtrl *listresults = (CListCtrl*) this->GetDlgItem(IDC_LIST_XPATHRESULTS);
// //listresults->DestroyWindow();// ->DeleteAllItems();
//
// CDialog::OnClose();
//}
void CXPathEvalDlg::OnSize(UINT nType, int cx, int cy) {
CDialog::OnSize(nType, cx, cy);
CWnd* btn_wnd = GetDlgItem(IDC_BTN_EVALUATE);
CWnd* cpy_wnd = GetDlgItem(IDC_BTN_COPY2CLIPBOARD);
CWnd* clr_wnd = GetDlgItem(IDC_BTN_CLEARLIST);
CWnd* xpath_lbl = GetDlgItem(IDC_STATIC_XPATH);
CWnd* xpath_wnd = GetDlgItem(IDC_EDIT_EXPRESSION);
CWnd* ns_lbl = GetDlgItem(IDC_STATIC_NS);
CWnd* ns_wnd = GetDlgItem(IDC_EDIT_NAMESPACE);
CWnd* out_wnd = GetDlgItem(IDC_LIST_XPATHRESULTS);
CWnd* nfo_wnd = GetDlgItem(IDC_STATIC_INFOS);
if (btn_wnd && xpath_wnd && out_wnd) {
wchar_t buffer[1024];
nfo_wnd->GetWindowTextW(buffer, 1023);
CClientDC dc(this);
CFont* font;
font = GetFont();
dc.SelectObject(font);
float ratio = ((float) GetDeviceCaps(dc, LOGPIXELSX) / 96.f);
LONG units = GetDialogBaseUnits();
SIZE size;
GetTextExtentPoint32(dc, buffer, lstrlen(buffer), &size);
// https://docs.microsoft.com/fr-fr/windows/win32/uxguide/vis-layout?redirectedfrom=MSDN#sizingandspacing
// http://msdn.microsoft.com/en-us/library/ms645502.aspx
const int border = 8;
const int wndspace = 6;
const int btnwidth = MulDiv(LOWORD(units), 50, 4);
const int wndheight = (int) (14.f * 1.5f * ratio); // MulDiv(HIWORD(units), 14, 8);
const int lblwidth = 192;
const int nfoheight = size.cy * (int) fmin(8, 1 + round(((float)size.cx) / ((float)(cx - 2 * border - lblwidth))));
xpath_lbl->MoveWindow(border,
border,
lblwidth,
wndheight);
xpath_wnd->MoveWindow(border + lblwidth + wndspace,
border,
cx - 2 * border - wndspace - lblwidth,
wndheight);
ns_lbl->MoveWindow(border,
border + wndheight + wndspace,
lblwidth,
wndheight);
ns_wnd->MoveWindow(border + lblwidth + wndspace,
border + wndheight + wndspace,
cx - 2 * border - wndspace - lblwidth,
wndheight);
nfo_wnd->MoveWindow(border + lblwidth + wndspace,
border + 2 * wndheight + 2 * wndspace,
cx - 2 * border - wndspace - lblwidth,
nfoheight);
btn_wnd->MoveWindow(cx - border - 3 * btnwidth - 2 * wndspace,
border + 2 * wndheight + nfoheight + 3 * wndspace,
btnwidth,
wndheight);
cpy_wnd->MoveWindow(cx - border - 2 * btnwidth - wndspace,
border + 2 * wndheight + nfoheight + 3 * wndspace,
btnwidth,
wndheight);
clr_wnd->MoveWindow(cx - border - btnwidth,
border + 2 * wndheight + nfoheight + 3 * wndspace,
btnwidth,
wndheight);
out_wnd->MoveWindow(border,
border + 3 * wndheight + nfoheight + 4 * wndspace,
cx - 2 * border,
cy - 2 * border - 3 * wndheight - nfoheight - 4 * wndspace);
CDialog::UpdateWindow();
}
}
void CXPathEvalDlg::OnBnClickedBtnCopy2clipboard() {
if (this->m_sResult.IsEmpty()) {
MessageBox(L"Result is empty.");
} else {
::OpenClipboard(NULL);
::EmptyClipboard();
HGLOBAL hClipboardData;
int bytelen = (this->m_sResult.GetLength() + 1) * sizeof(wchar_t);
hClipboardData = GlobalAlloc(GMEM_DDESHARE, bytelen);
if (hClipboardData != NULL) {
wchar_t* pchData = (wchar_t*)GlobalLock(hClipboardData);
if (pchData != NULL) {
wcscpy(pchData, this->m_sResult.GetBuffer());
::GlobalUnlock(hClipboardData);
::SetClipboardData(CF_UNICODETEXT, pchData);
}
}
::CloseClipboard();
MessageBox(L"Result has been copied into clipboard.");
}
}
void CXPathEvalDlg::OnBnClickedBtnClearlist() {
CListCtrl *listresults = (CListCtrl*) this->GetDlgItem(IDC_LIST_XPATHRESULTS);
listresults->DeleteAllItems();
this->m_sResult.Empty();
}