blob: f1094f170bb0137be6aa4f578ea247d044a4f350 [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
Frédéric Boisnarda409daa2012-10-18 18:20:03 +020046 if (!isParsable(serializingContext)) {
Georges-Henri Baron326a31d2012-06-28 12:05:09 +020047
48 return false;
49 }
50
51 // Validate
52 if (!isInstanceDocumentValid()) {
53
54 serializingContext.setError("Document " + _strXmlInstanceFile + " is not valid");
55
56 return false;
57 }
58
59 // Check Root element type
60 if (getRootElementName() != _strRootElementType) {
61
62 serializingContext.setError("Error: Wrong XML structure file " + _strXmlInstanceFile);
63 serializingContext.appendLineToError("Root Element " + getRootElementName() + " mismatches expected type " + _strRootElementType);
64
65 return false;
66 }
67
68 // Check Root element name attribute (if any)
69 if (_bNameCheck) {
70
71 string strRootElementNameCheck = getRootElementAttributeString(_strNameAttrituteName);
72
73 if (!_strRootElementName.empty() && strRootElementNameCheck != _strRootElementName) {
74
75 serializingContext.setError("Error: Wrong XML structure file " + _strXmlInstanceFile);
76 serializingContext.appendLineToError(_strRootElementType + " element " + _strRootElementName + " mismatches expected " + _strRootElementType + " type " + strRootElementNameCheck);
77
78 return false;
79 }
80 }
81
82 return true;
83}
84
Frédéric Boisnarda409daa2012-10-18 18:20:03 +020085bool CXmlFileDocSource::isParsable(CXmlSerializingContext& serializingContext) const
86{
87 // Check that the doc has been created
88 if (!_pDoc) {
89
90 serializingContext.setError("Could not parse file " + _strXmlInstanceFile);
91
92 return false;
93 }
94
95 return true;
96}
97
Georges-Henri Baron326a31d2012-06-28 12:05:09 +020098bool CXmlFileDocSource::isInstanceDocumentValid()
99{
100#ifdef LIBXML_SCHEMAS_ENABLED
101 xmlDocPtr pSchemaDoc = xmlReadFile(_strXmlSchemaFile.c_str(), NULL, XML_PARSE_NONET);
102
103 if (!pSchemaDoc) {
104 // Unable to load Schema
105 return false;
106 }
107
108 xmlSchemaParserCtxtPtr pParserCtxt = xmlSchemaNewDocParserCtxt(pSchemaDoc);
109
110 if (!pParserCtxt) {
111
112 // Unable to create schema context
113 xmlFreeDoc(pSchemaDoc);
114 return false;
115 }
116
117 // Get Schema
118 xmlSchemaPtr pSchema = xmlSchemaParse(pParserCtxt);
119
120 if (!pSchema) {
121
122 // Invalid Schema
123 xmlSchemaFreeParserCtxt(pParserCtxt);
124 xmlFreeDoc(pSchemaDoc);
125 return false;
126 }
127 xmlSchemaValidCtxtPtr pValidationCtxt = xmlSchemaNewValidCtxt(pSchema);
128
129 if (!pValidationCtxt) {
130
131 // Unable to create validation context
132 xmlSchemaFree(pSchema);
133 xmlSchemaFreeParserCtxt(pParserCtxt);
134 xmlFreeDoc(pSchemaDoc);
135 return false;
136 }
137
138 xmlSetStructuredErrorFunc(this, schemaValidityStructuredErrorFunc);
139 //xmlSchemaSetValidErrors(pValidationCtxt, schemaValidityErrorFunc, schemaValidityWarningFunc, NULL);
140
141 bool isDocValid = xmlSchemaValidateDoc(pValidationCtxt, _pDoc) == 0;
142
143 xmlSchemaFreeValidCtxt(pValidationCtxt);
144 xmlSchemaFree(pSchema);
145 xmlSchemaFreeParserCtxt(pParserCtxt);
146 xmlFreeDoc(pSchemaDoc);
147
148 return isDocValid;
149#else
150 return true;
151#endif
152}
153
154void CXmlFileDocSource::schemaValidityStructuredErrorFunc(void* pUserData, _xmlError* pError)
155{
156 (void)pUserData;
157
158#ifdef LIBXML_SCHEMAS_ENABLED
159 // Display message
160 puts(pError->message);
161#endif
162}