blob: ff607dddc6d5aebc52957ac7c7964137775df00a [file] [log] [blame]
Patrick Benavoli68a91282011-08-31 11:23:23 +02001/* <auto_header>
2 * <FILENAME>
3 *
4 * INTEL CONFIDENTIAL
5 * Copyright © 2011 Intel
6 * Corporation All Rights Reserved.
7 *
8 * The source code contained or described herein and all documents related to
9 * the source code ("Material") are owned by Intel Corporation or its suppliers
10 * or licensors. Title to the Material remains with Intel Corporation or its
11 * suppliers and licensors. The Material contains trade secrets and proprietary
12 * and confidential information of Intel or its suppliers and licensors. The
13 * Material is protected by worldwide copyright and trade secret laws and
14 * treaty provisions. No part of the Material may be used, copied, reproduced,
15 * modified, published, uploaded, posted, transmitted, distributed, or
16 * disclosed in any way without Intel’s prior express written permission.
17 *
18 * No license under any patent, copyright, trade secret or other intellectual
19 * property right is granted to or conferred upon you by disclosure or delivery
20 * of the Materials, either expressly, by implication, inducement, estoppel or
21 * otherwise. Any license under such intellectual property rights must be
22 * express and approved by Intel in writing.
23 *
24 * AUTHOR: Patrick Benavoli (patrickx.benavoli@intel.com)
25 * CREATED: 2011-06-01
26 * UPDATED: 2011-07-27
27 *
28 *
29 * </auto_header>
30 */
31#include "XmlComposer.h"
32#include <libxml/parser.h>
33#include <libxml/tree.h>
34#include <time.h>
35
36#define base CXmlSerializer
37
38#ifndef LIBXML_TREE_ENABLED
39#warning "LIBXML_TREE_ENABLED undefined. XML file exporting feature won't be supported!"
40#endif
41
42CXmlComposer::CXmlComposer(const string& strXmlInstanceFile, const string& strXmlSchemaFile, const string& strRootElementType, CXmlSerializingContext& serializingContext) :
43 base(strXmlInstanceFile, strXmlSchemaFile, strRootElementType, serializingContext)
44{
45}
46
47CXmlComposer::~CXmlComposer()
48{
49}
50
51// open / close
52bool CXmlComposer::open()
53{
54#ifdef LIBXML_TREE_ENABLED
55 // Create document from scratch
56 _pDoc = xmlNewDoc(BAD_CAST "1.0");
57
58 // Create root node
59 _pRootNode = xmlNewNode(NULL, BAD_CAST _strRootElementType.c_str());
60
61 // Assign it to document
62 xmlDocSetRootElement(_pDoc, _pRootNode);
63#else
64 _serializingContext.setError("XML file exporting feature unsupported on this image. This easiest way to activate it is to do a global recompilation with LIBXML_TREE_ENABLED compiler switch set");
65#endif
66 return base::open();
67}
68
69bool CXmlComposer::close()
70{
71 // Write file (formatted)
72 if (xmlSaveFormatFileEnc(_strXmlInstanceFile.c_str(), _pDoc, "UTF-8", 1) == -1) {
73
74 _serializingContext.setError("Could not write file " + _strXmlInstanceFile);
75
76 return false;
77 }
78
79 return base::close();
80}
81
82// Composing contents
Patrick Benavoli63499d42011-10-24 18:50:03 +020083void CXmlComposer::compose(const IXmlSource* pXmlSource, const string& strProduct, const string& strVersion)
Patrick Benavoli68a91282011-08-31 11:23:23 +020084{
85 // Compose document
86 CXmlElement docElement(_pRootNode);
87
88 // Schema namespace
89 docElement.setAttributeString("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
90
91 // Schema location
92 docElement.setAttributeString("xsi:noNamespaceSchemaLocation", _strXmlSchemaFile);
93
94 // Comment for date/time
Patrick Benavoli63499d42011-10-24 18:50:03 +020095 docElement.setComment(string(" Exported on ") + getTimeAsString() + " from " + strProduct + " version " + strVersion + " ");
Patrick Benavoli68a91282011-08-31 11:23:23 +020096
97 pXmlSource->toXml(docElement, _serializingContext);
98}
99
100string CXmlComposer::getTimeAsString()
101{
102 char acBuf[200];
103 time_t t;
104 struct tm *tmp;
105 t = time(NULL);
106 tmp = localtime(&t);
107
108 strftime(acBuf, sizeof(acBuf), "%F, %T", tmp);
109
110 return acBuf;
111}