Merge lp:~zorba-coders/zorba/bug-1039284 into lp:zorba

Proposed by Ghislain Fourny
Status: Merged
Approved by: Till Westmann
Approved revision: 11103
Merged at revision: 11106
Proposed branch: lp:~zorba-coders/zorba/bug-1039284
Merge into: lp:zorba
Diff against target: 804 lines (+432/-74)
28 files modified
ChangeLog (+1/-0)
src/functions/pregenerated/func_jsoniq_functions.cpp (+22/-0)
src/functions/pregenerated/func_jsoniq_functions.h (+19/-0)
src/functions/pregenerated/function_enum.h (+1/-0)
src/runtime/CMakeLists.txt (+1/-0)
src/runtime/json/jsoniq_functions_impl.cpp (+92/-6)
src/runtime/json/pregenerated/jsoniq_functions.cpp (+40/-0)
src/runtime/json/pregenerated/jsoniq_functions.h (+46/-0)
src/runtime/pregenerated/iterator_enum.h (+1/-0)
src/runtime/sequences/sequences_impl.cpp (+6/-68)
src/runtime/spec/json/jsoniq_functions.xml (+34/-0)
src/runtime/util/doc_uri_heuristics.cpp (+92/-0)
src/runtime/util/doc_uri_heuristics.h (+43/-0)
src/runtime/visitors/pregenerated/planiter_visitor.h (+5/-0)
src/runtime/visitors/pregenerated/printer_visitor.cpp (+14/-0)
src/runtime/visitors/pregenerated/printer_visitor.h (+3/-0)
test/rbkt/ExpQueryResults/zorba/jsoniq/json_doc_1.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/jsoniq/json_doc_2.xml.res (+1/-0)
test/rbkt/Queries/zorba/jsoniq/input1.json (+1/-0)
test/rbkt/Queries/zorba/jsoniq/input2.json (+1/-0)
test/rbkt/Queries/zorba/jsoniq/input3.json (+1/-0)
test/rbkt/Queries/zorba/jsoniq/json_doc_1.xq (+1/-0)
test/rbkt/Queries/zorba/jsoniq/json_doc_2.xq (+1/-0)
test/rbkt/Queries/zorba/jsoniq/json_doc_3.spec (+1/-0)
test/rbkt/Queries/zorba/jsoniq/json_doc_3.xq (+1/-0)
test/rbkt/Queries/zorba/jsoniq/json_doc_4.xq (+1/-0)
test/rbkt/Queries/zorba/jsoniq/json_doc_5.spec (+1/-0)
test/rbkt/Queries/zorba/jsoniq/json_doc_5.xq (+1/-0)
To merge this branch: bzr merge lp:~zorba-coders/zorba/bug-1039284
Reviewer Review Type Date Requested Status
Till Westmann Approve
Matthias Brantner Approve
Ghislain Fourny Approve
Review via email: mp+129677@code.launchpad.net

Commit message

Fixes bug 1039284 (json-doc).

Description of the change

Fixes bug 1039284 (json-doc).

To post a comment you must log in.
Revision history for this message
Zorba Build Bot (zorba-buildbot) wrote :
Revision history for this message
Zorba Build Bot (zorba-buildbot) wrote :

Validation queue job bug-1039284-2012-10-15T16-56-53.572Z is finished. The final status was:

All tests succeeded!

Revision history for this message
Zorba Build Bot (zorba-buildbot) wrote :

Voting does not meet specified criteria. Required: Approve > 1, Disapprove < 1, Needs Fixing < 1, Pending < 1. Got: 2 Pending.

Revision history for this message
Till Westmann (tillw) wrote :

Looks good and works well.

The only thing that should be fixed is the normalizeInput function. Since the same function is probably used for fn:doc, we should only have one implementation of this.
I guess that "runtime/util" (or "runtime/misc"?) would be an appropriate locations for this.

review: Needs Fixing
lp:~zorba-coders/zorba/bug-1039284 updated
11103. By Ghislain Fourny

Factored a function out.

Revision history for this message
Ghislain Fourny (gislenius) :
review: Needs Resubmitting
Revision history for this message
Ghislain Fourny (gislenius) :
review: Approve
Revision history for this message
Ghislain Fourny (gislenius) wrote :

Hi Till,

Good point. I factored the function out.

Revision history for this message
Matthias Brantner (matthias-brantner) wrote :

The function is not stable because it doesn't add the new document into the store. However, I approve anyway because I don't think it's important for the moment.

review: Approve
Revision history for this message
Till Westmann (tillw) wrote :

Looks good - especially the name of the new source file :)

review: Approve
Revision history for this message
Zorba Build Bot (zorba-buildbot) wrote :
Revision history for this message
Zorba Build Bot (zorba-buildbot) wrote :

Validation queue job bug-1039284-2012-10-22T19-38-54.354Z is finished. The final status was:

All tests succeeded!

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ChangeLog'
2--- ChangeLog 2012-10-12 10:46:46 +0000
3+++ ChangeLog 2012-10-16 14:32:24 +0000
4@@ -4,6 +4,7 @@
5 version 2.8
6
7 New Features:
8+ * (bug #1039284) Implemented jn:json-doc().
9
10 Optimizations:
11
12
13=== modified file 'src/functions/pregenerated/func_jsoniq_functions.cpp'
14--- src/functions/pregenerated/func_jsoniq_functions.cpp 2012-10-08 12:09:36 +0000
15+++ src/functions/pregenerated/func_jsoniq_functions.cpp 2012-10-16 14:32:24 +0000
16@@ -141,6 +141,16 @@
17 }
18
19 #endif
20+
21+PlanIter_t fn_jsoniq_json_doc::codegen(
22+ CompilerCB*,
23+ static_context* sctx,
24+ const QueryLoc& loc,
25+ std::vector<PlanIter_t>& argv,
26+ expr& ann) const
27+{
28+ return new JSONDocIterator(sctx, loc, argv);
29+}
30 #ifdef ZORBA_WITH_JSON
31 PlanIter_t op_zorba_json_item_accessor::codegen(
32 CompilerCB*,
33@@ -470,6 +480,18 @@
34 #endif
35
36
37+
38+ {
39+ DECL_WITH_KIND(sctx, fn_jsoniq_json_doc,
40+ (createQName("http://jsoniq.org/functions","","json-doc"),
41+ GENV_TYPESYSTEM.STRING_TYPE_QUESTION,
42+ GENV_TYPESYSTEM.JSON_ITEM_TYPE_STAR),
43+ FunctionConsts::FN_JSONIQ_JSON_DOC_1);
44+
45+ }
46+
47+
48+
49 #ifdef ZORBA_WITH_JSON
50
51
52
53=== modified file 'src/functions/pregenerated/func_jsoniq_functions.h'
54--- src/functions/pregenerated/func_jsoniq_functions.h 2012-10-08 12:09:36 +0000
55+++ src/functions/pregenerated/func_jsoniq_functions.h 2012-10-16 14:32:24 +0000
56@@ -236,6 +236,25 @@
57 CODEGEN_DECL();
58 };
59 #endif
60+
61+
62+//fn-jsoniq:json-doc
63+class fn_jsoniq_json_doc : public function
64+{
65+public:
66+ fn_jsoniq_json_doc(const signature& sig, FunctionConsts::FunctionKind kind)
67+ :
68+ function(sig, kind)
69+ {
70+
71+ }
72+
73+ bool accessesDynCtx() const { return true; }
74+
75+ bool isSource() const { return true; }
76+
77+ CODEGEN_DECL();
78+};
79 #ifdef ZORBA_WITH_JSON
80
81 //op-zorba:json-item-accessor
82
83=== modified file 'src/functions/pregenerated/function_enum.h'
84--- src/functions/pregenerated/function_enum.h 2012-10-15 13:35:59 +0000
85+++ src/functions/pregenerated/function_enum.h 2012-10-16 14:32:24 +0000
86@@ -240,6 +240,7 @@
87 FN_JSONIQ_MEMBER_2,
88 FN_JSONIQ_MEMBERS_1,
89 FN_JSONIQ_FLATTEN_1,
90+ FN_JSONIQ_JSON_DOC_1,
91 OP_ZORBA_JSON_ITEM_ACCESSOR_2,
92 FN_JSONIQ_NULL_0,
93 FN_JSONIQ_IS_NULL_1,
94
95=== modified file 'src/runtime/CMakeLists.txt'
96--- src/runtime/CMakeLists.txt 2012-09-19 21:16:15 +0000
97+++ src/runtime/CMakeLists.txt 2012-10-16 14:32:24 +0000
98@@ -138,6 +138,7 @@
99 util/item_iterator.cpp
100 util/timeout.cpp
101 util/flowctl_exception.cpp
102+ util/doc_uri_heuristics.cpp
103 function_item/function_item.cpp
104 function_item/dynamic_fncall_iterator.cpp
105 eval/eval.cpp
106
107=== modified file 'src/runtime/json/jsoniq_functions_impl.cpp'
108--- src/runtime/json/jsoniq_functions_impl.cpp 2012-10-08 12:09:36 +0000
109+++ src/runtime/json/jsoniq_functions_impl.cpp 2012-10-16 14:32:24 +0000
110@@ -46,13 +46,18 @@
111 #include "types/typeops.h"
112 #include "types/root_typemanager.h"
113
114-#include "store/api/pul.h"
115-#include "store/api/item.h"
116-#include "store/api/item_factory.h"
117-#include "store/api/store.h"
118-#include "store/api/copymode.h"
119-
120+#include <runtime/util/doc_uri_heuristics.h>
121+
122+#include <store/api/pul.h>
123+#include <store/api/item.h>
124+#include <store/api/item_factory.h>
125+#include <store/api/store.h>
126+#include <store/api/copymode.h>
127+
128+#include <util/uri_util.h>
129 #include <zorba/store_consts.h>
130+#include <zorbatypes/URI.h>
131+
132
133 namespace zorba {
134
135@@ -1597,6 +1602,87 @@
136 STACK_END(state);
137 }
138
139+/*******************************************************************************
140+
141+********************************************************************************/
142+bool JSONDocIterator::nextImpl(store::Item_t& result, PlanState& planState) const
143+{
144+ store::Item_t uriItem;
145+ JSONDocIteratorState* state;
146+ zstring uriString;
147+ zstring lErrorMessage;
148+ internal::StreamResource* lStreamResource;
149+ zstring lNormUri;
150+ DEFAULT_STACK_INIT(JSONDocIteratorState, state, planState);
151+
152+ if (consumeNext(uriItem, theChildren[0].getp(), planState))
153+ {
154+ uriItem->getStringValue2(uriString);
155+ // Normalize input to handle filesystem paths, etc.
156+ normalizeInputUri(uriString, theSctx, loc, &lNormUri);
157+
158+ // Resolve URI to a stream
159+ state->theResource = theSctx->resolve_uri(
160+ lNormUri,
161+ internal::EntityData::DOCUMENT,
162+ lErrorMessage);
163+
164+ lStreamResource =
165+ dynamic_cast<internal::StreamResource*>(state->theResource.get());
166+ if (lStreamResource == NULL) {
167+ throw XQUERY_EXCEPTION(
168+ err::FODC0002,
169+ ERROR_PARAMS(uriString, lErrorMessage),
170+ ERROR_LOC(loc));
171+ }
172+
173+ state->theStream = lStreamResource->getStream();
174+ if (state->theStream == NULL) {
175+ throw XQUERY_EXCEPTION(
176+ err::FODC0002,
177+ ERROR_PARAMS( uriString ),
178+ ERROR_LOC(loc));
179+ }
180+
181+ state->theGotOne = false;
182+
183+ while (true)
184+ {
185+ try
186+ {
187+ result = GENV_STORE.parseJSON(*state->theStream, 0);
188+ }
189+ catch (zorba::XQueryException& e)
190+ {
191+ // rethrow with JNDY0021
192+ XQueryException xq = XQUERY_EXCEPTION(
193+ jerr::JNDY0021,
194+ ERROR_PARAMS(e.what()),
195+ ERROR_LOC(loc));
196+
197+ // use location of e in case of literal string
198+ throw xq;
199+ }
200+ if (result != NULL)
201+ {
202+ if (!state->theGotOne)
203+ {
204+ state->theGotOne = true;
205+ STACK_PUSH(true, state);
206+ } else {
207+ RAISE_ERROR(
208+ jerr::JNDY0021,
209+ loc,
210+ ERROR_PARAMS(ZED(JSON_UNEXPECTED_EXTRA_CONTENT)));
211+ }
212+ } else {
213+ break;
214+ }
215+ }
216+ }
217+
218+ STACK_END(state);
219+}
220
221 } /* namespace zorba */
222 /* vim:set et sw=2 ts=2: */
223
224=== modified file 'src/runtime/json/pregenerated/jsoniq_functions.cpp'
225--- src/runtime/json/pregenerated/jsoniq_functions.cpp 2012-10-08 12:09:36 +0000
226+++ src/runtime/json/pregenerated/jsoniq_functions.cpp 2012-10-16 14:32:24 +0000
227@@ -357,6 +357,46 @@
228 // </JSONArrayFlattenIterator>
229
230 #endif
231+// <JSONDocIterator>
232+SERIALIZABLE_CLASS_VERSIONS(JSONDocIterator)
233+
234+void JSONDocIterator::serialize(::zorba::serialization::Archiver& ar)
235+{
236+ serialize_baseclass(ar,
237+ (NaryBaseIterator<JSONDocIterator, JSONDocIteratorState>*)this);
238+}
239+
240+
241+void JSONDocIterator::accept(PlanIterVisitor& v) const
242+{
243+ v.beginVisit(*this);
244+
245+ std::vector<PlanIter_t>::const_iterator lIter = theChildren.begin();
246+ std::vector<PlanIter_t>::const_iterator lEnd = theChildren.end();
247+ for ( ; lIter != lEnd; ++lIter ){
248+ (*lIter)->accept(v);
249+ }
250+
251+ v.endVisit(*this);
252+}
253+
254+JSONDocIterator::~JSONDocIterator() {}
255+
256+JSONDocIteratorState::JSONDocIteratorState() {}
257+
258+JSONDocIteratorState::~JSONDocIteratorState() {}
259+
260+
261+void JSONDocIteratorState::init(PlanState& planState) {
262+ PlanIteratorState::init(planState);
263+}
264+
265+void JSONDocIteratorState::reset(PlanState& planState) {
266+ PlanIteratorState::reset(planState);
267+}
268+// </JSONDocIterator>
269+
270+
271 #ifdef ZORBA_WITH_JSON
272 // <JSONItemAccessorIterator>
273 SERIALIZABLE_CLASS_VERSIONS(JSONItemAccessorIterator)
274
275=== modified file 'src/runtime/json/pregenerated/jsoniq_functions.h'
276--- src/runtime/json/pregenerated/jsoniq_functions.h 2012-10-08 12:09:36 +0000
277+++ src/runtime/json/pregenerated/jsoniq_functions.h 2012-10-16 14:32:24 +0000
278@@ -29,6 +29,7 @@
279 #include "runtime/base/binarybase.h"
280 #include "runtime/base/noarybase.h"
281 #include "runtime/base/narybase.h"
282+#include <context/uri_resolver.h>
283
284
285 namespace zorba {
286@@ -457,6 +458,51 @@
287
288 #endif
289
290+/**
291+ * jn:json-doc
292+ * Author: Zorba Team
293+ */
294+class JSONDocIteratorState : public PlanIteratorState
295+{
296+public:
297+ std::auto_ptr<internal::Resource> theResource; //
298+ std::istream* theStream; //
299+ bool theGotOne; //
300+
301+ JSONDocIteratorState();
302+
303+ ~JSONDocIteratorState();
304+
305+ void init(PlanState&);
306+ void reset(PlanState&);
307+};
308+
309+class JSONDocIterator : public NaryBaseIterator<JSONDocIterator, JSONDocIteratorState>
310+{
311+public:
312+ SERIALIZABLE_CLASS(JSONDocIterator);
313+
314+ SERIALIZABLE_CLASS_CONSTRUCTOR2T(JSONDocIterator,
315+ NaryBaseIterator<JSONDocIterator, JSONDocIteratorState>);
316+
317+ void serialize( ::zorba::serialization::Archiver& ar);
318+
319+ JSONDocIterator(
320+ static_context* sctx,
321+ const QueryLoc& loc,
322+ std::vector<PlanIter_t>& children)
323+ :
324+ NaryBaseIterator<JSONDocIterator, JSONDocIteratorState>(sctx, loc, children)
325+ {}
326+
327+ virtual ~JSONDocIterator();
328+
329+ void accept(PlanIterVisitor& v) const;
330+
331+ bool nextImpl(store::Item_t& result, PlanState& aPlanState) const;
332+};
333+
334+
335 #ifdef ZORBA_WITH_JSON
336 /**
337 *
338
339=== modified file 'src/runtime/pregenerated/iterator_enum.h'
340--- src/runtime/pregenerated/iterator_enum.h 2012-10-08 12:09:36 +0000
341+++ src/runtime/pregenerated/iterator_enum.h 2012-10-16 14:32:24 +0000
342@@ -161,6 +161,7 @@
343 TYPE_JSONArrayMemberIterator,
344 TYPE_JSONArrayMembersIterator,
345 TYPE_JSONArrayFlattenIterator,
346+ TYPE_JSONDocIterator,
347 TYPE_JSONItemAccessorIterator,
348 TYPE_JSONNullIterator,
349 TYPE_JSONIsNullIterator,
350
351=== modified file 'src/runtime/sequences/sequences_impl.cpp'
352--- src/runtime/sequences/sequences_impl.cpp 2012-09-19 21:16:15 +0000
353+++ src/runtime/sequences/sequences_impl.cpp 2012-10-16 14:32:24 +0000
354@@ -41,6 +41,7 @@
355 #include <runtime/core/arithmetic_impl.h>
356 #include <runtime/util/iterator_impl.h>
357 #include <runtime/visitors/planiter_visitor.h>
358+#include <runtime/util/doc_uri_heuristics.h>
359
360 #include <system/globalenv.h>
361
362@@ -1718,71 +1719,6 @@
363 zorbatm::get_walltime_elapsed(t0, t1);
364 }
365
366-/**
367- * Utility method for fn:doc() and fn:doc-available(). Given an input string,
368- * use a few heuristics to create a valid URI, assuming that the input might
369- * be an absolute or relative filesystem path, etc.
370- */
371-static zstring normalizeInput(zstring const& aUri, static_context* aSctx,
372- QueryLoc const& loc)
373-{
374- zstring const aBaseUri = aSctx->get_base_uri();
375- zstring lResolvedURI;
376-
377- try
378- {
379- // To support the very common (if technically incorrect) use
380- // case of users passing local filesystem paths to fn:doc(),
381- // we use the following heuristic: IF the base URI has a file:
382- // scheme AND the incoming URI has no scheme, we will assume
383- // the incoming URI is actually a filesystem path. QQQ For
384- // the moment, we assume any "unknown" schemes are probably
385- // Windows drive letters.
386- if ((uri::get_scheme(aUri) == uri::none ||
387- uri::get_scheme(aUri) == uri::unknown) &&
388- uri::get_scheme(aBaseUri) == uri::file)
389- {
390- // Ok, we assume it's a filesystem path. First normalize it.
391- zstring lNormalizedPath =
392- fs::get_normalized_path(aUri, zstring(""));
393- // QQQ For now, get_normalized_path() doesn't do what we
394- // want when base URI represents a file. So, when the
395- // normalized path is relative, we pretend it's a relative
396- // URI and resolve it as such.
397- if (fs::is_absolute(lNormalizedPath))
398- {
399- URI::encode_file_URI(lNormalizedPath, lResolvedURI);
400- }
401- else
402- {
403-#ifdef WIN32
404- ascii::replace_all(lNormalizedPath, '\\', '/');
405-#endif
406- lResolvedURI = aSctx->resolve_relative_uri(lNormalizedPath, true);
407- }
408- }
409- else
410- {
411- // We do NOT assume it's a filesystem path; just resolve it.
412- lResolvedURI = aSctx->resolve_relative_uri(aUri, true);
413- }
414- }
415- catch (ZorbaException& e)
416- {
417- if (e.diagnostic() == err::XQST0046)
418- // the value of a URILiteral is of nonzero length and is not in the
419- // lexical space of xs:anyURI.
420- e.set_diagnostic(err::FODC0005);
421- else
422- e.set_diagnostic(err::FODC0002);
423-
424- set_source(e, loc);
425- throw;
426- }
427-
428- return lResolvedURI;
429-}
430-
431 static void loadDocument(
432 zstring const& aUri,
433 static_context* aSctx,
434@@ -1791,7 +1727,8 @@
435 store::Item_t& oResult)
436 {
437 // Normalize input to handle filesystem paths, etc.
438- zstring const lNormUri(normalizeInput(aUri, aSctx, loc));
439+ zstring lNormUri;
440+ normalizeInputUri(aUri, aSctx, loc, &lNormUri);
441
442 // See if this (normalized) URI is already loaded in the store.
443 try {
444@@ -1968,7 +1905,8 @@
445 store::Item_t& oResult)
446 {
447 //Normalize input to handle filesystem paths, etc.
448- zstring const lNormUri(normalizeInput(aUri, aSctx, loc));
449+ zstring lNormUri;
450+ normalizeInputUri(aUri, aSctx, loc, &lNormUri);
451
452 //Resolve URI to stream
453 zstring lErrorMessage;
454@@ -2119,7 +2057,7 @@
455
456 //Normalize input to handle filesystem paths, etc.
457 uriItem->getStringValue2(uriString);
458- lNormUri = normalizeInput(uriString, theSctx, loc);
459+ normalizeInputUri(uriString, theSctx, loc, &lNormUri);
460
461 //Resolve URI to stream
462 lResource = theSctx->resolve_uri
463
464=== modified file 'src/runtime/spec/json/jsoniq_functions.xml'
465--- src/runtime/spec/json/jsoniq_functions.xml 2012-10-08 12:09:36 +0000
466+++ src/runtime/spec/json/jsoniq_functions.xml 2012-10-16 14:32:24 +0000
467@@ -9,6 +9,10 @@
468 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
469 xsi:schemaLocation="http://www.zorba-xquery.com ../runtime.xsd">
470
471+<zorba:header>
472+ <zorba:include form="Angle-bracket">context/uri_resolver.h</zorba:include>
473+</zorba:header>
474+
475 <!--
476 /*******************************************************************************
477 ********************************************************************************/
478@@ -377,6 +381,35 @@
479
480 </zorba:iterator>
481
482+<!--
483+/*******************************************************************************
484+********************************************************************************/
485+-->
486+<zorba:iterator name="JSONDocIterator">
487+
488+ <zorba:description author="Zorba Team">jn:json-doc</zorba:description>
489+
490+ <zorba:function>
491+
492+ <zorba:signature localname="json-doc" prefix="fn-jsoniq">
493+ <zorba:param>xs:string?</zorba:param>
494+ <zorba:output>json-item()*</zorba:output>
495+ </zorba:signature>
496+
497+ <zorba:methods>
498+ <zorba:accessesDynCtx returnValue="true"/>
499+ <zorba:isSource returnValue="true"/>
500+ </zorba:methods>
501+
502+ </zorba:function>
503+
504+ <zorba:state>
505+ <zorba:member type="std::auto_ptr&lt;internal::Resource&gt;" name="theResource" brief=""/>
506+ <zorba:member type="std::istream*" name="theStream" brief=""/>
507+ <zorba:member type="bool" name="theGotOne" brief=""/>
508+ </zorba:state>
509+
510+</zorba:iterator>
511
512 <!--
513 /*******************************************************************************
514@@ -659,4 +692,5 @@
515
516 </zorba:iterator>
517
518+
519 </zorba:iterators>
520
521=== added file 'src/runtime/util/doc_uri_heuristics.cpp'
522--- src/runtime/util/doc_uri_heuristics.cpp 1970-01-01 00:00:00 +0000
523+++ src/runtime/util/doc_uri_heuristics.cpp 2012-10-16 14:32:24 +0000
524@@ -0,0 +1,92 @@
525+/*
526+ * Copyright 2006-2012 The FLWOR Foundation.
527+ *
528+ * Licensed under the Apache License, Version 2.0 (the "License");
529+ * you may not use this file except in compliance with the License.
530+ * You may obtain a copy of the License at
531+ *
532+ * http://www.apache.org/licenses/LICENSE-2.0
533+ *
534+ * Unless required by applicable law or agreed to in writing, software
535+ * distributed under the License is distributed on an "AS IS" BASIS,
536+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
537+ * See the License for the specific language governing permissions and
538+ * limitations under the License.
539+ */
540+#include "stdafx.h"
541+#include <zorba/config.h>
542+
543+#include "doc_uri_heuristics.h"
544+
545+#include <context/static_context.h>
546+#include <diagnostics/xquery_exception.h>
547+#include <util/uri_util.h>
548+#include <zorbatypes/URI.h>
549+
550+
551+namespace zorba {
552+
553+void normalizeInputUri(
554+ zstring const& aUri,
555+ static_context* aSctx,
556+ QueryLoc const& aLoc,
557+ zstring* aResult)
558+{
559+ zstring const lBaseUri = aSctx->get_base_uri();
560+
561+ try
562+ {
563+ // To support the very common (if technically incorrect) use
564+ // case of users passing local filesystem paths to fn:doc(),
565+ // we use the following heuristic: IF the base URI has a file:
566+ // scheme AND the incoming URI has no scheme, we will assume
567+ // the incoming URI is actually a filesystem path. QQQ For
568+ // the moment, we assume any "unknown" schemes are probably
569+ // Windows drive letters.
570+ if ((uri::get_scheme(aUri) == uri::none ||
571+ uri::get_scheme(aUri) == uri::unknown) &&
572+ uri::get_scheme(lBaseUri) == uri::file)
573+ {
574+ // Ok, we assume it's a filesystem path. First normalize it.
575+ zstring lNormalizedPath = fs::get_normalized_path(
576+ aUri,
577+ zstring(""));
578+ // QQQ For now, get_normalized_path() doesn't do what we
579+ // want when base URI represents a file. So, when the
580+ // normalized path is relative, we pretend it's a relative
581+ // URI and resolve it as such.
582+ if (fs::is_absolute(lNormalizedPath))
583+ {
584+ URI::encode_file_URI(lNormalizedPath, *aResult);
585+ }
586+ else
587+ {
588+#ifdef WIN32
589+ ascii::replace_all(lNormalizedPath, '\\', '/');
590+#endif
591+ *aResult = aSctx->resolve_relative_uri(lNormalizedPath, true);
592+ }
593+ }
594+ else
595+ {
596+ // We do NOT assume it's a filesystem path; just resolve it.
597+ *aResult = aSctx->resolve_relative_uri(aUri, true);
598+ }
599+ }
600+ catch (ZorbaException& e)
601+ {
602+ if (e.diagnostic() == err::XQST0046)
603+ // the value of a URILiteral is of nonzero length and is not in the
604+ // lexical space of xs:anyURI.
605+ e.set_diagnostic(err::FODC0005);
606+ else
607+ e.set_diagnostic(err::FODC0002);
608+
609+ set_source(e, aLoc);
610+ throw;
611+ }
612+}
613+
614+} /* namespace zorba */
615+/* vim:set et sw=2 ts=2: */
616+
617
618=== added file 'src/runtime/util/doc_uri_heuristics.h'
619--- src/runtime/util/doc_uri_heuristics.h 1970-01-01 00:00:00 +0000
620+++ src/runtime/util/doc_uri_heuristics.h 2012-10-16 14:32:24 +0000
621@@ -0,0 +1,43 @@
622+/*
623+ * Copyright 2006-2012 The FLWOR Foundation.
624+ *
625+ * Licensed under the Apache License, Version 2.0 (the "License");
626+ * you may not use this file except in compliance with the License.
627+ * You may obtain a copy of the License at
628+ *
629+ * http://www.apache.org/licenses/LICENSE-2.0
630+ *
631+ * Unless required by applicable law or agreed to in writing, software
632+ * distributed under the License is distributed on an "AS IS" BASIS,
633+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
634+ * See the License for the specific language governing permissions and
635+ * limitations under the License.
636+ */
637+#ifndef ZORBA_RUNTIME_UTIL_DOC_URI_HEURISTICS_H
638+#define ZORBA_RUNTIME_UTIL_DOC_URI_HEURISTICS_H
639+
640+#include "stdafx.h"
641+#include <zorba/config.h>
642+
643+#include <zorbatypes/zstring.h>
644+
645+namespace zorba {
646+
647+class static_context;
648+class QueryLoc;
649+
650+/**
651+ * Utility method for fn:doc() and jn:json-doc(). Given an input string,
652+ * use a few heuristics to create a valid URI, assuming that the input might
653+ * be an absolute or relative filesystem path, etc.
654+ */
655+void normalizeInputUri(
656+ zstring const& aUri,
657+ static_context* aSctx,
658+ QueryLoc const& aLoc,
659+ zstring* aResult);
660+
661+} /* namespace zorba */
662+/* vim:set et sw=2 ts=2: */
663+
664+#endif /* ZORBA_RUNTIME_UTIL_DOC_URI_HEURISTICS_H */
665
666=== modified file 'src/runtime/visitors/pregenerated/planiter_visitor.h'
667--- src/runtime/visitors/pregenerated/planiter_visitor.h 2012-10-08 12:09:36 +0000
668+++ src/runtime/visitors/pregenerated/planiter_visitor.h 2012-10-16 14:32:24 +0000
669@@ -338,6 +338,8 @@
670 #ifdef ZORBA_WITH_JSON
671 class JSONArrayFlattenIterator;
672 #endif
673+ class JSONDocIterator;
674+
675 #ifdef ZORBA_WITH_JSON
676 class JSONItemAccessorIterator;
677 #endif
678@@ -1188,6 +1190,9 @@
679 virtual void beginVisit ( const JSONArrayFlattenIterator& ) = 0;
680 virtual void endVisit ( const JSONArrayFlattenIterator& ) = 0;
681 #endif
682+ virtual void beginVisit ( const JSONDocIterator& ) = 0;
683+ virtual void endVisit ( const JSONDocIterator& ) = 0;
684+
685 #ifdef ZORBA_WITH_JSON
686 virtual void beginVisit ( const JSONItemAccessorIterator& ) = 0;
687 virtual void endVisit ( const JSONItemAccessorIterator& ) = 0;
688
689=== modified file 'src/runtime/visitors/pregenerated/printer_visitor.cpp'
690--- src/runtime/visitors/pregenerated/printer_visitor.cpp 2012-10-08 12:09:36 +0000
691+++ src/runtime/visitors/pregenerated/printer_visitor.cpp 2012-10-16 14:32:24 +0000
692@@ -1973,6 +1973,20 @@
693 // </JSONArrayFlattenIterator>
694
695 #endif
696+
697+// <JSONDocIterator>
698+void PrinterVisitor::beginVisit ( const JSONDocIterator& a) {
699+ thePrinter.startBeginVisit("JSONDocIterator", ++theId);
700+ printCommons( &a, theId );
701+ thePrinter.endBeginVisit( theId );
702+}
703+
704+void PrinterVisitor::endVisit ( const JSONDocIterator& ) {
705+ thePrinter.startEndVisit();
706+ thePrinter.endEndVisit();
707+}
708+// </JSONDocIterator>
709+
710 #ifdef ZORBA_WITH_JSON
711 // <JSONItemAccessorIterator>
712 void PrinterVisitor::beginVisit ( const JSONItemAccessorIterator& a) {
713
714=== modified file 'src/runtime/visitors/pregenerated/printer_visitor.h'
715--- src/runtime/visitors/pregenerated/printer_visitor.h 2012-10-08 12:09:36 +0000
716+++ src/runtime/visitors/pregenerated/printer_visitor.h 2012-10-16 14:32:24 +0000
717@@ -522,6 +522,9 @@
718 void endVisit ( const JSONArrayFlattenIterator& );
719 #endif
720
721+ void beginVisit( const JSONDocIterator& );
722+ void endVisit ( const JSONDocIterator& );
723+
724 #ifdef ZORBA_WITH_JSON
725 void beginVisit( const JSONItemAccessorIterator& );
726 void endVisit ( const JSONItemAccessorIterator& );
727
728=== added file 'test/rbkt/ExpQueryResults/zorba/jsoniq/json_doc_1.xml.res'
729--- test/rbkt/ExpQueryResults/zorba/jsoniq/json_doc_1.xml.res 1970-01-01 00:00:00 +0000
730+++ test/rbkt/ExpQueryResults/zorba/jsoniq/json_doc_1.xml.res 2012-10-16 14:32:24 +0000
731@@ -0,0 +1,1 @@
732+{ "foo" : "bar" }
733
734=== added file 'test/rbkt/ExpQueryResults/zorba/jsoniq/json_doc_2.xml.res'
735--- test/rbkt/ExpQueryResults/zorba/jsoniq/json_doc_2.xml.res 1970-01-01 00:00:00 +0000
736+++ test/rbkt/ExpQueryResults/zorba/jsoniq/json_doc_2.xml.res 2012-10-16 14:32:24 +0000
737@@ -0,0 +1,1 @@
738+[ "bar" ]
739
740=== added file 'test/rbkt/ExpQueryResults/zorba/jsoniq/json_doc_4.xml.res'
741=== added file 'test/rbkt/Queries/zorba/jsoniq/input1.json'
742--- test/rbkt/Queries/zorba/jsoniq/input1.json 1970-01-01 00:00:00 +0000
743+++ test/rbkt/Queries/zorba/jsoniq/input1.json 2012-10-16 14:32:24 +0000
744@@ -0,0 +1,1 @@
745+{ "foo" : "bar" }
746
747=== added file 'test/rbkt/Queries/zorba/jsoniq/input2.json'
748--- test/rbkt/Queries/zorba/jsoniq/input2.json 1970-01-01 00:00:00 +0000
749+++ test/rbkt/Queries/zorba/jsoniq/input2.json 2012-10-16 14:32:24 +0000
750@@ -0,0 +1,1 @@
751+{ "foo" : "bar" } { "bar" : "foo" }
752
753=== added file 'test/rbkt/Queries/zorba/jsoniq/input3.json'
754--- test/rbkt/Queries/zorba/jsoniq/input3.json 1970-01-01 00:00:00 +0000
755+++ test/rbkt/Queries/zorba/jsoniq/input3.json 2012-10-16 14:32:24 +0000
756@@ -0,0 +1,1 @@
757+{ "foo" : "bar" } { "bar" : "foo"
758
759=== added file 'test/rbkt/Queries/zorba/jsoniq/json_doc_1.xq'
760--- test/rbkt/Queries/zorba/jsoniq/json_doc_1.xq 1970-01-01 00:00:00 +0000
761+++ test/rbkt/Queries/zorba/jsoniq/json_doc_1.xq 2012-10-16 14:32:24 +0000
762@@ -0,0 +1,1 @@
763+jn:json-doc("input1.json")
764\ No newline at end of file
765
766=== added file 'test/rbkt/Queries/zorba/jsoniq/json_doc_2.xq'
767--- test/rbkt/Queries/zorba/jsoniq/json_doc_2.xq 1970-01-01 00:00:00 +0000
768+++ test/rbkt/Queries/zorba/jsoniq/json_doc_2.xq 2012-10-16 14:32:24 +0000
769@@ -0,0 +1,1 @@
770+[ jn:json-doc("input1.json")("foo") ]
771\ No newline at end of file
772
773=== added file 'test/rbkt/Queries/zorba/jsoniq/json_doc_3.spec'
774--- test/rbkt/Queries/zorba/jsoniq/json_doc_3.spec 1970-01-01 00:00:00 +0000
775+++ test/rbkt/Queries/zorba/jsoniq/json_doc_3.spec 2012-10-16 14:32:24 +0000
776@@ -0,0 +1,1 @@
777+Error: http://jsoniq.org/errors:JNDY0021
778
779=== added file 'test/rbkt/Queries/zorba/jsoniq/json_doc_3.xq'
780--- test/rbkt/Queries/zorba/jsoniq/json_doc_3.xq 1970-01-01 00:00:00 +0000
781+++ test/rbkt/Queries/zorba/jsoniq/json_doc_3.xq 2012-10-16 14:32:24 +0000
782@@ -0,0 +1,1 @@
783+jn:json-doc("input2.json")
784\ No newline at end of file
785
786=== added file 'test/rbkt/Queries/zorba/jsoniq/json_doc_4.xq'
787--- test/rbkt/Queries/zorba/jsoniq/json_doc_4.xq 1970-01-01 00:00:00 +0000
788+++ test/rbkt/Queries/zorba/jsoniq/json_doc_4.xq 2012-10-16 14:32:24 +0000
789@@ -0,0 +1,1 @@
790+jn:json-doc(())
791\ No newline at end of file
792
793=== added file 'test/rbkt/Queries/zorba/jsoniq/json_doc_5.spec'
794--- test/rbkt/Queries/zorba/jsoniq/json_doc_5.spec 1970-01-01 00:00:00 +0000
795+++ test/rbkt/Queries/zorba/jsoniq/json_doc_5.spec 2012-10-16 14:32:24 +0000
796@@ -0,0 +1,1 @@
797+Error: http://jsoniq.org/errors:JNDY0021
798
799=== added file 'test/rbkt/Queries/zorba/jsoniq/json_doc_5.xq'
800--- test/rbkt/Queries/zorba/jsoniq/json_doc_5.xq 1970-01-01 00:00:00 +0000
801+++ test/rbkt/Queries/zorba/jsoniq/json_doc_5.xq 2012-10-16 14:32:24 +0000
802@@ -0,0 +1,1 @@
803+jn:json-doc("input3.json")
804\ No newline at end of file

Subscribers

People subscribed via source and target branches