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 "XmlParser.h" |
| 26 | #include <stdio.h> |
| 27 | #include <stdarg.h> |
| 28 | #include <libxml/parser.h> |
| 29 | #include <libxml/tree.h> |
| 30 | #include <libxml/xmlschemas.h> |
| 31 | |
| 32 | #define base CXmlSerializer |
| 33 | |
| 34 | |
| 35 | CXmlParser::CXmlParser(const string& strXmlInstanceFile, const string& strXmlSchemaFile, const string& strRootElementType, CXmlSerializingContext& serializingContext) : |
| 36 | base(strXmlInstanceFile, strXmlSchemaFile, strRootElementType, serializingContext) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | CXmlParser::~CXmlParser() |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | void CXmlParser::schemaValidityStructuredErrorFunc(void* pUserData, _xmlError* pError) |
| 45 | { |
| 46 | (void)pUserData; |
| 47 | |
| 48 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 49 | // Display message |
| 50 | puts(pError->message); |
| 51 | #endif |
| 52 | } |
| 53 | |
| 54 | bool CXmlParser::isInstanceDocumentValid() |
| 55 | { |
| 56 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 57 | xmlDocPtr pSchemaDoc = xmlReadFile(_strXmlSchemaFile.c_str(), NULL, XML_PARSE_NONET); |
| 58 | |
| 59 | if (!pSchemaDoc) { |
| 60 | // Unable to load Schema |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | xmlSchemaParserCtxtPtr pParserCtxt = xmlSchemaNewDocParserCtxt(pSchemaDoc); |
| 65 | |
| 66 | if (!pParserCtxt) { |
| 67 | |
| 68 | // Unable to create schema context |
| 69 | xmlFreeDoc(pSchemaDoc); |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | // Get Schema |
| 74 | xmlSchemaPtr pSchema = xmlSchemaParse(pParserCtxt); |
| 75 | |
| 76 | if (!pSchema) { |
| 77 | |
| 78 | // Invalid Schema |
| 79 | xmlSchemaFreeParserCtxt(pParserCtxt); |
| 80 | xmlFreeDoc(pSchemaDoc); |
| 81 | return false; |
| 82 | } |
| 83 | xmlSchemaValidCtxtPtr pValidationCtxt = xmlSchemaNewValidCtxt(pSchema); |
| 84 | |
| 85 | if (!pValidationCtxt) { |
| 86 | |
| 87 | // Unable to create validation context |
| 88 | xmlSchemaFree(pSchema); |
| 89 | xmlSchemaFreeParserCtxt(pParserCtxt); |
| 90 | xmlFreeDoc(pSchemaDoc); |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | xmlSetStructuredErrorFunc(this, schemaValidityStructuredErrorFunc); |
| 95 | //xmlSchemaSetValidErrors(pValidationCtxt, schemaValidityErrorFunc, schemaValidityWarningFunc, NULL); |
| 96 | |
| 97 | bool isDocValid = xmlSchemaValidateDoc(pValidationCtxt, _pDoc) == 0; |
| 98 | |
| 99 | xmlSchemaFreeValidCtxt(pValidationCtxt); |
| 100 | xmlSchemaFree(pSchema); |
| 101 | xmlSchemaFreeParserCtxt(pParserCtxt); |
| 102 | xmlFreeDoc(pSchemaDoc); |
| 103 | |
| 104 | return isDocValid; |
| 105 | #else |
| 106 | return true; |
| 107 | #endif |
| 108 | } |
| 109 | |
| 110 | bool CXmlParser::open() |
| 111 | { |
| 112 | // Parse the file and get the DOM |
| 113 | _pDoc = xmlReadFile(_strXmlInstanceFile.c_str(), NULL, 0); |
| 114 | |
| 115 | if (!_pDoc) { |
| 116 | |
| 117 | _serializingContext.setError("Could not parse file " + _strXmlInstanceFile); |
| 118 | |
| 119 | return false; |
| 120 | } |
| 121 | // Validate |
| 122 | if (!isInstanceDocumentValid()) { |
| 123 | |
| 124 | _serializingContext.setError("Document " + _strXmlInstanceFile + " is not valid"); |
| 125 | |
| 126 | // Free XML doc |
| 127 | xmlFreeDoc(_pDoc); |
| 128 | |
| 129 | _pDoc = NULL; |
| 130 | |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | // Get the root element node |
| 135 | _pRootNode = xmlDocGetRootElement(_pDoc); |
| 136 | |
| 137 | // Check Root element type |
| 138 | if (getRootElementName() != _strRootElementType) { |
| 139 | |
| 140 | _serializingContext.setError("Error: Wrong XML structure file " + _strXmlInstanceFile); |
| 141 | _serializingContext.appendLineToError("Root Element " + getRootElementName() + " mismatches expected type " + _strRootElementType); |
| 142 | |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | return base::open(); |
| 147 | } |
| 148 | |
| 149 | bool CXmlParser::parse(IXmlSink* pXmlSink) |
| 150 | { |
| 151 | // Parse document |
| 152 | CXmlElement topMostElement(_pRootNode); |
| 153 | |
| 154 | return pXmlSink->fromXml(topMostElement, _serializingContext); |
| 155 | } |
| 156 | |