OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_UTILITY_MEDIA_GALLERIES_IAPPS_XML_UTILS_H_ | 5 #ifndef CHROME_UTILITY_MEDIA_GALLERIES_IAPPS_XML_UTILS_H_ |
6 #define CHROME_UTILITY_MEDIA_GALLERIES_IAPPS_XML_UTILS_H_ | 6 #define CHROME_UTILITY_MEDIA_GALLERIES_IAPPS_XML_UTILS_H_ |
7 | 7 |
| 8 #include <set> |
8 #include <string> | 9 #include <string> |
9 | 10 |
10 #include "base/files/file.h" | 11 #include "base/files/file.h" |
| 12 #include "base/stl_util.h" |
11 | 13 |
12 class XmlReader; | 14 class XmlReader; |
13 | 15 |
14 namespace iapps { | 16 namespace iapps { |
15 | 17 |
16 // Like XmlReader::SkipToElement, but will advance to the next open tag if the | 18 // Like XmlReader::SkipToElement, but will advance to the next open tag if the |
17 // cursor is on a close tag. | 19 // cursor is on a close tag. |
18 bool SkipToNextElement(XmlReader* reader); | 20 bool SkipToNextElement(XmlReader* reader); |
19 | 21 |
20 // Traverse |reader| looking for a node named |name| at the current depth | 22 // Traverse |reader| looking for a node named |name| at the current depth |
21 // of |reader|. | 23 // of |reader|. |
22 bool SeekToNodeAtCurrentDepth(XmlReader* reader, const std::string& name); | 24 bool SeekToNodeAtCurrentDepth(XmlReader* reader, const std::string& name); |
23 | 25 |
24 // Search within a "dict" node for |key|. The cursor must be on the starting | 26 // Search within a "dict" node for |key|. The cursor must be on the starting |
25 // "dict" node when entering this function. | 27 // "dict" node when entering this function. |
26 bool SeekInDict(XmlReader* reader, const std::string& key); | 28 bool SeekInDict(XmlReader* reader, const std::string& key); |
27 | 29 |
28 // Get the value out of a string node. | 30 // Get the value out of a string node. |
29 bool ReadString(XmlReader* reader, std::string* result); | 31 bool ReadString(XmlReader* reader, std::string* result); |
30 | 32 |
31 // Get the value out of an integer node. | 33 // Get the value out of an integer node. |
32 bool ReadInteger(XmlReader* reader, uint64* result); | 34 bool ReadInteger(XmlReader* reader, uint64* result); |
33 | 35 |
34 // Read in the contents of the given library xml |file| and return as a string. | 36 // Read in the contents of the given library xml |file| and return as a string. |
35 std::string ReadFileAsString(base::File file); | 37 std::string ReadFileAsString(base::File file); |
36 | 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()|. |
| 43 class 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 |
37 } // namespace iapps | 87 } // namespace iapps |
38 | 88 |
39 #endif // CHROME_UTILITY_MEDIA_GALLERIES_IAPPS_XML_UTILS_H_ | 89 #endif // CHROME_UTILITY_MEDIA_GALLERIES_IAPPS_XML_UTILS_H_ |
OLD | NEW |