1 /*
2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2012 - Scilab Enterprises - Calixte DENIZET
4 *
5 * Copyright (C) 2012 - 2016 - Scilab Enterprises
6 *
7 * This file is hereby licensed under the terms of the GNU GPL v2.0,
8 * pursuant to article 5.3.4 of the CeCILL v.2.1.
9 * This file was originally licensed under the terms of the CeCILL v2.1,
10 * and continues to be available under such terms.
11 * For more information, see the COPYING file which you should have received
12 * along with this program.
13 *
14 */
15
16 #include "XMLObject.hxx"
17 #include "XMLValidation.hxx"
18
19 extern "C"
20 {
21 #include "expandPathVariable.h"
22 #include "sci_malloc.h"
23 #include "localization.h"
24 }
25
26 #define BUFFER_SIZE 1024
27
28 namespace org_modules_xml
29 {
30
31 std::string XMLValidation::errorBuffer;
32 std::list<XMLValidation *> XMLValidation::openValidationFiles;
33
XMLValidation()34 XMLValidation::XMLValidation(): XMLObject(), validationFile(0)
35 {
36 scilabType = XMLVALID;
37 }
38
errorFunction(void * ctx,const char * msg,...)39 void XMLValidation::errorFunction(void *ctx, const char *msg, ...)
40 {
41 char str[BUFFER_SIZE];
42 va_list args;
43
44 va_start(args, msg);
45 #ifdef _MSC_VER
46 _vsnprintf(str, BUFFER_SIZE, msg, args);
47 #else
48 vsnprintf(str, BUFFER_SIZE, msg, args);
49 #endif
50 va_end(args);
51 errorBuffer.append(str);
52 }
53
errorReaderFunction(void * arg,const char * msg,xmlParserSeverities severity,xmlTextReaderLocatorPtr locator)54 void XMLValidation::errorReaderFunction(void * arg, const char * msg, xmlParserSeverities severity, xmlTextReaderLocatorPtr locator)
55 {
56 std::ostringstream oss;
57
58 oss << xmlTextReaderLocatorBaseURI(locator) << gettext(" at line ")
59 << xmlTextReaderLocatorLineNumber(locator) << std::endl
60 << msg << std::endl;
61
62 errorBuffer.append(oss.str());
63 }
64
65
validate(const std::string & xmlCode,std::string * error) const66 bool XMLValidation::validate(const std::string & xmlCode, std::string * error) const
67 {
68 xmlParserInputBuffer * buffer = xmlParserInputBufferCreateMem(xmlCode.c_str(), (int)xmlCode.size(), (xmlCharEncoding) 0);
69 if (!buffer)
70 {
71 error->append(gettext("Cannot create a buffer"));
72 return false;
73 }
74
75 xmlTextReader * reader = xmlNewTextReader(buffer, 0);
76 if (!reader)
77 {
78 xmlFreeParserInputBuffer(buffer);
79 error->append(gettext("Cannot create a reader"));
80 return false;
81 }
82
83 bool valid = validate(reader, error);
84 xmlFreeParserInputBuffer(buffer);
85
86 return valid;
87 }
88
validate(const char * path,std::string * error) const89 bool XMLValidation::validate(const char *path, std::string * error)const
90 {
91 char *expandedPath = expandPathVariable(const_cast<char *>(path));
92 if (expandedPath)
93 {
94 xmlTextReader *reader = xmlNewTextReaderFilename(expandedPath);
95 FREE(expandedPath);
96 if (!reader)
97 {
98 error->append(gettext("Invalid file"));
99 return false;
100 }
101
102 return validate(reader, error);
103 }
104 else
105 {
106 *error = std::string(gettext("Invalid file name: ")) + std::string(path);
107 return false;
108 }
109 }
110
getOpenValidationFiles()111 const std::list<XMLValidation *>& XMLValidation::getOpenValidationFiles()
112 {
113 return openValidationFiles;
114 }
115
closeAllValidationFiles()116 void XMLValidation::closeAllValidationFiles()
117 {
118 int size = (int)openValidationFiles.size();
119 XMLValidation **arr = new XMLValidation *[size];
120 int j = 0;
121
122 for (std::list < XMLValidation * >::iterator i = openValidationFiles.begin(); i != openValidationFiles.end(); i++, j++)
123 {
124 arr[j] = *i;
125 }
126 for (j = 0; j < size; j++)
127 {
128 delete arr[j];
129 }
130 delete[]arr;
131 }
132 }
133
134