/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.vxquery.runtime.functions;

import java.io.IOException;
import java.io.InputStream;

import org.xml.sax.InputSource;

import org.apache.vxquery.context.DocumentURIResolver;
import org.apache.vxquery.context.StaticContext;
import org.apache.vxquery.datamodel.XDMAtomicValue;
import org.apache.vxquery.exceptions.ErrorCode;
import org.apache.vxquery.exceptions.SystemException;
import org.apache.vxquery.functions.Function;
import org.apache.vxquery.runtime.CallStackFrame;
import org.apache.vxquery.runtime.RegisterAllocator;
import org.apache.vxquery.runtime.RuntimeControlBlock;
import org.apache.vxquery.runtime.base.AbstractEagerlyEvaluatedFunctionIterator;
import org.apache.vxquery.runtime.base.RuntimeIterator;
import org.apache.vxquery.runtime.util.XMLParserUtils;

public class FnDocIterator extends AbstractEagerlyEvaluatedFunctionIterator {
    public FnDocIterator(RegisterAllocator rAllocator, Function fn, RuntimeIterator[] arguments, StaticContext ctx) {
        super(rAllocator, fn, arguments, ctx);
    }

    @Override
    public Object evaluateEagerly(CallStackFrame frame) throws SystemException {
        XDMAtomicValue uriValue = (XDMAtomicValue) arguments[0].evaluateEagerly(frame);
        RuntimeControlBlock rcb = frame.getRuntimeControlBlock();
        String uri = uriValue.getStringValue().toString();
        DocumentURIResolver docUriResolver = rcb.getDynamicContext().getStaticContext().getDataspaceContext()
                .getDocumentURIResolver();
        InputStream in;
        try {
            in = docUriResolver.resolveDocumentURI(uri, rcb.getDynamicContext().getStaticContext().getBaseUri());
        } catch (IOException e) {
            throw new SystemException(ErrorCode.FODC0002, e, uri);
        }
        return XMLParserUtils.parseInputSource(rcb, new InputSource(in));
    }
}
@@ -1,11 +1,12 @@
 /*%0A
+ 
 * Licens
@@ -66,16 +66,17 @@
 or more%0A
+ 
 * contri
@@ -100,17 +100,16 @@
 eements.
- 
  See the
@@ -134,24 +134,25 @@
 ibuted with%0A
+ 
 * this work 
@@ -209,16 +209,17 @@
 ership.%0A
+ 
 * The AS
@@ -284,16 +284,17 @@
 ion 2.0%0A
+ 
 * (the %22
@@ -356,16 +356,17 @@
 ce with%0A
+ 
 * the Li
@@ -371,17 +371,16 @@
 License.
- 
  You may
@@ -416,15 +416,10 @@
  at%0A
-*%0A*   
  
+*
  htt
@@ -458,18 +458,17 @@
 NSE-2.0%0A
-*%0A
+ 
 * Unless
@@ -529,16 +529,17 @@
 oftware%0A
+ 
 * distri
@@ -598,16 +598,17 @@
  BASIS,%0A
+ 
 * WITHOU
@@ -674,16 +674,17 @@
 mplied.%0A
+ 
 * See th
@@ -745,16 +745,17 @@
 ons and%0A
+ 
 * limita
@@ -779,16 +779,17 @@
 icense.%0A
+ 
 */%0Apacka
@@ -891,41 +891,8 @@
 m;%0A%0A
-import org.xml.sax.InputSource;%0A%0A
 impo
@@ -1540,16 +1540,48 @@
 erUtils;
+%0Aimport org.xml.sax.InputSource;
 %0A%0Apublic
@@ -2007,16 +2007,83 @@
 frame);%0A
+        if (uriValue == null) %7B%0A            return null;%0A        %7D%0A
         
@@ -2364,22 +2364,86 @@
     
-InputStream in
+final String baseUri = rcb.getDynamicContext().getStaticContext().getBaseUri()
 ;%0A  
@@ -2466,16 +2466,28 @@
         
+InputStream 
 in = doc
@@ -2526,62 +2526,92 @@
 ri, 
-rcb.getDynamicContext().getStaticContext().getBaseUri(
+baseUri);%0A            return XMLParserUtils.parseInputSource(rcb, new InputSource(in
 ));%0A
@@ -2725,85 +2725,12 @@
   %7D%0A
-        return XMLParserUtils.parseInputSource(rcb, new InputSource(in));%0A
     %7D%0A%7D
+%0A

fn:doc accepts the empty sequence