summaryrefslogtreecommitdiffstats
path: root/chrome/browser/parsers
diff options
context:
space:
mode:
authorbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-07-23 23:18:12 +0000
committerbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-07-23 23:18:12 +0000
commitfa89702052f10d610859e2d724a7262743fe7418 (patch)
tree8a3671e7dd6924bb181cdfbeec96a6f51232a38b /chrome/browser/parsers
parent0b0f60d96020eb44648869a7199520fd5b9f75e0 (diff)
downloadchromium_src-fa89702052f10d610859e2d724a7262743fe7418.zip
chromium_src-fa89702052f10d610859e2d724a7262743fe7418.tar.gz
chromium_src-fa89702052f10d610859e2d724a7262743fe7418.tar.bz2
Adding a new parsing system to the metadata url request.
BUG=none TEST=none Original review: http://codereview.chromium.org/155803 Patch by: dhg@google.com Review URL: http://codereview.chromium.org/160059 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@21469 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/parsers')
-rw-r--r--chrome/browser/parsers/metadata_parser.cc9
-rw-r--r--chrome/browser/parsers/metadata_parser.h48
-rw-r--r--chrome/browser/parsers/metadata_parser_factory.h27
-rw-r--r--chrome/browser/parsers/metadata_parser_filebase.cc74
-rw-r--r--chrome/browser/parsers/metadata_parser_filebase.h48
-rw-r--r--chrome/browser/parsers/metadata_parser_jpeg.cc14
-rw-r--r--chrome/browser/parsers/metadata_parser_jpeg.h20
-rw-r--r--chrome/browser/parsers/metadata_parser_jpeg_factory.cc29
-rw-r--r--chrome/browser/parsers/metadata_parser_jpeg_factory.h22
-rw-r--r--chrome/browser/parsers/metadata_parser_manager.cc56
-rw-r--r--chrome/browser/parsers/metadata_parser_manager.h40
11 files changed, 387 insertions, 0 deletions
diff --git a/chrome/browser/parsers/metadata_parser.cc b/chrome/browser/parsers/metadata_parser.cc
new file mode 100644
index 0000000..0bf120a
--- /dev/null
+++ b/chrome/browser/parsers/metadata_parser.cc
@@ -0,0 +1,9 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/parsers/metadata_parser.h"
+
+const char* MetadataParser::kPropertyFilesize = "filesize";
+const char* MetadataParser::kPropertyType = "type";
+const char* MetadataParser::kPropertyTitle = "title";
diff --git a/chrome/browser/parsers/metadata_parser.h b/chrome/browser/parsers/metadata_parser.h
new file mode 100644
index 0000000..97a4e85
--- /dev/null
+++ b/chrome/browser/parsers/metadata_parser.h
@@ -0,0 +1,48 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_METADATA_PARSER_H_
+#define CHROME_BROWSER_METADATA_PARSER_H_
+
+#include <string>
+
+#include "base/file_path.h"
+
+// Allows for Iteration on the Properties of a given file.
+class MetadataPropertyIterator {
+ public:
+ MetadataPropertyIterator() {}
+
+ // Gets the next Property in the iterator. Returns false if at the end
+ // of the list.
+ virtual bool GetNext(std::string* key, std::string* value) = 0;
+
+ // Gets the number of Properties in this iterator.
+ virtual int Length() = 0;
+
+ // Checks to see if we're at the end of the list.
+ virtual bool IsEnd() = 0;
+};
+
+// Represents a single instance of parsing on a particular file.
+class MetadataParser {
+ public:
+ MetadataParser(const FilePath& path) {}
+
+ static const char* kPropertyType;
+ static const char* kPropertyFilesize;
+ static const char* kPropertyTitle;
+
+ // Does all the heavy work of parsing out the file. Blocking until complete.
+ virtual bool Parse() = 0;
+
+ // Gets a particular property found in a parse call.
+ virtual bool GetProperty(const std::string& key, std::string* value) = 0;
+
+ // Gets an interator allowing you to iterate over all the properties found
+ // in a parse call.
+ virtual MetadataPropertyIterator* GetPropertyIterator() = 0;
+};
+
+#endif // CHROME_BROWSER-METADATA_PARSER_H_
diff --git a/chrome/browser/parsers/metadata_parser_factory.h b/chrome/browser/parsers/metadata_parser_factory.h
new file mode 100644
index 0000000..c42e01d
--- /dev/null
+++ b/chrome/browser/parsers/metadata_parser_factory.h
@@ -0,0 +1,27 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_METADATA_PARSER_FACTORY_H_
+#define CHROME_BROWSER_METADATA_PARSER_FACTORY_H_
+
+#include "chrome/browser/parsers/metadata_parser.h"
+
+// Used to check to see if a parser can parse a particular file, and allows
+// for creation of a parser on a particular file.
+class MetadataParserFactory {
+ public:
+ MetadataParserFactory() {}
+
+ // Used to check to see if the parser can parse the given file. This
+ // should not do any additional reading of the file.
+ virtual bool CanParse(const FilePath& path,
+ char* bytes,
+ int bytes_size) = 0;
+
+ // Creates the parser on the given file. Creating the parser does not
+ // do any parsing on the file. Parse has to be called on the parser.
+ virtual MetadataParser* CreateParser(const FilePath& path) = 0;
+};
+
+#endif // CHROME_BROWSER_METADATA_PARSER_FACTORY_H_
diff --git a/chrome/browser/parsers/metadata_parser_filebase.cc b/chrome/browser/parsers/metadata_parser_filebase.cc
new file mode 100644
index 0000000..2d515c6
--- /dev/null
+++ b/chrome/browser/parsers/metadata_parser_filebase.cc
@@ -0,0 +1,74 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <iostream>
+#include <sstream>
+
+#include "chrome/browser/parsers/metadata_parser_filebase.h"
+
+#include "base/file_util.h"
+#include "base/string_util.h"
+
+FileMetadataParser::FileMetadataParser(const FilePath& path)
+ : MetadataParser(path){
+ path_ = path;
+}
+
+bool FileMetadataParser::Parse() {
+ std::string value;
+ int64 size;
+ if (file_util::GetFileSize(path_, &size)) {
+ properties_[MetadataParser::kPropertyFilesize] = Int64ToString(size);
+ }
+#if defined(OS_WIN)
+ value = WideToUTF8(path_.BaseName().value());
+ properties_[MetadataParser::kPropertyTitle] = value;
+#elif defined(OS_POSIX)
+ properties_[MetadataParser::kPropertyTitle] = path_.BaseName().value();
+#endif
+ return true;
+}
+
+bool FileMetadataParser::GetProperty(const std::string&key,
+ std::string* value) {
+ PropertyMap::iterator it = properties_.find(key.c_str());
+ if (it == properties_.end()) {
+ return false;
+ }
+
+ *value = properties_[key.c_str()];
+ return true;
+}
+
+MetadataPropertyIterator* FileMetadataParser::GetPropertyIterator() {
+ return new FileMetadataPropertyIterator(properties_);
+}
+
+FileMetadataPropertyIterator::FileMetadataPropertyIterator(
+ PropertyMap& properties): properties_(properties){
+ it = properties_.begin();
+}
+
+bool FileMetadataPropertyIterator::GetNext(std::string* key,
+ std::string* value) {
+ if (it == properties_.end()) {
+ return false;
+ }
+ *key = it->first;
+ *value = it->second;
+ it++;
+ return true;
+}
+
+int FileMetadataPropertyIterator::Length() {
+ return properties_.size();
+}
+
+bool FileMetadataPropertyIterator::IsEnd() {
+ if (it == properties_.end()) {
+ return true;
+ } else {
+ return false;
+ }
+}
diff --git a/chrome/browser/parsers/metadata_parser_filebase.h b/chrome/browser/parsers/metadata_parser_filebase.h
new file mode 100644
index 0000000..8b67a2d
--- /dev/null
+++ b/chrome/browser/parsers/metadata_parser_filebase.h
@@ -0,0 +1,48 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_METADATA_PARSER_FILEBASE_H_
+#define CHROME_BROWSER_METADATA_PARSER_FILEBASE_H_
+
+#include "chrome/browser/parsers/metadata_parser.h"
+
+typedef base::hash_map<std::string, std::string> PropertyMap;
+
+// Parser for the file type. Allows for parsing of files, and gets
+// properties assiciated with files.
+class FileMetadataParser : public MetadataParser {
+ public:
+ FileMetadataParser(const FilePath& path);
+
+ // Implementation of MetadataParser
+ virtual bool Parse();
+ virtual bool GetProperty(const std::string& key, std::string* value);
+
+ MetadataPropertyIterator* GetPropertyIterator();
+
+ protected:
+ PropertyMap properties_;
+ FilePath path_;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(FileMetadataParser);
+};
+
+class FileMetadataPropertyIterator : public MetadataPropertyIterator {
+ public:
+ FileMetadataPropertyIterator(PropertyMap& properties);
+
+ // Implementation of MetadataPropertyIterator
+ virtual bool GetNext(std::string* key, std::string* value);
+ virtual int Length();
+ virtual bool IsEnd();
+
+ private:
+ PropertyMap& properties_;
+ PropertyMap::iterator it;
+
+ DISALLOW_COPY_AND_ASSIGN(FileMetadataPropertyIterator);
+};
+
+#endif // CHROME_BROWSER_METADATA_PARSER_FILEBASE_H_
diff --git a/chrome/browser/parsers/metadata_parser_jpeg.cc b/chrome/browser/parsers/metadata_parser_jpeg.cc
new file mode 100644
index 0000000..3e78978
--- /dev/null
+++ b/chrome/browser/parsers/metadata_parser_jpeg.cc
@@ -0,0 +1,14 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/parsers/metadata_parser_jpeg.h"
+
+JpegMetadataParser::JpegMetadataParser(const FilePath& path)
+ : FileMetadataParser(path) {}
+
+bool JpegMetadataParser::Parse() {
+ FileMetadataParser::Parse();
+ properties_[MetadataParser::kPropertyType] = "jpeg";
+ return true;
+}
diff --git a/chrome/browser/parsers/metadata_parser_jpeg.h b/chrome/browser/parsers/metadata_parser_jpeg.h
new file mode 100644
index 0000000..449e816
--- /dev/null
+++ b/chrome/browser/parsers/metadata_parser_jpeg.h
@@ -0,0 +1,20 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_METADATA_PARSER_JPEG_H_
+#define CHROME_BROWSER_METADATA_PARSER_JPEG_H_
+
+#include "chrome/browser/parsers/metadata_parser_filebase.h"
+
+class JpegMetadataParser : public FileMetadataParser {
+ public:
+ JpegMetadataParser(const FilePath& path);
+ // Implementation of MetadataParser
+ virtual bool Parse();
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(JpegMetadataParser);
+};
+
+#endif // CHROME_BROWSER_METADATA_PARSER_JPEG_H_
diff --git a/chrome/browser/parsers/metadata_parser_jpeg_factory.cc b/chrome/browser/parsers/metadata_parser_jpeg_factory.cc
new file mode 100644
index 0000000..e49cbcb
--- /dev/null
+++ b/chrome/browser/parsers/metadata_parser_jpeg_factory.cc
@@ -0,0 +1,29 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/parsers/metadata_parser_jpeg_factory.h"
+
+#include "chrome/browser/parsers/metadata_parser_jpeg.h"
+#include "base/logging.h"
+#include "base/string_util.h"
+
+MetadataParserJpegFactory::MetadataParserJpegFactory()
+ : MetadataParserFactory(){}
+
+bool MetadataParserJpegFactory::CanParse(const FilePath& path,
+ char* bytes,
+ int bytes_size) {
+#if defined(OS_WIN)
+ FilePath::StringType ext = UTF8ToWide(std::string(".jpg"));
+#elif defined(OS_POSIX)
+ FilePath::StringType ext = ".jpg";
+#endif
+ return path.MatchesExtension(ext);
+}
+
+MetadataParser* MetadataParserJpegFactory::CreateParser(const FilePath& path) {
+ JpegMetadataParser* parser;
+ parser = new JpegMetadataParser(path);
+ return parser;
+}
diff --git a/chrome/browser/parsers/metadata_parser_jpeg_factory.h b/chrome/browser/parsers/metadata_parser_jpeg_factory.h
new file mode 100644
index 0000000..48c059b
--- /dev/null
+++ b/chrome/browser/parsers/metadata_parser_jpeg_factory.h
@@ -0,0 +1,22 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_METADATA_PARSER_JPEG_FACTORY_H_
+#define CHROME_BROWSER_METADATA_PARSER_JPEG_FACTORY_H_
+
+#include "chrome/browser/parsers/metadata_parser_factory.h"
+
+class MetadataParserJpegFactory : public MetadataParserFactory {
+ public:
+ MetadataParserJpegFactory();
+
+ // Implementation of MetadataParserFactory
+ virtual bool CanParse(const FilePath& path, char* bytes, int bytes_size);
+ virtual MetadataParser* CreateParser(const FilePath& path);
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(MetadataParserJpegFactory);
+};
+
+#endif // CHROME_BROWSER_METADATA_PARSER_JPEG_FACTORY_H_
diff --git a/chrome/browser/parsers/metadata_parser_manager.cc b/chrome/browser/parsers/metadata_parser_manager.cc
new file mode 100644
index 0000000..3efce7d
--- /dev/null
+++ b/chrome/browser/parsers/metadata_parser_manager.cc
@@ -0,0 +1,56 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/parsers/metadata_parser_manager.h"
+
+#include "base/logging.h"
+#include "base/file_util.h"
+#include "base/singleton.h"
+#include "base/stl_util-inl.h"
+#include "build/build_config.h"
+#include "chrome/browser/parsers/metadata_parser_factory.h"
+#include "chrome/browser/parsers/metadata_parser_jpeg_factory.h"
+
+static const int kAmountToRead = 256;
+
+// Gets the singleton
+MetadataParserManager* MetadataParserManager::Get() {
+ // Uses the LeakySingletonTrait because cleanup is optional.
+ return
+ Singleton<MetadataParserManager,
+ LeakySingletonTraits<MetadataParserManager> >::get();
+ }
+
+bool MetadataParserManager::RegisterParserFactory(
+ MetadataParserFactory* parser) {
+ factories_.push_back(parser);
+ return true;
+}
+
+MetadataParserManager::MetadataParserManager() {
+ MetadataParserJpegFactory *factory = new MetadataParserJpegFactory();
+ RegisterParserFactory(factory);
+}
+
+MetadataParserManager::~MetadataParserManager() {
+ STLDeleteContainerPointers(factories_.begin(), factories_.end());
+}
+
+MetadataParser* MetadataParserManager::GetParserForFile(const FilePath& path) {
+
+ char buffer[kAmountToRead];
+ int amount_read = 0;
+ DLOG(ERROR) << path.value();
+ amount_read = file_util::ReadFile(path, buffer, sizeof(buffer));
+ if (amount_read <= 0) {
+ DLOG(ERROR) << "Unable to read file";
+ return NULL;
+ }
+ for (size_t x = 0; x < factories_.size(); ++x) {
+ if (factories_[x]->CanParse(path, buffer, amount_read)) {
+ return factories_[x]->CreateParser(path);
+ }
+ }
+ return NULL;
+}
diff --git a/chrome/browser/parsers/metadata_parser_manager.h b/chrome/browser/parsers/metadata_parser_manager.h
new file mode 100644
index 0000000..3249eb2
--- /dev/null
+++ b/chrome/browser/parsers/metadata_parser_manager.h
@@ -0,0 +1,40 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_METADATA_PARSER_MANAGER_H_
+#define CHROME_BROWSER_METADATA_PARSER_MANAGER_H_
+
+#include <vector>
+
+#include "base/basictypes.h"
+
+class MetadataParserFactory;
+class FilePath;
+class MetadataParser;
+
+// Metadata Parser manager is used to find the correct parser for a
+// given file. Allows parsers to register themselves.
+class MetadataParserManager {
+ public:
+ // Creates a new MetadataParserManager.
+ MetadataParserManager();
+ ~MetadataParserManager();
+
+ // Gets the singleton
+ static MetadataParserManager* Get();
+
+ // Adds a new Parser to the manager, when requests come in for a parser
+ // the manager will loop through the list of parsers, and query each.
+ bool RegisterParserFactory(MetadataParserFactory* parser);
+
+ // Returns a new metadata parser for a given file.
+ MetadataParser* GetParserForFile(const FilePath& path);
+
+ private:
+ std::vector<MetadataParserFactory*> factories_;
+
+ DISALLOW_COPY_AND_ASSIGN(MetadataParserManager);
+};
+
+#endif // CHROME_BROWSER_METADATA_PARSER_MANAGER_H_