]> git.donarmstrong.com Git - flightcrew.git/blob - src/FlightCrew/Validators/AttributesPresentValidator.cpp
Imported Upstream version 0.7.2+dfsg
[flightcrew.git] / src / FlightCrew / Validators / AttributesPresentValidator.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 "AttributesPresentValidator.h"\r
24 #include "ToXercesStringConverter.h"\r
25 #include "FromXercesStringConverter.h"\r
26 #include "Misc/Utilities.h"\r
27 \r
28 \r
29 namespace FlightCrew\r
30 {\r
31 \r
32 std::vector< Result > AttributesPresentValidator::HasOnlyAllowedAttributes( \r
33     const QName &element_qname,\r
34     const std::vector< QName > &attribute_qnames, \r
35     const xc::DOMDocument &document )\r
36 {\r
37     xc::DOMNodeList *elements = document.getElementsByTagNameNS(\r
38         toX( element_qname.namespace_name ),  toX( element_qname.local_name ) );\r
39 \r
40     std::vector< Result > results;\r
41 \r
42     for ( uint i = 0; i < elements->getLength(); ++i )\r
43     {\r
44         xc::DOMElement* element         = static_cast< xc::DOMElement* >( elements->item( i ) );\r
45         xc::DOMNamedNodeMap *attributes = element->getAttributes();\r
46 \r
47         for ( uint j = 0; j < attributes->getLength(); ++j )\r
48         {\r
49             xc::DOMAttr* attribute = static_cast< xc::DOMAttr* >( attributes->item( j ) );\r
50 \r
51             if ( !IsAllowedAttribute( *attribute, attribute_qnames ) )\r
52             {\r
53                 results.push_back( \r
54                     ResultWithNodeLocation( ERROR_XML_ATTRIBUTE_NOT_RECOGNIZED, *element )\r
55                     .AddMessageArgument( fromX( attribute->getName() ) )\r
56                     .AddMessageArgument( element_qname.local_name )\r
57                     );\r
58             }\r
59         }\r
60     }    \r
61 \r
62     return results;\r
63 }\r
64 \r
65 \r
66 std::vector< Result > AttributesPresentValidator::HasMandatoryAttributes( \r
67     const QName &element_qname, \r
68     const std::vector< QName > &attribute_qnames, \r
69     const xc::DOMDocument &document )\r
70 {\r
71     xc::DOMNodeList *elements = document.getElementsByTagNameNS(\r
72         toX( element_qname.namespace_name ),  toX( element_qname.local_name ) );\r
73 \r
74     std::vector< Result > results;\r
75 \r
76     for ( uint i = 0; i < elements->getLength(); ++i )\r
77     {\r
78         xc::DOMElement* element = static_cast< xc::DOMElement* >( elements->item( i ) );\r
79 \r
80         foreach( QName attribute_qname, attribute_qnames )\r
81         {\r
82             if ( !element->hasAttributeNS( \r
83                     toX( attribute_qname.namespace_name ), toX( attribute_qname.local_name ) ) )\r
84             {\r
85                 results.push_back( \r
86                     ResultWithNodeLocation( ERROR_XML_REQUIRED_ATTRIBUTE_MISSING, *element )\r
87                     .AddMessageArgument( attribute_qname.local_name )\r
88                     .AddMessageArgument( element_qname.local_name )\r
89                     );\r
90             }\r
91         }\r
92     }\r
93 \r
94     return results;\r
95 }\r
96 \r
97 bool AttributesPresentValidator::IsAllowedAttribute( \r
98     const xc::DOMAttr &attribute, \r
99     const std::vector< QName > &allowed_attribute_qnames )\r
100 {\r
101     QName attribute_qname( fromX( attribute.getLocalName() ), fromX( attribute.getNamespaceURI() ) );\r
102  \r
103     bool allowed_name = Util::Contains< QName >( \r
104                             allowed_attribute_qnames, attribute_qname );\r
105 \r
106     // This includes the namespace prefix,\r
107     // so e.g. opf:scheme\r
108     std::string full_attribute_name = fromX( attribute.getName() );\r
109 \r
110     bool is_xmlns = boost::starts_with( full_attribute_name, "xmlns" );\r
111 \r
112     return allowed_name || is_xmlns;\r
113 }\r
114 \r
115 } //namespace FlightCrew\r