Back to index

lightning-sunbird  0.9+nobinonly
nsSchema.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.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications.
00019  * Portions created by the Initial Developer are Copyright (C) 2001
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Vidur Apparao <vidur@netscape.com> (original author)
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsSchemaPrivate.h"
00040 #include "nsReadableUtils.h"
00041 
00043 //
00044 // nsSchema implementation
00045 //
00047 nsSchema::nsSchema(nsISchemaCollection* aCollection,
00048                    nsIDOMElement* aSchemaElement) 
00049 {
00050   mCollection = aCollection;  // Weak reference
00051   
00052   if (aSchemaElement) {
00053     const nsAFlatString& empty = EmptyString();
00054 
00055     aSchemaElement->GetAttributeNS(empty, 
00056                                    NS_LITERAL_STRING("targetNamespace"), 
00057                                    mTargetNamespace);
00058     mTargetNamespace.Trim(" \r\n\t");
00059     aSchemaElement->GetNamespaceURI(mSchemaNamespace);
00060 
00061     nsAutoString elementFormDefault;
00062     aSchemaElement->GetAttributeNS(empty, 
00063                                    NS_LITERAL_STRING("elementFormDefault"), 
00064                                    elementFormDefault);
00065     elementFormDefault.Trim(" \r\n\t");
00066     mElementFormQualified = 
00067       elementFormDefault.EqualsLiteral("qualified");
00068   }
00069 }
00070 
00071 nsSchema::~nsSchema()
00072 {
00073   Clear();
00074 }
00075 
00076 NS_IMPL_ISUPPORTS2_CI(nsSchema, nsISchema, nsISchemaComponent)
00077 
00078 nsresult
00079 nsSchema::Init()
00080 {
00081   PRBool ok = mTypesHash.Init();
00082   NS_ENSURE_TRUE(ok, NS_ERROR_FAILURE);
00083 
00084   ok = mAttributesHash.Init();
00085   NS_ENSURE_TRUE(ok, NS_ERROR_FAILURE);
00086 
00087   ok = mElementsHash.Init();
00088   NS_ENSURE_TRUE(ok, NS_ERROR_FAILURE);
00089 
00090   ok = mAttributeGroupsHash.Init();
00091   NS_ENSURE_TRUE(ok, NS_ERROR_FAILURE);
00092 
00093   return mModelGroupsHash.Init() ? NS_OK : NS_ERROR_FAILURE;
00094 }
00095 
00096 /* readonly attribute wstring targetNamespace; */
00097 NS_IMETHODIMP
00098 nsSchema::GetTargetNamespace(nsAString& aTargetNamespace)
00099 {
00100   aTargetNamespace.Assign(mTargetNamespace);
00101   return NS_OK;
00102 }
00103 
00104 NS_IMETHODIMP
00105 nsSchema::GetSchemaNamespace(nsAString& aSchemaNamespace)
00106 {
00107   aSchemaNamespace.Assign(mSchemaNamespace);
00108   return NS_OK;
00109 }
00110 
00111 NS_IMETHODIMP
00112 nsSchema::Resolve(nsIWebServiceErrorHandler* aErrorHandler)
00113 {
00114   nsresult rv;
00115   PRUint32 i, count;
00116 
00117   count = mTypes.Count();
00118   for (i = 0; i < count; ++i) {
00119     rv = mTypes.ObjectAt(i)->Resolve(aErrorHandler);
00120     if (NS_FAILED(rv)) {
00121       nsAutoString name;
00122       nsresult rc = mTypes.ObjectAt(i)->GetName(name);
00123       NS_ENSURE_SUCCESS(rc, rc);
00124       
00125       nsAutoString errorMsg;
00126       errorMsg.AppendLiteral("Failure resolving schema, cannot resolve schema type \"");
00127       errorMsg.Append(name);
00128       errorMsg.AppendLiteral("\"");
00129       
00130       NS_SCHEMALOADER_FIRE_ERROR(rv, errorMsg);
00131     }
00132     NS_ENSURE_SUCCESS(rv, rv);
00133   }
00134 
00135   count = mAttributes.Count();
00136   for (i = 0; i < count; ++i) {
00137     rv = mAttributes.ObjectAt(i)->Resolve(aErrorHandler);
00138     if (NS_FAILED(rv)) {
00139       nsAutoString name;
00140       nsresult rc = mAttributes.ObjectAt(i)->GetName(name);
00141       NS_ENSURE_SUCCESS(rc, rc);
00142       
00143       nsAutoString errorMsg;
00144       errorMsg.AppendLiteral("Failure resolving schema, cannot resolve attribute \"");
00145       errorMsg.Append(name);
00146       errorMsg.AppendLiteral("\"");
00147       
00148       NS_SCHEMALOADER_FIRE_ERROR(rv, errorMsg);
00149     }
00150     NS_ENSURE_SUCCESS(rv, rv);
00151   }
00152 
00153   count = mElements.Count();
00154   for (i = 0; i < count; ++i) {
00155     rv = mElements.ObjectAt(i)->Resolve(aErrorHandler);
00156     if (NS_FAILED(rv)) {
00157       nsAutoString name;
00158       nsresult rc = mElements.ObjectAt(i)->GetName(name);
00159       NS_ENSURE_SUCCESS(rc, rc);
00160       
00161       nsAutoString errorMsg;
00162       errorMsg.AppendLiteral("Failure resolving schema, cannot resolve element \"");
00163       errorMsg.Append(name);
00164       errorMsg.AppendLiteral("\"");
00165       
00166       NS_SCHEMALOADER_FIRE_ERROR(rv, errorMsg);
00167     }
00168     NS_ENSURE_SUCCESS(rv, rv);
00169   }
00170 
00171   count = mAttributeGroups.Count();
00172   for (i = 0; i < count; ++i) {
00173     rv = mAttributeGroups.ObjectAt(i)->Resolve(aErrorHandler);
00174     if (NS_FAILED(rv)) {
00175       nsAutoString errorMsg(NS_LITERAL_STRING("Failure resolving schema, "));
00176       errorMsg.AppendLiteral("cannot resolve attribute groups");
00177       NS_SCHEMALOADER_FIRE_ERROR(rv, errorMsg);
00178 
00179       return rv;
00180     }
00181   }
00182 
00183   count = mModelGroups.Count();
00184   for (i = 0; i < count; ++i) {
00185     rv = mModelGroups.ObjectAt(i)->Resolve(aErrorHandler);
00186     if (NS_FAILED(rv)) {
00187       nsAutoString errorMsg(NS_LITERAL_STRING("Failure resolving schema, "));
00188       errorMsg.AppendLiteral("cannot resolve model group");
00189       NS_SCHEMALOADER_FIRE_ERROR(rv, errorMsg);
00190       
00191       return rv;
00192     }
00193   }
00194 
00195   return NS_OK;
00196 }
00197 
00198 NS_IMETHODIMP
00199 nsSchema::Clear()
00200 {
00201   PRUint32 i, count;
00202 
00203   count = mTypes.Count();
00204   for (i = 0; i < count; ++i) {
00205     mTypes.ObjectAt(i)->Clear();
00206   }
00207   mTypes.Clear();
00208   mTypesHash.Clear();
00209 
00210   count = mAttributes.Count();
00211   for (i = 0; i < count; ++i) {
00212     mAttributes.ObjectAt(i)->Clear();
00213   }
00214   mAttributes.Clear();
00215   mAttributesHash.Clear();
00216 
00217   count = mElements.Count();
00218   for (i = 0; i < count; ++i) {
00219     mElements.ObjectAt(i)->Clear();
00220   }
00221   mElements.Clear();
00222   mElementsHash.Clear();
00223 
00224   count = mAttributeGroups.Count();
00225   for (i = 0; i < count; ++i) {
00226     mAttributeGroups.ObjectAt(i)->Clear();
00227   }
00228   mAttributeGroups.Clear();
00229   mAttributeGroupsHash.Clear();
00230 
00231   count = mModelGroups.Count();
00232   for (i = 0; i < count; ++i) {
00233     mModelGroups.ObjectAt(i)->Clear();
00234   }
00235   mModelGroups.Clear();
00236   mModelGroupsHash.Clear();
00237 
00238   return NS_OK;
00239 }
00240 
00241 NS_IMETHODIMP
00242 nsSchema::GetTypeCount(PRUint32 *aTypeCount)
00243 {
00244   NS_ENSURE_ARG_POINTER(aTypeCount);
00245 
00246   *aTypeCount = mTypes.Count();
00247 
00248   return NS_OK;
00249 }
00250 
00251 NS_IMETHODIMP
00252 nsSchema::GetTypeByIndex(PRUint32 aIndex, nsISchemaType** aResult)
00253 {
00254   NS_ENSURE_ARG_POINTER(aResult);
00255 
00256   if (aIndex >= (PRUint32)mTypes.Count()) {
00257     return NS_ERROR_FAILURE;
00258   }
00259 
00260   NS_ADDREF(*aResult = mTypes.ObjectAt(aIndex));
00261 
00262   return NS_OK;
00263 }
00264 
00265 NS_IMETHODIMP
00266 nsSchema::GetTypeByName(const nsAString& aName, nsISchemaType** aResult)
00267 {
00268   NS_ENSURE_ARG_POINTER(aResult);
00269 
00270   mTypesHash.Get(aName, aResult);
00271 
00272   return NS_OK;
00273 }
00274 
00275 NS_IMETHODIMP
00276 nsSchema::GetAttributeCount(PRUint32 *aAttributeCount)
00277 {
00278   NS_ENSURE_ARG_POINTER(aAttributeCount);
00279 
00280   *aAttributeCount = mAttributes.Count();
00281 
00282   return NS_OK;
00283 }
00284 
00285 NS_IMETHODIMP
00286 nsSchema::GetAttributeByIndex(PRUint32 aIndex, nsISchemaAttribute** aResult)
00287 {
00288   NS_ENSURE_ARG_POINTER(aResult);
00289 
00290   if (aIndex >= (PRUint32)mAttributes.Count()) {
00291     return NS_ERROR_FAILURE;
00292   }
00293 
00294   NS_ADDREF(*aResult = mAttributes.ObjectAt(aIndex));
00295 
00296   return NS_OK;
00297 }
00298 
00299 NS_IMETHODIMP
00300 nsSchema::GetAttributeByName(const nsAString& aName,
00301                              nsISchemaAttribute** aResult)
00302 {
00303   NS_ENSURE_ARG_POINTER(aResult);
00304 
00305   mAttributesHash.Get(aName, aResult);
00306 
00307   return NS_OK;
00308 }
00309 
00310 NS_IMETHODIMP
00311 nsSchema::GetElementCount(PRUint32 *aElementCount)
00312 {
00313   NS_ENSURE_ARG_POINTER(aElementCount);
00314 
00315   *aElementCount = mElements.Count();
00316 
00317   return NS_OK;
00318 }
00319 
00320 NS_IMETHODIMP
00321 nsSchema::GetElementByIndex(PRUint32 aIndex, nsISchemaElement** aResult)
00322 {
00323   NS_ENSURE_ARG_POINTER(aResult);
00324 
00325   if (aIndex >= (PRUint32)mElements.Count()) {
00326     return NS_ERROR_FAILURE;
00327   }
00328 
00329   NS_ADDREF(*aResult = mElements.ObjectAt(aIndex));
00330 
00331   return NS_OK;
00332 }
00333 
00334 NS_IMETHODIMP
00335 nsSchema::GetElementByName(const nsAString& aName, nsISchemaElement** aResult)
00336 {
00337   NS_ENSURE_ARG_POINTER(aResult);
00338 
00339   mElementsHash.Get(aName, aResult);
00340 
00341   return NS_OK;
00342 }
00343 
00344 NS_IMETHODIMP
00345 nsSchema::GetAttributeGroupCount(PRUint32 *aAttributeGroupCount)
00346 {
00347   NS_ENSURE_ARG_POINTER(aAttributeGroupCount);
00348 
00349   *aAttributeGroupCount = mAttributeGroups.Count();
00350 
00351   return NS_OK;
00352 }
00353 
00354 NS_IMETHODIMP
00355 nsSchema::GetAttributeGroupByIndex(PRUint32 aIndex,
00356                                    nsISchemaAttributeGroup** aResult)
00357 {
00358   NS_ENSURE_ARG_POINTER(aResult);
00359 
00360   if (aIndex >= (PRUint32)mAttributeGroups.Count()) {
00361     return NS_ERROR_FAILURE;
00362   }
00363 
00364   NS_ADDREF(*aResult = mAttributeGroups.ObjectAt(aIndex));
00365 
00366   return NS_OK;
00367 }
00368 
00369 NS_IMETHODIMP
00370 nsSchema::GetAttributeGroupByName(const nsAString& aName,
00371                                   nsISchemaAttributeGroup** aResult)
00372 {
00373   NS_ENSURE_ARG_POINTER(aResult);
00374 
00375   mAttributeGroupsHash.Get(aName, aResult);
00376 
00377   return NS_OK;
00378 }
00379 
00380 NS_IMETHODIMP
00381 nsSchema::GetModelGroupCount(PRUint32 *aModelGroupCount)
00382 {
00383   NS_ENSURE_ARG_POINTER(aModelGroupCount);
00384 
00385   *aModelGroupCount = mModelGroups.Count();
00386 
00387   return NS_OK;
00388 }
00389 
00390 NS_IMETHODIMP
00391 nsSchema::GetModelGroupByIndex(PRUint32 aIndex, nsISchemaModelGroup** aResult)
00392 {
00393   NS_ENSURE_ARG_POINTER(aResult);
00394 
00395   if (aIndex >= (PRUint32)mModelGroups.Count()) {
00396     return NS_ERROR_FAILURE;
00397   }
00398 
00399   NS_ADDREF(*aResult = mModelGroups.ObjectAt(aIndex));
00400 
00401   return NS_OK;
00402 }
00403 
00404 NS_IMETHODIMP
00405 nsSchema::GetModelGroupByName(const nsAString& aName,
00406                               nsISchemaModelGroup** aResult)
00407 {
00408   NS_ENSURE_ARG_POINTER(aResult);
00409 
00410   mModelGroupsHash.Get(aName, aResult);
00411 
00412   return NS_OK;
00413 }
00414 
00415 NS_IMETHODIMP
00416 nsSchema::GetCollection(nsISchemaCollection** aResult)
00417 {
00418   NS_ENSURE_ARG_POINTER(aResult);
00419 
00420   NS_IF_ADDREF(*aResult = mCollection);
00421 
00422   return NS_OK;
00423 }
00424 
00425 NS_IMETHODIMP
00426 nsSchema::AddType(nsISchemaType* aType)
00427 {
00428   NS_ENSURE_ARG_POINTER(aType);
00429 
00430   nsAutoString name;
00431   aType->GetName(name);
00432 
00433   mTypes.AppendObject(aType);
00434   mTypesHash.Put(name, aType);
00435 
00436   return NS_OK;
00437 }
00438 
00439 NS_IMETHODIMP
00440 nsSchema::AddAttribute(nsISchemaAttribute* aAttribute)
00441 {
00442   NS_ENSURE_ARG_POINTER(aAttribute);
00443 
00444   nsAutoString name;
00445   aAttribute->GetName(name);
00446 
00447   mAttributes.AppendObject(aAttribute);
00448   mAttributesHash.Put(name, aAttribute);
00449 
00450   return NS_OK;
00451 }
00452 
00453 NS_IMETHODIMP
00454 nsSchema::AddElement(nsISchemaElement* aElement)
00455 {
00456   NS_ENSURE_ARG_POINTER(aElement);
00457 
00458   nsAutoString name;
00459   aElement->GetName(name);
00460 
00461   mElements.AppendObject(aElement);
00462   mElementsHash.Put(name, aElement);
00463 
00464   return NS_OK;
00465 }
00466 
00467 NS_IMETHODIMP
00468 nsSchema::AddAttributeGroup(nsISchemaAttributeGroup* aAttributeGroup)
00469 {
00470   NS_ENSURE_ARG_POINTER(aAttributeGroup);
00471 
00472   nsAutoString name;
00473   aAttributeGroup->GetName(name);
00474 
00475   mAttributeGroups.AppendObject(aAttributeGroup);
00476   mAttributeGroupsHash.Put(name, aAttributeGroup);
00477 
00478   return NS_OK;
00479 }
00480 
00481 NS_IMETHODIMP
00482 nsSchema::AddModelGroup(nsISchemaModelGroup* aModelGroup)
00483 {
00484   NS_ENSURE_ARG_POINTER(aModelGroup);
00485 
00486   nsAutoString name;
00487   aModelGroup->GetName(name);
00488 
00489   mModelGroups.AppendObject(aModelGroup);
00490   mModelGroupsHash.Put(name, aModelGroup);
00491 
00492   return NS_OK;
00493 }
00494 
00495 void 
00496 nsSchema::DropCollectionReference()
00497 {
00498   mCollection = nsnull;
00499 }
00500 
00501 nsresult
00502 nsSchema::ResolveTypePlaceholder(nsIWebServiceErrorHandler* aErrorHandler, 
00503                                  nsISchemaType* aPlaceholder,
00504                                  nsISchemaType** aType)
00505 {
00506   PRUint16 schemaType;
00507 
00508   aPlaceholder->GetSchemaType(&schemaType);
00509   if (schemaType == nsISchemaType::SCHEMA_TYPE_PLACEHOLDER) {
00510     nsAutoString name;
00511     aPlaceholder->GetName(name);
00512     
00513     nsresult rv = GetTypeByName(name, aType);
00514     if (NS_FAILED(rv) || !*aType) {
00515       *aType = nsnull;
00516       nsAutoString errorMsg;
00517       errorMsg.AppendLiteral("Failure resolving schema type, ");
00518       errorMsg.AppendLiteral("cannot resolve schema type place holder for \"");
00519       errorMsg.Append(name);
00520       errorMsg.AppendLiteral("\"");
00521 
00522       NS_SCHEMALOADER_FIRE_ERROR(rv, errorMsg);
00523 
00524       return NS_ERROR_FAILURE;
00525     }
00526   }
00527   else {
00528     NS_ADDREF(*aType = aPlaceholder);
00529   }
00530 
00531   return NS_OK;
00532 }