|
1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 |
|
17 #include <stringpool.h> |
|
18 #include <xml/documentparameters.h> |
|
19 #include <xml/taginfo.h> |
|
20 #include <xml/attribute.h> |
|
21 #include <e32def.h> |
|
22 |
|
23 |
|
24 #include "htmlhandler.h" |
|
25 #include "htmltagstable.h" |
|
26 #include "htmlattributestable.h" |
|
27 #include "browsertransaction.h" |
|
28 |
|
29 using namespace Xml; |
|
30 |
|
31 _LIT8 ( KStyleSheet, "stylesheet" ); |
|
32 |
|
33 CHtmlHandler::CHtmlHandler ( MBrowserTransactionObserver& aObserver ) |
|
34 : iTransObserver ( aObserver ) |
|
35 { |
|
36 |
|
37 } |
|
38 |
|
39 CHtmlHandler::~CHtmlHandler () |
|
40 { |
|
41 delete iHtmlParser; |
|
42 } |
|
43 |
|
44 CHtmlHandler* CHtmlHandler::NewLC ( MBrowserTransactionObserver& aObserver, const TDesC8& aType ) |
|
45 { |
|
46 CHtmlHandler* me = new (ELeave) CHtmlHandler ( aObserver ); |
|
47 CleanupStack::PushL ( me ); |
|
48 me->ConstructL ( aType ); |
|
49 return me; |
|
50 } |
|
51 |
|
52 CHtmlHandler* CHtmlHandler::NewL ( MBrowserTransactionObserver& aObserver, const TDesC8& aType ) |
|
53 { |
|
54 CHtmlHandler* me = CHtmlHandler::NewLC ( aObserver, aType ); |
|
55 CleanupStack::Pop ( me ); |
|
56 return me; |
|
57 } |
|
58 |
|
59 void CHtmlHandler::ConstructL ( const TDesC8& aType ) |
|
60 { |
|
61 iHtmlParser = CParser::NewL ( aType, *this ); |
|
62 |
|
63 iStringPool = iHtmlParser->StringPool(); |
|
64 iStringPool.OpenL ( HtmlTagsStringTable::Table ); // open tags table |
|
65 iStringPool.OpenL ( HtmlAttributesStringTable::Table ); // open attributes table |
|
66 |
|
67 iHtmlParser->ParseBeginL (); |
|
68 } |
|
69 |
|
70 void CHtmlHandler::ParseHtmlContentL ( const TDesC8& aContent ) |
|
71 { |
|
72 iHtmlParser->ParseL ( aContent ); |
|
73 } |
|
74 |
|
75 // Virtual functions from MContentHandler |
|
76 void CHtmlHandler::OnStartDocumentL( const RDocumentParameters& /* aDocParam */, TInt /* aErrorCode */ ) |
|
77 { |
|
78 return; |
|
79 } |
|
80 |
|
81 void CHtmlHandler::OnEndDocumentL( TInt /* aErrorCode */ ) |
|
82 { |
|
83 return; |
|
84 } |
|
85 |
|
86 void CHtmlHandler::OnStartElementL( const RTagInfo& aElement, const RAttributeArray& aAttributes, TInt /* aErrorCode */ ) |
|
87 { |
|
88 TBool parseHtml = EFalse; |
|
89 const TDesC8& KDesc = aElement.LocalName ().DesC(); |
|
90 HBufC8* des = KDesc.AllocLC(); |
|
91 TPtr8 ptr( des->Des() ); |
|
92 ptr.LowerCase (); |
|
93 RString tagName = iStringPool.OpenStringL ( ptr ); |
|
94 CleanupClosePushL ( tagName ); |
|
95 TInt index = tagName.Index ( HtmlTagsStringTable::Table ); |
|
96 CleanupStack::PopAndDestroy (); |
|
97 |
|
98 RString attributeValue; |
|
99 TInt attributeIndex = KErrNotFound; |
|
100 |
|
101 // Only body, img, input, frame, script & link tags are handled. |
|
102 switch ( index ) |
|
103 { |
|
104 case HtmlTagsStringTable::EBody: |
|
105 { |
|
106 attributeIndex = HtmlAttributesStringTable::EBackground; |
|
107 } |
|
108 break; |
|
109 case HtmlTagsStringTable::EFrame: |
|
110 case HtmlTagsStringTable::EIFrame: |
|
111 { |
|
112 parseHtml = ETrue; |
|
113 } |
|
114 case HtmlTagsStringTable::EImg: |
|
115 case HtmlTagsStringTable::EInput: |
|
116 case HtmlTagsStringTable::EScript: |
|
117 { |
|
118 attributeIndex = HtmlAttributesStringTable::ESrc; |
|
119 } |
|
120 break; |
|
121 |
|
122 case HtmlTagsStringTable::ELink: |
|
123 { |
|
124 RString relValue; |
|
125 GetAttributeValueL ( relValue, iStringPool.String ( HtmlAttributesStringTable::ERel, HtmlAttributesStringTable::Table ), aAttributes ); |
|
126 if ( !relValue.DesC().CompareF( KStyleSheet() ) ) |
|
127 { |
|
128 attributeIndex = HtmlAttributesStringTable::EHref; |
|
129 } |
|
130 } |
|
131 break; |
|
132 default: |
|
133 break; |
|
134 } |
|
135 |
|
136 // Get the attribute value corresponds to the attribute name. The attribute index is mapped depends on the tag. |
|
137 // Only required tags are handled else the attribute value will stay as KErrNotFound. |
|
138 TBool result = ( attributeIndex != KErrNotFound ) ? GetAttributeValueL ( attributeValue, iStringPool.String ( attributeIndex, HtmlAttributesStringTable::Table ), aAttributes ) : EFalse; |
|
139 |
|
140 if ( result ) |
|
141 { |
|
142 // There is an included uri that need to be downloaded. Create a new transaction |
|
143 // for the uri. |
|
144 iTransObserver.OnTransactionCreateL ( attributeValue.DesC (), parseHtml ); |
|
145 } |
|
146 CleanupStack::PopAndDestroy ();// des |
|
147 return; |
|
148 } |
|
149 |
|
150 void CHtmlHandler::OnEndElementL( const RTagInfo& aElement, TInt /* aErrorCode */ ) |
|
151 { |
|
152 return; |
|
153 } |
|
154 |
|
155 void CHtmlHandler::OnContentL( const TDesC8& aBytes, TInt aErrorCode ) |
|
156 { |
|
157 return; |
|
158 } |
|
159 |
|
160 void CHtmlHandler::OnStartPrefixMappingL( const RString& /* aPrefix */, const RString& /* aUri */, TInt /* aErrorCode */ ) |
|
161 { |
|
162 return; // do nothing |
|
163 } |
|
164 |
|
165 void CHtmlHandler::OnEndPrefixMappingL( const RString& /* aPrefix */, TInt /* aErrorCode */ ) |
|
166 { |
|
167 return; // do nothing |
|
168 } |
|
169 |
|
170 void CHtmlHandler::OnIgnorableWhiteSpaceL( const TDesC8& /* aBytes */, TInt /* aErrorCode */ ) |
|
171 { |
|
172 return; // do nothing |
|
173 } |
|
174 |
|
175 void CHtmlHandler::OnSkippedEntityL( const RString& /* aName */, TInt /* aErrorCode */ ) |
|
176 { |
|
177 return; // do nothing |
|
178 } |
|
179 |
|
180 void CHtmlHandler::OnProcessingInstructionL( const TDesC8& /* aTarget */, const TDesC8& /* aData */, TInt /* aErrorCode */ ) |
|
181 { |
|
182 return; // do nothing |
|
183 } |
|
184 |
|
185 void CHtmlHandler::OnError( TInt /* aErrorCode */ ) |
|
186 { |
|
187 return; |
|
188 } |
|
189 |
|
190 TAny* CHtmlHandler::GetExtendedInterface( const TInt32 /* aUid */ ) |
|
191 { |
|
192 return NULL; |
|
193 } |
|
194 |
|
195 |
|
196 TBool CHtmlHandler::GetAttributeValueL ( RString& aAttributeValue, const RString& aAttribute, const Xml::RAttributeArray& aAttributeArray ) |
|
197 { |
|
198 const TInt count = aAttributeArray.Count (); |
|
199 for ( TInt i = 0; i < count; ++i ) |
|
200 { |
|
201 if ( !aAttribute.DesC().CompareF ( aAttributeArray[i].Attribute().LocalName ().DesC() ) ) |
|
202 { |
|
203 aAttributeValue = aAttributeArray[i].Value (); |
|
204 return ETrue; |
|
205 } |
|
206 } |
|
207 return EFalse; |
|
208 } |
|
209 |
|
210 void CHtmlHandler::ParseEndL () |
|
211 { |
|
212 iHtmlParser->ParseEndL (); |
|
213 } |
|
214 |
|
215 |
|
216 |
|
217 |