GME  13
XMLAttDef.cpp
Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (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  *
00009  *      http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00023 // ---------------------------------------------------------------------------
00024 //  Includes
00025 // ---------------------------------------------------------------------------
00026 #include <xercesc/framework/XMLAttDef.hpp>
00027 #include <xercesc/util/ArrayIndexOutOfBoundsException.hpp>
00028 #include <xercesc/util/XMLUni.hpp>
00029 #include <xercesc/util/OutOfMemoryException.hpp>
00030 
00031 XERCES_CPP_NAMESPACE_BEGIN
00032 
00033 // ---------------------------------------------------------------------------
00034 //  Local const data
00035 //
00036 //  gAttTypeStrings
00037 //      A list of strings which are used to map attribute type numbers to
00038 //      attribute type names.
00039 //
00040 //  gDefAttTypesStrings
00041 //      A list of strings which are used to map default attribute type
00042 //      numbers to default attribute type names.
00043 // ---------------------------------------------------------------------------
00044 const XMLCh* const gAttTypeStrings[XMLAttDef::AttTypes_Count] =
00045 {
00046     XMLUni::fgCDATAString
00047     , XMLUni::fgIDString
00048     , XMLUni::fgIDRefString
00049     , XMLUni::fgIDRefsString
00050     , XMLUni::fgEntityString
00051     , XMLUni::fgEntitiesString
00052     , XMLUni::fgNmTokenString
00053     , XMLUni::fgNmTokensString
00054     , XMLUni::fgNotationString
00055     , XMLUni::fgEnumerationString
00056     , XMLUni::fgCDATAString
00057     , XMLUni::fgCDATAString
00058     , XMLUni::fgCDATAString
00059     , XMLUni::fgCDATAString
00060 
00061 };
00062 
00063 const XMLCh* const gDefAttTypeStrings[XMLAttDef::DefAttTypes_Count] =
00064 {
00065     XMLUni::fgDefaultString
00066     , XMLUni::fgFixedString
00067     , XMLUni::fgRequiredString
00068     , XMLUni::fgImpliedString
00069     , XMLUni::fgImpliedString
00070     , XMLUni::fgImpliedString
00071     , XMLUni::fgImpliedString
00072     , XMLUni::fgImpliedString
00073     , XMLUni::fgImpliedString
00074 };
00075 
00076 
00077 
00078 // ---------------------------------------------------------------------------
00079 //  XMLAttDef: Public, static data members
00080 // ---------------------------------------------------------------------------
00081 const unsigned int XMLAttDef::fgInvalidAttrId = 0xFFFFFFFE;
00082 
00083 
00084 // ---------------------------------------------------------------------------
00085 //  XMLAttDef: Public, static methods
00086 // ---------------------------------------------------------------------------
00087 const XMLCh* XMLAttDef::getAttTypeString(const XMLAttDef::AttTypes attrType
00088                                          , MemoryManager* const manager)
00089 {
00090     // Check for an invalid attribute type and return a null
00091     if ((attrType < AttTypes_Min) || (attrType > AttTypes_Max))
00092         ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::AttDef_BadAttType, manager);
00093     return gAttTypeStrings[attrType];
00094 }
00095 
00096 const XMLCh* XMLAttDef::getDefAttTypeString(const XMLAttDef::DefAttTypes attrType
00097                                             , MemoryManager* const manager)
00098 {
00099     // Check for an invalid attribute type and return a null
00100     if ((attrType < DefAttTypes_Min) || (attrType > DefAttTypes_Max))
00101         ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::AttDef_BadDefAttType, manager);
00102     return gDefAttTypeStrings[attrType];
00103 }
00104 
00105 
00106 // ---------------------------------------------------------------------------
00107 //  XMLAttDef: Destructor
00108 // ---------------------------------------------------------------------------
00109 XMLAttDef::~XMLAttDef()
00110 {
00111     cleanUp();
00112 }
00113 
00114 
00115 // ---------------------------------------------------------------------------
00116 //  XMLAttDef: Hidden constructors
00117 // ---------------------------------------------------------------------------
00118 XMLAttDef::XMLAttDef( const XMLAttDef::AttTypes    type
00119                     , const XMLAttDef::DefAttTypes defType
00120                     , MemoryManager* const         manager) :
00121 
00122     fDefaultType(defType)
00123     , fType(type)
00124     , fCreateReason(XMLAttDef::NoReason)
00125     , fExternalAttribute(false)
00126     , fId(XMLAttDef::fgInvalidAttrId)
00127     , fValue(0)
00128     , fEnumeration(0)
00129     , fMemoryManager(manager)
00130 {
00131 }
00132 
00133 typedef JanitorMemFunCall<XMLAttDef>    CleanupType;
00134 
00135 XMLAttDef::XMLAttDef( const XMLCh* const           attrValue
00136                     , const XMLAttDef::AttTypes    type
00137                     , const XMLAttDef::DefAttTypes defType
00138                     , const XMLCh* const           enumValues
00139                     , MemoryManager* const         manager) :
00140 
00141     fDefaultType(defType)
00142     , fType(type)
00143     , fCreateReason(XMLAttDef::NoReason)
00144     , fExternalAttribute(false)
00145     , fId(XMLAttDef::fgInvalidAttrId)
00146     , fValue(0)
00147     , fEnumeration(0)
00148     , fMemoryManager(manager)
00149 {
00150     CleanupType cleanup(this, &XMLAttDef::cleanUp);
00151 
00152     try
00153     {
00154         fValue = XMLString::replicate(attrValue, fMemoryManager);
00155         fEnumeration = XMLString::replicate(enumValues, fMemoryManager);
00156     }
00157     catch(const OutOfMemoryException&)
00158     {
00159         cleanup.release();
00160 
00161         throw;
00162     }
00163 
00164     cleanup.release();
00165 }
00166 
00167 
00168 // ---------------------------------------------------------------------------
00169 //  XMLAttDef: Private helper methods
00170 // ---------------------------------------------------------------------------
00171 void XMLAttDef::cleanUp()
00172 {
00173     if (fEnumeration)
00174         fMemoryManager->deallocate(fEnumeration);
00175 
00176     if (fValue)
00177         fMemoryManager->deallocate(fValue);
00178 }
00179 
00180 /***
00181  * Support for Serialization/De-serialization
00182  ***/
00183 
00184 IMPL_XSERIALIZABLE_NOCREATE(XMLAttDef)
00185 
00186 void XMLAttDef::serialize(XSerializeEngine& serEng)
00187 {
00188 
00189     if (serEng.isStoring())
00190     {
00191         serEng<<(int)fDefaultType;
00192         serEng<<(int)fType;
00193         serEng<<(int)fCreateReason;
00194         serEng<<fExternalAttribute;
00195         serEng.writeSize (fId);
00196 
00197         serEng.writeString(fValue);
00198         serEng.writeString(fEnumeration);
00199     }
00200     else
00201     {
00202         int i;
00203         serEng>>i;
00204         fDefaultType = (DefAttTypes) i;
00205 
00206         serEng>>i;
00207         fType = (AttTypes)i;
00208 
00209         serEng>>i;
00210         fCreateReason = (CreateReasons)i;
00211 
00212         serEng>>fExternalAttribute;
00213         serEng.readSize (fId);
00214 
00215         serEng.readString(fValue);
00216         serEng.readString(fEnumeration);
00217     }
00218 }
00219 
00220 XERCES_CPP_NAMESPACE_END