1 /**************************************************************************\
2 * Copyright (c) Kongsberg Oil & Gas Technologies AS
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * Neither the name of the copyright holder nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 \**************************************************************************/
32
33 #include <Inventor/scxml/ScXMLValidateElt.h>
34
35 /*!
36 \class ScXMLValidateElt ScXMLValidateElt.h Inventor/scxml/ScXMLValidateElt.h
37 \brief implements the <validate> SCXML element.
38
39 \since Coin 3.1
40 \ingroup scxml
41 */
42
43 #include <cassert>
44 #include <cstring>
45
46 #include <Inventor/errors/SoDebugError.h>
47 #include <Inventor/C/XML/element.h>
48
49 #include "scxml/ScXMLCommonP.h"
50 #include "SbBasicP.h"
51
52 #ifndef COIN_WORKAROUND_NO_USING_STD_FUNCS
53 using std::strcmp;
54 #endif // !COIN_WORKAROUND_NO_USING_STD_FUNCS
55
56 // *************************************************************************
57
58 class ScXMLValidateEltReader : public ScXMLEltReader {
59 public:
60 ScXMLValidateEltReader(void);
61 virtual ScXMLElt * read(ScXMLElt * container, cc_xml_elt * xmlelt, ScXMLDocument * doc, ScXMLStateMachine * sm);
62 };
63
ScXMLValidateEltReader(void)64 ScXMLValidateEltReader::ScXMLValidateEltReader(void)
65 : ScXMLEltReader("validate")
66 {
67 }
68
69 ScXMLElt *
read(ScXMLElt * container,cc_xml_elt * xmlelt,ScXMLDocument * COIN_UNUSED_ARG (doc),ScXMLStateMachine * COIN_UNUSED_ARG (sm))70 ScXMLValidateEltReader::read(ScXMLElt * container, cc_xml_elt * xmlelt, ScXMLDocument * COIN_UNUSED_ARG(doc), ScXMLStateMachine * COIN_UNUSED_ARG(sm))
71 {
72 assert(container && xmlelt);
73 ScXMLValidateElt * validate = new ScXMLValidateElt;
74 validate->setContainer(container);
75 this->setXMLAttributes(validate, xmlelt);
76
77 // handle XML attributes
78 if (unlikely(!validate->handleXMLAttributes())) {
79 delete validate;
80 return NULL;
81 }
82
83 const int numchildren = cc_xml_elt_get_num_children(xmlelt);
84 for (int c = 0; c < numchildren; ++c) {
85 cc_xml_elt * element = cc_xml_elt_get_child(xmlelt, c);
86 const char * elementtype = cc_xml_elt_get_type(element);
87
88 if (strcmp(elementtype, COIN_XML_CDATA_TYPE) == 0) {
89 // ignore CDATA
90 continue;
91 }
92
93 SoDebugError::post("ScXMLValidateEltReader::read",
94 "<validate> contains unexpected <%s> element", elementtype);
95 delete validate;
96 return NULL;
97 }
98
99 return validate;
100 }
101
102 // *************************************************************************
103
104 class ScXMLValidateElt::PImpl {
105 };
106
107 #define PRIVATE
108
109 SCXML_ELEMENT_SOURCE(ScXMLValidateElt);
110
111 void
initClass(void)112 ScXMLValidateElt::initClass(void)
113 {
114 SCXML_OBJECT_INIT_CLASS(ScXMLValidateElt, ScXMLElt, "ScXMLElt");
115 SCXML_ELEMENT_REGISTER_READER(ScXMLValidateElt, "validate", ScXMLValidateEltReader);
116 }
117
118 void
cleanClass(void)119 ScXMLValidateElt::cleanClass(void)
120 {
121 SCXML_ELEMENT_UNREGISTER_READER(ScXMLValidateElt);
122 ScXMLValidateElt::classTypeId = SoType::badType();
123 }
124
ScXMLValidateElt(void)125 ScXMLValidateElt::ScXMLValidateElt(void)
126 : location(NULL),
127 schema(NULL)
128 {
129 }
~ScXMLValidateElt(void)130 ScXMLValidateElt::~ScXMLValidateElt(void)
131 {
132 this->setLocationAttribute(NULL);
133 this->setSchemaAttribute(NULL);
134 }
135
136 void
setLocationAttribute(const char * locationstr)137 ScXMLValidateElt::setLocationAttribute(const char * locationstr)
138 {
139 SCXML__SET_ATTRIBUTE_VALUE(this->location, "location", locationstr);
140 }
141
142 // const char * getLocationAttribute(void) const { return this->location; }
143
144 void
setSchemaAttribute(const char * schemastr)145 ScXMLValidateElt::setSchemaAttribute(const char * schemastr)
146 {
147 SCXML__SET_ATTRIBUTE_VALUE(this->schema, "schema", schemastr);
148 }
149
150 // const char * getSchemaAttribute(void) const { return this->schema; }
151
152 SbBool
handleXMLAttributes(void)153 ScXMLValidateElt::handleXMLAttributes(void)
154 {
155 if (!inherited::handleXMLAttributes()) {
156 return FALSE;
157 }
158
159 this->setLocationAttribute(this->getXMLAttribute("location"));
160 this->setSchemaAttribute(this->getXMLAttribute("schema"));
161
162 return TRUE;
163 }
164
165 void
copyContents(const ScXMLElt * rhs)166 ScXMLValidateElt::copyContents(const ScXMLElt * rhs)
167 {
168 inherited::copyContents(rhs);
169 const ScXMLValidateElt * orig = coin_assert_cast<const ScXMLValidateElt *>(rhs);
170 this->setLocationAttribute(orig->getLocationAttribute());
171 this->setSchemaAttribute(orig->getSchemaAttribute());
172 }
173
174 const ScXMLElt *
search(const char * attrname,const char * attrvalue) const175 ScXMLValidateElt::search(const char * attrname, const char * attrvalue) const
176 {
177 const ScXMLElt * hit = inherited::search(attrname, attrvalue);
178 if (hit) {
179 return hit;
180 }
181 if (strcmp(attrname, "location") == 0) {
182 if (this->location && strcmp(attrvalue, this->location) == 0) {
183 return this;
184 }
185 }
186 else if (strcmp(attrname, "schema") == 0) {
187 if (this->schema && strcmp(attrvalue, this->schema) == 0) {
188 return this;
189 }
190 }
191 return NULL;
192 }
193
194 void
execute(ScXMLStateMachine * statemachine) const195 ScXMLValidateElt::execute(ScXMLStateMachine * statemachine) const
196 {
197 inherited::execute(statemachine);
198 // FIXME: validate datamodel
199 }
200
201 #undef PRIVATE
202