Page Speed Optimization Libraries
1.5.27.2
|
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 00053 private: 00054 int line_number_; 00055 00056 DISALLOW_COPY_AND_ASSIGN(HtmlEvent); 00057 }; 00058 00059 class HtmlStartDocumentEvent: public HtmlEvent { 00060 public: 00061 explicit HtmlStartDocumentEvent(int line_number) : HtmlEvent(line_number) {} 00062 virtual void Run(HtmlFilter* filter) { filter->StartDocument(); } 00063 virtual void ToString(GoogleString* str) { *str += "StartDocument"; } 00064 00065 private: 00066 DISALLOW_COPY_AND_ASSIGN(HtmlStartDocumentEvent); 00067 }; 00068 00069 class HtmlEndDocumentEvent: public HtmlEvent { 00070 public: 00071 explicit HtmlEndDocumentEvent(int line_number) : HtmlEvent(line_number) {} 00072 virtual void Run(HtmlFilter* filter) { filter->EndDocument(); } 00073 virtual void ToString(GoogleString* str) { *str += "EndDocument"; } 00074 00075 private: 00076 DISALLOW_COPY_AND_ASSIGN(HtmlEndDocumentEvent); 00077 }; 00078 00079 class HtmlStartElementEvent: public HtmlEvent { 00080 public: 00081 HtmlStartElementEvent(HtmlElement* element, int line_number) 00082 : HtmlEvent(line_number), 00083 element_(element) { 00084 } 00085 virtual void Run(HtmlFilter* filter) { filter->StartElement(element_); } 00086 virtual void ToString(GoogleString* str) { 00087 *str += "StartElement "; 00088 *str += element_->name_str(); 00089 } 00090 virtual HtmlElement* GetElementIfStartEvent() { return element_; } 00091 virtual HtmlElement* GetNode() { return element_; } 00092 private: 00093 HtmlElement* element_; 00094 00095 DISALLOW_COPY_AND_ASSIGN(HtmlStartElementEvent); 00096 }; 00097 00098 class HtmlEndElementEvent: public HtmlEvent { 00099 public: 00100 HtmlEndElementEvent(HtmlElement* element, int line_number) 00101 : HtmlEvent(line_number), 00102 element_(element) { 00103 } 00104 virtual void Run(HtmlFilter* filter) { filter->EndElement(element_); } 00105 virtual void ToString(GoogleString* str) { 00106 *str += "EndElement "; 00107 *str += element_->name_str(); 00108 } 00109 virtual HtmlElement* GetElementIfEndEvent() { return element_; } 00110 virtual HtmlElement* GetNode() { return element_; } 00111 private: 00112 HtmlElement* element_; 00113 00114 DISALLOW_COPY_AND_ASSIGN(HtmlEndElementEvent); 00115 }; 00116 00117 class HtmlLeafNodeEvent: public HtmlEvent { 00118 public: 00119 explicit HtmlLeafNodeEvent(int line_number) : HtmlEvent(line_number) { } 00120 virtual HtmlNode* GetNode() { return GetLeafNode(); } 00121 00122 private: 00123 DISALLOW_COPY_AND_ASSIGN(HtmlLeafNodeEvent); 00124 }; 00125 00126 class HtmlIEDirectiveEvent: public HtmlLeafNodeEvent { 00127 public: 00128 HtmlIEDirectiveEvent(HtmlIEDirectiveNode* directive, int line_number) 00129 : HtmlLeafNodeEvent(line_number), 00130 directive_(directive) { 00131 } 00132 virtual void Run(HtmlFilter* filter) { filter->IEDirective(directive_); } 00133 virtual void ToString(GoogleString* str) { 00134 *str += "IEDirective "; 00135 *str += directive_->contents(); 00136 } 00137 virtual HtmlLeafNode* GetLeafNode() { return directive_; } 00138 private: 00139 HtmlIEDirectiveNode* directive_; 00140 00141 DISALLOW_COPY_AND_ASSIGN(HtmlIEDirectiveEvent); 00142 }; 00143 00144 class HtmlCdataEvent: public HtmlLeafNodeEvent { 00145 public: 00146 HtmlCdataEvent(HtmlCdataNode* cdata, int line_number) 00147 : HtmlLeafNodeEvent(line_number), 00148 cdata_(cdata) { 00149 } 00150 virtual void Run(HtmlFilter* filter) { filter->Cdata(cdata_); } 00151 virtual void ToString(GoogleString* str) { 00152 *str += "Cdata "; 00153 *str += cdata_->contents(); 00154 } 00155 virtual HtmlLeafNode* GetLeafNode() { return cdata_; } 00156 private: 00157 HtmlCdataNode* cdata_; 00158 00159 DISALLOW_COPY_AND_ASSIGN(HtmlCdataEvent); 00160 }; 00161 00162 class HtmlCommentEvent: public HtmlLeafNodeEvent { 00163 public: 00164 HtmlCommentEvent(HtmlCommentNode* comment, int line_number) 00165 : HtmlLeafNodeEvent(line_number), 00166 comment_(comment) { 00167 } 00168 virtual void Run(HtmlFilter* filter) { filter->Comment(comment_); } 00169 virtual void ToString(GoogleString* str) { 00170 *str += "Comment "; 00171 *str += comment_->contents(); 00172 } 00173 virtual HtmlLeafNode* GetLeafNode() { return comment_; } 00174 00175 private: 00176 HtmlCommentNode* comment_; 00177 00178 DISALLOW_COPY_AND_ASSIGN(HtmlCommentEvent); 00179 }; 00180 00181 class HtmlCharactersEvent: public HtmlLeafNodeEvent { 00182 public: 00183 HtmlCharactersEvent(HtmlCharactersNode* characters, int line_number) 00184 : HtmlLeafNodeEvent(line_number), 00185 characters_(characters) { 00186 } 00187 virtual void Run(HtmlFilter* filter) { filter->Characters(characters_); } 00188 virtual void ToString(GoogleString* str) { 00189 *str += "Characters "; 00190 *str += characters_->contents(); 00191 } 00192 virtual HtmlLeafNode* GetLeafNode() { return characters_; } 00193 virtual HtmlCharactersNode* GetCharactersNode() { return characters_; } 00194 private: 00195 HtmlCharactersNode* characters_; 00196 00197 DISALLOW_COPY_AND_ASSIGN(HtmlCharactersEvent); 00198 }; 00199 00200 class HtmlDirectiveEvent: public HtmlLeafNodeEvent { 00201 public: 00202 HtmlDirectiveEvent(HtmlDirectiveNode* directive, int line_number) 00203 : HtmlLeafNodeEvent(line_number), 00204 directive_(directive) { 00205 } 00206 virtual void Run(HtmlFilter* filter) { filter->Directive(directive_); } 00207 virtual void ToString(GoogleString* str) { 00208 *str += "Directive: "; 00209 *str += directive_->contents(); 00210 } 00211 virtual HtmlLeafNode* GetLeafNode() { return directive_; } 00212 private: 00213 HtmlDirectiveNode* directive_; 00214 00215 DISALLOW_COPY_AND_ASSIGN(HtmlDirectiveEvent); 00216 }; 00217 00218 } 00219 00220 #endif ///< NET_INSTAWEB_HTMLPARSE_HTML_EVENT_H_