51#include <wx/xml/xml.h>
52#include <wx/wfstream.h>
53#include <wx/sstream.h>
54#include <wx/mstream.h>
55#include <wx/protocol/http.h>
56#include <wx/protocol/ftp.h>
57#include <wx/webrequest.h>
58#include <wx/mimetype.h>
91 return(
Assign(MetaData) );
104 return(
Load(File, Extension) );
117 return(
Load(Stream) );
129void CSG_MetaData::_On_Construction(
void)
149 delete(m_pChildren[i]);
152 m_Children.Destroy();
159 m_Prop_Names .Clear();
160 m_Prop_Values.Clear();
201 return(
Ins_Child(MetaData, -1, bAddChildren) );
212 if( !m_Children.Inc_Array() )
226 pChildren[i] = pChildren[i - 1];
239 pChild->m_Name = Name;
240 pChild->m_Content = Content;
272 pChild->
Assign(MetaData, bAddChildren);
292 if( from_Index != to_Index )
297 if( from_Index < to_Index )
299 for(
int i=from_Index; i<to_Index; i++)
301 pChildren[i] = pChildren[i + 1];
306 for(
int i=from_Index; i>to_Index; i--)
308 pChildren[i] = pChildren[i - 1];
312 pChildren[to_Index] = pChild;
330 delete(pChildren[Index]);
334 pChildren[i] = pChildren[j];
337 m_Children.Dec_Array();
348 if( Name.
Find(
'.') > 0 )
364 if( &MetaData !=
this )
390 else if( Name && *Name )
422 m_Children.Destroy();
431 if( Name.
Find(
'.') > 0 )
447 if( JSON_Pointer.
Find(
'/') > 0 )
468int CSG_MetaData::_Get_Child(
const CSG_String &Name)
const
489 return( bNoCase ? !m_Name.CmpNoCase(String) : !m_Name.Cmp(String) );
538 wxString s; va_list argptr;
541 wxString _Format(Format); _Format.Replace(
"%s",
"%ls");
542 va_start(argptr, _Format);
543 s.PrintfV(_Format, argptr);
545 va_start(argptr, Format);
546 s.PrintfV(Format, argptr);
557 wxString s; va_list argptr;
560 wxString _Format(Format); _Format.Replace(
"%s",
"%ls");
561 va_start(argptr, _Format);
562 s.PrintfV(_Format, argptr);
564 va_start(argptr, Format);
565 s.PrintfV(Format, argptr);
576 return( bNoCase ? !m_Content.CmpNoCase(String) : !m_Content.Cmp(String) );
587 if( !Value.
is_Empty() &&_Get_Property(Name) < 0 )
589 m_Prop_Names .Add(Name );
590 m_Prop_Values.Add(Value);
631 m_Prop_Names .Del(i);
632 m_Prop_Values.Del(i);
643 int Index = _Get_Property(Name);
647 m_Prop_Values[Index] = Value;
651 else if( bAddIfNotExists )
653 m_Prop_Names .Add(Name);
654 m_Prop_Values.Add(Value);
705int CSG_MetaData::_Get_Property(
const CSG_String &Name)
const
707 for(
int i=0; i<m_Prop_Names.
Get_Count(); i++)
709 if( Name.
CmpNoCase(m_Prop_Names[i]) == 0 )
737 wxXmlDocument XML; wxXmlNode *pRoot =
new wxXmlNode(NULL, wxXML_ELEMENT_NODE,
Get_Name().c_str());
743 wxStringOutputStream Stream;
747 s = &Stream.GetString();
785 if( &MetaData !=
this )
817 if( File.
Find(
"http://") == 0 )
841 if( XML.Load(_File.
c_str()) )
872 m_Name = pNode->GetName ().wc_str();
873 m_Content = pNode->GetNodeContent().wc_str();
876 wxXmlAttribute *pProperty = pNode->GetAttributes();
880 Add_Property(&pProperty->GetName(), &pProperty->GetValue());
882 pProperty = pProperty->GetNext();
886 wxXmlNode *pChild = pNode->GetChildren();
890 if( pChild->GetType() != wxXML_TEXT_NODE )
895 pChild = pChild->GetNext();
909 wxXmlDocument XML; wxXmlNode *pRoot =
new wxXmlNode(NULL, wxXML_ELEMENT_NODE,
Get_Name().c_str());
926 wxXmlDocument XML; wxXmlNode *pRoot =
new wxXmlNode(NULL, wxXML_ELEMENT_NODE,
Get_Name().c_str());
945 pNode->SetName(Name.
c_str());
951 wxXmlNode *pChild =
new wxXmlNode(pNode, wxXML_TEXT_NODE,
SG_T(
"TEXT"));
981 wxXmlDocument XML; wxMemoryInputStream Stream((
const void *)_XML,
Length);
983#if wxCHECK_VERSION(3, 3, 0)
984 if( XML.Load(Stream) )
986 if( XML.Load(Stream, Encoding) )
1002 wxXmlDocument XML; wxMemoryInputStream Stream((
const void *)_XML.
c_str(),
sizeof(
SG_Char) * _XML.
Length());
1004 if( XML.Load(Stream) )
1006 Load(XML.GetRoot());
1017 wxXmlDocument XML; wxXmlNode *pRoot =
new wxXmlNode(NULL, wxXML_ELEMENT_NODE,
Get_Name().c_str());
1023 wxMemoryOutputStream Stream;
1025 if( XML.Save(Stream) )
1027 CSG_Array s(
sizeof(
char), Stream.GetSize());
1052 if( Username && *Username ) { HTTP.SetUser (Username); }
1053 if( Password && *Password ) { HTTP.SetPassword(Password); }
1055 wxString s = Server.
c_str();
1057 if( s.Find(
"http://") == 0 )
1059 s = s.Right(s.Length() - wxString(
"http://").
Length());
1062 if( !HTTP.Connect(s) )
1075 wxInputStream *pStream = HTTP.GetInputStream(s);
1084 if( XML.Load(*pStream) )
1086 Load(XML.GetRoot());
1137 picojson::value root;
1138 std::string err = picojson::parse(root,
JSON.b_str());
1145 _JSON_To_MetaData(root,
this);
1212void CSG_MetaData::_JSON_To_MetaData(
const picojson::value &j,
CSG_MetaData *pNode)
1214 if( j.is<picojson::object>() )
1216 const picojson::object &obj = j.get<picojson::object>();
1218 for(
auto it = obj.begin(); it != obj.end(); ++it )
1221 _JSON_To_MetaData(it->second, pChild);
1225 else if( j.is<picojson::array>() )
1227 const picojson::array &arr = j.get<picojson::array>();
1231 for(
size_t i = 0; i < arr.size(); ++i )
1234 _JSON_To_MetaData(arr[i], pItem);
1240 if( j.is<std::string>() )
1245 else if( j.is<
double>() )
1251 else if( j.is<
bool>() )
1253 pNode->
Set_Content(j.get<
bool>() ?
"true" :
"false");
1270 for(
size_t i=0; i<s.
Length(); ++i)
1274 case '\"': out +=
"\\\"";
break;
1275 case '\\': out +=
"\\\\";
break;
1276 case '\b': out +=
"\\b" ;
break;
1277 case '\f': out +=
"\\f" ;
break;
1278 case '\n': out +=
"\\n" ;
break;
1279 case '\r': out +=
"\\r" ;
break;
1280 case '\t': out +=
"\\t" ;
break;
1281 default: out += s[i] ;
break;
1310 Create(Server, User, Password);
1317 m_pHTTP =
new wxHTTP;
1319 if( !User .is_Empty() ) { m_pHTTP->SetUser (User .wx_str()); }
1320 if( !Password.
is_Empty() ) { m_pHTTP->SetPassword(Password.
wx_str()); }
1322 wxString Host= Server.
wx_str();
unsigned short Port = 80;
1324 #define SERVER_TRIM(s, p) { wxString sp(p); sp += "://"; if( s.Find(p) == 0 ) { s = s.Right(s.Length() - sp.Length()); } }
1329 if( Host.Find(
":") >= 0 )
1333 if( Host.AfterLast(
':').ToLong(&_Port) )
1335 Port = (
unsigned short)_Port;
1338 Host = Host.BeforeLast(
':');
1341 if( !m_pHTTP->Connect(Host, Port) )
1370wxInputStream * CSG_HTTP::_Request(
const CSG_String &Request)
1374 wxString s(
Request.c_str());
if( s[0] !=
'/' ) { s.
Prepend(
"/"); }
1376 wxInputStream *pStream = m_pHTTP->GetInputStream(s);
1380 if( pStream->CanRead() )
1395 wxInputStream *pStream = _Request(
Request);
if( !pStream ) {
return(
false ); }
1399 if( !XML.Load(*pStream) )
1416 wxInputStream *pStream = _Request(
Request);
if( !pStream ) {
return(
false ); }
1420 while( pStream->CanRead() )
1422 char Byte; pStream->Read(&Byte,
sizeof(Byte)); Answer += Byte;
1433 wxInputStream *pStream = _Request(
Request);
if( !pStream ) {
return(
false ); }
1437 while( pStream->CanRead() )
1439 char Byte; pStream->Read(&Byte,
sizeof(Byte)); Answer += Byte;
1450 wxInputStream *pStream = _Request(
Request);
if( !pStream ) {
return(
false ); }
1452 wxFileOutputStream *pFile =
new wxFileOutputStream(File);
1461 pFile->Write(*pStream);
1493 Create(Server, User, Password);
1504 #if !wxCHECK_VERSION(3, 3, 0)
1505 m_User = User; m_Password = Password;
1507 wxURI URI; URI.SetUserAndPassword(User.
wx_str(), Password.
wx_str());
1509 m_User = URI.GetUser().
wx_str(); m_Password = URI.GetPassword().wx_str();
1519 m_Server.
Clear(); m_User.Clear(), m_Password.Clear();
1530#if !wxCHECK_VERSION(3, 3, 0)
1554 wxWebSessionSync::GetDefault().SetBaseURL(Server.
wx_str());
1559 auto request = wxWebSessionSync::GetDefault().CreateRequest(URL.
wx_str());
1561 #if wxCHECK_VERSION(3, 3, 2)
1564 request.UseBasicAuth(wxWebCredentials(User.
wx_str(), wxSecretValue(Password.
wx_str())));
1572static bool _Response_to_Bytes(
const wxWebResponse &Response,
CSG_Bytes &Answer)
1574 wxInputStream *pStream = Response.GetStream();
1576 if( pStream->IsOk() )
1578 int Byte = pStream->GetC();
1580 while( Byte != wxEOF )
1582 Answer += (BYTE)Byte;
1584 Byte = pStream->GetC();
1594static bool _Response_to_MetaData(
const wxWebResponse &Response,
CSG_MetaData &Answer)
1596 wxString MimeType = Response.GetMimeType();
1599 if( MimeType.Contains(
"xml") )
1603 if( XML.Load(*Response.GetStream()) )
1605 Answer.
Load(XML.GetRoot());
1610 else if( MimeType.Contains(
"json") )
1619static bool _Response_to_String(
const wxWebResponse &Response,
CSG_String &Answer)
1621 Answer = Response.AsString().
wx_str();
1627static bool _Response_to_File(
const wxWebResponse &Response,
const SG_Char *File)
1629 wxFileOutputStream Output(File);
1633 Output.Write(*Response.GetStream());
1649 auto request = _Create_Request(m_Server, m_User, m_Password,
Request);
1650 if( !request.Execute() ) {
return(
false ); }
1653 return( _Response_to_Bytes(request.GetResponse(), Answer) );
1659 auto request = _Create_Request(m_Server, m_User, m_Password,
Request);
1660 if( !request.Execute() ) {
return(
false ); }
1663 return( _Response_to_MetaData(request.GetResponse(), Answer) );
1669 auto request = _Create_Request(m_Server, m_User, m_Password,
Request);
1670 if( !request.Execute() ) {
return(
false ); }
1673 return( _Response_to_String(request.GetResponse(), Answer) );
1679 auto request = _Create_Request(m_Server, m_User, m_Password,
Request);
1680 if( !request.Execute() ) {
return(
false ); }
1683 return( _Response_to_File(request.GetResponse(), File) );
1694 auto request = _Create_Request(m_Server, m_User, m_Password,
Request);
1695 request.SetMethod(
"POST");
1697 if( !request.Execute() ) {
return false; }
1700 return( _Response_to_Bytes(request.GetResponse(), Answer) );
1706 auto request = _Create_Request(m_Server, m_User, m_Password,
Request);
1707 request.SetMethod(
"POST");
1709 if( !request.Execute() ) {
return false; }
1712 return( _Response_to_MetaData(request.GetResponse(), Answer) );
1718 auto request = _Create_Request(m_Server, m_User, m_Password,
Request);
1719 request.SetMethod(
"POST");
1721 if( !request.Execute() ) {
return false; }
1724 return( _Response_to_String(request.GetResponse(), Answer) );
1730 auto request = _Create_Request(m_Server, m_User, m_Password,
Request);
1731 request.SetMethod(
"POST");
1733 if( !request.Execute() ) {
return false; }
1736 return( _Response_to_File(request.GetResponse(), File) );
1747 auto request = _Create_Request(Server,
"",
"",
Request);
1748 if( !request.Execute() ) {
return(
false ); }
1751 return( _Response_to_Bytes(request.GetResponse(), Answer) );
1757 auto request = _Create_Request(Server,
"",
"",
Request);
1758 if( !request.Execute() ) {
return(
false ); }
1761 return( _Response_to_MetaData(request.GetResponse(), Answer) );
1767 auto request = _Create_Request(Server,
"",
"",
Request);
1768 if( !request.Execute() ) {
return(
false ); }
1771 return( _Response_to_String(request.GetResponse(), Answer) );
1778 auto request = _Create_Request(Server,
"",
"",
Request);
1779 if( !request.Execute() ) {
return(
false ); }
1782 return( _Response_to_File(request.GetResponse(), File) );
1793 auto request = _Create_Request(Server,
"",
"",
Request);
1794 request.SetMethod(
"POST");
1796 if( !request.Execute() ) {
return false; }
1799 return( _Response_to_Bytes(request.GetResponse(), Answer) );
1806 auto request = _Create_Request(Server,
"",
"",
Request);
1807 request.SetMethod(
"POST");
1809 if( !request.Execute() ) {
return false; }
1812 return( _Response_to_MetaData(request.GetResponse(), Answer) );
1818 auto request = _Create_Request(Server,
"",
"",
Request);
1819 request.SetMethod(
"POST");
1821 if( !request.Execute() ) {
return false; }
1824 return( _Response_to_String(request.GetResponse(), Answer) );
1830 auto request = _Create_Request(Server,
"",
"",
Request);
1831 request.SetMethod(
"POST");
1833 if( !request.Execute() ) {
return false; }
1836 return( _Response_to_File(request.GetResponse(), File) );
1852 if( _Source.
Find(
"ftp://") == 0 )
1864 if( !User .is_Empty() ) { ftp.SetUser (User .wx_str()); }
1865 if( !Password.
is_Empty() ) { ftp.SetPassword(Password.
wx_str()); }
1867 if( !ftp.Connect(ftpHost.
c_str(), Port) )
1888 if( ftp.GetFileSize(ftpFile.
c_str()) == -1 )
1897 wxInputStream *pStream = ftp.GetInputStream(ftpFile.
c_str());
1910 wxFileOutputStream *pFile =
new wxFileOutputStream(
SG_File_Make_Path(Target_Directory, ftpFile).c_str());
1925 pFile->Write(*pStream);
1941 return( wxLaunchDefaultBrowser(URL.
c_str()) );
1959 if( Reference.
Find(
"ftp:" ) == 0
1960 || Reference.
Find(
"file:" ) == 0
1961 || Reference.
Find(
"http:" ) == 0
1962 || Reference.
Find(
"https:" ) == 0
1963 || Reference.
Find(
"mailto:") == 0 )
1965 return( wxLaunchDefaultBrowser(Reference.
c_str()) );
1975 bool bResult =
false;
1979 if( (bResult = wxLaunchDefaultApplication(Reference.
c_str())) ==
false )
1988 wxFileType *pFileType = wxTheMimeTypesManager->GetFileTypeFromExtension(Mime_Extension.
c_str());
1994 if( !pFileType->GetOpenCommand(&Command, wxFileType::MessageParameters(Reference.
c_str(),
"")) || !(bResult = wxExecute(Command) == 0) )
void SG_UI_Msg_Add_Error(const char *Message)
SAGA_API_DLL_EXPORT bool SG_File_Exists(const CSG_String &FileName)
SAGA_API_DLL_EXPORT bool SG_File_Cmp_Extension(const CSG_String &File, const CSG_String &Extension)
SAGA_API_DLL_EXPORT CSG_String SG_File_Get_Path_Absolute(const CSG_String &full_Path, const CSG_String &CWD="")
SAGA_API_DLL_EXPORT CSG_String SG_File_Make_Path(const CSG_String &Directory, const CSG_String &Name)
SAGA_API_DLL_EXPORT bool SG_File_is_Relative(const CSG_String &full_Path)
SAGA_API_DLL_EXPORT CSG_String SG_Get_String(double Value, int Precision=-99)
void * Get_Array(void) const
sLong Get_Size(void) const
virtual bool Open(const SG_Char *FileName, int Mode=SG_FILE_R, bool bBinary=true, int Encoding=SG_FILE_ENCODING_ANSI)
size_t Write(void *Buffer, size_t Size, size_t Count=1) const
size_t Read(void *Buffer, size_t Size, size_t Count=1) const
class wxStreamBase * Get_Stream(void) const
bool is_Writing(void) const
bool is_Reading(void) const
bool is_Connected(void) const
bool Request(const CSG_String &Request, CSG_Bytes &Answer)
size_t Length(void) const
CSG_String AfterFirst(char Character) const
bool asInt(int &Value) const
int CmpNoCase(const CSG_String &String) const
CSG_String AfterLast(char Character) const
int Cmp(const CSG_String &String) const
CSG_String BeforeFirst(char Character) const
size_t Replace(const CSG_String &Old, const CSG_String &New, bool bReplaceAll=true)
CSG_String BeforeLast(char Character) const
static CSG_String Format(const char *Format,...)
int Trim(bool fromRight=false)
int Find(char Character, bool fromEnd=false) const
bool asDouble(double &Value) const
CSG_String Right(size_t count) const
const SG_Char * c_str(void) const
bool is_Empty(void) const
const class wxString & wx_str(void) const
CSG_String & Prepend(const CSG_String &String)
sLong asLongLong(void) const
int Get_Count(void) const
bool Set_Value(int Field, const CSG_String &Value)
virtual CSG_Table_Record * Add_Record(CSG_Table_Record *pCopy=NULL)
virtual bool Add_Field(const CSG_String &Name, TSG_Data_Type Type, int Position=-1)
bool Post(const CSG_String &Request, const CSG_String &Body, CSG_Bytes &Answer, const CSG_String &ContentType="application/json")
bool Request(const CSG_String &Request, CSG_Bytes &Answer)