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 #include <iostream>
19
20 #include "diagnostics/xquery_diagnostics.h"
21 #include "diagnostics/user_exception.h"
22 #include "diagnostics/util_macros.h"
23
24 #include "context/static_context.h"
25
26 #include "runtime/errors_and_diagnostics/errors_and_diagnostics.h"
27 #include "runtime/util/iterator_impl.h"
28
29 #include "store/api/item.h"
30 #include "store/api/item_factory.h"
31 #include "store/api/store.h"
32 #include "store/api/temp_seq.h"
33
34 #include "system/globalenv.h"
35 #include "zorbatypes/zstring.h"
36
37 #include "api/serialization/serializer.h"
38 #include "api/serializerimpl.h"
39
40 namespace zorba
41 {
42
43 /*******************************************************************************
44 3.1.1 fn:error
45 ********************************************************************************/
46 bool
nextImpl(store::Item_t & result,PlanState & planState) const47 ErrorIterator::nextImpl(store::Item_t& result, PlanState& planState) const
48 {
49 static const char* err_ns = "http://www.w3.org/2005/xqt-errors";
50 store::Item_t err_qname;
51 GENV_ITEMFACTORY->createQName(err_qname, err_ns, "err", "FOER0000");
52 store::Item_t lTmpQName;
53 store::Item_t lTmpErrorObject;
54 store::Item_t lTmpDescr;
55 zstring description;
56 UserException::error_object_type lErrorObject;
57
58 PlanIteratorState* state;
59 DEFAULT_STACK_INIT(PlanIteratorState, state, planState);
60
61 if (theChildren.size() >= 1)
62 {
63 if (consumeNext(lTmpQName, theChildren[0].getp(), planState))
64 err_qname = lTmpQName;
65 }
66
67 if (theChildren.size() >= 2)
68 {
69 consumeNext(lTmpDescr, theChildren[1].getp(), planState);
70 description = lTmpDescr->getStringValue();
71 }
72
73 if (theChildren.size() == 3)
74 {
75 while (consumeNext(lTmpErrorObject, theChildren[2].getp(), planState))
76 {
77 lErrorObject.push_back( Item( lTmpErrorObject.getp() ) );
78 }
79 }
80
81 throw USER_EXCEPTION(err_qname, description.c_str(), ERROR_LOC(loc), &lErrorObject);
82
83 STACK_END(state);
84 }
85
86
87 /*******************************************************************************
88 3.2.1 fn:trace
89 ********************************************************************************/
90 bool
nextImpl(store::Item_t & result,PlanState & planState) const91 TraceIterator::nextImpl(store::Item_t& result, PlanState& planState) const
92 {
93 TraceIteratorState *state;
94 DEFAULT_STACK_INIT(TraceIteratorState, state, planState);
95
96 state->theIndex = 1; // XQuery sequences start with 1
97
98 if (!consumeNext(state->theTagItem, theChildren[1], planState))
99 {
100 RAISE_ERROR(err::FORG0006, loc,
101 ERROR_PARAMS(ZED(BadArgTypeForFn_2o34o), ZED(EmptySequence), "fn:trace"));
102 }
103
104 if (state->theSerializer == NULL)
105 {
106 state->theSerializer = new serializer(0);
107
108 Zorba_SerializerOptions options;
109 options.omit_xml_declaration = ZORBA_OMIT_XML_DECLARATION_YES;
110 SerializerImpl::setSerializationParameters(*(state->theSerializer), options);
111 }
112
113 state->theOS = theSctx->get_trace_stream();
114
115 while (consumeNext(result, theChildren[0], planState))
116 {
117 (*state->theOS) << state->theTagItem->getStringValue()
118 << " [" << state->theIndex << "]: ";
119
120 {
121 store::Item_t lTmp = result;
122
123 // implicitly materialize non-seekable streamable strings to
124 // avoid nasty "streamable string has already been consumed"
125 // errors during debugging
126 if (lTmp->isStreamable() && !lTmp->isSeekable() &&
127 lTmp->getTypeCode() == store::XS_STRING)
128 {
129 zstring lString = lTmp->getString();
130 GENV_ITEMFACTORY->createString(lTmp, lString);
131 result = lTmp;
132 }
133
134 store::TempSeq_t lSequence = GENV_STORE.createTempSeq(lTmp);
135 store::Iterator_t seq_iter = lSequence->getIterator();
136 seq_iter->open();
137 state->theSerializer->serialize(seq_iter, (*state->theOS), true);
138 seq_iter->close();
139 }
140
141 (*state->theOS) << std::endl;
142 ++state->theIndex;
143
144 STACK_PUSH(true, state);
145 }
146
147
148 STACK_END(state);
149 }
150
151 } // namespace zorba
152 /* vim:set et sw=2 ts=2: */
153