]> git.donarmstrong.com Git - flightcrew.git/blob - src/FlightCrew/Validators/AllowedChildrenValidator.cpp
Imported Upstream version 0.7.2+dfsg
[flightcrew.git] / src / FlightCrew / Validators / AllowedChildrenValidator.cpp
1 /************************************************************************\r
2 **\r
3 **  Copyright (C) 2010  Strahinja Markovic\r
4 **\r
5 **  This file is part of FlightCrew.\r
6 **\r
7 **  FlightCrew is free software: you can redistribute it and/or modify\r
8 **  it under the terms of the GNU Lesser General Public License as published\r
9 **  by the Free Software Foundation, either version 3 of the License, or\r
10 **  (at your option) any later version.\r
11 **\r
12 **  FlightCrew is distributed in the hope that it will be useful,\r
13 **  but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15 **  GNU Lesser General Public License for more details.\r
16 **\r
17 **  You should have received a copy of the GNU Lesser General Public License\r
18 **  along with FlightCrew.  If not, see <http://www.gnu.org/licenses/>.\r
19 **\r
20 *************************************************************************/\r
21 \r
22 #include <stdafx.h>\r
23 #include "AllowedChildrenValidator.h"\r
24 #include "Misc/Utilities.h"\r
25 #include <FromXercesStringConverter.h>\r
26 #include <ToXercesStringConverter.h>\r
27 #include <XmlUtils.h>\r
28 #include <algorithm>\r
29 \r
30 namespace FlightCrew\r
31 {\r
32 \r
33 std::vector< Result > AllowedChildrenValidator::ValidateAllowedChildren( \r
34     const QName &parent_qname, \r
35     const std::vector< QName > &allowed_children, \r
36     const xc::DOMDocument &document )\r
37 {\r
38     xc::DOMNodeList *parents_matching = document.getElementsByTagNameNS(\r
39         toX( parent_qname.namespace_name ),  toX( parent_qname.local_name ) );\r
40 \r
41     std::vector< Result > results;\r
42 \r
43     if ( parents_matching->getLength() < 1 )\r
44     \r
45         return results;    \r
46 \r
47     xc::DOMElement* parent = static_cast< xc::DOMElement* >( parents_matching->item( 0 ) );\r
48     std::vector< xc::DOMElement* > children = xe::GetElementChildren( *parent );\r
49 \r
50     for ( uint i = 0; i < children.size(); ++i )\r
51     {\r
52         xc::DOMElement *child = children[ i ];\r
53         QName child_qname( fromX( child->getLocalName() ), fromX( child->getNamespaceURI() ) );\r
54 \r
55         if ( !Util::Contains< QName >( allowed_children, child_qname ) )\r
56 \r
57             results.push_back( NotAllowedChildResult( *children[ i ] ) );\r
58     }\r
59 \r
60     return results;\r
61 }\r
62 \r
63 \r
64 Result AllowedChildrenValidator::NotAllowedChildResult( const xc::DOMNode &child )\r
65 {\r
66     const xc::DOMElement* element = static_cast< const xc::DOMElement* >( &child );\r
67     const xc::DOMElement* parent  = static_cast< const xc::DOMElement* >( child.getParentNode() );\r
68 \r
69     return ResultWithNodeLocation( ERROR_XML_CHILD_NOT_RECOGNIZED, child )\r
70            .AddMessageArgument( fromX( element->getLocalName() ) )\r
71            .AddMessageArgument( fromX( parent->getLocalName() ) );\r
72 }\r
73 \r
74 } //namespace FlightCrew\r