|
| 1 | +/* |
| 2 | + * eXist-db Open Source Native XML Database |
| 3 | + * Copyright (C) 2001 The eXist-db Authors |
| 4 | + * |
| 5 | + |
| 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 | + |
| 23 | +package org.exist.xquery.functions.fn; |
| 24 | + |
| 25 | +import org.exist.collections.Collection; |
| 26 | +import org.exist.dom.persistent.BinaryDocument; |
| 27 | +import org.exist.dom.persistent.DocumentImpl; |
| 28 | +import org.exist.security.PermissionDeniedException; |
| 29 | +import org.exist.storage.lock.Lock; |
| 30 | +import org.exist.util.LockException; |
| 31 | +import org.exist.xmldb.XmldbURI; |
| 32 | +import org.exist.xquery.*; |
| 33 | +import org.exist.xquery.value.*; |
| 34 | + |
| 35 | +import java.net.URISyntaxException; |
| 36 | +import java.util.*; |
| 37 | +import java.util.regex.Pattern; |
| 38 | +import java.util.stream.Collectors; |
| 39 | + |
| 40 | +import static org.exist.xquery.FunctionDSL.*; |
| 41 | +import static org.exist.xquery.functions.fn.FnModule.functionSignatures; |
| 42 | + |
| 43 | +public class FunUriCollection extends BasicFunction { |
| 44 | + |
| 45 | + private static final String FN_NAME = "uri-collection"; |
| 46 | + private static final String FN_DESCRIPTION = "Returns a sequence of xs:anyURI values that represent the URIs in a URI collection."; |
| 47 | + private static final FunctionReturnSequenceType FN_RETURN = returnsOptMany(Type.STRING, |
| 48 | + "the default URI collection, if $arg is not specified or is an empty sequence, " + |
| 49 | + "or the sequence of URIs that correspond to the supplied URI"); |
| 50 | + private static final FunctionParameterSequenceType ARG = optParam("arg", Type.STRING, "The base-URI property from the static context, or an empty sequence"); |
| 51 | + public static final FunctionSignature[] FS_URI_COLLECTION_SIGNATURES = functionSignatures( |
| 52 | + FN_NAME, |
| 53 | + FN_DESCRIPTION, |
| 54 | + FN_RETURN, |
| 55 | + arities( |
| 56 | + arity(), |
| 57 | + arity(ARG) |
| 58 | + ) |
| 59 | + ); |
| 60 | + |
| 61 | + private static final String KEY_CONTENT_TYPE = "content-type"; |
| 62 | + private static final String VALUE_CONTENT_TYPE_DOCUMENT = "application/vnd.existdb.document"; |
| 63 | + private static final String VALUE_CONTENT_TYPE_DOCUMENT_BINARY = "application/vnd.existdb.document+binary"; |
| 64 | + private static final String VALUE_CONTENT_TYPE_DOCUMENT_XML = "application/vnd.existdb.document+xml"; |
| 65 | + private static final String VALUE_CONTENT_TYPE_SUBCOLLECTION = "application/vnd.existdb.collection"; |
| 66 | + private static final String[] VALUE_CONTENT_TYPES = { |
| 67 | + VALUE_CONTENT_TYPE_DOCUMENT, |
| 68 | + VALUE_CONTENT_TYPE_DOCUMENT_BINARY, |
| 69 | + VALUE_CONTENT_TYPE_DOCUMENT_XML, |
| 70 | + VALUE_CONTENT_TYPE_SUBCOLLECTION |
| 71 | + }; |
| 72 | + |
| 73 | + private static final String KEY_STABLE = "stable"; |
| 74 | + private static final String VALUE_STABLE_NO = "no"; |
| 75 | + private static final String VALUE_STABLE_YES = "yes"; |
| 76 | + private static final String[] VALUE_STABLES = { |
| 77 | + VALUE_STABLE_NO, |
| 78 | + VALUE_STABLE_YES |
| 79 | + }; |
| 80 | + |
| 81 | + private static final String KEY_MATCH = "match"; |
| 82 | + |
| 83 | + public FunUriCollection(final XQueryContext context, final FunctionSignature signature) { |
| 84 | + super(context, signature); |
| 85 | + } |
| 86 | + |
| 87 | + public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException { |
| 88 | + final Sequence result; |
| 89 | + if (args.length == 0 || args[0].isEmpty() || args[0].toString().isEmpty()) { |
| 90 | + if (XQueryContext.DEFAULT_URI_COLLECTION != null && XQueryContext.DEFAULT_URI_COLLECTION.length() > 0) { |
| 91 | + result = new StringValue(XQueryContext.DEFAULT_URI_COLLECTION); |
| 92 | + } else { |
| 93 | + throw new XPathException(this, ErrorCodes.FODC0002, "No URI is supplied and default resource collection is absent."); |
| 94 | + } |
| 95 | + } else { |
| 96 | + final List<String> resultUris = new LinkedList<String>(); |
| 97 | + |
| 98 | + final String uriWithQueryString = args[0].toString(); |
| 99 | + final int queryStringIndex = uriWithQueryString.indexOf('?'); |
| 100 | + final String uriWithoutQueryString = (queryStringIndex >= 0) ? uriWithQueryString.substring(0, queryStringIndex) : uriWithQueryString; |
| 101 | + String uriWithoutStableQueryString = uriWithQueryString.replaceAll(String.format("%s\\s*=\\s*\\byes|no\\b\\s*&+", KEY_STABLE), ""); |
| 102 | + if (uriWithoutStableQueryString.endsWith("?")) { |
| 103 | + uriWithoutStableQueryString = uriWithoutStableQueryString.substring(0, uriWithoutStableQueryString.length() - 1); |
| 104 | + } |
| 105 | + |
| 106 | + final XmldbURI uri; |
| 107 | + try { |
| 108 | + uri = XmldbURI.xmldbUriFor(uriWithoutQueryString); |
| 109 | + } catch (URISyntaxException e) { |
| 110 | + throw new XPathException(this, ErrorCodes.FODC0004, String.format("\"%s\" is not a valid URI.", args[0].toString())); |
| 111 | + } |
| 112 | + |
| 113 | + final Map<String, String> queryStringMap = parseQueryString(uriWithQueryString); |
| 114 | + checkQueryStringMap(queryStringMap); |
| 115 | + |
| 116 | + if ((!queryStringMap.containsKey(KEY_STABLE) || queryStringMap.get(KEY_STABLE).equals(VALUE_STABLE_YES)) && |
| 117 | + context.getCachedUriCollectionResults().containsKey(uriWithoutStableQueryString)) { |
| 118 | + result = context.getCachedUriCollectionResults().get(uriWithoutStableQueryString); |
| 119 | + } else { |
| 120 | + final boolean binaryUrisIncluded = !queryStringMap.containsKey(KEY_CONTENT_TYPE) || |
| 121 | + (queryStringMap.get(KEY_CONTENT_TYPE).equals(VALUE_CONTENT_TYPE_DOCUMENT) || |
| 122 | + queryStringMap.get(KEY_CONTENT_TYPE).equals(VALUE_CONTENT_TYPE_DOCUMENT_BINARY)); |
| 123 | + final boolean subcollectionUrisIncluded = !queryStringMap.containsKey(KEY_CONTENT_TYPE) || |
| 124 | + queryStringMap.get(KEY_CONTENT_TYPE).equals(VALUE_CONTENT_TYPE_SUBCOLLECTION); |
| 125 | + final boolean xmlUrisIncluded = !queryStringMap.containsKey(KEY_CONTENT_TYPE) || |
| 126 | + (queryStringMap.get(KEY_CONTENT_TYPE).equals(VALUE_CONTENT_TYPE_DOCUMENT) || |
| 127 | + queryStringMap.get(KEY_CONTENT_TYPE).equals(VALUE_CONTENT_TYPE_DOCUMENT_XML)); |
| 128 | + |
| 129 | + try (final Collection collection = context.getBroker().openCollection(uri, Lock.LockMode.READ_LOCK)) { |
| 130 | + if (binaryUrisIncluded || xmlUrisIncluded) { |
| 131 | + final Iterator<DocumentImpl> documentIterator = collection.iterator(context.getBroker()); |
| 132 | + while (documentIterator.hasNext()) { |
| 133 | + final DocumentImpl document = documentIterator.next(); |
| 134 | + if ((xmlUrisIncluded && !(document instanceof BinaryDocument)) || |
| 135 | + (binaryUrisIncluded && document instanceof BinaryDocument)) { |
| 136 | + resultUris.add(document.getURI().toString()); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + if (subcollectionUrisIncluded) { |
| 142 | + final Iterator<XmldbURI> collectionsIterator = collection.collectionIterator(context.getBroker()); |
| 143 | + while (collectionsIterator.hasNext()) { |
| 144 | + resultUris.add(uri.append(collectionsIterator.next()).toString()); |
| 145 | + } |
| 146 | + } |
| 147 | + } catch (final LockException | PermissionDeniedException e) { |
| 148 | + throw new XPathException(this, ErrorCodes.FODC0002, e); |
| 149 | + } |
| 150 | + |
| 151 | + if (queryStringMap.containsKey(KEY_MATCH) && queryStringMap.get(KEY_MATCH).length() > 0) { |
| 152 | + final Pattern pattern = Pattern.compile(queryStringMap.get(KEY_MATCH)); |
| 153 | + final List<String> matchedResultUris = resultUris.stream().filter(resultUri -> pattern.matcher(resultUri).find()).collect(Collectors.toList()); |
| 154 | + if (matchedResultUris.isEmpty()) { |
| 155 | + result = Sequence.EMPTY_SEQUENCE; |
| 156 | + } else { |
| 157 | + result = new ValueSequence(); |
| 158 | + for (String resultUri : matchedResultUris) { |
| 159 | + result.add(new StringValue(resultUri)); |
| 160 | + } |
| 161 | + } |
| 162 | + } else { |
| 163 | + result = new ValueSequence(); |
| 164 | + for (String resultUri : resultUris) { |
| 165 | + result.add(new StringValue(resultUri)); |
| 166 | + } |
| 167 | + } |
| 168 | + context.getCachedUriCollectionResults().put(uriWithoutStableQueryString, result); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + return result; |
| 173 | + } |
| 174 | + |
| 175 | + private static Map<String, String> parseQueryString(final String uri) { |
| 176 | + final Map<String, String> map = new HashMap<>(); |
| 177 | + final int questionMarkIndex = (uri == null) ? -1 : uri.indexOf('?'); |
| 178 | + if (questionMarkIndex >= 0 && questionMarkIndex + 1 < uri.length()) { |
| 179 | + String[] keyValuePairs = uri.substring(questionMarkIndex + 1).split("&"); |
| 180 | + for (String keyValuePair : keyValuePairs) { |
| 181 | + int equalIndex = keyValuePair.indexOf('='); |
| 182 | + if (equalIndex >= 0) { |
| 183 | + if (equalIndex + 1 < uri.length()) { |
| 184 | + map.put(keyValuePair.substring(0, equalIndex).trim(), keyValuePair.substring(equalIndex + 1).trim()); |
| 185 | + } else { |
| 186 | + map.put(keyValuePair.substring(0, equalIndex).trim(), ""); |
| 187 | + } |
| 188 | + } else { |
| 189 | + map.put(keyValuePair.trim(), ""); |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + return map; |
| 195 | + } |
| 196 | + |
| 197 | + private void checkQueryStringMap(final Map<String, String> queryStringMap) throws XPathException { |
| 198 | + for (Map.Entry<String, String> queryStringEntry : queryStringMap.entrySet()) { |
| 199 | + final String key = queryStringEntry.getKey(); |
| 200 | + final String value = queryStringEntry.getValue(); |
| 201 | + if (key.equals(KEY_CONTENT_TYPE)) { |
| 202 | + if (!Arrays.stream(VALUE_CONTENT_TYPES).anyMatch(contentTypeValue -> contentTypeValue.equals(value))) { |
| 203 | + throw new XPathException(this, ErrorCodes.FODC0004, String.format("Invalid query-string value \"%s\".", queryStringEntry)); |
| 204 | + } |
| 205 | + } else if (key.equals(KEY_STABLE)) { |
| 206 | + if (!Arrays.stream(VALUE_STABLES).anyMatch(stableValue -> stableValue.equals(value))) { |
| 207 | + throw new XPathException(this, ErrorCodes.FODC0004, String.format("Invalid query-string value \"%s\".", queryStringEntry)); |
| 208 | + } |
| 209 | + } else if (!key.equals(KEY_MATCH)) { |
| 210 | + throw new XPathException(this, ErrorCodes.FODC0004, String.format("Unexpected query string \"%s\".", queryStringEntry)); |
| 211 | + } |
| 212 | + } |
| 213 | + } |
| 214 | +} |
0 commit comments