forked from morbac/xmltools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelectFileDlg.cpp
126 lines (100 loc) · 4.33 KB
/
SelectFileDlg.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
// SelectFileDlg.cpp : implementation file
//
#include "StdAfx.h"
#include "XMLTools.h"
#include "SelectFileDlg.h"
#include <string>
#include <fstream>
#include <streambuf>
#include "XmlWrapperInterface.h"
#include "MSXMLWrapper.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CSelectFileDlg dialog
CSelectFileDlg::CSelectFileDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSelectFileDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSelectFileDlg)
m_sSelectedFilename = L"";
m_sValidationNamespace = L"";
m_sRootElementName = L"";
m_sRootElemenSample = L"Please select a XML schema file";
//}}AFX_DATA_INIT
}
void CSelectFileDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSelectFileDlg)
DDX_Text(pDX, IDC_EDIT_FILENAME, m_sSelectedFilename);
DDX_Text(pDX, IDC_EDIT_NAMESPACE, m_sValidationNamespace);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSelectFileDlg, CDialog)
//{{AFX_MSG_MAP(CSelectFileDlg)
ON_BN_CLICKED(IDC_BTN_EXPLORE_XSDFILE, OnBtnExploreXSDFile)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSelectFileDlg message handlers
CStringW CSelectFileDlg::ShowOpenFileDlg(CStringW filetypes)
{
CFileDialog dlg(TRUE, NULL, NULL, NULL, filetypes);
INT_PTR ret = dlg.DoModal();
if (ret == IDOK) {
return dlg.GetPathName();
}
return "";
}
BOOL CSelectFileDlg::OnInitDialog() {
if (!m_sSelectedFilename.IsEmpty()) GetDlgItem(IDC_EDIT_FILENAME)->SetWindowText(m_sSelectedFilename);
if (!m_sValidationNamespace.IsEmpty()) GetDlgItem(IDC_EDIT_NAMESPACE)->SetWindowText(m_sValidationNamespace);
if (!m_sRootElemenSample.IsEmpty()) GetDlgItem(IDC_EDIT_ROOTELEMSAMPLE)->SetWindowText(m_sRootElemenSample);
return TRUE;
}
void CSelectFileDlg::OnBtnExploreXSDFile() {
CStringW ret = ShowOpenFileDlg("XML Schema (*.xsd)|*.xsd|All files (*.*)|*.*|");
if (ret.GetLength()) {
if (this->m_sRootElementName.GetLength() > 0) {
// let's parse the selected file and search for a @targetNamespace attribute in root element
std::string str;
std::ifstream t((LPCTSTR)ret);
t.seekg(0, std::ios::end);
str.reserve((size_t) t.tellg());
t.seekg(0, std::ios::beg);
str.assign((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());
XmlWrapperInterface* wrapper = new MSXMLWrapper(str.c_str(), str.size());
std::vector<XPathResultEntryType> nodes = wrapper->xpathEvaluate(L"/xs:schema/@targetNamespace", L"xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"");
if (nodes.size() == 1) {
std::wstring targetNamespace = nodes.at(0).value;
GetDlgItem(IDC_EDIT_NAMESPACE)->SetWindowText(targetNamespace.c_str());
// write info text
m_sRootElemenSample = "<";
m_sRootElemenSample += this->m_sRootElementName;
m_sRootElemenSample += "\r\n xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"";
m_sRootElemenSample += "\r\n xsi:schemaLocation=\"";
m_sRootElemenSample += targetNamespace.c_str();
m_sRootElemenSample += " ";
m_sRootElemenSample += ret;
m_sRootElemenSample += "\">";
GetDlgItem(IDC_EDIT_ROOTELEMSAMPLE)->SetWindowText(m_sRootElemenSample);
}
else {
GetDlgItem(IDC_EDIT_NAMESPACE)->SetWindowText(L"");
// write info text
m_sRootElemenSample = "<";
m_sRootElemenSample += this->m_sRootElementName;
m_sRootElemenSample += "\r\n xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"";
m_sRootElemenSample += "\r\n xsi:noNamespaceSchemaLocation=\"";
m_sRootElemenSample += ret;
m_sRootElemenSample += "\">";
GetDlgItem(IDC_EDIT_ROOTELEMSAMPLE)->SetWindowText(m_sRootElemenSample);
}
delete wrapper;
}
GetDlgItem(IDC_EDIT_FILENAME)->SetWindowText(ret);
}
}