summaryrefslogtreecommitdiff
path: root/src/XmlHandler/XmlWriter.cpp
blob: 86e39d576acc06e8321b5f4e6478209d0a025529 (plain)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//
// Open Service Platform
// Copyright (c) 2012 Samsung Electronics Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the License);
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
/**
 * @file	XmlWriter.cpp
 * @brief	This is the implementation file for %XmlWriter class.
 */

#include <new>
#include <libxml/parser.h>
#include <libxml/parserInternals.h>
#include <libxml/tree.h>
#include <libxml/xmlstring.h>

#include <FBaseLog.h>
#include <FBaseUtilStringUtil.h>
#include <FIoFile.h>

#include "InstallerDefs.h"
#include "XmlWriter.h"
#include "InstallerUtil.h"

using namespace Osp::Base;
using namespace Osp::Base::Collection;
using namespace Osp::Base::Utility;
using namespace Osp::Io;

XmlWriter::XmlWriter(void)
:__pXmlWriter(null), __pXmlBuffer(null)
{
}

XmlWriter::~XmlWriter(void)
{
	result r = E_SUCCESS;

	xmlTextWriterEndDocument(__pXmlWriter);

	File file;
	r = file.Construct(__xmlFilePath, "w");
	if (IsFailed(r))
	{
		AppLogTag(OSP_INSTALLER, "file.Construct() failed, __xmlFilePath=[%ls]", __xmlFilePath.GetPointer());
	}
	else
	{
		r = file.Write((char *)__pXmlBuffer->content, __pXmlBuffer->use);
		if (IsFailed(r))
		{
			AppLogTag(OSP_INSTALLER, "file.Write() failed, __xmlFilePath=[%ls]", __xmlFilePath.GetPointer());
		}

		AppLogTag(OSP_INSTALLER, "------------------------------------------");
		AppLogTag(OSP_INSTALLER, "%ls is generated", __xmlFilePath.GetPointer());
		AppLogTag(OSP_INSTALLER, "------------------------------------------");
		// AppLogTag(OSP_INSTALLER, "%s", __pXmlBuffer->content);
		InstallerUtil::DumpLog((const char*)__pXmlBuffer->content);
	}

	if (__pXmlWriter)
	{
		xmlFreeTextWriter(__pXmlWriter);
	}

	if (__pXmlBuffer)
	{
		xmlBufferFree(__pXmlBuffer);
	}
}

bool
XmlWriter::Construct(const Osp::Base::String& filepath)
{
	bool res = false;
	int err = 0;

	__pXmlBuffer = xmlBufferCreate();
	TryCatch(__pXmlBuffer, res = false, "[osp-installer] __pXmlBuffer is null");

	__xmlFilePath = filepath;

	__pXmlWriter = xmlNewTextWriterMemory(__pXmlBuffer, 0);
	TryCatch(__pXmlWriter, res = false, "[osp-installer] __pXmlWriter is null");

	xmlTextWriterSetIndent(__pXmlWriter, 1);

	err = xmlTextWriterStartDocument(__pXmlWriter, null, null, null);
	TryCatch(err == 0, res = false, "[osp-installer] err is %d", err);

	return true;
CATCH:

	if (__pXmlWriter)
	{
		xmlFreeTextWriter(__pXmlWriter);
		__pXmlWriter = null;
	}

	if (__pXmlBuffer)
	{
		xmlBufferFree(__pXmlBuffer);
		__pXmlBuffer = null;
	}

	return res;
}

bool
XmlWriter::StartElement(const Osp::Base::String& name)
{
	ByteBuffer* pNameBuf = null;

	pNameBuf = StringUtil::StringToUtf8N(name);
	if (pNameBuf)
	{
		xmlTextWriterStartElement(__pXmlWriter, (xmlChar *)pNameBuf->GetPointer());
	}

	delete pNameBuf;
	return true;
}

bool
XmlWriter::WriteAttribute(const Osp::Base::String& name, const Osp::Base::String& content)
{
	ByteBuffer* pNameBuf = null;
	ByteBuffer* pContentBuf = null;

	pNameBuf = StringUtil::StringToUtf8N(name);
	pContentBuf = StringUtil::StringToUtf8N(content);

	if (pNameBuf && pContentBuf)
	{
		xmlTextWriterWriteAttribute(__pXmlWriter, (xmlChar *)pNameBuf->GetPointer(), (xmlChar *)pContentBuf->GetPointer());
	}

	delete pNameBuf;
	delete pContentBuf;
	return true;
}

bool
XmlWriter::WriteString(const Osp::Base::String& content)
{
	ByteBuffer* pContentBuf = null;

	pContentBuf = StringUtil::StringToUtf8N(content);
	if (pContentBuf)
	{
		xmlTextWriterWriteString(__pXmlWriter, (xmlChar *)pContentBuf->GetPointer());
	}

	delete pContentBuf;
	return true;
}

bool
XmlWriter::EndElement(void)
{
	xmlTextWriterEndElement(__pXmlWriter);
	return true;
}