]> git.donarmstrong.com Git - flightcrew.git/blob - src/FlightCrew/Validators/Opf/IdsValid.cpp
Imported Upstream version 0.7.2+dfsg
[flightcrew.git] / src / FlightCrew / Validators / Opf / IdsValid.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 "IdsValid.h"\r
24 #include <ToXercesStringConverter.h>\r
25 #include <FromXercesStringConverter.h>\r
26 #include <XmlUtils.h>\r
27 \r
28 namespace FlightCrew\r
29 {\r
30 \r
31 std::vector< Result > IdsValid::ValidateXml(\r
32     const xc::DOMDocument &document,\r
33     const fs::path& )\r
34 {\r
35     std::vector< xc::DOMElement* > elements = xe::GetElementsByQName( \r
36         document, QName( "*", "*" ) );\r
37 \r
38     std::vector< Result > results;\r
39 \r
40     foreach( xc::DOMElement* element, elements )\r
41     {\r
42         if ( !element->hasAttribute( toX( "id" ) ) )\r
43 \r
44             continue;\r
45 \r
46         std::string id = fromX( element->getAttribute( toX( "id" ) ) );\r
47 \r
48         if ( !ValidId( id ) )\r
49         {\r
50             results.push_back( \r
51                 ResultWithNodeLocation( ERROR_XML_BAD_ID_VALUE, *element )\r
52                 .AddMessageArgument( id )\r
53                 );\r
54         }       \r
55     }\r
56 \r
57     return results;\r
58 }\r
59 \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
64 {\r
65     if ( id.empty() )\r
66 \r
67         return false;\r
68 \r
69     std::string::const_iterator iter = id.begin();\r
70     bool first_code_point = true;\r
71 \r
72     while ( iter != id.end() )\r
73     {\r
74         utf8::uint32_t code_point = utf8::next( iter, id.end() );\r
75 \r
76         if ( first_code_point )\r
77         {\r
78             if ( !ValidIdNameStartChar( code_point )  )\r
79 \r
80                 return false;\r
81 \r
82             first_code_point = false;\r
83         }\r
84 \r
85         else\r
86         {\r
87             if ( !ValidIdNameChar( code_point ) )\r
88 \r
89                 return false;\r
90         }\r
91     }\r
92 \r
93     return true;\r
94 }\r
95 \r
96 \r
97 bool IdsValid::ValidIdNameStartChar( utf8::uint32_t code_point )\r
98 {\r
99     return \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
115 }\r
116 \r
117 bool IdsValid::ValidIdNameChar( utf8::uint32_t code_point )\r
118 {\r
119     return \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
127 }\r
128 \r
129 } // namespace FlightCrew\r
130 \r