1// Copyright 2013 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#ifndef CHROME_UTILITY_MEDIA_GALLERIES_IAPPS_XML_UTILS_H_ 6#define CHROME_UTILITY_MEDIA_GALLERIES_IAPPS_XML_UTILS_H_ 7 8#include <set> 9#include <string> 10 11#include "base/files/file.h" 12#include "base/stl_util.h" 13 14class XmlReader; 15 16namespace iapps { 17 18// Like XmlReader::SkipToElement, but will advance to the next open tag if the 19// cursor is on a close tag. 20bool SkipToNextElement(XmlReader* reader); 21 22// Traverse |reader| looking for a node named |name| at the current depth 23// of |reader|. 24bool SeekToNodeAtCurrentDepth(XmlReader* reader, const std::string& name); 25 26// Search within a "dict" node for |key|. The cursor must be on the starting 27// "dict" node when entering this function. 28bool SeekInDict(XmlReader* reader, const std::string& key); 29 30// Get the value out of a string node. 31bool ReadString(XmlReader* reader, std::string* result); 32 33// Get the value out of an integer node. 34bool ReadInteger(XmlReader* reader, uint64* result); 35 36// Read in the contents of the given library xml |file| and return as a string. 37std::string ReadFileAsString(base::File file); 38 39// Contains the common code and main loop for reading the key/values 40// of an XML dict. The derived class must implement |HandleKeyImpl()| 41// which is called with each key, and may re-implement |ShouldLoop()|, 42// |FinishedOk()| and/or |AllowRepeats()|. 43class XmlDictReader { 44 public: 45 explicit XmlDictReader(XmlReader* reader); 46 virtual ~XmlDictReader(); 47 48 // The main loop of this class. Reads all the keys in the 49 // current element and calls |HandleKey()| with each. 50 bool Read(); 51 52 // Re-implemented by derived class if it should bail from the 53 // loop earlier, such as if it encountered all required fields. 54 virtual bool ShouldLoop(); 55 56 // Called by |Read()| with each key. Calls derived |HandleKeyImpl()|. 57 bool HandleKey(const std::string& key); 58 59 virtual bool HandleKeyImpl(const std::string& key) = 0; 60 61 // Re-implemented by the derived class (to return true) if 62 // it should allow fields to be repeated, but skipped. 63 virtual bool AllowRepeats(); 64 65 // Re-implemented by derived class if it should test for required 66 // fields instead of just returning true. 67 virtual bool FinishedOk(); 68 69 // A convenience function for the derived classes. 70 // Skips to next element. 71 bool SkipToNext(); 72 73 // A convenience function for the derived classes. 74 // Used to test if all required keys have been encountered. 75 bool Found(const std::string& key) const; 76 77 protected: 78 XmlReader* reader_; 79 80 private: 81 // The keys that the reader has run into in this element. 82 std::set<std::string> found_; 83 84 DISALLOW_COPY_AND_ASSIGN(XmlDictReader); 85}; 86 87} // namespace iapps 88 89#endif // CHROME_UTILITY_MEDIA_GALLERIES_IAPPS_XML_UTILS_H_ 90