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 "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
30 namespace FlightCrew
\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
38 xc::DOMNodeList *parents_matching = document.getElementsByTagNameNS(
\r
39 toX( parent_qname.namespace_name ), toX( parent_qname.local_name ) );
\r
41 std::vector< Result > results;
\r
43 if ( parents_matching->getLength() < 1 )
\r
47 xc::DOMElement* parent = static_cast< xc::DOMElement* >( parents_matching->item( 0 ) );
\r
48 std::vector< xc::DOMElement* > children = xe::GetElementChildren( *parent );
\r
50 for ( uint i = 0; i < children.size(); ++i )
\r
52 xc::DOMElement *child = children[ i ];
\r
53 QName child_qname( fromX( child->getLocalName() ), fromX( child->getNamespaceURI() ) );
\r
55 if ( !Util::Contains< QName >( allowed_children, child_qname ) )
\r
57 results.push_back( NotAllowedChildResult( *children[ i ] ) );
\r
64 Result AllowedChildrenValidator::NotAllowedChildResult( const xc::DOMNode &child )
\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
69 return ResultWithNodeLocation( ERROR_XML_CHILD_NOT_RECOGNIZED, child )
\r
70 .AddMessageArgument( fromX( element->getLocalName() ) )
\r
71 .AddMessageArgument( fromX( parent->getLocalName() ) );
\r
74 } //namespace FlightCrew
\r