1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/parser/xml/src/nsSAXAttributes.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,332 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "nsSAXAttributes.h" 1.10 + 1.11 +NS_IMPL_ISUPPORTS(nsSAXAttributes, nsISAXAttributes, nsISAXMutableAttributes) 1.12 + 1.13 +NS_IMETHODIMP 1.14 +nsSAXAttributes::GetIndexFromName(const nsAString &aURI, 1.15 + const nsAString &aLocalName, 1.16 + int32_t *aResult) 1.17 +{ 1.18 + int32_t len = mAttrs.Length(); 1.19 + int32_t i; 1.20 + for (i = 0; i < len; ++i) { 1.21 + const SAXAttr &att = mAttrs[i]; 1.22 + if (att.localName.Equals(aLocalName) && att.uri.Equals(aURI)) { 1.23 + *aResult = i; 1.24 + return NS_OK; 1.25 + } 1.26 + } 1.27 + *aResult = -1; 1.28 + 1.29 + return NS_OK; 1.30 +} 1.31 + 1.32 +NS_IMETHODIMP 1.33 +nsSAXAttributes::GetIndexFromQName(const nsAString &aQName, int32_t *aResult) 1.34 +{ 1.35 + int32_t len = mAttrs.Length(); 1.36 + int32_t i; 1.37 + for (i = 0; i < len; ++i) { 1.38 + const SAXAttr &att = mAttrs[i]; 1.39 + if (att.qName.Equals(aQName)) { 1.40 + *aResult = i; 1.41 + return NS_OK; 1.42 + } 1.43 + } 1.44 + *aResult = -1; 1.45 + 1.46 + return NS_OK; 1.47 +} 1.48 + 1.49 +NS_IMETHODIMP 1.50 +nsSAXAttributes::GetLength(int32_t *aResult) 1.51 +{ 1.52 + *aResult = mAttrs.Length(); 1.53 + return NS_OK; 1.54 +} 1.55 + 1.56 +NS_IMETHODIMP 1.57 +nsSAXAttributes::GetLocalName(uint32_t aIndex, nsAString &aResult) 1.58 +{ 1.59 + uint32_t len = mAttrs.Length(); 1.60 + if (aIndex >= len) { 1.61 + aResult.SetIsVoid(true); 1.62 + } else { 1.63 + const SAXAttr &att = mAttrs[aIndex]; 1.64 + aResult = att.localName; 1.65 + } 1.66 + 1.67 + return NS_OK; 1.68 +} 1.69 + 1.70 +NS_IMETHODIMP 1.71 +nsSAXAttributes::GetQName(uint32_t aIndex, nsAString &aResult) 1.72 +{ 1.73 + uint32_t len = mAttrs.Length(); 1.74 + if (aIndex >= len) { 1.75 + aResult.SetIsVoid(true); 1.76 + } else { 1.77 + const SAXAttr &att = mAttrs[aIndex]; 1.78 + aResult = att.qName; 1.79 + } 1.80 + 1.81 + return NS_OK; 1.82 +} 1.83 + 1.84 +NS_IMETHODIMP 1.85 +nsSAXAttributes::GetType(uint32_t aIndex, nsAString &aResult) 1.86 +{ 1.87 + uint32_t len = mAttrs.Length(); 1.88 + if (aIndex >= len) { 1.89 + aResult.SetIsVoid(true); 1.90 + } else { 1.91 + const SAXAttr &att = mAttrs[aIndex]; 1.92 + aResult = att.type; 1.93 + } 1.94 + 1.95 + return NS_OK; 1.96 +} 1.97 + 1.98 +NS_IMETHODIMP 1.99 +nsSAXAttributes::GetTypeFromName(const nsAString &aURI, 1.100 + const nsAString &aLocalName, 1.101 + nsAString &aResult) 1.102 +{ 1.103 + int32_t index = -1; 1.104 + GetIndexFromName(aURI, aLocalName, &index); 1.105 + if (index >= 0) { 1.106 + aResult = mAttrs[index].type; 1.107 + } else { 1.108 + aResult.SetIsVoid(true); 1.109 + } 1.110 + 1.111 + return NS_OK; 1.112 +} 1.113 + 1.114 +NS_IMETHODIMP 1.115 +nsSAXAttributes::GetTypeFromQName(const nsAString &aQName, nsAString &aResult) 1.116 +{ 1.117 + int32_t index = -1; 1.118 + GetIndexFromQName(aQName, &index); 1.119 + if (index >= 0) { 1.120 + aResult = mAttrs[index].type; 1.121 + } else { 1.122 + aResult.SetIsVoid(true); 1.123 + } 1.124 + 1.125 + return NS_OK; 1.126 +} 1.127 + 1.128 +NS_IMETHODIMP 1.129 +nsSAXAttributes::GetURI(uint32_t aIndex, nsAString &aResult) 1.130 +{ 1.131 + uint32_t len = mAttrs.Length(); 1.132 + if (aIndex >= len) { 1.133 + aResult.SetIsVoid(true); 1.134 + } else { 1.135 + const SAXAttr &att = mAttrs[aIndex]; 1.136 + aResult = att.uri; 1.137 + } 1.138 + 1.139 + return NS_OK; 1.140 +} 1.141 + 1.142 +NS_IMETHODIMP 1.143 +nsSAXAttributes::GetValue(uint32_t aIndex, nsAString &aResult) 1.144 +{ 1.145 + uint32_t len = mAttrs.Length(); 1.146 + if (aIndex >= len) { 1.147 + aResult.SetIsVoid(true); 1.148 + } else { 1.149 + const SAXAttr &att = mAttrs[aIndex]; 1.150 + aResult = att.value; 1.151 + } 1.152 + 1.153 + return NS_OK; 1.154 +} 1.155 + 1.156 +NS_IMETHODIMP 1.157 +nsSAXAttributes::GetValueFromName(const nsAString &aURI, 1.158 + const nsAString &aLocalName, 1.159 + nsAString &aResult) 1.160 +{ 1.161 + int32_t index = -1; 1.162 + GetIndexFromName(aURI, aLocalName, &index); 1.163 + if (index >= 0) { 1.164 + aResult = mAttrs[index].value; 1.165 + } else { 1.166 + aResult.SetIsVoid(true); 1.167 + } 1.168 + 1.169 + return NS_OK; 1.170 +} 1.171 + 1.172 +NS_IMETHODIMP 1.173 +nsSAXAttributes::GetValueFromQName(const nsAString &aQName, 1.174 + nsAString &aResult) 1.175 +{ 1.176 + int32_t index = -1; 1.177 + GetIndexFromQName(aQName, &index); 1.178 + if (index >= 0) { 1.179 + aResult = mAttrs[index].value; 1.180 + } else { 1.181 + aResult.SetIsVoid(true); 1.182 + } 1.183 + 1.184 + return NS_OK; 1.185 +} 1.186 + 1.187 +NS_IMETHODIMP 1.188 +nsSAXAttributes::AddAttribute(const nsAString &aURI, 1.189 + const nsAString &aLocalName, 1.190 + const nsAString &aQName, 1.191 + const nsAString &aType, 1.192 + const nsAString &aValue) 1.193 +{ 1.194 + SAXAttr *att = mAttrs.AppendElement(); 1.195 + if (!att) { 1.196 + return NS_ERROR_OUT_OF_MEMORY; 1.197 + } 1.198 + 1.199 + att->uri = aURI; 1.200 + att->localName = aLocalName; 1.201 + att->qName = aQName; 1.202 + att->type = aType; 1.203 + att->value = aValue; 1.204 + 1.205 + return NS_OK; 1.206 +} 1.207 + 1.208 +NS_IMETHODIMP 1.209 +nsSAXAttributes::Clear() 1.210 +{ 1.211 + mAttrs.Clear(); 1.212 + 1.213 + return NS_OK; 1.214 +} 1.215 + 1.216 +NS_IMETHODIMP 1.217 +nsSAXAttributes::RemoveAttribute(uint32_t aIndex) 1.218 +{ 1.219 + if (aIndex >= mAttrs.Length()) { 1.220 + return NS_ERROR_FAILURE; 1.221 + } 1.222 + mAttrs.RemoveElementAt(aIndex); 1.223 + 1.224 + return NS_OK; 1.225 +} 1.226 + 1.227 +NS_IMETHODIMP 1.228 +nsSAXAttributes::SetAttributes(nsISAXAttributes *aAttributes) 1.229 +{ 1.230 + NS_ENSURE_ARG(aAttributes); 1.231 + 1.232 + nsresult rv; 1.233 + int32_t len; 1.234 + rv = aAttributes->GetLength(&len); 1.235 + NS_ENSURE_SUCCESS(rv, rv); 1.236 + 1.237 + mAttrs.Clear(); 1.238 + SAXAttr *att; 1.239 + int32_t i; 1.240 + for (i = 0; i < len; ++i) { 1.241 + att = mAttrs.AppendElement(); 1.242 + if (!att) { 1.243 + return NS_ERROR_OUT_OF_MEMORY; 1.244 + } 1.245 + rv = aAttributes->GetURI(i, att->uri); 1.246 + NS_ENSURE_SUCCESS(rv, rv); 1.247 + rv = aAttributes->GetLocalName(i, att->localName); 1.248 + NS_ENSURE_SUCCESS(rv, rv); 1.249 + rv = aAttributes->GetQName(i, att->qName); 1.250 + NS_ENSURE_SUCCESS(rv, rv); 1.251 + rv = aAttributes->GetType(i, att->type); 1.252 + NS_ENSURE_SUCCESS(rv, rv); 1.253 + rv = aAttributes->GetValue(i, att->value); 1.254 + NS_ENSURE_SUCCESS(rv, rv); 1.255 + } 1.256 + 1.257 + return NS_OK; 1.258 +} 1.259 + 1.260 +NS_IMETHODIMP 1.261 +nsSAXAttributes::SetAttribute(uint32_t aIndex, 1.262 + const nsAString &aURI, 1.263 + const nsAString &aLocalName, 1.264 + const nsAString &aQName, 1.265 + const nsAString &aType, 1.266 + const nsAString &aValue) 1.267 +{ 1.268 + if (aIndex >= mAttrs.Length()) { 1.269 + return NS_ERROR_FAILURE; 1.270 + } 1.271 + 1.272 + SAXAttr &att = mAttrs[aIndex]; 1.273 + att.uri = aURI; 1.274 + att.localName = aLocalName; 1.275 + att.qName = aQName; 1.276 + att.type = aType; 1.277 + att.value = aValue; 1.278 + 1.279 + return NS_OK; 1.280 +} 1.281 + 1.282 +NS_IMETHODIMP 1.283 +nsSAXAttributes::SetLocalName(uint32_t aIndex, const nsAString &aLocalName) 1.284 +{ 1.285 + if (aIndex >= mAttrs.Length()) { 1.286 + return NS_ERROR_FAILURE; 1.287 + } 1.288 + mAttrs[aIndex].localName = aLocalName; 1.289 + 1.290 + return NS_OK; 1.291 +} 1.292 + 1.293 +NS_IMETHODIMP 1.294 +nsSAXAttributes::SetQName(uint32_t aIndex, const nsAString &aQName) 1.295 +{ 1.296 + if (aIndex >= mAttrs.Length()) { 1.297 + return NS_ERROR_FAILURE; 1.298 + } 1.299 + mAttrs[aIndex].qName = aQName; 1.300 + 1.301 + return NS_OK; 1.302 +} 1.303 + 1.304 +NS_IMETHODIMP 1.305 +nsSAXAttributes::SetType(uint32_t aIndex, const nsAString &aType) 1.306 +{ 1.307 + if (aIndex >= mAttrs.Length()) { 1.308 + return NS_ERROR_FAILURE; 1.309 + } 1.310 + mAttrs[aIndex].type = aType; 1.311 + 1.312 + return NS_OK; 1.313 +} 1.314 + 1.315 +NS_IMETHODIMP 1.316 +nsSAXAttributes::SetURI(uint32_t aIndex, const nsAString &aURI) 1.317 +{ 1.318 + if (aIndex >= mAttrs.Length()) { 1.319 + return NS_ERROR_FAILURE; 1.320 + } 1.321 + mAttrs[aIndex].uri = aURI; 1.322 + 1.323 + return NS_OK; 1.324 +} 1.325 + 1.326 +NS_IMETHODIMP 1.327 +nsSAXAttributes::SetValue(uint32_t aIndex, const nsAString &aValue) 1.328 +{ 1.329 + if (aIndex >= mAttrs.Length()) { 1.330 + return NS_ERROR_FAILURE; 1.331 + } 1.332 + mAttrs[aIndex].value = aValue; 1.333 + 1.334 + return NS_OK; 1.335 +}