Debian Codegraph @

net/sf/saxon/resource/MetadataResource.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.resource;
9
10 import net.sf.saxon.expr.Callable;
11 import net.sf.saxon.expr.XPathContext;
12 import net.sf.saxon.functions.CallableFunction;
13 import net.sf.saxon.lib.Resource;
14 import net.sf.saxon.ma.map.DictionaryMap;
15 import net.sf.saxon.om.GroundedValue;
16 import net.sf.saxon.om.Item;
17 import net.sf.saxon.type.FunctionItemType;
18 import net.sf.saxon.type.SpecificFunctionType;
19 import net.sf.saxon.value.SequenceType;
20 import net.sf.saxon.value.StringValue;
21
22 import java.util.Map;
23
24 /**
25 * Created by mike on 28/10/15.
26 */
27 public class MetadataResource implements Resource {
28
29 private Map<String, GroundedValue<?>> properties;
30 private String resourceURI;
31 private Resource content;
32
33 public MetadataResource(String resourceURI, Resource content, Map<String, GroundedValue<?>> properties) {
34 this.resourceURI = resourceURI;
35 this.content = content;
36 this.properties = properties;
37 }
38
39 public String getContentType() {
40 return content.getContentType();
41 }
42
43 public String getResourceURI() {
44 return resourceURI;
45 }
46
47 public Item getItem(XPathContext context) {
48
49 // Create a map for the result
50 DictionaryMap map = new DictionaryMap();
51
52 // Add the custom properties of the resource
53 for (Map.Entry<String, GroundedValue<?>> entry : properties.entrySet()) {
54 map.initialPut(entry.getKey(), entry.getValue());
55 }
56
57 // Add the resourceURI of the resource as the "name" property
58 map.initialPut("name", StringValue.makeStringValue(resourceURI));
59
60 // Add a fetch() function, which can be used to fetch the resource
61 Callable fetcher = (context1, arguments) -> content.getItem(context1);
62
63 FunctionItemType fetcherType = new SpecificFunctionType(new SequenceType[0], SequenceType.SINGLE_ITEM);
64 CallableFunction fetcherFunction = new CallableFunction(0, fetcher, fetcherType);
65
66 map.initialPut("fetch", fetcherFunction);
67 return map;
68 }
69 }
70