blob: c8a0837ef71bf9172873e522cc69a1f72b260157 [file] [log] [blame]
Georges-Henri Baron326a31d2012-06-28 12:05:09 +02001/*
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 *
22 * CREATED: 2012-08-10
23 */
24
25#include "XmlFileDocSource.h"
26#include <libxml/parser.h>
27#include <libxml/xmlschemas.h>
28
29#define base CXmlDocSource
30
31
32
33CXmlFileDocSource::CXmlFileDocSource(const string& strXmlInstanceFile, const string& strXmlSchemaFile, const string& strRootElementType, const string& strRootElementName, const string& strNameAttrituteName) :
34 base(xmlReadFile(strXmlInstanceFile.c_str(), NULL, 0)), _strXmlInstanceFile(strXmlInstanceFile), _strXmlSchemaFile(strXmlSchemaFile), _strRootElementType(strRootElementType), _strRootElementName(strRootElementName), _strNameAttrituteName(strNameAttrituteName), _bNameCheck(true)
35{
36}
37
38CXmlFileDocSource::CXmlFileDocSource(const string& strXmlInstanceFile, const string& strXmlSchemaFile, const string& strRootElementType) :
39 base(xmlReadFile(strXmlInstanceFile.c_str(), NULL, 0)), _strXmlInstanceFile(strXmlInstanceFile), _strXmlSchemaFile(strXmlSchemaFile), _strRootElementType(strRootElementType), _strRootElementName(""), _strNameAttrituteName(""), _bNameCheck(false)
40{
41}
42
43bool CXmlFileDocSource::populate(CXmlSerializingContext& serializingContext)
44{
45 // Check that the doc has been created
46 if (!_pDoc) {
47
48 serializingContext.setError("Could not parse file " + _strXmlInstanceFile);
49
50 return false;
51 }
52
53 // Validate
54 if (!isInstanceDocumentValid()) {
55
56 serializingContext.setError("Document " + _strXmlInstanceFile + " is not valid");
57
58 return false;
59 }
60
61 // Check Root element type
62 if (getRootElementName() != _strRootElementType) {
63
64 serializingContext.setError("Error: Wrong XML structure file " + _strXmlInstanceFile);
65 serializingContext.appendLineToError("Root Element " + getRootElementName() + " mismatches expected type " + _strRootElementType);
66
67 return false;
68 }
69
70 // Check Root element name attribute (if any)
71 if (_bNameCheck) {
72
73 string strRootElementNameCheck = getRootElementAttributeString(_strNameAttrituteName);
74
75 if (!_strRootElementName.empty() && strRootElementNameCheck != _strRootElementName) {
76
77 serializingContext.setError("Error: Wrong XML structure file " + _strXmlInstanceFile);
78 serializingContext.appendLineToError(_strRootElementType + " element " + _strRootElementName + " mismatches expected " + _strRootElementType + " type " + strRootElementNameCheck);
79
80 return false;
81 }
82 }
83
84 return true;
85}
86
87bool CXmlFileDocSource::isInstanceDocumentValid()
88{
89#ifdef LIBXML_SCHEMAS_ENABLED
90 xmlDocPtr pSchemaDoc = xmlReadFile(_strXmlSchemaFile.c_str(), NULL, XML_PARSE_NONET);
91
92 if (!pSchemaDoc) {
93 // Unable to load Schema
94 return false;
95 }
96
97 xmlSchemaParserCtxtPtr pParserCtxt = xmlSchemaNewDocParserCtxt(pSchemaDoc);
98
99 if (!pParserCtxt) {
100
101 // Unable to create schema context
102 xmlFreeDoc(pSchemaDoc);
103 return false;
104 }
105
106 // Get Schema
107 xmlSchemaPtr pSchema = xmlSchemaParse(pParserCtxt);
108
109 if (!pSchema) {
110
111 // Invalid Schema
112 xmlSchemaFreeParserCtxt(pParserCtxt);
113 xmlFreeDoc(pSchemaDoc);
114 return false;
115 }
116 xmlSchemaValidCtxtPtr pValidationCtxt = xmlSchemaNewValidCtxt(pSchema);
117
118 if (!pValidationCtxt) {
119
120 // Unable to create validation context
121 xmlSchemaFree(pSchema);
122 xmlSchemaFreeParserCtxt(pParserCtxt);
123 xmlFreeDoc(pSchemaDoc);
124 return false;
125 }
126
127 xmlSetStructuredErrorFunc(this, schemaValidityStructuredErrorFunc);
128 //xmlSchemaSetValidErrors(pValidationCtxt, schemaValidityErrorFunc, schemaValidityWarningFunc, NULL);
129
130 bool isDocValid = xmlSchemaValidateDoc(pValidationCtxt, _pDoc) == 0;
131
132 xmlSchemaFreeValidCtxt(pValidationCtxt);
133 xmlSchemaFree(pSchema);
134 xmlSchemaFreeParserCtxt(pParserCtxt);
135 xmlFreeDoc(pSchemaDoc);
136
137 return isDocValid;
138#else
139 return true;
140#endif
141}
142
143void CXmlFileDocSource::schemaValidityStructuredErrorFunc(void* pUserData, _xmlError* pError)
144{
145 (void)pUserData;
146
147#ifdef LIBXML_SCHEMAS_ENABLED
148 // Display message
149 puts(pError->message);
150#endif
151}