Home | History | Annotate | Download | only in media_galleries
      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 #include "chrome/common/media_galleries/itunes_xml_utils.h"
      6 
      7 #include <string>
      8 
      9 #include "base/logging.h"
     10 #include "third_party/libxml/chromium/libxml_utils.h"
     11 
     12 namespace itunes {
     13 
     14 bool SkipToNextElement(XmlReader* reader) {
     15   if (!reader->SkipToElement()) {
     16     // SkipToElement returns false if the current node is an end element,
     17     // try to advance to the next element and then try again.
     18     if (!reader->Read() || !reader->SkipToElement())
     19       return false;
     20   }
     21   return true;
     22 }
     23 
     24 bool SeekToNodeAtCurrentDepth(XmlReader* reader, const std::string& name) {
     25   int depth = reader->Depth();
     26   do {
     27     if (!SkipToNextElement(reader) || reader->Depth() < depth)
     28       return false;
     29     DCHECK_EQ(depth, reader->Depth());
     30     if (reader->NodeName() == name)
     31       return true;
     32   } while (reader->Next());
     33 
     34   return false;
     35 }
     36 
     37 bool SeekInDict(XmlReader* reader, const std::string& key) {
     38   DCHECK_EQ("dict", reader->NodeName());
     39 
     40   int dict_content_depth = reader->Depth() + 1;
     41   // Advance past the dict node and into the body of the dictionary.
     42   if (!reader->Read())
     43     return false;
     44 
     45   while (reader->Depth() >= dict_content_depth) {
     46     if (!SeekToNodeAtCurrentDepth(reader, "key"))
     47       return false;
     48     std::string found_key;
     49     if (!reader->ReadElementContent(&found_key))
     50       return false;
     51     DCHECK_EQ(dict_content_depth, reader->Depth());
     52     if (found_key == key)
     53       return true;
     54   }
     55   return false;
     56 }
     57 
     58 }  // namespace itunes
     59