Skip to content

Commit f7a6c68

Browse files
committed
CollectionInterface: overrideMap = handle errors when parsing XML
1 parent 5fc629f commit f7a6c68

4 files changed

Lines changed: 66 additions & 26 deletions

File tree

src/Classes/OverrideMapUpdaterClass.cpp

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,27 @@ OverrideMapUpdater::OverrideMapUpdater(void)
6666
/*tinyxml2::XMLElement* pAssoc =*/ add_udl_assoc("nppexec.xml", "NppExec");
6767
}
6868
else {
69-
// TODO: !!! NEED TO ADD ERROR CHECKING !!!
7069
// load file and extract the main elements
71-
/*tinyxml2::XMLError eResult =*/ pOverrideMapXML->LoadFile(sOverMapPath().c_str());
70+
tinyxml2::XMLError eResult = pOverrideMapXML->LoadFile(sOverMapPath().c_str());
71+
if(_xml_check_result(eResult, pOverrideMapXML, wsOverMapPath())) return;
72+
7273
pRoot = pOverrideMapXML->FirstChildElement("NotepadPlus");
74+
if (!pRoot) {
75+
_xml_check_result(tinyxml2::XML_ERROR_PARSING, pOverrideMapXML, wsOverMapPath());
76+
return;
77+
}
78+
7379
pFunctionList = pRoot->FirstChildElement("functionList");
80+
if (!pFunctionList) {
81+
_xml_check_result(tinyxml2::XML_ERROR_PARSING, pOverrideMapXML, wsOverMapPath());
82+
return;
83+
}
84+
7485
pAssociationMap = pFunctionList->FirstChildElement("associationMap");
86+
if (!pAssociationMap) {
87+
_xml_check_result(tinyxml2::XML_ERROR_PARSING, pOverrideMapXML, wsOverMapPath());
88+
return;
89+
}
7590
}
7691
}
7792

@@ -122,20 +137,6 @@ std::vector<tinyxml2::XMLElement*> OverrideMapUpdater::add_udl_assoc(std::map<st
122137
return vpAssoc;
123138
}
124139

125-
bool OverrideMapUpdater::experiment(void)
126-
{
127-
add_udl_assoc("fake.xml", "FakeUDL");
128-
std::map<std::wstring, std::wstring> myMap = {
129-
{L"udl1.xml", L"UDL 1"},
130-
{L"udl2.xml", L"UDL 2"},
131-
{L"udl3.xml", L"UDL 3"}
132-
};
133-
add_udl_assoc(myMap);
134-
pOverrideMapXML->SaveFile(sOverMapPath().c_str());
135-
136-
return true;
137-
}
138-
139140
// private: case-insensitive std::string equality check
140141
bool _string_insensitive_eq(std::string a, std::string b)
141142
{
@@ -194,3 +195,44 @@ tinyxml2::XMLElement* OverrideMapUpdater::_find_element_with_attribute_value(tin
194195
}
195196
return pFoundElement;
196197
}
198+
199+
200+
// compares the XMLError result to XML_SUCCESS, and returns a TRUE boolean to indicate failure
201+
// p_doc defaults to nullptr, wsFilePath to L""
202+
bool OverrideMapUpdater::_xml_check_result(tinyxml2::XMLError a_eResult, tinyxml2::XMLDocument* p_doc, std::wstring wsFilePath)
203+
{
204+
if (a_eResult != tinyxml2::XML_SUCCESS) {
205+
std::string sMsg = std::string("XML Error #") + std::to_string(static_cast<int>(a_eResult));
206+
if (p_doc != NULL) {
207+
sMsg += std::string(": ") + std::string(p_doc->ErrorStr());
208+
if (p_doc->ErrorLineNum()) {
209+
sMsg += "\n\nI will try to open the file near that error.";
210+
}
211+
}
212+
::MessageBoxA(NULL, sMsg.c_str(), "XML Error", MB_ICONWARNING | MB_OK);
213+
if (p_doc != NULL && p_doc->ErrorLineNum() && wsFilePath.size()) {
214+
if (::SendMessage(gNppMetaInfo.hwnd._nppHandle, NPPM_DOOPEN, 0, reinterpret_cast<LPARAM>(wsFilePath.c_str()))) {
215+
extern NppData nppData; // not in PluginDefinition.h
216+
217+
// Get the current scintilla
218+
int which = -1;
219+
::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)&which);
220+
HWND curScintilla = (which < 1) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle;
221+
222+
// SCI_GOTOLINE in the current scintilla instance
223+
WPARAM zeroLine = static_cast<WPARAM>(p_doc->ErrorLineNum() - 1);
224+
::SendMessage(curScintilla, SCI_GOTOLINE, zeroLine, 0);
225+
226+
// do annotation
227+
::SendMessage(curScintilla, SCI_ANNOTATIONCLEARALL, 0, 0);
228+
::SendMessage(curScintilla, SCI_ANNOTATIONSETVISIBLE, ANNOTATION_BOXED, 0);
229+
::SendMessageA(curScintilla, SCI_ANNOTATIONSETTEXT, zeroLine, reinterpret_cast<LPARAM>(p_doc->ErrorStr()));
230+
231+
// need to stop
232+
// TODO: DO I NEED?? _doAbort = true;
233+
};
234+
}
235+
return true;
236+
}
237+
return false;
238+
};

src/Classes/OverrideMapUpdaterClass.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ class OverrideMapUpdater {
1717
// Instantiate OverrideMapUpdater object
1818
OverrideMapUpdater(void);
1919

20-
// Experiment with concepts needed, might eventually morph into actual behavior
21-
bool experiment(void);
22-
2320
// add an <association> tag for a given UDL
2421
tinyxml2::XMLElement* OverrideMapUpdater::add_udl_assoc(std::wstring wsFilename, std::wstring wsUDLname);
2522
tinyxml2::XMLElement* OverrideMapUpdater::add_udl_assoc(std::string sFilename, std::string sUDLname);
@@ -48,6 +45,10 @@ class OverrideMapUpdater {
4845
// look for an element, based on {Parent, FirstChild, or both} which is of a specific ElementType, having a specific AttributeName with specific AttributeValue
4946
tinyxml2::XMLElement* _find_element_with_attribute_value(tinyxml2::XMLElement* pParent, tinyxml2::XMLElement* pFirst, std::string sElementType, std::string sAttributeName, std::string sAttributeValue, bool caseSensitive);
5047

48+
// compares the XMLError result to XML_SUCCESS, and returns a TRUE boolean to indicate failure
49+
// p_doc defaults to nullptr, wsFilePath to L""
50+
bool OverrideMapUpdater::_xml_check_result(tinyxml2::XMLError a_eResult, tinyxml2::XMLDocument* p_doc = nullptr, std::wstring wsFilePath = std::wstring(L""));
51+
5152
////////////////
5253
// properties
5354
////////////////

src/PluginDefinition.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,9 @@ void commandMenuInit()
7575
// bool check0nInit // optional. Make this menu item be checked visually
7676
// );
7777
setCommand(0, TEXT("Collection Interface: Download"), showCollectionInterface, NULL, false);
78-
setCommand(1, TEXT("Collection Interface:: Experiment"), hello, NULL, false);
79-
setCommand(2, TEXT("Help: Download"), showCIDownloadHelp, NULL, false);
80-
setCommand(3, TEXT(""), NULL, NULL, false);
81-
setCommand(4, TEXT("About"), showAboutModal, NULL, false);
78+
setCommand(1, TEXT("Help: Download"), showCIDownloadHelp, NULL, false);
79+
setCommand(2, TEXT(""), NULL, NULL, false);
80+
setCommand(3, TEXT("About"), showAboutModal, NULL, false);
8281
}
8382

8483
//
@@ -114,8 +113,6 @@ bool setCommand(size_t index, TCHAR *cmdName, PFUNCPLUGINCMD pFunc, ShortcutKey
114113
//----------------------------------------------//
115114
void hello()
116115
{
117-
OverrideMapUpdater overMapUpdater;
118-
overMapUpdater.experiment();
119116
}
120117

121118
void helloDlg()

src/PluginDefinition.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const TCHAR NPP_PLUGIN_NAME[] = TEXT("CollectionInterface");
3838
//
3939
// Here define the number of your plugin commands
4040
//
41-
const int nbFunc = 5;
41+
const int nbFunc = 4;
4242

4343

4444
//

0 commit comments

Comments
 (0)