1 /*
2  * Copyright 2006-2008 The FLWOR Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "stdafx.h"
17 
18 #ifndef ZORBA_TYPEIDENT_IMPL_H
19 #define ZORBA_TYPEIDENT_IMPL_H
20 
21 #include <zorba/typeident.h>
22 
23 #include "diagnostics/assert.h"
24 
25 namespace zorba {
26 
TypeIdentifier()27 TypeIdentifier::TypeIdentifier()
28   :
29   m_kind(IdentTypes::INVALID_TYPE),
30   m_quantifier(IdentTypes::QUANT_ONE),
31   m_uri(""),
32   m_uriWildcard(false),
33   m_localName(""),
34   m_localNameWildcard(false),
35   m_contentType()
36 {
37 }
38 
39 
~TypeIdentifier()40 TypeIdentifier::~TypeIdentifier()
41 {
42 }
43 
44 
getKind() const45 IdentTypes::kind_t TypeIdentifier::getKind() const
46 {
47   return m_kind;
48 }
49 
50 
getQuantifier() const51 IdentTypes::quantifier_t TypeIdentifier::getQuantifier() const
52 {
53   return m_quantifier;
54 }
55 
56 
getUri() const57 const String& TypeIdentifier::getUri() const
58 {
59   return m_uri;
60 }
61 
62 
isUriWildcard() const63 bool TypeIdentifier::isUriWildcard() const
64 {
65   return m_uriWildcard;
66 }
67 
68 
getLocalName() const69 const String& TypeIdentifier::getLocalName() const
70 {
71   return m_localName;
72 }
73 
74 
isLocalNameWildcard() const75 bool TypeIdentifier::isLocalNameWildcard() const
76 {
77   return m_localNameWildcard;
78 }
79 
80 
getContentType() const81 TypeIdentifier_t TypeIdentifier::getContentType() const
82 {
83   return m_contentType;
84 }
85 
86 
createNamedType(const String & uri,const String & localName,IdentTypes::quantifier_t quantifier)87 TypeIdentifier_t TypeIdentifier::createNamedType(
88     const String& uri,
89     const String& localName,
90     IdentTypes::quantifier_t quantifier)
91 {
92     TypeIdentifier_t ti(new TypeIdentifier());
93     ti->m_kind = IdentTypes::NAMED_TYPE;
94     ti->m_quantifier = quantifier;
95     ti->m_uri = uri;
96     ti->m_localName = localName;
97 
98     return ti;
99 }
100 
101 
createElementType(const String & uri,bool uriWildcard,const String & localName,bool localNameWildcard,TypeIdentifier_t contentType,IdentTypes::quantifier_t quantifier)102 TypeIdentifier_t TypeIdentifier::createElementType(
103     const String& uri,
104     bool uriWildcard,
105     const String& localName,
106     bool localNameWildcard,
107     TypeIdentifier_t contentType,
108     IdentTypes::quantifier_t quantifier)
109 {
110   // not sure why those were 2 different flags, we maintain 2 flags for
111   // compatibility, but they always need to be the same
112   ZORBA_ASSERT(uriWildcard == localNameWildcard);
113   TypeIdentifier_t ti(new TypeIdentifier());
114   ti->m_kind = IdentTypes::ELEMENT_TYPE;
115   ti->m_quantifier = quantifier;
116   ti->m_uri = uri;
117   ti->m_uriWildcard = uriWildcard;
118   ti->m_localName = localName;
119   ti->m_localNameWildcard = localNameWildcard;
120   ti->m_contentType = contentType;
121 
122   return ti;
123 }
124 
125 
createAttributeType(const String & uri,bool uriWildcard,const String & localName,bool localNameWildcard,TypeIdentifier_t contentType,IdentTypes::quantifier_t quantifier)126 TypeIdentifier_t TypeIdentifier::createAttributeType(
127     const String& uri,
128     bool uriWildcard,
129     const String& localName,
130     bool localNameWildcard,
131     TypeIdentifier_t contentType,
132     IdentTypes::quantifier_t quantifier)
133 {
134     // not sure why those were 2 different flags, we maintain 2 flags for
135     // compatibility, but they always need to be the same
136     ZORBA_ASSERT(uriWildcard == localNameWildcard);
137     TypeIdentifier_t ti(new TypeIdentifier());
138     ti->m_kind = IdentTypes::ATTRIBUTE_TYPE;
139     ti->m_quantifier = quantifier;
140     ti->m_uri = uri;
141     ti->m_uriWildcard = uriWildcard;
142     ti->m_localName = localName;
143     ti->m_localNameWildcard = localNameWildcard;
144     ti->m_contentType = contentType;
145 
146     return ti;
147 }
148 
149 
createDocumentType(TypeIdentifier_t contentType,IdentTypes::quantifier_t quantifier)150 TypeIdentifier_t TypeIdentifier::createDocumentType(
151     TypeIdentifier_t contentType,
152     IdentTypes::quantifier_t quantifier)
153 {
154   TypeIdentifier_t ti(new TypeIdentifier());
155   ti->m_kind = IdentTypes::DOCUMENT_TYPE;
156   ti->m_quantifier = quantifier;
157   ti->m_contentType = contentType;
158 
159   return ti;
160 }
161 
162 
createPIType(IdentTypes::quantifier_t quantifier)163 TypeIdentifier_t TypeIdentifier::createPIType(IdentTypes::quantifier_t quantifier)
164 {
165     TypeIdentifier_t ti(new TypeIdentifier());
166     ti->m_kind = IdentTypes::PI_TYPE;
167     ti->m_quantifier = quantifier;
168 
169     return ti;
170 }
171 
172 
createTextType(IdentTypes::quantifier_t quantifier)173 TypeIdentifier_t TypeIdentifier::createTextType(IdentTypes::quantifier_t quantifier)
174 {
175   TypeIdentifier_t ti(new TypeIdentifier());
176   ti->m_kind = IdentTypes::TEXT_TYPE;
177   ti->m_quantifier = quantifier;
178 
179   return ti;
180 }
181 
182 
createCommentType(IdentTypes::quantifier_t quantifier)183 TypeIdentifier_t TypeIdentifier::createCommentType(IdentTypes::quantifier_t quantifier)
184 {
185   TypeIdentifier_t ti(new TypeIdentifier());
186   ti->m_kind = IdentTypes::COMMENT_TYPE;
187   ti->m_quantifier = quantifier;
188 
189   return ti;
190 }
191 
192 
createAnyNodeType(IdentTypes::quantifier_t quantifier)193 TypeIdentifier_t TypeIdentifier::createAnyNodeType(IdentTypes::quantifier_t quantifier)
194 {
195   TypeIdentifier_t ti(new TypeIdentifier());
196   ti->m_kind = IdentTypes::ANY_NODE_TYPE;
197   ti->m_quantifier = quantifier;
198 
199   return ti;
200 }
201 
202 
createStructuredItemType(IdentTypes::quantifier_t q)203 TypeIdentifier_t TypeIdentifier::createStructuredItemType(IdentTypes::quantifier_t q)
204 {
205   TypeIdentifier_t ti(new TypeIdentifier());
206   ti->m_kind = IdentTypes::STRUCTURED_ITEM_TYPE;
207   ti->m_quantifier = q;
208 
209   return ti;
210 }
211 
212 
213 #ifdef ZORBA_WITH_JSON
214 
createJSONItemType(IdentTypes::quantifier_t q)215 TypeIdentifier_t TypeIdentifier::createJSONItemType(IdentTypes::quantifier_t q)
216 {
217   TypeIdentifier_t ti(new TypeIdentifier());
218   ti->m_kind = IdentTypes::JSON_ITEM_TYPE;
219   ti->m_quantifier = q;
220 
221   return ti;
222 }
223 
224 
createJSONObjectType(IdentTypes::quantifier_t q)225 TypeIdentifier_t TypeIdentifier::createJSONObjectType(IdentTypes::quantifier_t q)
226 {
227   TypeIdentifier_t ti(new TypeIdentifier());
228   ti->m_kind = IdentTypes::JSON_OBJECT_TYPE;
229   ti->m_quantifier = q;
230 
231   return ti;
232 }
233 
234 
createJSONArrayType(IdentTypes::quantifier_t q)235 TypeIdentifier_t TypeIdentifier::createJSONArrayType(IdentTypes::quantifier_t q)
236 {
237   TypeIdentifier_t ti(new TypeIdentifier());
238   ti->m_kind = IdentTypes::JSON_ARRAY_TYPE;
239   ti->m_quantifier = q;
240 
241   return ti;
242 }
243 
244 
245 #endif
246 
247 
createItemType(IdentTypes::quantifier_t quantifier)248 TypeIdentifier_t TypeIdentifier::createItemType(IdentTypes::quantifier_t quantifier)
249 {
250   TypeIdentifier_t ti(new TypeIdentifier());
251   ti->m_kind = IdentTypes::ITEM_TYPE;
252   ti->m_quantifier = quantifier;
253 
254   return ti;
255 }
256 
257 
createEmptyType()258 TypeIdentifier_t TypeIdentifier::createEmptyType()
259 {
260   TypeIdentifier_t ti(new TypeIdentifier());
261   ti->m_kind = IdentTypes::EMPTY_TYPE;
262   ti->m_quantifier = IdentTypes::QUANT_ONE;
263 
264   return ti;
265 }
266 
createSchemaElementType(const String & uri,const String & localName,IdentTypes::quantifier_t quantifier)267 TypeIdentifier_t TypeIdentifier::createSchemaElementType(
268     const String& uri,
269     const String& localName,
270     IdentTypes::quantifier_t quantifier)
271 {
272   TypeIdentifier_t ti(new TypeIdentifier());
273   ti->m_kind = IdentTypes::SCHEMA_ELEMENT_TYPE;
274   ti->m_quantifier = quantifier;
275   ti->m_uri = uri;
276   ti->m_uriWildcard = false;
277   ti->m_localName = localName;
278   ti->m_localNameWildcard = false;
279 
280   return ti;
281 }
282 
283 
createSchemaAttributeType(const String & uri,const String & localName,IdentTypes::quantifier_t quantifier)284 TypeIdentifier_t TypeIdentifier::createSchemaAttributeType(
285     const String& uri,
286     const String& localName,
287     IdentTypes::quantifier_t quantifier)
288 {
289   TypeIdentifier_t ti(new TypeIdentifier());
290   ti->m_kind = IdentTypes::SCHEMA_ATTRIBUTE_TYPE;
291   ti->m_quantifier = quantifier;
292   ti->m_uri = uri;
293   ti->m_uriWildcard = false;
294   ti->m_localName = localName;
295   ti->m_localNameWildcard = false;
296 
297   return ti;
298 }
299 
300 
emit(std::ostream & os) const301 std::ostream& TypeIdentifier::emit(std::ostream& os) const
302 {
303   emitItemType(os);
304   return os << m_quantifier;
305 }
306 
307 
emitItemType(std::ostream & os) const308 std::ostream& TypeIdentifier::emitItemType(std::ostream& os) const
309 {
310   if (m_kind == IdentTypes::NAMED_TYPE)
311   {
312     return emitName(os);
313   }
314 
315   os << m_kind;
316 
317   switch (m_kind)
318   {
319     case IdentTypes::DOCUMENT_TYPE:
320       os << "(";
321       if (m_contentType != NULL)
322       {
323         os << m_contentType;
324       }
325       return os << ")";
326 
327     case IdentTypes::ELEMENT_TYPE:
328     case IdentTypes::ATTRIBUTE_TYPE:
329       os << "(";
330       if (m_uriWildcard)
331       {
332         os << "*";
333       }
334       else
335       {
336         emitName(os);
337       }
338       if (! m_contentType.isNull())
339       {
340         os << "," << m_contentType;
341       }
342       return os << ")";
343 
344     case IdentTypes::SCHEMA_ELEMENT_TYPE:
345     case IdentTypes::SCHEMA_ATTRIBUTE_TYPE:
346       os << "(";
347       emitName(os);
348       return os << ")";
349 
350     case IdentTypes::ANY_NODE_TYPE:
351     case IdentTypes::COMMENT_TYPE:
352     case IdentTypes::EMPTY_TYPE:
353     case IdentTypes::ITEM_TYPE:
354     case IdentTypes::PI_TYPE:
355     case IdentTypes::TEXT_TYPE:
356       return os << "()";
357 
358     case IdentTypes::INVALID_TYPE:
359       return os;
360 
361     case IdentTypes::NAMED_TYPE:
362     default:
363       ZORBA_ASSERT(false);
364   }
365 }
366 
367 
emitName(std::ostream & os) const368 std::ostream& TypeIdentifier::emitName(std::ostream& os) const
369 {
370     return os << "{" << m_uri << "}" << m_localName;
371 }
372 
373 }
374 
375 namespace std
376 {
377 
operator <<(ostream & o,const zorba::TypeIdentifier & ti)378 ostream& operator<<(ostream& o, const zorba::TypeIdentifier& ti)
379 {
380   return ti.emit(o);
381 }
382 
operator <<(ostream & o,const zorba::TypeIdentifier_t ti)383 ostream& operator<<(ostream& o, const zorba::TypeIdentifier_t ti)
384 {
385   return ti->emit(o);
386 }
387 
388 }
389 
390 
391 #endif
392 /* vim:set et sw=2 ts=2: */
393