1 /************************************************************************
\r
3 ** Copyright (C) 2010 Strahinja Markovic
\r
5 ** This file is part of FlightCrew.
\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
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
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
20 *************************************************************************/
\r
23 #include "AttributesPresentValidator.h"
\r
24 #include "ToXercesStringConverter.h"
\r
25 #include "FromXercesStringConverter.h"
\r
26 #include "Misc/Utilities.h"
\r
29 namespace FlightCrew
\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
37 xc::DOMNodeList *elements = document.getElementsByTagNameNS(
\r
38 toX( element_qname.namespace_name ), toX( element_qname.local_name ) );
\r
40 std::vector< Result > results;
\r
42 for ( uint i = 0; i < elements->getLength(); ++i )
\r
44 xc::DOMElement* element = static_cast< xc::DOMElement* >( elements->item( i ) );
\r
45 xc::DOMNamedNodeMap *attributes = element->getAttributes();
\r
47 for ( uint j = 0; j < attributes->getLength(); ++j )
\r
49 xc::DOMAttr* attribute = static_cast< xc::DOMAttr* >( attributes->item( j ) );
\r
51 if ( !IsAllowedAttribute( *attribute, attribute_qnames ) )
\r
54 ResultWithNodeLocation( ERROR_XML_ATTRIBUTE_NOT_RECOGNIZED, *element )
\r
55 .AddMessageArgument( fromX( attribute->getName() ) )
\r
56 .AddMessageArgument( element_qname.local_name )
\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
71 xc::DOMNodeList *elements = document.getElementsByTagNameNS(
\r
72 toX( element_qname.namespace_name ), toX( element_qname.local_name ) );
\r
74 std::vector< Result > results;
\r
76 for ( uint i = 0; i < elements->getLength(); ++i )
\r
78 xc::DOMElement* element = static_cast< xc::DOMElement* >( elements->item( i ) );
\r
80 foreach( QName attribute_qname, attribute_qnames )
\r
82 if ( !element->hasAttributeNS(
\r
83 toX( attribute_qname.namespace_name ), toX( attribute_qname.local_name ) ) )
\r
86 ResultWithNodeLocation( ERROR_XML_REQUIRED_ATTRIBUTE_MISSING, *element )
\r
87 .AddMessageArgument( attribute_qname.local_name )
\r
88 .AddMessageArgument( element_qname.local_name )
\r
97 bool AttributesPresentValidator::IsAllowedAttribute(
\r
98 const xc::DOMAttr &attribute,
\r
99 const std::vector< QName > &allowed_attribute_qnames )
\r
101 QName attribute_qname( fromX( attribute.getLocalName() ), fromX( attribute.getNamespaceURI() ) );
\r
103 bool allowed_name = Util::Contains< QName >(
\r
104 allowed_attribute_qnames, attribute_qname );
\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
110 bool is_xmlns = boost::starts_with( full_attribute_name, "xmlns" );
\r
112 return allowed_name || is_xmlns;
\r
115 } //namespace FlightCrew
\r