Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.xmlcalabash.runtime;
  
 
 
Select.java Copyright 2008 Mark Logic Corporation. Portions Copyright 2007 Sun Microsystems, Inc. All rights reserved. The contents of this file are subject to the terms of either the GNU General Public License Version 2 only ("GPL") or the Common Development and Distribution License("CDDL") (collectively, the "License"). You may not use this file except in compliance with the License. You can obtain a copy of the License at https://runtime.dev.java.net/public/CDDL+GPL.html or docs/CDDL+GPL.txt in the distribution. See the License for the specific language governing permissions and limitations under the License. When distributing the software, include this License Header Notice in each file and include the License file at docs/CDDL+GPL.txt. Ideally, I'd like this code to perform the selections in a lazy fashion, but that's hard because it has to be possible to answer questions about how many documents will be returned. So for now, I'm just doing it all up front. Created by IntelliJ IDEA. User: ndw Date: Oct 10, 2008 Time: 10:13:58 PM To change this template use File | Settings | File Templates.
 
 public class XSelect implements ReadablePipe {
     private Logger logger = Logger.getLogger(this.getClass().getName());
     private ReadablePipe source = null;
     private String select = null;
     private XdmNode context = null;
     private DocumentSequence documents = null;
     private XPathSelector selector = null;
     private XProcRuntime runtime = null;
     private boolean initialized = false;
     private int docindex = 0;
     private Step reader = null;
     private XStep forStep = null;

    
Creates a new instance of Select
 
     public XSelect(XProcRuntime runtimeXStep forStepReadablePipe readFromString xpathExprXdmNode context) {
          = readFrom;
          = xpathExpr;
         this. = runtime;
         this. = context;
          = new DocumentSequence(runtime);
         this. = forStep;
     }
 
     public void canReadSequence(boolean sequence) {
         // nop; always true
     }
 
     public boolean readSequence() {
         return true;
     }
     
     private void readSource() {
          = true;
 
         try {
             NamespaceBinding bindings = new NamespaceBinding(,);
             XPathCompiler xcomp = .getProcessor().newXPathCompiler();
             xcomp.setBaseURI(.getBaseURI());
 
             IndependentContext icontext = (IndependentContextxcomp.getUnderlyingStaticContext();
 
             Hashtable<QNameRuntimeValueinScopeOptions = new Hashtable<QNameRuntimeValue> ();
             try {
                 inScopeOptions = ((XCompoundStep).getInScopeOptions();
             } catch (ClassCastException cce) {
                 // FIXME: Surely there's a better way to do this!!!
             }
             
             Hashtable<QNameRuntimeValueboundOpts = new Hashtable<QNameRuntimeValue> ();
             for (QName name : inScopeOptions.keySet()) {
                 RuntimeValue v = inScopeOptions.get(name);
                 if (v.initialized()) {
                     boundOpts.put(namev);
                }
            }
            for (QName varname : boundOpts.keySet()) {
                xcomp.declareVariable(varname);
            }
            for (String prefix : bindings.getNamespaceBindings().keySet()) {
                xcomp.declareNamespace(prefixbindings.getNamespaceBindings().get(prefix));
            }
            XPathExecutable xexec = xcomp.compile();
             = xexec.load();
            for (QName varname : boundOpts.keySet()) {
                XdmAtomicValue avalue = boundOpts.get(varname).getUntypedAtomic();
                .setVariable(varname,avalue);
            }
        } catch (SaxonApiException sae) {
            if (S9apiUtils.xpathSyntaxError(sae)) {
                throw XProcException.dynamicError(23, "Invalid XPath expression: '" +  + "'.");
            } else {
                throw new XProcException(sae);
            }
        }
        while (.moreDocuments()) {
            // Ok, time to go looking for things to select from.
            try {
                XdmNode doc = .read();
                if ( != null) {
                    .finest(null.getNode(), .getName() + " select read '" + (doc == null ? "null" : doc.getBaseURI()) + "' from " + );
                }
                .setContextItem(doc);
                Iterator iter = .iterator();
                while (iter.hasNext()) {
                    XdmItem item = (XdmItemiter.next();
                    XdmNode node = null;
                    try {
                        node = (XdmNodeitem;
                        if ((node.getNodeKind() != .)
                            && (node.getNodeKind() != .)) {
                            throw XProcException.dynamicError(16);
                        }
                    } catch (ClassCastException cce) {
                        throw XProcException.dynamicError(16);
                    }
                    XdmDestination dest = new XdmDestination();
                    S9apiUtils.writeXdmValue(nodedestnode.getBaseURI());
                    XdmNode sdoc = dest.getXdmNode();
                    if ( != null) {
                        .finest(null.getNode(), .getName() + " select wrote '" + (sdoc == null ? "null" : sdoc.getBaseURI()) + "' to " + );
                    }
                    
                    .add(sdoc);
                }
            } catch (SaxonApiException sae) {
                throw new XProcException(sae);
            }
        }
    }
    public void resetReader() {
         = 0;
        .resetReader();
        .reset();
         = false;
    }
    public boolean moreDocuments() {
        if (!) {
            readSource();
        }
        return  < .size();
    }
    public boolean closed() {
        return true;
    }
    public int documentCount() {
        if (!) {
            readSource();
        }
        return .size();
    }
    public DocumentSequence documents() {
        return ;
    }
    public void setReader(Step step) {
         = step;
    }
    public XdmNode read () throws SaxonApiException {
        if (!) {
            readSource();
        }
        XdmNode doc = null;
        if (moreDocuments()) {
            doc = .get(++);
        }
        if ( != null) {
            .finest(null.getNode(), .getName() + " read '" + (doc == null ? "null" : doc.getBaseURI()) + "' from " + this);
        }
        return doc;
    }
    public String toString() {
        return "xselect " + ;
    }
New to GrepCode? Check out our FAQ X