Add WriteData method & add feature to send js variables to builder native methods

This commit is contained in:
Oleg Korshul
2019-02-08 17:14:23 +03:00
parent 299ef4873f
commit 07b0ba71d9
5 changed files with 418 additions and 14 deletions

View File

@ -57,5 +57,5 @@ namespace NSDoctRenderer
bool CDocBuilder::ExecuteCommand(const wchar_t* command)
{
return m_pInternal->ExecuteCommand(command);
}
}
}

View File

@ -59,6 +59,9 @@ namespace NSDoctRenderer
void SetProperty(const char* param, const wchar_t* value);
void SetPropertyW(const wchar_t* param, const wchar_t* value);
void WriteData(const wchar_t* path, const wchar_t* value, const bool& append);
bool IsSaveWithDoctrendererMode();
char* GetVersion();
public:

View File

@ -223,6 +223,51 @@ public:
return sReturn;
}
std::wstring GetJSVariable(std::wstring sParam)
{
std::string sParamA = U_TO_UTF8(sParam);
NSCommon::string_replaceA(sParamA, "\\\"", "\"");
std::string commandA = "(function(){ return (" + sParamA + "); })()";
v8::Context::Scope context_scope(m_context);
v8::TryCatch try_catch;
v8::Local<v8::String> source = v8::String::NewFromUtf8(m_isolate, commandA.c_str());
v8::Local<v8::Script> script = v8::Script::Compile(source);
if (try_catch.HasCaught())
{
std::wstring strCode = to_cstring(try_catch.Message()->GetSourceLine());
std::wstring strException = to_cstring(try_catch.Message()->Get());
_LOGGING_ERROR_(L"execute_compile_code", strCode);
_LOGGING_ERROR_(L"execute_compile", strException);
return L"jsValue(" + sParam + L")";
}
else
{
v8::Local<v8::Value> _value = script->Run();
if (try_catch.HasCaught())
{
std::wstring strCode = to_cstring(try_catch.Message()->GetSourceLine());
std::wstring strException = to_cstring(try_catch.Message()->Get());
_LOGGING_ERROR_(L"execute_run_code", strCode);
_LOGGING_ERROR_(L"execute_run", strException);
return L"jsValue(" + sParam + L")";
}
if (_value->IsString())
return to_cstring(_value);
}
return L"jsValue(" + sParam + L")";
}
bool OpenFile(const std::wstring& sBasePath, const std::wstring& path, const std::string& sString, const std::wstring& sCachePath)
{
LOGGER_SPEED_START
@ -1154,6 +1199,25 @@ namespace NSDoctRenderer
RELEASEOBJECT(m_pWorker);
}
std::wstring GetSaveFilePath(const std::wstring& path)
{
std::wstring _path = path;
if (!m_sFolderForSaveOnlyUseNames.empty())
{
_path = m_sFolderForSaveOnlyUseNames;
wchar_t last = m_sFolderForSaveOnlyUseNames.c_str()[m_sFolderForSaveOnlyUseNames.length() - 1];
if (last != '/' && last != '\\')
_path += L"/";
_path += NSCommon::GetFileName(path);
}
std::wstring sDstFileDir = NSCommon::GetDirectoryName(_path);
if ((sDstFileDir != _path) && !NSDirectory::Exists(sDstFileDir))
NSDirectory::CreateDirectories(sDstFileDir);
return _path;
}
int SaveFile(const int& type, const std::wstring& path, const wchar_t* params = NULL)
{
Init();
@ -1176,19 +1240,7 @@ namespace NSDoctRenderer
NSStringUtils::CStringBuilder oBuilder;
std::wstring _path = path;
if (!m_sFolderForSaveOnlyUseNames.empty())
{
_path = m_sFolderForSaveOnlyUseNames;
wchar_t last = m_sFolderForSaveOnlyUseNames.c_str()[m_sFolderForSaveOnlyUseNames.length() - 1];
if (last != '/' && last != '\\')
_path += L"/";
_path += NSCommon::GetFileName(path);
}
std::wstring sDstFileDir = NSCommon::GetDirectoryName(_path);
if ((sDstFileDir != _path) && !NSDirectory::Exists(sDstFileDir))
NSDirectory::CreateDirectories(sDstFileDir);
std::wstring _path = GetSaveFilePath(path);
oBuilder.WriteString(L"<?xml version=\"1.0\" encoding=\"utf-8\"?><TaskQueueDataConvert><m_sFileFrom>");
oBuilder.WriteEncodeXmlString(m_sFileDir);
@ -1498,6 +1550,27 @@ namespace NSDoctRenderer
RELEASEARRAYOBJECTS(pData);
return sReturn;
}
void WriteData(const wchar_t* path, const wchar_t* value, const bool& append)
{
std::wstring sValue(value);
std::string sValueA = U_TO_UTF8(sValue);
NSCommon::string_replaceA(sValueA, "%", "%%");
std::wstring _sFile(path);
std::wstring sFile = GetSaveFilePath(_sFile);
if (!append && NSFile::CFileBinary::Exists(sFile))
NSFile::CFileBinary::Remove(sFile);
NSFile::CFileBinary oFile;
FILE* pFile = oFile.OpenFileNative(sFile, append ? L"a+" : L"a");
if (pFile)
{
fprintf(pFile, sValueA.c_str());
fclose(pFile);
}
}
};
}
@ -1702,6 +1775,15 @@ namespace NSDoctRenderer
int nCountParameters = 0;
ParceParameters(command, _builder_params, nCountParameters);
for (int nCheckParam = 0; nCheckParam < nCountParameters; ++nCheckParam)
{
if (0 == _builder_params[nCheckParam].find(L"jsValue(") && _builder_params[nCheckParam].length() > 9)
{
std::wstring sParam = _builder_params[nCheckParam].substr(8, _builder_params[nCheckParam].length() - 9);
_builder_params[nCheckParam] = m_pInternal->m_pWorker->GetJSVariable(sParam);
}
}
if ("OpenFile" == sFuncNum)
bIsNoError = (0 == this->OpenFile(_builder_params[0].c_str(), _builder_params[1].c_str()));
else if ("CreateFile" == sFuncNum)
@ -1764,6 +1846,14 @@ namespace NSDoctRenderer
this->SaveFile(nFormat, _builder_params[1].c_str(), sParams);
}
else if ("WriteData" == sFuncNum)
{
bool isAppend = true;
if (nCountParameters > 2)
isAppend = (L"true" == _builder_params[2]) ? true : false;
this->WriteData(_builder_params[0].c_str(), _builder_params[1].c_str(), isAppend);
}
}
else
{
@ -1819,6 +1909,16 @@ namespace NSDoctRenderer
{
CV8Worker::Dispose();
}
void CDocBuilder::WriteData(const wchar_t* path, const wchar_t* value, const bool& append)
{
return m_pInternal->WriteData(path, value, append);
}
bool CDocBuilder::IsSaveWithDoctrendererMode()
{
return m_pInternal->m_oParams.m_bSaveWithDoctrendererMode;
}
}
#endif // DOC_BUILDER_PRIVATE

View File

@ -0,0 +1,234 @@
/*
* (c) Copyright Ascensio System SIA 2010-2019
*
* This program is a free software product. You can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License (AGPL)
* version 3 as published by the Free Software Foundation. In accordance with
* Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
* that Ascensio System SIA expressly excludes the warranty of non-infringement
* of any third-party rights.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
* details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
*
* You can contact Ascensio System SIA at 20A-12 Ernesta Birznieka-Upisha
* street, Riga, Latvia, EU, LV-1050.
*
* The interactive user interfaces in modified source and object code versions
* of the Program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU AGPL version 3.
*
* Pursuant to Section 7(b) of the License you must retain the original Product
* logo when distributing the program. Pursuant to Section 7(e) we decline to
* grant you any rights under trademark law for use of our trademarks.
*
* All the Product's GUI elements, including illustrations and icon sets, as
* well as technical writing content are licensed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International. See the License
* terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
*
*/
#include "builder.h"
#include "../../../Common/OfficeFileFormats.h"
#include "../../fontengine/application_generate_fonts_common.h"
// wrap_methods -------------
CBuilderObject* unwrap_builder(v8::Handle<v8::Object> obj)
{
v8::Handle<v8::External> field = v8::Handle<v8::External>::Cast(obj->GetInternalField(0));
return static_cast<CBuilderObject*>(field->Value());
}
void _b_openfile(const v8::FunctionCallbackInfo<v8::Value>& args)
{
CBuilderObject* builder = unwrap_builder(args.This());
if (!builder->m_pBuilder)
{
args.GetReturnValue().Set(v8::Undefined(v8::Isolate::GetCurrent()));
return;
}
std::wstring sFile;
std::wstring sParams;
if (args.Length() > 0)
sFile = to_cstring(args[0]);
if (args.Length() > 1)
sParams = to_cstring(args[1]);
int nRet = builder->m_pBuilder->OpenFile(sFile.c_str(), sParams.c_str());
args.GetReturnValue().Set(v8::Integer::New(v8::Isolate::GetCurrent(), nRet));
}
void _b_createfile(const v8::FunctionCallbackInfo<v8::Value>& args)
{
CBuilderObject* builder = unwrap_builder(args.This());
if (!builder->m_pBuilder)
{
args.GetReturnValue().Set(v8::Undefined(v8::Isolate::GetCurrent()));
return;
}
std::string sType = "docx";
if (args.Length() > 0)
sType = to_cstringA(args[0]);
bool isNoError = false;
if ("docx" == sType)
isNoError = builder->m_pBuilder->CreateFile(AVS_OFFICESTUDIO_FILE_DOCUMENT_DOCX);
else if ("pptx" == sType)
isNoError = builder->m_pBuilder->CreateFile(AVS_OFFICESTUDIO_FILE_PRESENTATION_PPTX);
else if ("xlsx" == sType)
isNoError = builder->m_pBuilder->CreateFile(AVS_OFFICESTUDIO_FILE_SPREADSHEET_XLSX);
args.GetReturnValue().Set(v8::Boolean::New(v8::Isolate::GetCurrent(), isNoError));
}
void _b_settmpfolder(const v8::FunctionCallbackInfo<v8::Value>& args)
{
CBuilderObject* builder = unwrap_builder(args.This());
args.GetReturnValue().Set(v8::Undefined(v8::Isolate::GetCurrent()));
if (!builder->m_pBuilder)
return;
if (args.Length() > 0)
{
std::wstring sFolder = to_cstring(args[0]);
builder->m_pBuilder->SetTmpFolder(sFolder.c_str());
}
}
void _b_savefile(const v8::FunctionCallbackInfo<v8::Value>& args)
{
CBuilderObject* builder = unwrap_builder(args.This());
if (!builder->m_pBuilder || args.Length() < 2)
{
args.GetReturnValue().Set(v8::Undefined(v8::Isolate::GetCurrent()));
return;
}
int nFormat = AVS_OFFICESTUDIO_FILE_DOCUMENT_DOCX;
std::string sFormat = to_cstringA(args[0]);
if ("docx" == sFormat)
nFormat = AVS_OFFICESTUDIO_FILE_DOCUMENT_DOCX;
else if ("doc" == sFormat)
nFormat = AVS_OFFICESTUDIO_FILE_DOCUMENT_DOC;
else if ("odt" == sFormat)
nFormat = AVS_OFFICESTUDIO_FILE_DOCUMENT_ODT;
else if ("rtf" == sFormat)
nFormat = AVS_OFFICESTUDIO_FILE_DOCUMENT_RTF;
else if ("txt" == sFormat)
nFormat = AVS_OFFICESTUDIO_FILE_DOCUMENT_TXT;
else if ("pptx" == sFormat)
nFormat = AVS_OFFICESTUDIO_FILE_PRESENTATION_PPTX;
else if ("odp" == sFormat)
nFormat = AVS_OFFICESTUDIO_FILE_PRESENTATION_ODP;
else if ("xlsx" == sFormat)
nFormat = AVS_OFFICESTUDIO_FILE_SPREADSHEET_XLSX;
else if ("xls" == sFormat)
nFormat = AVS_OFFICESTUDIO_FILE_SPREADSHEET_XLS;
else if ("ods" == sFormat)
nFormat = AVS_OFFICESTUDIO_FILE_SPREADSHEET_ODS;
else if ("csv" == sFormat)
nFormat = AVS_OFFICESTUDIO_FILE_SPREADSHEET_CSV;
else if ("pdf" == sFormat)
nFormat = AVS_OFFICESTUDIO_FILE_CROSSPLATFORM_PDF;
else if ("image" == sFormat)
nFormat = AVS_OFFICESTUDIO_FILE_IMAGE;
else if ("jpg" == sFormat)
nFormat = AVS_OFFICESTUDIO_FILE_IMAGE;
else if ("png" == sFormat)
nFormat = AVS_OFFICESTUDIO_FILE_IMAGE;
if (builder->m_pBuilder->IsSaveWithDoctrendererMode())
{
// перед сохранением в такой схеме нужно скинуть изменения
builder->m_pBuilder->ExecuteCommand(L"_api.asc_Save();");
}
std::wstring sFile = to_cstring(args[1]);
std::wstring sFileParams;
if (args.Length() > 2)
sFileParams = to_cstring(args[2]);
int nRet = builder->m_pBuilder->SaveFile(nFormat, sFile.c_str(), sFileParams.empty() ? NULL : sFileParams.c_str());
args.GetReturnValue().Set(v8::Integer::New(v8::Isolate::GetCurrent(), nRet));
}
void _b_closefile(const v8::FunctionCallbackInfo<v8::Value>& args)
{
CBuilderObject* builder = unwrap_builder(args.This());
args.GetReturnValue().Set(v8::Undefined(v8::Isolate::GetCurrent()));
if (!builder->m_pBuilder)
return;
builder->m_pBuilder->CloseFile();
}
void _b_writefile(const v8::FunctionCallbackInfo<v8::Value>& args)
{
CBuilderObject* builder = unwrap_builder(args.This());
args.GetReturnValue().Set(v8::Undefined(v8::Isolate::GetCurrent()));
if (!builder->m_pBuilder || args.Length() < 2)
return;
bool bIsAppend = true;
if (args.Length() > 2)
bIsAppend = args[2]->BooleanValue();
std::wstring sFile = to_cstring(args[0]);
std::string sData = to_cstringA(args[1]);
NSCommon::string_replaceA(sData, "%", "%%");
if (!bIsAppend)
NSFile::CFileBinary::Remove(sFile);
NSFile::CFileBinary oFile;
FILE* pFile = oFile.OpenFileNative(sFile, bIsAppend ? L"a+" : L"a");
if (pFile)
{
fprintf(pFile, sData.c_str());
fclose(pFile);
}
}
v8::Handle<v8::ObjectTemplate> CreateBuilderTemplate(v8::Isolate* isolate)
{
//v8::HandleScope handle_scope(isolate);
v8::Local<v8::ObjectTemplate> result = v8::ObjectTemplate::New();
result->SetInternalFieldCount(1); // отводим в нем место для хранения CNativeControl
v8::Isolate* current = v8::Isolate::GetCurrent();
void _b_openfile(const v8::FunctionCallbackInfo<v8::Value>& args);
void _b_createfile(const v8::FunctionCallbackInfo<v8::Value>& args);
void _b_settmpfolder(const v8::FunctionCallbackInfo<v8::Value>& args);
void _b_savefile(const v8::FunctionCallbackInfo<v8::Value>& args);
void _b_closefile(const v8::FunctionCallbackInfo<v8::Value>& args);
// прописываем функции - методы объекта
result->Set(v8::String::NewFromUtf8(current, "OpenFile"), v8::FunctionTemplate::New(current, _b_openfile));
result->Set(v8::String::NewFromUtf8(current, "CreateFile"), v8::FunctionTemplate::New(current, _b_createfile));
result->Set(v8::String::NewFromUtf8(current, "SetTmpFolder"), v8::FunctionTemplate::New(current, _b_settmpfolder));
result->Set(v8::String::NewFromUtf8(current, "SaveFile"), v8::FunctionTemplate::New(current, _b_savefile));
result->Set(v8::String::NewFromUtf8(current, "CloseFile"), v8::FunctionTemplate::New(current, _b_closefile));
result->Set(v8::String::NewFromUtf8(current, "Write"), v8::FunctionTemplate::New(current, _b_writefile));
// возвращаем временный хэндл хитрым образом, который переносит наш хэндл в предыдущий HandleScope и не дает ему
// уничтожиться при уничтожении "нашего" HandleScope - handle_scope
//return handle_scope.Close(result);
return result;
}
void CreateBuilderObject(const v8::FunctionCallbackInfo<v8::Value>& args)
{
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::Handle<v8::ObjectTemplate> NativeObjectTemplate = CreateBuilderTemplate(isolate);
CBuilderObject* pNativeObject = new CBuilderObject();
v8::Local<v8::Object> obj = NativeObjectTemplate->NewInstance();
obj->SetInternalField(0, v8::External::New(v8::Isolate::GetCurrent(), pNativeObject));
args.GetReturnValue().Set(obj);
}

View File

@ -0,0 +1,67 @@
/*
* (c) Copyright Ascensio System SIA 2010-2019
*
* This program is a free software product. You can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License (AGPL)
* version 3 as published by the Free Software Foundation. In accordance with
* Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
* that Ascensio System SIA expressly excludes the warranty of non-infringement
* of any third-party rights.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
* details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
*
* You can contact Ascensio System SIA at 20A-12 Ernesta Birznieka-Upisha
* street, Riga, Latvia, EU, LV-1050.
*
* The interactive user interfaces in modified source and object code versions
* of the Program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU AGPL version 3.
*
* Pursuant to Section 7(b) of the License you must retain the original Product
* logo when distributing the program. Pursuant to Section 7(e) we decline to
* grant you any rights under trademark law for use of our trademarks.
*
* All the Product's GUI elements, including illustrations and icon sets, as
* well as technical writing content are licensed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International. See the License
* terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
*
*/
#ifndef BUILDER_WRAPPER_BUILDER
#define BUILDER_WRAPPER_BUILDER
#include "../docbuilder.h"
#include "../nativecontrol.h"
class CBuilderObject
{
public:
NSDoctRenderer::CDocBuilder* m_pBuilder;
public:
CBuilderObject()
{
m_pBuilder = NULL;
}
~CBuilderObject()
{
}
};
// wrap_methods -------------
CBuilderObject* unwrap_builder(v8::Handle<v8::Object> obj);
void _b_openfile(const v8::FunctionCallbackInfo<v8::Value>& args);
void _b_createfile(const v8::FunctionCallbackInfo<v8::Value>& args);
void _b_settmpfolder(const v8::FunctionCallbackInfo<v8::Value>& args);
void _b_savefile(const v8::FunctionCallbackInfo<v8::Value>& args);
void _b_closefile(const v8::FunctionCallbackInfo<v8::Value>& args);
void _b_writefile(const v8::FunctionCallbackInfo<v8::Value>& args);
v8::Handle<v8::ObjectTemplate> CreateBuilderTemplate(v8::Isolate* isolate);
void CreateBuilderObject(const v8::FunctionCallbackInfo<v8::Value>& args);
#endif // BUILDER_WRAPPER_BUILDER