-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathToolsXmlValidate.cpp
197 lines (151 loc) · 6.22 KB
/
ToolsXmlValidate.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
#include "StdAfx.h"
#include "Scintilla.h"
#include "PluginInterface.h"
#include "nppHelpers.h"
#include "Report.h"
#include "XMLTools.h"
#include "XmlParser.h"
#include "SelectFileDlg.h"
#include "XmlWrapperInterface.h"
#include "MSXMLWrapper.h"
#include <string>
using namespace QuickXml;
int performXMLCheck(int informIfNoError) {
dbgln("performXMLCheck()");
// 0. change current folder
TCHAR currenPath[MAX_PATH] = { '\0' };
::SendMessage(nppData._nppHandle, NPPM_GETCURRENTDIRECTORY, MAX_PATH, (LPARAM)currenPath);
if (_wchdir(currenPath)) {
dbg(L"Unable to change directory to ");
dbgln(currenPath);
}
int currentEdit, res = 0;
::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)¤tEdit);
HWND hCurrentEditView = getCurrentHScintilla(currentEdit);
clearErrors(hCurrentEditView);
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));
auto t_start = clock();
XmlWrapperInterface* wrapper = new MSXMLWrapper(data, currentLength);
delete[] data; data = NULL;
bool isok = wrapper->checkSyntax();
auto t_end = clock();
dbgln(L"crunch time: " + std::to_wstring(t_end - t_start) + L" ms", DBG_LEVEL::DBG_INFO);
if (isok) {
if (informIfNoError) {
Report::_printf_inf(L"No error detected.");
}
}
else {
displayXMLErrors(wrapper->getLastErrors(), hCurrentEditView, L"XML Parsing error");
}
delete wrapper;
return res;
}
void autoXMLCheck() {
dbgln("autoXMLCheck()");
performXMLCheck(0);
}
void manualXMLCheck() {
dbgln("manualXMLCheck()");
performXMLCheck(1);
}
///////////////////////////////////////////////////////////////////////////////
CSelectFileDlg* pSelectFileDlg = NULL;
void XMLValidation(int informIfNoError) {
dbgln("XMLValidation()");
// 0. change current folder
TCHAR currenPath[MAX_PATH] = { '\0' };
::SendMessage(nppData._nppHandle, NPPM_GETCURRENTDIRECTORY, MAX_PATH, (LPARAM)currenPath);
if (_wchdir(currenPath)) {
dbg(L"Unable to change directory to ");
dbgln(currenPath);
}
// 1. check xml syntax
//bool abortValidation = false;
std::string xml_schema("");
int currentEdit;
::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)¤tEdit);
HWND hCurrentEditView = getCurrentHScintilla(currentEdit);
clearErrors(hCurrentEditView);
size_t currentLength = (size_t) ::SendMessage(hCurrentEditView, SCI_GETLENGTH, 0, 0);
char* data = new char[currentLength + sizeof(char)];
if (!data) return; // 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);
bool isok = wrapper->checkSyntax();
if (isok) {
// check if a schema prompt is requested
std::vector<XPathResultEntryType> nodes = wrapper->xpathEvaluate(L"/*/@xsi:noNamespaceSchemaLocation", L"xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'");
bool nnsl = (nodes.size() > 0);
nodes.clear();
nodes = wrapper->xpathEvaluate(L"/*/@xsi:schemaLocation", L"xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'");
bool sl = (nodes.size() > 0);
bool hasSchemaOrDTD = (nnsl || sl);
if (!hasSchemaOrDTD) {
// search for DTD - this will be done using QuickXml
XmlParser parser(data, currentLength);
XmlToken token = undefinedToken;
do {
token = parser.parseUntil(XmlTokenType::DeclarationBeg | XmlTokenType::TagOpening);
if (token.type == XmlTokenType::DeclarationBeg && !strncmp(token.chars, "<!DOCTYPE", 9)) {
break;
}
} while (token.type != XmlTokenType::EndOfFile && token.type != XmlTokenType::TagOpening);
hasSchemaOrDTD = (token.type == XmlTokenType::DeclarationBeg);
}
if (hasSchemaOrDTD) {
if (!wrapper->checkValidity()) {
std::vector<ErrorEntryType> errors = wrapper->getLastErrors();
displayXMLErrors(errors, hCurrentEditView, L"XML Validation error");
}
else {
Report::_printf_inf(L"No error detected.");
}
}
else {
// if noNamespaceSchemaLocation or schemaLocation attributes are not present,
// we must prompt for XSD to user and create a schema cache
if (pSelectFileDlg == NULL) {
pSelectFileDlg = new CSelectFileDlg();
}
//pSelectFileDlg->m_sSelectedFilename = lastXMLSchema.c_str();
CStringW rootSample = "<";
nodes.clear();
nodes = wrapper->xpathEvaluate(L"/*", L"");
if (nodes.size() == 1) {
pSelectFileDlg->m_sRootElementName = nodes.at(0).name.c_str();
}
else {
pSelectFileDlg->m_sRootElementName = L"";
}
if (pSelectFileDlg->DoModal() == IDOK) {
//lastXMLSchema = pSelectFileDlg->m_sSelectedFilename;
if (!wrapper->checkValidity(pSelectFileDlg->m_sSelectedFilename.GetString(), pSelectFileDlg->m_sValidationNamespace.GetString())) {
std::vector<ErrorEntryType> errors = wrapper->getLastErrors();
displayXMLErrors(errors, hCurrentEditView, L"XML Validation error");
}
else {
Report::_printf_inf(L"No error detected.");
}
}
}
}
else {
Report::_printf_inf(L"Please fix xml syntax first.");
}
delete[] data; data = NULL;
delete wrapper;
}
void autoValidation() {
dbgln("autoValidation()");
XMLValidation(0);
}
void manualValidation() {
dbgln("manualValidation()");
XMLValidation(1);
}