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