|
| 1 | +/* |
| 2 | + * eXist-db Open Source Native XML Database |
| 3 | + * Copyright (C) 2001 The eXist-db Authors |
| 4 | + * |
| 5 | + * info@exist-db.org |
| 6 | + * http://www.exist-db.org |
| 7 | + * |
| 8 | + * This library is free software; you can redistribute it and/or |
| 9 | + * modify it under the terms of the GNU Lesser General Public |
| 10 | + * License as published by the Free Software Foundation; either |
| 11 | + * version 2.1 of the License, or (at your option) any later version. |
| 12 | + * |
| 13 | + * This library is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | + * Lesser General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU Lesser General Public |
| 19 | + * License along with this library; if not, write to the Free Software |
| 20 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | + */ |
| 22 | +package org.exist.xquery.functions.fn; |
| 23 | + |
| 24 | +import org.exist.dom.memtree.DocumentImpl; |
| 25 | +import org.exist.dom.memtree.NodeImpl; |
| 26 | +import org.exist.dom.persistent.*; |
| 27 | +import org.exist.util.XMLNames; |
| 28 | +import org.exist.xquery.*; |
| 29 | +import org.exist.xquery.Constants.Comparison; |
| 30 | +import org.exist.xquery.value.*; |
| 31 | +import org.w3c.dom.Node; |
| 32 | + |
| 33 | +import java.util.Set; |
| 34 | +import java.util.StringTokenizer; |
| 35 | +import java.util.TreeSet; |
| 36 | + |
| 37 | +import static org.exist.xquery.FunctionDSL.*; |
| 38 | +import static org.exist.xquery.functions.fn.FnModule.functionSignatures; |
| 39 | + |
| 40 | +public class FunElementWithId extends BasicFunction { |
| 41 | + private static final String FN_NAME = "element-with-id"; |
| 42 | + private static final String FN_DESCRIPTION = |
| 43 | + "Returns the sequence of element nodes that have an ID value " + |
| 44 | + "matching the value of one or more of the IDREF values supplied in $idrefs. " + |
| 45 | + "If none is matching or $idrefs is the empty sequence, returns the empty sequence."; |
| 46 | + private static final FunctionReturnSequenceType FN_RETURN = returnsOptMany(Type.STRING, "the elements with IDs matching IDREFs from $idref-sequence"); |
| 47 | + private static final FunctionParameterSequenceType PARAM_ID_REFS_STRING = optManyParam("idrefs", Type.STRING, "The IDREF sequence"); |
| 48 | + public static final FunctionSignature[] FS_ELEMENT_WITH_ID_SIGNATURES = functionSignatures( |
| 49 | + FN_NAME, |
| 50 | + FN_DESCRIPTION, |
| 51 | + FN_RETURN, |
| 52 | + arities( |
| 53 | + arity(), |
| 54 | + arity(PARAM_ID_REFS_STRING) |
| 55 | + ) |
| 56 | + ); |
| 57 | + |
| 58 | + public FunElementWithId(final XQueryContext context, final FunctionSignature signature) { |
| 59 | + super(context, signature); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public Sequence eval(final Sequence[] args, Sequence contextSequence) throws XPathException { |
| 64 | + if (getArgumentCount() < 1) { |
| 65 | + throw new XPathException(this, ErrorCodes.XPST0017, "function element-with-id requires one argument"); |
| 66 | + } |
| 67 | + |
| 68 | + final Sequence result; |
| 69 | + boolean processInMem = false; |
| 70 | + final Expression arg = getArgument(0); |
| 71 | + final Sequence idval = arg.eval(contextSequence); |
| 72 | + |
| 73 | + if (idval.isEmpty() || (getArgumentCount() == 1 && contextSequence != null && contextSequence.isEmpty())) { |
| 74 | + result = Sequence.EMPTY_SEQUENCE; |
| 75 | + } else { |
| 76 | + String nextId; |
| 77 | + DocumentSet docs = null; |
| 78 | + if (getArgumentCount() == 2) { |
| 79 | + final Sequence nodes = getArgument(1).eval(contextSequence); |
| 80 | + if (nodes.isEmpty()) { |
| 81 | + throw new XPathException(this, ErrorCodes.XPDY0002, "XPDY0002: no node or context item for fn:id", nodes); |
| 82 | + } else if (!Type.subTypeOf(nodes.itemAt(0).getType(), Type.NODE)) { |
| 83 | + throw new XPathException(this, ErrorCodes.XPTY0004, "XPTY0004: fn:id() argument is not a node", nodes); |
| 84 | + } |
| 85 | + NodeValue node = (NodeValue)nodes.itemAt(0); |
| 86 | + if (node.getImplementationType() == NodeValue.IN_MEMORY_NODE) { |
| 87 | + processInMem = true; |
| 88 | + } else { |
| 89 | + MutableDocumentSet ndocs = new DefaultDocumentSet(); |
| 90 | + ndocs.add(((NodeProxy)node).getOwnerDocument()); |
| 91 | + docs = ndocs; |
| 92 | + } |
| 93 | + contextSequence = node; |
| 94 | + } else if (contextSequence == null) { |
| 95 | + throw new XPathException(this, ErrorCodes.XPDY0002, "No context item specified"); |
| 96 | + } else if(!Type.subTypeOf(contextSequence.getItemType(), Type.NODE)) { |
| 97 | + throw new XPathException(this, ErrorCodes.XPTY0004, "Context item is not a node", contextSequence); |
| 98 | + } else { |
| 99 | + if (contextSequence.isPersistentSet()) { |
| 100 | + docs = contextSequence.toNodeSet().getDocumentSet(); |
| 101 | + } else { |
| 102 | + processInMem = true; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + if (processInMem) { |
| 107 | + result = new ValueSequence(); |
| 108 | + } else { |
| 109 | + result = new ExtArrayNodeSet(); |
| 110 | + } |
| 111 | + |
| 112 | + for(final SequenceIterator i = idval.iterate(); i.hasNext(); ) { |
| 113 | + nextId = i.nextItem().getStringValue(); |
| 114 | + if (!nextId.isEmpty()) { |
| 115 | + if (nextId.indexOf(' ') != Constants.STRING_NOT_FOUND) { |
| 116 | + final StringTokenizer tok = new StringTokenizer(nextId, " "); |
| 117 | + while (tok.hasMoreTokens()) { |
| 118 | + nextId = tok.nextToken(); |
| 119 | + if (XMLNames.isNCName(nextId)) { |
| 120 | + if (processInMem) { |
| 121 | + getId(result, contextSequence, nextId); |
| 122 | + } else { |
| 123 | + getId((NodeSet) result, docs, nextId); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } else { |
| 128 | + if (XMLNames.isNCName(nextId)) { |
| 129 | + if (processInMem) { |
| 130 | + getId(result, contextSequence, nextId); |
| 131 | + } else { |
| 132 | + getId((NodeSet) result, docs, nextId); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + result.removeDuplicates(); |
| 140 | + if (context.getProfiler().isEnabled()) { |
| 141 | + context.getProfiler().end(this, "", result); |
| 142 | + } |
| 143 | + |
| 144 | + return result; |
| 145 | + } |
| 146 | + |
| 147 | + private void getId(final NodeSet result, final DocumentSet docs, final String id) throws XPathException { |
| 148 | + final NodeSet attribs = context.getBroker().getValueIndex().find(context.getWatchDog(), Comparison.EQ, docs, null, -1, null, new StringValue(id, Type.ID)); |
| 149 | + NodeProxy p; |
| 150 | + for (final NodeProxy n : attribs) { |
| 151 | + p = new NodeProxy(n.getOwnerDocument(), n.getNodeId().getParentId(), Node.ELEMENT_NODE); |
| 152 | + result.add(p); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + private void getId(final Sequence result, final Sequence seq, final String id) throws XPathException { |
| 157 | + final Set<DocumentImpl> visitedDocs = new TreeSet<>(); |
| 158 | + for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) { |
| 159 | + final NodeImpl v = (NodeImpl) i.nextItem(); |
| 160 | + final DocumentImpl doc = v.getNodeType() == Node.DOCUMENT_NODE ? (DocumentImpl)v : v.getOwnerDocument(); |
| 161 | + |
| 162 | + if (doc != null && !visitedDocs.contains(doc)) { |
| 163 | + final NodeImpl elem = doc.selectById(id, true); |
| 164 | + if (elem != null) { |
| 165 | + result.add(elem); |
| 166 | + } |
| 167 | + visitedDocs.add(doc); |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public int getDependencies() { |
| 174 | + return getArgument(0).getDependencies(); |
| 175 | + } |
| 176 | +} |
0 commit comments