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 "IdsValid.h"
\r
24 #include <ToXercesStringConverter.h>
\r
25 #include <FromXercesStringConverter.h>
\r
26 #include <XmlUtils.h>
\r
28 namespace FlightCrew
\r
31 std::vector< Result > IdsValid::ValidateXml(
\r
32 const xc::DOMDocument &document,
\r
35 std::vector< xc::DOMElement* > elements = xe::GetElementsByQName(
\r
36 document, QName( "*", "*" ) );
\r
38 std::vector< Result > results;
\r
40 foreach( xc::DOMElement* element, elements )
\r
42 if ( !element->hasAttribute( toX( "id" ) ) )
\r
46 std::string id = fromX( element->getAttribute( toX( "id" ) ) );
\r
48 if ( !ValidId( id ) )
\r
51 ResultWithNodeLocation( ERROR_XML_BAD_ID_VALUE, *element )
\r
52 .AddMessageArgument( id )
\r
60 // The specification of what constitutes a valid ID name
\r
61 // can be found here:
\r
62 // http://www.w3.org/TR/REC-xml-names/#NT-NCName
\r
63 bool IdsValid::ValidId( const std::string &id )
\r
69 std::string::const_iterator iter = id.begin();
\r
70 bool first_code_point = true;
\r
72 while ( iter != id.end() )
\r
74 utf8::uint32_t code_point = utf8::next( iter, id.end() );
\r
76 if ( first_code_point )
\r
78 if ( !ValidIdNameStartChar( code_point ) )
\r
82 first_code_point = false;
\r
87 if ( !ValidIdNameChar( code_point ) )
\r
97 bool IdsValid::ValidIdNameStartChar( utf8::uint32_t code_point )
\r
100 code_point == '_' ||
\r
101 ( 'A' <= code_point && code_point <= 'Z' ) ||
\r
102 ( 'a' <= code_point && code_point <= 'z' ) ||
\r
103 ( 0xC0 <= code_point && code_point <= 0xD6 ) ||
\r
104 ( 0xD8 <= code_point && code_point <= 0xF6 ) ||
\r
105 ( 0xF8 <= code_point && code_point <= 0x2FF ) ||
\r
106 ( 0x370 <= code_point && code_point <= 0x37D ) ||
\r
107 ( 0x37F <= code_point && code_point <= 0x1FFF ) ||
\r
108 ( 0x200C <= code_point && code_point <= 0x200D ) ||
\r
109 ( 0x2070 <= code_point && code_point <= 0x218F ) ||
\r
110 ( 0x2C00 <= code_point && code_point <= 0x2FEF ) ||
\r
111 ( 0x3001 <= code_point && code_point <= 0xD7FF ) ||
\r
112 ( 0xF900 <= code_point && code_point <= 0xFDCF ) ||
\r
113 ( 0xFDF0 <= code_point && code_point <= 0xFFFD ) ||
\r
114 ( 0x10000 <= code_point && code_point <= 0xEFFFF );
\r
117 bool IdsValid::ValidIdNameChar( utf8::uint32_t code_point )
\r
120 ValidIdNameStartChar( code_point ) ||
\r
121 code_point == '-' ||
\r
122 code_point == '.' ||
\r
123 code_point == 0xB7 ||
\r
124 ( '0' <= code_point && code_point <= '9' ) ||
\r
125 ( 0x0300 <= code_point && code_point <= 0x036F ) ||
\r
126 ( 0x203F <= code_point && code_point <= 0x2040 );
\r
129 } // namespace FlightCrew
\r