Back to index

lightning-sunbird  0.9+nobinonly
nsSchemaAttributes.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 
00042 //
00043 // nsSchemaAttribute implementation
00044 //
00046 nsSchemaAttribute::nsSchemaAttribute(nsSchema* aSchema, 
00047                                      const nsAString& aName)
00048   : nsSchemaComponentBase(aSchema), mName(aName)
00049 {
00050 }
00051 
00052 nsSchemaAttribute::~nsSchemaAttribute()
00053 {
00054 }
00055 
00056 NS_IMPL_ISUPPORTS3_CI(nsSchemaAttribute,
00057                       nsISchemaComponent,
00058                       nsISchemaAttributeComponent,
00059                       nsISchemaAttribute)
00060 
00061 
00062 /* void resolve (in nsIWebServiceErrorHandler aErrorHandler); */
00063 NS_IMETHODIMP
00064 nsSchemaAttribute::Resolve(nsIWebServiceErrorHandler* aErrorHandler)
00065 {
00066   if (mIsResolved) {
00067     return NS_OK;
00068   }
00069 
00070   mIsResolved = PR_TRUE;
00071   nsresult rv = NS_OK;
00072   if (mType && mSchema) {
00073     nsCOMPtr<nsISchemaType> type;
00074     rv = mSchema->ResolveTypePlaceholder(aErrorHandler, mType, getter_AddRefs(type));
00075     if (NS_FAILED(rv)) {
00076       return NS_ERROR_FAILURE;
00077     }
00078     mType = do_QueryInterface(type);
00079     if (!mType) {
00080       return NS_ERROR_FAILURE;
00081     }
00082     rv = mType->Resolve(aErrorHandler);
00083   }
00084 
00085   return rv;
00086 }
00087 
00088 /* void clear (); */
00089 NS_IMETHODIMP
00090 nsSchemaAttribute::Clear()
00091 {
00092   if (mIsCleared) {
00093     return NS_OK;
00094   }
00095 
00096   mIsCleared = PR_TRUE;
00097   if (mType) {
00098     mType->Clear();
00099     mType = nsnull;
00100   }
00101 
00102   return NS_OK;
00103 }
00104 
00105 /* readonly attribute AString name; */
00106 NS_IMETHODIMP
00107 nsSchemaAttribute::GetName(nsAString & aName)
00108 {
00109   aName.Assign(mName);
00110 
00111   return NS_OK;
00112 }
00113 
00114 /* readonly attribute unsigned short componentType; */
00115 NS_IMETHODIMP
00116 nsSchemaAttribute::GetComponentType(PRUint16 *aComponentType)
00117 {
00118   NS_ENSURE_ARG_POINTER(aComponentType);
00119 
00120   *aComponentType = nsISchemaAttributeComponent::COMPONENT_TYPE_ATTRIBUTE;
00121 
00122   return NS_OK;
00123 }
00124 
00125 /* readonly attribute nsISchemaSimpleType type; */
00126 NS_IMETHODIMP
00127 nsSchemaAttribute::GetType(nsISchemaSimpleType** aType)
00128 {
00129   NS_ENSURE_ARG_POINTER(aType);
00130 
00131   NS_IF_ADDREF(*aType = mType);
00132 
00133   return NS_OK;
00134 }
00135 
00136 /* readonly attribute AString defaultValue; */
00137 NS_IMETHODIMP
00138 nsSchemaAttribute::GetDefaultValue(nsAString & aDefaultValue)
00139 {
00140   aDefaultValue.Assign(mDefaultValue);
00141   
00142   return NS_OK;
00143 }
00144 
00145 /* readonly attribute AString fixedValue; */
00146 NS_IMETHODIMP
00147 nsSchemaAttribute::GetFixedValue(nsAString & aFixedValue)
00148 {
00149   aFixedValue.Assign(mFixedValue);
00150   
00151   return NS_OK;
00152 }
00153 
00154 /* readonly attribute unsigned short use; */
00155 NS_IMETHODIMP
00156 nsSchemaAttribute::GetUse(PRUint16 *aUse)
00157 {
00158   NS_ENSURE_ARG_POINTER(aUse);
00159 
00160   *aUse = mUse;
00161 
00162   return NS_OK;
00163 }
00164 
00165 NS_IMETHODIMP
00166 nsSchemaAttribute::SetType(nsISchemaSimpleType* aType)
00167 {
00168   NS_ENSURE_ARG(aType);
00169   
00170   mType = aType;
00171 
00172   return NS_OK;
00173 }
00174 
00175 NS_IMETHODIMP
00176 nsSchemaAttribute::SetConstraints(const nsAString& aDefaultValue,
00177                                   const nsAString& aFixedValue)
00178 {
00179   mDefaultValue.Assign(aDefaultValue);
00180   mFixedValue.Assign(aFixedValue);
00181 
00182   return NS_OK;
00183 }
00184  
00185 NS_IMETHODIMP
00186 nsSchemaAttribute::SetUse(PRUint16 aUse)
00187 {
00188   mUse = aUse;
00189 
00190   return NS_OK;
00191 }
00192 
00194 //
00195 // nsSchemaAttributeRef implementation
00196 //
00198 nsSchemaAttributeRef::nsSchemaAttributeRef(nsSchema* aSchema, 
00199                                            const nsAString& aRef,
00200                                            const nsAString& aRefNS)
00201   : nsSchemaComponentBase(aSchema), mRef(aRef), mRefNS(aRefNS)
00202 {
00203 }
00204 
00205 nsSchemaAttributeRef::~nsSchemaAttributeRef()
00206 {
00207 }
00208 
00209 NS_IMPL_ISUPPORTS3_CI(nsSchemaAttributeRef,
00210                       nsISchemaComponent,
00211                       nsISchemaAttributeComponent,
00212                       nsISchemaAttribute)
00213 
00214 
00215 /* void resolve (in nsIWebServiceErrorHandler aErrorHandler); */
00216 NS_IMETHODIMP
00217 nsSchemaAttributeRef::Resolve(nsIWebServiceErrorHandler* aErrorHandler)
00218 {
00219   nsresult rv = NS_OK;
00220   if (mIsResolved) {
00221     return NS_OK;
00222   }
00223   
00224   mIsResolved = PR_TRUE;
00225   if (!mAttribute && mSchema) {
00226     if (mRefNS.IsEmpty()) {
00227     mSchema->GetAttributeByName(mRef, getter_AddRefs(mAttribute));
00228     } else {
00229       // use the namespace and type
00230       nsCOMPtr<nsISchemaCollection> schemaColl;
00231       mSchema->GetCollection(getter_AddRefs(schemaColl));
00232       NS_ENSURE_STATE(schemaColl);
00233 
00234       schemaColl->GetAttribute(mRef, mRefNS, getter_AddRefs(mAttribute));
00235     }
00236   }
00237 
00238   if (mAttribute) {
00239     rv = mAttribute->Resolve(aErrorHandler);
00240   }
00241 
00242   return rv;
00243 }
00244 
00245 /* void clear (); */
00246 NS_IMETHODIMP
00247 nsSchemaAttributeRef::Clear()
00248 {
00249   if (mIsCleared) {
00250     return NS_OK;
00251   }
00252 
00253   mIsCleared = PR_TRUE;
00254   if (mAttribute) {
00255     mAttribute->Clear();
00256     mAttribute = nsnull;
00257   }
00258 
00259   return NS_OK;
00260 }
00261 
00262 /* readonly attribute AString name; */
00263 NS_IMETHODIMP
00264 nsSchemaAttributeRef::GetName(nsAString & aName)
00265 {
00266   if (!mAttribute) {
00267     return NS_ERROR_NOT_INITIALIZED;
00268   }
00269 
00270   return mAttribute->GetName(aName);
00271 }
00272 
00273 /* readonly attribute unsigned short componentType; */
00274 NS_IMETHODIMP
00275 nsSchemaAttributeRef::GetComponentType(PRUint16 *aComponentType)
00276 {
00277   NS_ENSURE_ARG_POINTER(aComponentType);
00278 
00279   *aComponentType = nsISchemaAttributeComponent::COMPONENT_TYPE_ATTRIBUTE;
00280 
00281   return NS_OK;
00282 }
00283 
00284 /* readonly attribute nsISchemaSimpleType type; */
00285 NS_IMETHODIMP
00286 nsSchemaAttributeRef::GetType(nsISchemaSimpleType * *aType)
00287 {
00288   NS_ENSURE_ARG_POINTER(aType);
00289 
00290   if (!mAttribute) {
00291     return NS_ERROR_NOT_INITIALIZED;
00292   }
00293 
00294   return mAttribute->GetType(aType);
00295 }
00296 
00297 /* readonly attribute AString defaultValue; */
00298 NS_IMETHODIMP
00299 nsSchemaAttributeRef::GetDefaultValue(nsAString & aDefaultValue)
00300 {
00301   aDefaultValue.Assign(mDefaultValue);
00302   
00303   return NS_OK;
00304 }
00305 
00306 /* readonly attribute AString fixedValue; */
00307 NS_IMETHODIMP
00308 nsSchemaAttributeRef::GetFixedValue(nsAString & aFixedValue)
00309 {
00310   aFixedValue.Assign(mFixedValue);
00311   
00312   return NS_OK;
00313 }
00314 
00315 /* readonly attribute unsigned short use; */
00316 NS_IMETHODIMP
00317 nsSchemaAttributeRef::GetUse(PRUint16 *aUse)
00318 {
00319   NS_ENSURE_ARG_POINTER(aUse);
00320 
00321   *aUse = mUse;
00322 
00323   return NS_OK;
00324 }
00325 
00326 NS_IMETHODIMP
00327 nsSchemaAttributeRef::SetConstraints(const nsAString& aDefaultValue,
00328                                      const nsAString& aFixedValue)
00329 {
00330   mDefaultValue.Assign(aDefaultValue);
00331   mFixedValue.Assign(aFixedValue);
00332 
00333   return NS_OK;
00334 }
00335  
00336 NS_IMETHODIMP
00337 nsSchemaAttributeRef::SetUse(PRUint16 aUse)
00338 {
00339   mUse = aUse;
00340 
00341   return NS_OK;
00342 }
00343 
00345 //
00346 // nsSchemaAttributeGroup implementation
00347 //
00349 nsSchemaAttributeGroup::nsSchemaAttributeGroup(nsSchema* aSchema,
00350                                                const nsAString& aName)
00351   : nsSchemaComponentBase(aSchema), mName(aName)
00352 {
00353 }
00354 
00355 nsSchemaAttributeGroup::~nsSchemaAttributeGroup()
00356 {
00357 }
00358 
00359 NS_IMPL_ISUPPORTS3_CI(nsSchemaAttributeGroup,
00360                       nsISchemaComponent,
00361                       nsISchemaAttributeComponent,
00362                       nsISchemaAttributeGroup)
00363 
00364 /* void resolve (in nsIWebServiceErrorHandler aErrorHandler); */
00365 NS_IMETHODIMP
00366 nsSchemaAttributeGroup::Resolve(nsIWebServiceErrorHandler* aErrorHandler)
00367 {
00368   if (mIsResolved) {
00369     return NS_OK;
00370   }
00371 
00372   mIsResolved = PR_TRUE;
00373   nsresult rv;
00374   PRUint32 i, count;
00375 
00376   count = mAttributes.Count();
00377   for (i = 0; i < count; ++i) {
00378     rv = mAttributes.ObjectAt(i)->Resolve(aErrorHandler);
00379     if (NS_FAILED(rv)) {
00380       return rv;
00381     }
00382   }
00383   
00384   return NS_OK;
00385 }
00386 
00387 /* void clear (); */
00388 NS_IMETHODIMP
00389 nsSchemaAttributeGroup::Clear()
00390 {
00391   if (mIsCleared) {
00392     return NS_OK;
00393   }
00394 
00395   mIsCleared = PR_TRUE;
00396 
00397   PRUint32 i, count;
00398   count = mAttributes.Count();
00399   for (i = 0; i < count; ++i) {
00400     mAttributes.ObjectAt(i)->Clear();
00401   }
00402   mAttributes.Clear();
00403   mAttributesHash.Clear();
00404 
00405   return NS_OK;
00406 }
00407 
00408 /* readonly attribute AString name; */
00409 NS_IMETHODIMP
00410 nsSchemaAttributeGroup::GetName(nsAString & aName)
00411 {
00412   aName.Assign(mName);
00413 
00414   return NS_OK;
00415 }
00416 
00417 /* readonly attribute unsigned short componentType; */
00418 NS_IMETHODIMP
00419 nsSchemaAttributeGroup::GetComponentType(PRUint16 *aComponentType)
00420 {
00421   NS_ENSURE_ARG_POINTER(aComponentType);
00422 
00423   *aComponentType = nsISchemaAttributeComponent::COMPONENT_TYPE_GROUP;
00424 
00425   return NS_OK;
00426 }
00427 
00428 /* readonly attribute PRUint32 attributeCount; */
00429 NS_IMETHODIMP
00430 nsSchemaAttributeGroup::GetAttributeCount(PRUint32* aAttributeCount)
00431 {
00432   NS_ENSURE_ARG_POINTER(aAttributeCount);
00433 
00434   *aAttributeCount = mAttributes.Count();
00435 
00436   return NS_OK;
00437 }
00438 
00439 /* nsISchemaAttributeComponent getAttributeByIndex (in PRUint32 index); */
00440 NS_IMETHODIMP
00441 nsSchemaAttributeGroup::GetAttributeByIndex(PRUint32 aIndex, 
00442                                             nsISchemaAttributeComponent** aResult)
00443 {
00444   NS_ENSURE_ARG_POINTER(aResult);
00445 
00446   if (aIndex >= (PRUint32)mAttributes.Count()) {
00447     return NS_ERROR_FAILURE;
00448   }
00449 
00450   NS_ADDREF(*aResult = mAttributes.ObjectAt(aIndex));
00451 
00452   return NS_OK;
00453 }
00454 
00455 /* nsISchemaAttributeComponent getAttributeByName (in AString name); */
00456 NS_IMETHODIMP
00457 nsSchemaAttributeGroup::GetAttributeByName(const nsAString& aName, 
00458                                            nsISchemaAttributeComponent** aResult)
00459 {
00460   NS_ENSURE_ARG_POINTER(aResult);
00461 
00462   mAttributesHash.Get(aName, aResult);
00463 
00464   return NS_OK;
00465 }
00466 
00467 NS_IMETHODIMP
00468 nsSchemaAttributeGroup::AddAttribute(nsISchemaAttributeComponent* aAttribute)
00469 {
00470   NS_ENSURE_ARG_POINTER(aAttribute);
00471 
00472   nsAutoString name;
00473   aAttribute->GetName(name);
00474 
00475   mAttributes.AppendObject(aAttribute);
00476   mAttributesHash.Put(name, aAttribute);
00477 
00478   return NS_OK;    
00479 }
00480 
00482 //
00483 // nsSchemaAttributeGroupRef implementation
00484 //
00486 nsSchemaAttributeGroupRef::nsSchemaAttributeGroupRef(nsSchema* aSchema,
00487                                                      const nsAString& aRef,
00488                                                      const nsAString& aRefNS)
00489   : nsSchemaComponentBase(aSchema), mRef(aRef), mRefNS(aRefNS)
00490 {
00491 }
00492 
00493 nsSchemaAttributeGroupRef::~nsSchemaAttributeGroupRef()
00494 {
00495 }
00496 
00497 NS_IMPL_ISUPPORTS3_CI(nsSchemaAttributeGroupRef,
00498                       nsISchemaComponent,
00499                       nsISchemaAttributeComponent,
00500                       nsISchemaAttributeGroup)
00501 
00502 /* void resolve (in nsIWebServiceErrorHandler aErrorHandler); */
00503 NS_IMETHODIMP
00504 nsSchemaAttributeGroupRef::Resolve(nsIWebServiceErrorHandler* aErrorHandler)
00505 {
00506   nsresult rv = NS_OK;
00507   if (mIsResolved) {
00508     return NS_OK;
00509   }
00510 
00511   mIsResolved = PR_TRUE;
00512   if (!mAttributeGroup && mSchema) {
00513     if (mRefNS.IsEmpty()) {
00514     mSchema->GetAttributeGroupByName(mRef, getter_AddRefs(mAttributeGroup));
00515     } else {
00516       // use the namespace and type
00517       nsCOMPtr<nsISchemaCollection> schemaColl;
00518       mSchema->GetCollection(getter_AddRefs(schemaColl));
00519       NS_ENSURE_STATE(schemaColl);
00520 
00521       // get the right schema
00522       nsCOMPtr<nsISchema> schema;
00523       schemaColl->GetSchema(mRefNS, getter_AddRefs(schema));
00524       NS_ENSURE_STATE(schema);
00525 
00526       schema->GetAttributeGroupByName(mRef, getter_AddRefs(mAttributeGroup));
00527     }
00528   }
00529 
00530   if (mAttributeGroup) {
00531     rv = mAttributeGroup->Resolve(aErrorHandler);
00532   }
00533   
00534   return rv;
00535 }
00536 
00537 /* void clear (); */
00538 NS_IMETHODIMP
00539 nsSchemaAttributeGroupRef::Clear()
00540 {
00541   if (mIsCleared) {
00542     return NS_OK;
00543   }
00544 
00545   mIsCleared = PR_TRUE;
00546   if (mAttributeGroup) {
00547     mAttributeGroup->Clear();
00548     mAttributeGroup = nsnull;
00549   }
00550 
00551   return NS_OK;
00552 }
00553 
00554 /* readonly attribute AString name; */
00555 NS_IMETHODIMP
00556 nsSchemaAttributeGroupRef::GetName(nsAString & aName)
00557 {
00558   if (!mAttributeGroup) {
00559     return NS_ERROR_NOT_INITIALIZED;
00560   }
00561 
00562   return mAttributeGroup->GetName(aName);
00563 }
00564 
00565 /* readonly attribute unsigned short componentType; */
00566 NS_IMETHODIMP
00567 nsSchemaAttributeGroupRef::GetComponentType(PRUint16 *aComponentType)
00568 {
00569   NS_ENSURE_ARG_POINTER(aComponentType);
00570 
00571   *aComponentType = nsISchemaAttributeComponent::COMPONENT_TYPE_GROUP;
00572 
00573   return NS_OK;
00574 }
00575 
00576 /* readonly attribute PRUint32 attributeCount; */
00577 NS_IMETHODIMP
00578 nsSchemaAttributeGroupRef::GetAttributeCount(PRUint32 *aAttributeCount)
00579 {
00580   NS_ENSURE_ARG_POINTER(aAttributeCount);
00581 
00582   if (!mAttributeGroup) {
00583     return NS_ERROR_NOT_INITIALIZED;
00584   }
00585   
00586   return mAttributeGroup->GetAttributeCount(aAttributeCount);
00587 }
00588 
00589 /* nsISchemaAttributeComponent getAttributeByIndex (in PRUint32 index); */
00590 NS_IMETHODIMP
00591 nsSchemaAttributeGroupRef::GetAttributeByIndex(PRUint32 index, 
00592                                                nsISchemaAttributeComponent **_retval)
00593 {
00594   NS_ENSURE_ARG_POINTER(_retval);
00595 
00596   if (!mAttributeGroup) {
00597     return NS_ERROR_NOT_INITIALIZED;
00598   }
00599   
00600   return mAttributeGroup->GetAttributeByIndex(index, _retval);
00601 }
00602 
00603 /* nsISchemaAttributeComponent getAttributeByName (in AString name); */
00604 NS_IMETHODIMP
00605 nsSchemaAttributeGroupRef::GetAttributeByName(const nsAString & name, 
00606                                               nsISchemaAttributeComponent **_retval)
00607 {
00608   NS_ENSURE_ARG_POINTER(_retval);
00609 
00610   if (!mAttributeGroup) {
00611     return NS_ERROR_NOT_INITIALIZED;
00612   }
00613 
00614   return mAttributeGroup->GetAttributeByName(name, _retval);
00615 }
00616 
00618 //
00619 // nsSchemaAnyAttribute implementation
00620 //
00622 nsSchemaAnyAttribute::nsSchemaAnyAttribute(nsSchema* aSchema)
00623   : nsSchemaComponentBase(aSchema), mProcess(PROCESS_STRICT)
00624 {
00625 }
00626 
00627 nsSchemaAnyAttribute::~nsSchemaAnyAttribute()
00628 {
00629 }
00630 
00631 NS_IMPL_ISUPPORTS3_CI(nsSchemaAnyAttribute,
00632                       nsISchemaComponent,
00633                       nsISchemaAttributeComponent,
00634                       nsISchemaAnyAttribute)
00635 
00636 /* void resolve (in nsIWebServiceErrorHandler aErrorHandler); */
00637 NS_IMETHODIMP
00638 nsSchemaAnyAttribute::Resolve(nsIWebServiceErrorHandler* aErrorHandler)
00639 {
00640   return NS_OK;
00641 }
00642 
00643 /* void clear (); */
00644 NS_IMETHODIMP
00645 nsSchemaAnyAttribute::Clear()
00646 {
00647   return NS_OK;
00648 }
00649 
00650 /* readonly attribute unsigned short componentType; */
00651 NS_IMETHODIMP
00652 nsSchemaAnyAttribute::GetComponentType(PRUint16 *aComponentType)
00653 {
00654   NS_ENSURE_ARG_POINTER(aComponentType);
00655 
00656   *aComponentType = nsISchemaAttributeComponent::COMPONENT_TYPE_ANY;
00657 
00658   return NS_OK;
00659 }
00660 
00661 /* readonly attribute AString name; */
00662 NS_IMETHODIMP
00663 nsSchemaAnyAttribute::GetName(nsAString & aName)
00664 {
00665   aName.AssignLiteral("anyAttribute");
00666 
00667   return NS_OK;
00668 }
00669 
00670 /* readonly attribute unsigned short process; */
00671 NS_IMETHODIMP
00672 nsSchemaAnyAttribute::GetProcess(PRUint16 *aProcess)
00673 {
00674   NS_ENSURE_ARG_POINTER(aProcess);
00675 
00676   *aProcess = mProcess;
00677 
00678   return NS_OK;
00679 }
00680 
00681 /* readonly attribute AString namespace; */
00682 NS_IMETHODIMP
00683 nsSchemaAnyAttribute::GetNamespace(nsAString & aNamespace)
00684 {
00685   aNamespace.Assign(mNamespace);
00686 
00687   return NS_OK;
00688 }
00689 
00690 NS_IMETHODIMP
00691 nsSchemaAnyAttribute::SetProcess(PRUint16 aProcess)
00692 {
00693   mProcess = aProcess;
00694   
00695   return NS_OK;
00696 }
00697  
00698 NS_IMETHODIMP
00699 nsSchemaAnyAttribute::SetNamespace(const nsAString& aNamespace)
00700 {
00701   mNamespace.Assign(aNamespace);
00702 
00703   return NS_OK;
00704 }