mirror of
https://github.com/ONLYOFFICE/core.git
synced 2026-07-11 01:52:24 +08:00
git-svn-id: svn://fileserver/activex/AVS/Sources/TeamlabOffice/trunk/ServerComponents@62590 954022d7-b5bf-4e40-9824-e11837661b57
151 lines
3.9 KiB
C++
151 lines
3.9 KiB
C++
#include "ConvertTxt2Docx.h"
|
|
|
|
#include "../../../Common/DocxFormat/Source/DocxFormat/Docx.h"
|
|
#include "TxtFormat/TxtFormat.h"
|
|
#include "TxtXmlEvent.h"
|
|
|
|
namespace Txt2Docx
|
|
{
|
|
class Converter_Impl
|
|
{
|
|
public:
|
|
Converter_Impl(int encoding);
|
|
|
|
void convert(TxtXml::ITxtXmlEvent& Event);
|
|
|
|
Txt::File m_inputFile;
|
|
OOX::CDocument m_outputFile;
|
|
|
|
static CString PrepareToXML( const std::wstring & sInput)
|
|
{
|
|
CString sResult = std_string2string(sInput);
|
|
//& («&»), < («<»), > («>»), ' («'»), è " («"»)
|
|
sResult.Replace(_T("&"), _T("&"));
|
|
sResult.Replace(_T("<"), _T("<"));
|
|
sResult.Replace(_T(">"), _T(">"));
|
|
sResult.Replace(_T("\""), _T("""));
|
|
sResult.Replace(_T("'"), _T("'"));
|
|
return sResult;
|
|
}
|
|
};
|
|
|
|
Converter::Converter(int encoding) : converter_( new Converter_Impl(encoding) )
|
|
{
|
|
}
|
|
|
|
Converter::~Converter()
|
|
{
|
|
delete converter_;
|
|
}
|
|
|
|
void Converter::convert(TxtXml::ITxtXmlEvent& Event)
|
|
{
|
|
return converter_->convert(Event);
|
|
}
|
|
|
|
void Converter::read(const std::wstring& path)
|
|
{
|
|
return converter_->m_inputFile.read(path);
|
|
}
|
|
|
|
void Converter::write(/*const std::wstring& path*/XmlUtils::CStringWriter & stringWriter)
|
|
{
|
|
for (long i=0;i < converter_->m_outputFile.m_arrItems.size(); i++)
|
|
{
|
|
if (converter_->m_outputFile.m_arrItems[i] != NULL)
|
|
stringWriter.WriteString(converter_->m_outputFile.m_arrItems[i]->toXML());
|
|
}
|
|
//BOOL res = converter_->m_outputFile.Write(std_string2string(path.string()));
|
|
return;
|
|
}
|
|
|
|
Converter_Impl::Converter_Impl(int encoding)
|
|
{
|
|
m_inputFile.m_nEncoding = encoding;
|
|
|
|
}
|
|
|
|
void Converter_Impl::convert(TxtXml::ITxtXmlEvent& Event)
|
|
{
|
|
//smart_ptr<OOX::File> pFile = m_outputFile.Find(OOX::FileTypes::Document);
|
|
|
|
OOX::CDocument *pDocument = &m_outputFile;//NULL;
|
|
|
|
if (!m_inputFile.m_listContent.empty() /*&& pFile.IsInit() && OOX::FileTypes::Document == pFile->type()*/)
|
|
{
|
|
//pDocument = (OOX::CDocument*)pFile.operator->();
|
|
//pDocument->ClearItems();
|
|
|
|
int percent = 100000;
|
|
int step = 800000 / m_inputFile.m_listContentSize; // !!!!!
|
|
bool cancel = Event.Progress(0, 100000);
|
|
if(cancel)
|
|
return;
|
|
/*
|
|
OOX::Logic::ParagraphProperty pPr;
|
|
OOX::Logic::Spacing space;
|
|
space.After = 0;
|
|
space.Line = 240;
|
|
space.LineRule = "auto";
|
|
pPr.Spacing = space;
|
|
|
|
OOX::Logic::RFonts rFont;
|
|
rFont.Ascii = "Courier New";
|
|
rFont.HAnsi = "Courier New";
|
|
rFont.Cs = "Courier New";
|
|
|
|
OOX::Logic::RunProperty rPr;
|
|
rPr.RFonts = rFont;
|
|
|
|
pPr.RunProperty = rPr;
|
|
|
|
OOX::Logic::Paragraph paragraph;
|
|
paragraph.Property = pPr;
|
|
*/
|
|
for (std::list<std::wstring>::iterator line = m_inputFile.m_listContent.begin(); line != m_inputFile.m_listContent.end(); line++)
|
|
{
|
|
//OOX::Logic::ParagraphProperty pPr;
|
|
//OOX::Logic::Spacing space;
|
|
//space.After = 0;
|
|
//space.Line = 240;
|
|
//space.LineRule = "auto";
|
|
//pPr.Spacing = space;
|
|
|
|
//OOX::Logic::RFonts rFont;
|
|
//rFont.Ascii = "Courier New";
|
|
//rFont.HAnsi = "Courier New";
|
|
//rFont.Cs = "Courier New";
|
|
|
|
//OOX::Logic::RunProperty rPr;
|
|
//rPr.RFonts = rFont;
|
|
|
|
//pPr.RunProperty = rPr;
|
|
|
|
//OOX::Logic::Paragraph paragraph;
|
|
//paragraph.Property = pPr;
|
|
|
|
OOX::Logic::CParagraph *temp = new OOX::Logic::CParagraph();
|
|
|
|
while(line->find(_T("\x08")) != line->npos)
|
|
{
|
|
line->erase(line->find(_T("\x08")), 1);//, "");
|
|
}
|
|
|
|
if(line->length() > 0)
|
|
{
|
|
CString s = PrepareToXML(*line);
|
|
temp->AddText(s);//, rPr);
|
|
}
|
|
|
|
pDocument->m_arrItems.push_back(temp);
|
|
|
|
percent += step;
|
|
cancel = Event.Progress(0, percent);
|
|
if(cancel)
|
|
return;
|
|
}
|
|
}
|
|
Event.Progress(0, 900000);
|
|
}
|
|
} // namespace Txt2Docx
|