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