~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/extensions/xmlextras/ls/src/nsLSParser.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 
2
/* ***** BEGIN LICENSE BLOCK *****
 
3
 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
 
4
 *
 
5
 * The contents of this file are subject to the Netscape Public License
 
6
 * Version 1.1 (the "License"); you may not use this file except in
 
7
 * compliance with the License. You may obtain a copy of the License at
 
8
 * http://www.mozilla.org/NPL/
 
9
 *
 
10
 * Software distributed under the License is distributed on an "AS IS" basis,
 
11
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
12
 * for the specific language governing rights and limitations under the
 
13
 * License.
 
14
 *
 
15
 * The Original Code is mozilla.org code.
 
16
 *
 
17
 * The Initial Developer of the Original Code is 
 
18
 * Netscape Communications Corporation.
 
19
 * Portions created by the Initial Developer are Copyright (C) 1998
 
20
 * the Initial Developer. All Rights Reserved.
 
21
 *
 
22
 * Contributor(s):
 
23
 *   Johnny Stenback <jst@mozilla.jstenback.com> (original author)
 
24
 *
 
25
 * Alternatively, the contents of this file may be used under the terms of
 
26
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 
27
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
28
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
29
 * of those above. If you wish to allow use of your version of this file only
 
30
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
31
 * use your version of this file under the terms of the NPL, indicate your
 
32
 * decision by deleting the provisions above and replace them with the notice
 
33
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
34
 * the provisions above, a recipient may use your version of this file under
 
35
 * the terms of any one of the NPL, the GPL or the LGPL.
 
36
 *
 
37
 * ***** END LICENSE BLOCK ***** */
 
38
 
 
39
#include "nsLSParser.h"
 
40
#include "nsLSEvent.h"
 
41
#include "nsAutoPtr.h"
 
42
#include "nsXMLHttpRequest.h"
 
43
#include "nsIDOMLSParserFilter.h"
 
44
#include "nsIDOMDocument.h"
 
45
#include "nsIDOMDOMImplementation.h"
 
46
#include "nsIDOMEventListener.h"
 
47
#include "nsIDOMEventTarget.h"
 
48
 
 
49
#include "nsIDOMClassInfo.h"
 
50
 
 
51
 
 
52
// QueryInterface implementation for nsLSParser
 
53
NS_INTERFACE_MAP_BEGIN(nsLSParser)
 
54
  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMLSParser)
 
55
  NS_INTERFACE_MAP_ENTRY(nsIDOMLSParser)
 
56
  NS_INTERFACE_MAP_ENTRY(nsIDOMEventTarget)
 
57
  NS_INTERFACE_MAP_ENTRY_EXTERNAL_DOM_CLASSINFO(LSParser)
 
58
NS_INTERFACE_MAP_END
 
59
 
 
60
 
 
61
NS_IMPL_ADDREF(nsLSParser)
 
62
NS_IMPL_RELEASE(nsLSParser)
 
63
 
 
64
nsLSParser::nsLSParser()
 
65
  : mIsAsync(PR_TRUE)
 
66
{
 
67
}
 
68
 
 
69
nsLSParser::~nsLSParser()
 
70
{
 
71
}
 
72
 
 
73
NS_IMETHODIMP
 
74
nsLSParser::GetDomConfig(nsIDOMDOMConfiguration * *aDomConfig)
 
75
{
 
76
  *aDomConfig = nsnull;
 
77
 
 
78
  return NS_ERROR_NOT_IMPLEMENTED;
 
79
}
 
80
 
 
81
NS_IMETHODIMP
 
82
nsLSParser::GetFilter(nsIDOMLSParserFilter * *aFilter)
 
83
{
 
84
  *aFilter = mFilter;
 
85
 
 
86
  return NS_OK;
 
87
}
 
88
 
 
89
NS_IMETHODIMP
 
90
nsLSParser::SetFilter(nsIDOMLSParserFilter * aFilter)
 
91
{
 
92
  mFilter = aFilter;
 
93
 
 
94
  return NS_OK;
 
95
}
 
96
 
 
97
NS_IMETHODIMP
 
98
nsLSParser::GetAsync(PRBool *aAsync)
 
99
{
 
100
  *aAsync = mIsAsync;
 
101
 
 
102
  return NS_OK;
 
103
}
 
104
 
 
105
NS_IMETHODIMP
 
106
nsLSParser::GetBusy(PRBool *aBusy)
 
107
{
 
108
  PRInt32 readyState;
 
109
  nsresult rv = mXMLHttpRequest->GetReadyState(&readyState);
 
110
  NS_ENSURE_SUCCESS(rv, rv);
 
111
 
 
112
  *aBusy = readyState == 0 || readyState == 4;
 
113
 
 
114
  return NS_OK;
 
115
}
 
116
 
 
117
class nsLSParserEventListener : public nsIDOMEventListener
 
118
{
 
119
public:
 
120
  nsLSParserEventListener(nsLSParser *aParser)
 
121
    : mParser(aParser)
 
122
  {
 
123
  }
 
124
 
 
125
  virtual ~nsLSParserEventListener()
 
126
  {
 
127
  }
 
128
 
 
129
  NS_DECL_ISUPPORTS
 
130
 
 
131
  NS_DECL_NSIDOMEVENTLISTENER
 
132
 
 
133
protected:
 
134
  nsRefPtr<nsLSParser> mParser;
 
135
};
 
136
 
 
137
 
 
138
NS_IMPL_ISUPPORTS1(nsLSParserEventListener, nsIDOMEventListener)
 
139
 
 
140
NS_IMETHODIMP
 
141
nsLSParserEventListener::HandleEvent(nsIDOMEvent *aEvent)
 
142
{
 
143
  mParser->FireOnLoad();
 
144
 
 
145
  return NS_OK;
 
146
}
 
147
 
 
148
 
 
149
NS_IMETHODIMP
 
150
nsLSParser::Parse(nsIDOMLSInput *input, nsIDOMDocument **_retval)
 
151
{
 
152
  return NS_ERROR_NOT_IMPLEMENTED;
 
153
}
 
154
 
 
155
NS_IMETHODIMP
 
156
nsLSParser::ParseURI(const nsAString & uri, nsIDOMDocument **_retval)
 
157
{
 
158
  *_retval = nsnull;
 
159
 
 
160
  nsCOMPtr<nsIDOMEventListener> listener = new nsLSParserEventListener(this);
 
161
  NS_ENSURE_TRUE(listener, NS_ERROR_OUT_OF_MEMORY);
 
162
 
 
163
  nsresult rv;
 
164
  rv = mXMLHttpRequest->OpenRequest(NS_LITERAL_CSTRING("GET"),
 
165
                                    NS_ConvertUTF16toUTF8(uri),
 
166
                                    mIsAsync, EmptyString(), EmptyString());
 
167
  NS_ENSURE_SUCCESS(rv, rv);
 
168
 
 
169
  nsCOMPtr<nsIDOMEventTarget> target(do_QueryInterface(mXMLHttpRequest));
 
170
  rv = target->AddEventListener(NS_LITERAL_STRING("load"), listener, PR_FALSE);
 
171
  NS_ENSURE_SUCCESS(rv, rv);
 
172
 
 
173
  rv = mXMLHttpRequest->Send(nsnull);
 
174
 
 
175
  if (!mIsAsync && NS_SUCCEEDED(rv)) {
 
176
    rv = mXMLHttpRequest->GetResponseXML(_retval);
 
177
  }
 
178
 
 
179
  return rv;
 
180
}
 
181
 
 
182
NS_IMETHODIMP
 
183
nsLSParser::ParseWithContext(nsIDOMLSInput *input, nsIDOMNode *contextArg,
 
184
                             PRUint16 action, nsIDOMNode **_retval)
 
185
{
 
186
  return NS_ERROR_NOT_IMPLEMENTED;
 
187
}
 
188
 
 
189
NS_IMETHODIMP
 
190
nsLSParser::Abort()
 
191
{
 
192
  return mXMLHttpRequest->Abort();
 
193
}
 
194
 
 
195
NS_IMETHODIMP
 
196
nsLSParser::AddEventListener(const nsAString& aType,
 
197
                             nsIDOMEventListener *aListener,
 
198
                             PRBool aUseCapture)
 
199
{
 
200
  nsCOMArray<nsIDOMEventListener> *listeners;
 
201
 
 
202
  if (aType.Equals(NS_LITERAL_STRING("ls-load"))) {
 
203
    listeners = &mLoadListeners;
 
204
  } else if (aType.Equals(NS_LITERAL_STRING("ls-progress"))) {
 
205
    listeners = &mProgressListeners;
 
206
  } else {
 
207
    // Not a supported event
 
208
 
 
209
    return NS_OK;
 
210
  }
 
211
 
 
212
  if (listeners->IndexOf(aListener) < 0) {
 
213
    listeners->AppendObject(aListener);
 
214
  }
 
215
 
 
216
  return NS_OK;
 
217
}
 
218
 
 
219
NS_IMETHODIMP
 
220
nsLSParser::RemoveEventListener(const nsAString& aType,
 
221
                                nsIDOMEventListener *aListener,
 
222
                                PRBool aUseCapture)
 
223
{
 
224
  nsCOMArray<nsIDOMEventListener> *listeners;
 
225
 
 
226
  if (aType.Equals(NS_LITERAL_STRING("ls-load"))) {
 
227
    listeners = &mLoadListeners;
 
228
  } else if (aType.Equals(NS_LITERAL_STRING("ls-progress"))) {
 
229
    listeners = &mProgressListeners;
 
230
  } else {
 
231
    // Not a supported event
 
232
 
 
233
    return NS_OK;
 
234
  }
 
235
 
 
236
  listeners->RemoveObject(aListener);
 
237
 
 
238
  return NS_OK;
 
239
}
 
240
 
 
241
NS_IMETHODIMP
 
242
nsLSParser::DispatchEvent(nsIDOMEvent *aEvt, PRBool *_retval)
 
243
{
 
244
  return NS_ERROR_NOT_IMPLEMENTED;
 
245
}
 
246
 
 
247
void
 
248
nsLSParser::FireOnLoad()
 
249
{
 
250
  for (PRInt32 i = 0; i < mLoadListeners.Count(); ++i) {
 
251
    nsRefPtr<nsLSParserLoadEvent> event = new nsLSParserLoadEvent(this);
 
252
 
 
253
    if (event) {
 
254
      mLoadListeners.ObjectAt(i)->HandleEvent(NS_STATIC_CAST(nsLSEvent *,
 
255
                                                             event));
 
256
    }
 
257
  }
 
258
}
 
259
 
 
260
nsresult
 
261
nsLSParser::Init()
 
262
{
 
263
  mXMLHttpRequest = new nsXMLHttpRequest();
 
264
 
 
265
  return mXMLHttpRequest ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
 
266
}