Back to index

lightning-sunbird  0.9+nobinonly
nsSAXAttributes.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is Robert Sayre.
00018  *
00019  * Portions created by the Initial Developer are Copyright (C) 2005
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsSAXAttributes.h"
00039 
00040 NS_IMPL_ISUPPORTS2(nsSAXAttributes, nsISAXAttributes, nsISAXMutableAttributes)
00041 
00042 NS_IMETHODIMP
00043 nsSAXAttributes::GetIndexFromName(const nsAString &aURI,
00044                                   const nsAString &aLocalName,
00045                                   PRInt32 *aResult)
00046 {
00047   PRInt32 len = mAttrs.Length();
00048   PRInt32 i;
00049   for (i = 0; i < len; ++i) {
00050     const SAXAttr &att = mAttrs[i];
00051     if (att.localName.Equals(aLocalName) && att.uri.Equals(aURI)) {
00052       *aResult = i;
00053       return NS_OK;
00054     }
00055   }
00056   *aResult = -1;
00057 
00058   return NS_OK;
00059 }
00060 
00061 NS_IMETHODIMP
00062 nsSAXAttributes::GetIndexFromQName(const nsAString &aQName, PRInt32 *aResult)
00063 {
00064   PRInt32 len = mAttrs.Length();
00065   PRInt32 i;
00066   for (i = 0; i < len; ++i) {
00067     const SAXAttr &att = mAttrs[i];
00068     if (att.qName.Equals(aQName)) {
00069       *aResult = i;
00070       return NS_OK;
00071     }
00072   }
00073   *aResult = -1;
00074 
00075   return NS_OK;
00076 }
00077 
00078 NS_IMETHODIMP
00079 nsSAXAttributes::GetLength(PRInt32 *aResult)
00080 {
00081   *aResult = mAttrs.Length();
00082   return NS_OK;
00083 }
00084 
00085 NS_IMETHODIMP
00086 nsSAXAttributes::GetLocalName(PRUint32 aIndex, nsAString &aResult)
00087 {
00088   PRUint32 len = mAttrs.Length();
00089   if (aIndex >= len) {
00090     aResult.SetIsVoid(PR_TRUE);
00091   } else {
00092     const SAXAttr &att = mAttrs[aIndex];
00093     aResult = att.localName;
00094   }
00095 
00096   return NS_OK;
00097 }
00098 
00099 NS_IMETHODIMP
00100 nsSAXAttributes::GetQName(PRUint32 aIndex, nsAString &aResult)
00101 {
00102   PRUint32 len = mAttrs.Length();
00103   if (aIndex >= len) {
00104     aResult.SetIsVoid(PR_TRUE);
00105   } else {
00106     const SAXAttr &att = mAttrs[aIndex];
00107     aResult = att.qName;
00108   }
00109 
00110   return NS_OK;
00111 }
00112 
00113 NS_IMETHODIMP
00114 nsSAXAttributes::GetType(PRUint32 aIndex, nsAString &aResult)
00115 {
00116   PRUint32 len = mAttrs.Length();
00117   if (aIndex >= len) {
00118     aResult.SetIsVoid(PR_TRUE);
00119   } else {
00120     const SAXAttr &att = mAttrs[aIndex];
00121     aResult = att.type;
00122   }
00123 
00124   return NS_OK;
00125 }
00126 
00127 NS_IMETHODIMP
00128 nsSAXAttributes::GetTypeFromName(const nsAString &aURI,
00129                                  const nsAString &aLocalName,
00130                                  nsAString &aResult)
00131 {
00132   PRInt32 index = -1;
00133   GetIndexFromName(aURI, aLocalName, &index);
00134   if (index >= 0) {
00135     aResult = mAttrs[index].type;
00136   } else {
00137     aResult.SetIsVoid(PR_TRUE);
00138   }
00139 
00140   return NS_OK;
00141 }
00142 
00143 NS_IMETHODIMP
00144 nsSAXAttributes::GetTypeFromQName(const nsAString &aQName, nsAString &aResult)
00145 {
00146   PRInt32 index = -1;
00147   GetIndexFromQName(aQName, &index);
00148   if (index >= 0) {
00149     aResult = mAttrs[index].type;
00150   } else {
00151     aResult.SetIsVoid(PR_TRUE);
00152   }
00153 
00154   return NS_OK;
00155 }
00156 
00157 NS_IMETHODIMP
00158 nsSAXAttributes::GetURI(PRUint32 aIndex, nsAString &aResult)
00159 {
00160   PRUint32 len = mAttrs.Length();
00161   if (aIndex >= len) {
00162     aResult.SetIsVoid(PR_TRUE);
00163   } else {
00164     const SAXAttr &att = mAttrs[aIndex];
00165     aResult = att.uri;
00166   }
00167 
00168   return NS_OK;
00169 }
00170 
00171 NS_IMETHODIMP
00172 nsSAXAttributes::GetValue(PRUint32 aIndex, nsAString &aResult)
00173 {
00174   PRUint32 len = mAttrs.Length();
00175   if (aIndex >= len) {
00176     aResult.SetIsVoid(PR_TRUE);
00177   } else {
00178     const SAXAttr &att = mAttrs[aIndex];
00179     aResult = att.value;
00180   }
00181 
00182   return NS_OK;
00183 }
00184 
00185 NS_IMETHODIMP
00186 nsSAXAttributes::GetValueFromName(const nsAString &aURI,
00187                                   const nsAString &aLocalName,
00188                                   nsAString &aResult)
00189 {
00190   PRInt32 index = -1;
00191   GetIndexFromName(aURI, aLocalName, &index);
00192   if (index >= 0) {
00193     aResult = mAttrs[index].value;
00194   } else {
00195     aResult.SetIsVoid(PR_TRUE);
00196   }
00197 
00198   return NS_OK;
00199 }
00200 
00201 NS_IMETHODIMP
00202 nsSAXAttributes::GetValueFromQName(const nsAString &aQName,
00203                                    nsAString &aResult)
00204 {
00205   PRInt32 index = -1;
00206   GetIndexFromQName(aQName, &index);
00207   if (index >= 0) {
00208     aResult = mAttrs[index].value;
00209   } else {
00210     aResult.SetIsVoid(PR_TRUE);
00211   }
00212 
00213   return NS_OK;
00214 }
00215 
00216 NS_IMETHODIMP
00217 nsSAXAttributes::AddAttribute(const nsAString &aURI,
00218                               const nsAString &aLocalName,
00219                               const nsAString &aQName,
00220                               const nsAString &aType,
00221                               const nsAString &aValue)
00222 {
00223   SAXAttr *att = mAttrs.AppendElement();
00224   if (!att) {
00225     return NS_ERROR_OUT_OF_MEMORY;
00226   }
00227   
00228   att->uri = aURI;
00229   att->localName = aLocalName;
00230   att->qName = aQName;
00231   att->type = aType;
00232   att->value = aValue;
00233 
00234   return NS_OK;
00235 }
00236 
00237 NS_IMETHODIMP
00238 nsSAXAttributes::Clear()
00239 {
00240   mAttrs.Clear();
00241 
00242   return NS_OK;
00243 }
00244 
00245 NS_IMETHODIMP
00246 nsSAXAttributes::RemoveAttribute(PRUint32 aIndex)
00247 {
00248   if (aIndex >= mAttrs.Length()) {
00249     return NS_ERROR_FAILURE;
00250   }
00251   mAttrs.RemoveElementAt(aIndex);
00252 
00253   return NS_OK;
00254 }
00255 
00256 NS_IMETHODIMP
00257 nsSAXAttributes::SetAttributes(nsISAXAttributes *aAttributes)
00258 {
00259   NS_ENSURE_ARG(aAttributes);
00260 
00261   nsresult rv;
00262   PRInt32 len;
00263   rv = aAttributes->GetLength(&len);
00264   NS_ENSURE_SUCCESS(rv, rv);
00265 
00266   mAttrs.Clear();
00267   SAXAttr *att;
00268   PRInt32 i;
00269   for (i = 0; i < len; ++i) {
00270     att = mAttrs.AppendElement();
00271     if (!att) {
00272       return NS_ERROR_OUT_OF_MEMORY;
00273     }
00274     rv = aAttributes->GetURI(i, att->uri);
00275     NS_ENSURE_SUCCESS(rv, rv);
00276     rv = aAttributes->GetLocalName(i, att->localName);
00277     NS_ENSURE_SUCCESS(rv, rv);
00278     rv = aAttributes->GetQName(i, att->qName);
00279     NS_ENSURE_SUCCESS(rv, rv);
00280     rv = aAttributes->GetType(i, att->type);
00281     NS_ENSURE_SUCCESS(rv, rv);
00282     rv = aAttributes->GetValue(i, att->value);
00283     NS_ENSURE_SUCCESS(rv, rv);
00284   }
00285 
00286   return NS_OK;
00287 }
00288 
00289 NS_IMETHODIMP
00290 nsSAXAttributes::SetAttribute(PRUint32 aIndex,
00291                               const nsAString &aURI,
00292                               const nsAString &aLocalName,
00293                               const nsAString &aQName,
00294                               const nsAString &aType,
00295                               const nsAString &aValue)
00296 {
00297   if (aIndex >= mAttrs.Length()) {
00298     return NS_ERROR_FAILURE;
00299   }
00300 
00301   SAXAttr &att = mAttrs[aIndex];
00302   att.uri = aURI;
00303   att.localName = aLocalName;
00304   att.qName = aQName;
00305   att.type = aType;
00306   att.value = aValue;
00307 
00308   return NS_OK;
00309 }
00310 
00311 NS_IMETHODIMP
00312 nsSAXAttributes::SetLocalName(PRUint32 aIndex, const nsAString &aLocalName)
00313 {
00314   if (aIndex >= mAttrs.Length()) {
00315     return NS_ERROR_FAILURE;
00316   }
00317   mAttrs[aIndex].localName = aLocalName;
00318 
00319   return NS_OK;
00320 }
00321 
00322 NS_IMETHODIMP
00323 nsSAXAttributes::SetQName(PRUint32 aIndex, const nsAString &aQName)
00324 {
00325   if (aIndex >= mAttrs.Length()) {
00326     return NS_ERROR_FAILURE;
00327   }
00328   mAttrs[aIndex].qName = aQName;
00329 
00330   return NS_OK;
00331 }
00332 
00333 NS_IMETHODIMP
00334 nsSAXAttributes::SetType(PRUint32 aIndex, const nsAString &aType)
00335 {
00336   if (aIndex >= mAttrs.Length()) {
00337     return NS_ERROR_FAILURE;
00338   }
00339   mAttrs[aIndex].type = aType;
00340 
00341   return NS_OK;
00342 }
00343 
00344 NS_IMETHODIMP
00345 nsSAXAttributes::SetURI(PRUint32 aIndex, const nsAString &aURI)
00346 {
00347   if (aIndex >= mAttrs.Length()) {
00348     return NS_ERROR_FAILURE;
00349   }
00350   mAttrs[aIndex].uri = aURI;
00351 
00352   return NS_OK;
00353 }
00354 
00355 NS_IMETHODIMP
00356 nsSAXAttributes::SetValue(PRUint32 aIndex, const nsAString &aValue)
00357 {
00358   if (aIndex >= mAttrs.Length()) {
00359     return NS_ERROR_FAILURE;
00360   }
00361   mAttrs[aIndex].value = aValue;
00362 
00363   return NS_OK;
00364 }