Page Speed Optimization Libraries  1.2.24.1
net/instaweb/htmlparse/html_event.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Google Inc.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http:///www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00018 
00019 #ifndef NET_INSTAWEB_HTMLPARSE_HTML_EVENT_H_
00020 #define NET_INSTAWEB_HTMLPARSE_HTML_EVENT_H_
00021 
00022 #include "net/instaweb/util/public/basictypes.h"
00023 #include "net/instaweb/htmlparse/public/html_element.h"
00024 #include "net/instaweb/htmlparse/public/html_filter.h"
00025 #include "net/instaweb/htmlparse/public/html_node.h"
00026 #include "net/instaweb/util/public/string.h"
00027 
00028 namespace net_instaweb {
00029 
00030 class HtmlEvent {
00031  public:
00032   explicit HtmlEvent(int line_number) : line_number_(line_number) {
00033   }
00034   virtual ~HtmlEvent();
00035   virtual void Run(HtmlFilter* filter) = 0;
00036   virtual void ToString(GoogleString* buffer) = 0;
00037 
00040   virtual HtmlElement* GetElementIfStartEvent() { return NULL; }
00041 
00044   virtual HtmlElement* GetElementIfEndEvent() { return NULL; }
00045 
00046   virtual HtmlLeafNode* GetLeafNode() { return NULL; }
00047   virtual HtmlNode* GetNode() { return NULL; }
00048   virtual HtmlCharactersNode* GetCharactersNode() { return NULL; }
00049   void DebugPrint();
00050 
00051   int line_number() const { return line_number_; }
00052  private:
00053   int line_number_;
00054 
00055   DISALLOW_COPY_AND_ASSIGN(HtmlEvent);
00056 };
00057 
00058 class HtmlStartDocumentEvent: public HtmlEvent {
00059  public:
00060   explicit HtmlStartDocumentEvent(int line_number) : HtmlEvent(line_number) {}
00061   virtual void Run(HtmlFilter* filter) { filter->StartDocument(); }
00062   virtual void ToString(GoogleString* str) { *str += "StartDocument"; }
00063 
00064  private:
00065   DISALLOW_COPY_AND_ASSIGN(HtmlStartDocumentEvent);
00066 };
00067 
00068 class HtmlEndDocumentEvent: public HtmlEvent {
00069  public:
00070   explicit HtmlEndDocumentEvent(int line_number) : HtmlEvent(line_number) {}
00071   virtual void Run(HtmlFilter* filter) { filter->EndDocument(); }
00072   virtual void ToString(GoogleString* str) { *str += "EndDocument"; }
00073 
00074  private:
00075   DISALLOW_COPY_AND_ASSIGN(HtmlEndDocumentEvent);
00076 };
00077 
00078 class HtmlStartElementEvent: public HtmlEvent {
00079  public:
00080   HtmlStartElementEvent(HtmlElement* element, int line_number)
00081       : HtmlEvent(line_number),
00082         element_(element) {
00083   }
00084   virtual void Run(HtmlFilter* filter) { filter->StartElement(element_); }
00085   virtual void ToString(GoogleString* str) {
00086     *str += "StartElement ";
00087     *str += element_->name_str();
00088   }
00089   virtual HtmlElement* GetElementIfStartEvent() { return element_; }
00090   virtual HtmlElement* GetNode() { return element_; }
00091  private:
00092   HtmlElement* element_;
00093 
00094   DISALLOW_COPY_AND_ASSIGN(HtmlStartElementEvent);
00095 };
00096 
00097 class HtmlEndElementEvent: public HtmlEvent {
00098  public:
00099   HtmlEndElementEvent(HtmlElement* element, int line_number)
00100       : HtmlEvent(line_number),
00101         element_(element) {
00102   }
00103   virtual void Run(HtmlFilter* filter) { filter->EndElement(element_); }
00104   virtual void ToString(GoogleString* str) {
00105     *str += "EndElement ";
00106     *str += element_->name_str();
00107   }
00108   virtual HtmlElement* GetElementIfEndEvent() { return element_; }
00109   virtual HtmlElement* GetNode() { return element_; }
00110  private:
00111   HtmlElement* element_;
00112 
00113   DISALLOW_COPY_AND_ASSIGN(HtmlEndElementEvent);
00114 };
00115 
00116 class HtmlLeafNodeEvent: public HtmlEvent {
00117  public:
00118   explicit HtmlLeafNodeEvent(int line_number) : HtmlEvent(line_number) { }
00119   virtual HtmlNode* GetNode() { return GetLeafNode(); }
00120 
00121  private:
00122   DISALLOW_COPY_AND_ASSIGN(HtmlLeafNodeEvent);
00123 };
00124 
00125 class HtmlIEDirectiveEvent: public HtmlLeafNodeEvent {
00126  public:
00127   HtmlIEDirectiveEvent(HtmlIEDirectiveNode* directive, int line_number)
00128       : HtmlLeafNodeEvent(line_number),
00129         directive_(directive) {
00130   }
00131   virtual void Run(HtmlFilter* filter) { filter->IEDirective(directive_); }
00132   virtual void ToString(GoogleString* str) {
00133     *str += "IEDirective ";
00134     *str += directive_->contents();
00135   }
00136   virtual HtmlLeafNode* GetLeafNode() { return directive_; }
00137  private:
00138   HtmlIEDirectiveNode* directive_;
00139 
00140   DISALLOW_COPY_AND_ASSIGN(HtmlIEDirectiveEvent);
00141 };
00142 
00143 class HtmlCdataEvent: public HtmlLeafNodeEvent {
00144  public:
00145   HtmlCdataEvent(HtmlCdataNode* cdata, int line_number)
00146       : HtmlLeafNodeEvent(line_number),
00147         cdata_(cdata) {
00148   }
00149   virtual void Run(HtmlFilter* filter) { filter->Cdata(cdata_); }
00150   virtual void ToString(GoogleString* str) {
00151     *str += "Cdata ";
00152     *str += cdata_->contents();
00153   }
00154   virtual HtmlLeafNode* GetLeafNode() { return cdata_; }
00155  private:
00156   HtmlCdataNode* cdata_;
00157 
00158   DISALLOW_COPY_AND_ASSIGN(HtmlCdataEvent);
00159 };
00160 
00161 class HtmlCommentEvent: public HtmlLeafNodeEvent {
00162  public:
00163   HtmlCommentEvent(HtmlCommentNode* comment, int line_number)
00164       : HtmlLeafNodeEvent(line_number),
00165         comment_(comment) {
00166   }
00167   virtual void Run(HtmlFilter* filter) { filter->Comment(comment_); }
00168   virtual void ToString(GoogleString* str) {
00169     *str += "Comment ";
00170     *str += comment_->contents();
00171   }
00172   virtual HtmlLeafNode* GetLeafNode() { return comment_; }
00173 
00174  private:
00175   HtmlCommentNode* comment_;
00176 
00177   DISALLOW_COPY_AND_ASSIGN(HtmlCommentEvent);
00178 };
00179 
00180 class HtmlCharactersEvent: public HtmlLeafNodeEvent {
00181  public:
00182   HtmlCharactersEvent(HtmlCharactersNode* characters, int line_number)
00183       : HtmlLeafNodeEvent(line_number),
00184         characters_(characters) {
00185   }
00186   virtual void Run(HtmlFilter* filter) { filter->Characters(characters_); }
00187   virtual void ToString(GoogleString* str) {
00188     *str += "Characters ";
00189     *str += characters_->contents();
00190   }
00191   virtual HtmlLeafNode* GetLeafNode() { return characters_; }
00192   virtual HtmlCharactersNode* GetCharactersNode() { return characters_; }
00193  private:
00194   HtmlCharactersNode* characters_;
00195 
00196   DISALLOW_COPY_AND_ASSIGN(HtmlCharactersEvent);
00197 };
00198 
00199 class HtmlDirectiveEvent: public HtmlLeafNodeEvent {
00200  public:
00201   HtmlDirectiveEvent(HtmlDirectiveNode* directive, int line_number)
00202       : HtmlLeafNodeEvent(line_number),
00203         directive_(directive) {
00204   }
00205   virtual void Run(HtmlFilter* filter) { filter->Directive(directive_); }
00206   virtual void ToString(GoogleString* str) {
00207     *str += "Directive: ";
00208     *str += directive_->contents();
00209   }
00210   virtual HtmlLeafNode* GetLeafNode() { return directive_; }
00211  private:
00212   HtmlDirectiveNode* directive_;
00213 
00214   DISALLOW_COPY_AND_ASSIGN(HtmlDirectiveEvent);
00215 };
00216 
00217 }  
00218 
00219 #endif  ///< NET_INSTAWEB_HTMLPARSE_HTML_EVENT_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines