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 "ContentTargetsPresent.h"
\r
24 #include <FromXercesStringConverter.h>
\r
25 #include <XmlUtils.h>
\r
26 #include "Misc/Utilities.h"
\r
28 namespace FlightCrew
\r
31 std::vector< Result > ContentTargetsPresent::ValidateXml(
\r
32 const xc::DOMDocument &document,
\r
33 const fs::path &filepath )
\r
35 std::vector< Result > results;
\r
36 std::vector< ContentTargetsPresent::ContentTarget > targets =
\r
37 GetContentTargets( document, filepath.parent_path() );
\r
39 fs::path current_xhtml_path;
\r
40 boost::unordered_set< std::string > document_ids;
\r
42 foreach( const ContentTargetsPresent::ContentTarget &target, targets )
\r
44 // They're sorted by content file path, so until
\r
45 // the path changes, they all refer to the same file
\r
46 if ( target.content_file != current_xhtml_path )
\r
48 if ( !fs::exists( target.content_file ) )
\r
51 Result( ERROR_NCX_CONTENT_FILE_DOES_NOT_EXIST, target.content_node )
\r
52 .AddMessageArgument( target.raw_src_path )
\r
58 current_xhtml_path = target.content_file;
\r
59 document_ids = GetAllIdsFromDocument( current_xhtml_path );
\r
62 if ( !target.fragment.empty() && !document_ids.count( target.fragment ) )
\r
65 Result( ERROR_NCX_CONTENT_FRAGMENT_DOES_NOT_EXIST, target.content_node )
\r
66 .AddMessageArgument( target.raw_src_path )
\r
74 std::vector< ContentTargetsPresent::ContentTarget > ContentTargetsPresent::GetContentTargets(
\r
75 const xc::DOMDocument &document,
\r
76 const fs::path &folderpath )
\r
78 std::vector< xc::DOMAttr* > srcs = xe::GetAllAttributesFromElements(
\r
79 QName( "content", NCX_XML_NAMESPACE ),
\r
83 std::vector< ContentTargetsPresent::ContentTarget > targets;
\r
85 foreach( xc::DOMAttr* src, srcs )
\r
87 ContentTargetsPresent::ContentTarget target;
\r
89 target.raw_src_path = fromX( src->getValue() );
\r
90 std::string decoded_url = Util::UrlDecode( target.raw_src_path );
\r
92 fs::path resource_path =
\r
93 Util::Utf8PathToBoostPath(
\r
94 Util::UrlWithoutFragment( decoded_url ) );
\r
96 target.content_file = Util::NormalizePath( folderpath / resource_path );
\r
97 target.fragment = Util::GetUrlFragment( decoded_url );
\r
98 target.content_node = xe::GetNodeLocationInfo( *src );
\r
100 targets.push_back( target );
\r
103 std::sort( targets.begin(), targets.end() );
\r
109 boost::unordered_set< std::string > ContentTargetsPresent::GetAllIdsFromDocument(
\r
110 const fs::path &filepath )
\r
112 boost::shared_ptr< xc::DOMDocument > document;
\r
116 document = Util::LoadXhtmlDocument( filepath );
\r
119 catch ( std::exception& )
\r
121 // If the file doesn't exist or some other
\r
122 // snafu, then there are obviously no ids.
\r
123 return boost::unordered_set< std::string > ();
\r
126 std::vector< xc::DOMAttr* > ids = xe::GetAllAttributesFromElements(
\r
128 QName( "id", "*" ),
\r
131 boost::unordered_set< std::string > id_values;
\r
133 foreach( xc::DOMAttr* id, ids )
\r
135 std::string id_value = fromX( id->getValue() );
\r
137 id_values.insert( id_value );
\r
143 } // namespace FlightCrew
\r