Back to index

lightning-sunbird  0.9+nobinonly
nsWebDAVUtils.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 // vim:expandtab:ts=4 sw=4:
00003 /* ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Mozilla.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Oracle Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 2004
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Mike Shaver <shaver@off.net> (original author)
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "nsWebDAVInternal.h"
00041 
00042 #include "nsComponentManagerUtils.h"
00043 
00044 #include "nsIDOMElement.h"
00045 #include "nsIDOMNode.h"
00046 #include "nsIDOM3Node.h"
00047 #include "nsIDOMNodeList.h"
00048 #include "nsIDOMDocument.h"
00049 #include "nsIDOMParser.h"
00050 #include "nsContentErrors.h"
00051 
00052 #if defined(PR_LOGGING)
00053 PRLogModuleInfo *gDAVLog = nsnull;
00054 #endif
00055 
00056 nsresult
00057 NS_WD_GetElementByTagName(nsIDOMElement *parentElt, const nsAString &tagName,
00058                           nsIDOMElement **elt)
00059 {
00060     nsresult rv;
00061 
00062     nsCOMPtr<nsIDOMNodeList> list;
00063     rv = parentElt->GetElementsByTagNameNS(NS_LITERAL_STRING("DAV:"), tagName, getter_AddRefs(list));
00064     NS_ENSURE_SUCCESS(rv, rv);
00065 
00066     nsCOMPtr<nsIDOMNode> node;
00067     rv = list->Item(0, getter_AddRefs(node));
00068     NS_ENSURE_SUCCESS(rv, rv);
00069 
00070     if (!node) {
00071         return NS_OK;
00072     }
00073 
00074     return CallQueryInterface(node, elt);
00075 }
00076 
00077 nsresult
00078 NS_WD_ElementTextChildValue(nsIDOMElement *elt, const nsAString &tagName,
00079                             nsAString &value)
00080 {
00081     nsCOMPtr<nsIDOMElement> childElt;
00082     nsresult rv = NS_WD_GetElementByTagName(elt, tagName,
00083                                             getter_AddRefs(childElt));
00084     NS_ENSURE_SUCCESS(rv, rv);
00085 
00086     nsCOMPtr<nsIDOM3Node> node3 = do_QueryInterface(childElt, &rv);
00087     NS_ENSURE_SUCCESS(rv, rv);
00088 
00089     return node3->GetTextContent(value);
00090 }
00091 
00092 nsresult
00093 NS_WD_AppendElementWithNS(nsIDOMDocument *doc, nsIDOMNode *parent,
00094                           const nsAString& ns, const nsAString& tagName,
00095                           nsIDOMElement **child)
00096 {
00097     nsresult rv;
00098     nsCOMPtr<nsIDOMElement> childElt;
00099     nsCOMPtr<nsIDOMNode> junk;
00100 
00101     rv = doc->CreateElementNS(ns, tagName, getter_AddRefs(childElt));
00102     NS_ENSURE_SUCCESS(rv, rv);
00103     
00104     rv = parent->AppendChild(childElt, getter_AddRefs(junk));
00105     NS_ENSURE_SUCCESS(rv, rv);
00106     
00107     *child = childElt.get();
00108     NS_ADDREF(*child);
00109     
00110     return NS_OK;
00111 }
00112                           
00113 nsresult
00114 NS_WD_GetDocAndResponseListFromBuffer(const nsACString &buffer,
00115                                       nsIDOMDocument **xmldoc,
00116                                       nsIDOMNodeList **responseList,
00117                                       PRUint32 *length)
00118 {
00119     nsresult rv;
00120 
00121     nsCOMPtr<nsIDOMParser>
00122         parser(do_CreateInstance("@mozilla.org/xmlextras/domparser;1", &rv));
00123     NS_ENSURE_SUCCESS(rv, rv);
00124 
00125     nsPromiseFlatCString flat(buffer);
00126 
00127     PR_LOG(gDAVLog, 5, ("XML:\n\n%*s\n\n", flat.Length(), flat.get()));
00128 
00129     nsCOMPtr<nsIDOMDocument> doc;
00130     rv = parser->ParseFromBuffer(NS_REINTERPRET_CAST(const PRUint8 *,
00131                                                      flat.get()),
00132                                  flat.Length(), "text/xml",
00133                                  getter_AddRefs(doc));
00134     NS_ENSURE_SUCCESS(rv, rv);
00135 
00136     nsCOMPtr<nsIDOMNodeList> list;
00137     rv = doc->GetElementsByTagNameNS(NS_LITERAL_STRING("DAV:"),
00138                                      NS_LITERAL_STRING("response"),
00139                                      getter_AddRefs(list));
00140     NS_ENSURE_SUCCESS(rv, rv);
00141 
00142     rv = list->GetLength(length);
00143     NS_ENSURE_SUCCESS(rv, rv);
00144     NS_ENSURE_TRUE(length, NS_ERROR_UNEXPECTED);
00145 
00146     NS_ADDREF(*xmldoc = doc.get());
00147     NS_ADDREF(*responseList = list.get());
00148     return NS_OK;
00149 }