1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 /**
19  * $Id: XMLElementDecl.cpp 679359 2008-07-24 11:15:19Z borisk $
20  */
21 
22 
23 // ---------------------------------------------------------------------------
24 //  Includes
25 // ---------------------------------------------------------------------------
26 #include <xercesc/framework/XMLElementDecl.hpp>
27 #include <xercesc/util/XMLUniDefs.hpp>
28 #include <xercesc/util/XMLUni.hpp>
29 
30 #include <xercesc/validators/schema/SchemaElementDecl.hpp>
31 #include <xercesc/validators/DTD/DTDElementDecl.hpp>
32 
33 XERCES_CPP_NAMESPACE_BEGIN
34 
35 // ---------------------------------------------------------------------------
36 //  XMLElementDecl: Public, static data
37 // ---------------------------------------------------------------------------
38 const unsigned int  XMLElementDecl::fgInvalidElemId    = 0xFFFFFFFE;
39 const unsigned int  XMLElementDecl::fgPCDataElemId     = 0xFFFFFFFF;
40 const XMLCh         XMLElementDecl::fgPCDataElemName[] =
41 {
42         chPound, chLatin_P, chLatin_C, chLatin_D, chLatin_A
43     ,   chLatin_T, chLatin_A, chNull
44 };
45 
46 
47 
48 // ---------------------------------------------------------------------------
49 //  XMLElementDecl: Destructor
50 // ---------------------------------------------------------------------------
~XMLElementDecl()51 XMLElementDecl::~XMLElementDecl()
52 {
53     delete fElementName;
54 }
55 
56 // ---------------------------------------------------------------------------
57 //  XMLElementDecl: Setter Methods
58 // ---------------------------------------------------------------------------
59 void
setElementName(const XMLCh * const prefix,const XMLCh * const localPart,const int uriId)60 XMLElementDecl::setElementName(const XMLCh* const       prefix
61                             , const XMLCh* const        localPart
62                             , const int                 uriId )
63 {
64     if (fElementName)
65         fElementName->setName(prefix, localPart, uriId);
66     else
67         fElementName = new (fMemoryManager) QName(prefix, localPart, uriId, fMemoryManager);
68 }
69 
70 void
setElementName(const XMLCh * const rawName,const int uriId)71 XMLElementDecl::setElementName(const XMLCh* const       rawName
72                             , const int                 uriId )
73 {
74     if (fElementName)
75         fElementName->setName(rawName, uriId);
76     else
77         fElementName = new (fMemoryManager) QName(rawName, uriId, fMemoryManager);
78 }
79 
80 void
setElementName(const QName * const elementName)81 XMLElementDecl::setElementName(const QName* const    elementName)
82 {
83     if (fElementName)
84         fElementName->setValues(*elementName);
85     else
86         fElementName = new (fMemoryManager) QName(*elementName);
87 }
88 
89 // ---------------------------------------------------------------------------
90 //  ElementDecl: Hidden constructors
91 // ---------------------------------------------------------------------------
XMLElementDecl(MemoryManager * const manager)92 XMLElementDecl::XMLElementDecl(MemoryManager* const manager) :
93 
94     fMemoryManager(manager)
95     , fElementName(0)
96     , fCreateReason(XMLElementDecl::NoReason)
97     , fId(XMLElementDecl::fgInvalidElemId)
98     , fExternalElement(false)
99 {
100 }
101 
102 /***
103  * Support for Serialization/De-serialization
104  ***/
105 
IMPL_XSERIALIZABLE_NOCREATE(XMLElementDecl)106 IMPL_XSERIALIZABLE_NOCREATE(XMLElementDecl)
107 
108 void XMLElementDecl::serialize(XSerializeEngine& serEng)
109 {
110 
111     if (serEng.isStoring())
112     {
113         serEng<<fElementName;
114         serEng<<(int) fCreateReason;
115         serEng.writeSize (fId);
116         serEng<<fExternalElement;
117     }
118     else
119     {
120         serEng>>fElementName;
121 
122         int i;
123         serEng>>i;
124         fCreateReason=(CreateReasons)i;
125 
126         serEng.readSize (fId);
127         serEng>>fExternalElement;
128     }
129 
130 }
131 
132 void
storeElementDecl(XSerializeEngine & serEng,XMLElementDecl * const element)133 XMLElementDecl::storeElementDecl(XSerializeEngine&        serEng
134                                , XMLElementDecl*    const element)
135 {
136     if (element)
137     {
138         serEng<<(int) element->getObjectType();
139         serEng<<element;
140     }
141     else
142     {
143         serEng<<(int) UnKnown;
144     }
145 }
146 
147 XMLElementDecl*
loadElementDecl(XSerializeEngine & serEng)148 XMLElementDecl::loadElementDecl(XSerializeEngine& serEng)
149 {
150     int type;
151     serEng>>type;
152 
153     switch((XMLElementDecl::objectType)type)
154     {
155     case Schema:
156         SchemaElementDecl* schemaElementDecl;
157         serEng>>schemaElementDecl;
158         return schemaElementDecl;
159     case DTD:
160         DTDElementDecl* dtdElementDecl;
161         serEng>>dtdElementDecl;
162         return dtdElementDecl;
163     case UnKnown:
164          //fall through
165     default:
166         return 0;
167     }
168 }
169 
170 XERCES_CPP_NAMESPACE_END
171