2
* Copyright 2012 Google Inc.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
// Author: sligocki@google.com (Shawn Ligocki)
19
#ifndef NET_INSTAWEB_REWRITER_PUBLIC_CSS_URL_COUNTER_H_
20
#define NET_INSTAWEB_REWRITER_PUBLIC_CSS_URL_COUNTER_H_
22
#include "net/instaweb/rewriter/public/css_tag_scanner.h"
23
#include "net/instaweb/util/public/basictypes.h"
24
#include "net/instaweb/util/public/string.h"
25
#include "net/instaweb/util/public/string_util.h"
27
namespace net_instaweb {
32
// "Transformer" that records the URLs it sees (with counts) instead of
33
// applying any transformation.
34
class CssUrlCounter : public CssTagScanner::Transformer {
36
// base_url and handler must live longer than CssUrlCounter.
37
CssUrlCounter(const GoogleUrl* base_url, MessageHandler* handler)
38
: base_url_(base_url), handler_(handler) {}
39
virtual ~CssUrlCounter();
41
// Record and count URLs in in_text. Does not reset url_counts_, so if you
42
// call this multiple times it will accumulate url_counts_ over all inputs.
43
// Returns False if CssUrlCounter found unparseable URLs.
44
bool Count(const StringPiece& in_text);
46
// Access URL occurrence counts after you've scanned a CSS file.
47
const StringIntMap& url_counts() const { return url_counts_; }
50
// CssTagScanner::Transform interface. Called indirectly by Count().
51
virtual TransformStatus Transform(GoogleString* str);
53
// Counts for how many times each URL was found in the CSS file.
54
StringIntMap url_counts_;
56
// Base URL for CSS file, needed to absolutify URLs in Transform.
57
const GoogleUrl* base_url_;
59
MessageHandler* handler_;
61
DISALLOW_COPY_AND_ASSIGN(CssUrlCounter);
64
} // namespace net_instaweb
66
#endif // NET_INSTAWEB_REWRITER_PUBLIC_CSS_URL_COUNTER_H_