Debian Codegraph @

net/sf/saxon/resource/UnparsedTextResource.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.Configuration;
11 import net.sf.saxon.expr.XPathContext;
12 import net.sf.saxon.lib.Resource;
13 import net.sf.saxon.lib.ResourceFactory;
14 import net.sf.saxon.lib.StandardUnparsedTextResolver;
15 import net.sf.saxon.om.Item;
16 import net.sf.saxon.trans.XPathException;
17 import net.sf.saxon.value.StringValue;
18
19 import java.io.FileNotFoundException;
20 import java.io.IOException;
21 import java.io.InputStream;
22
23 /**
24 * This class implements th interface Resource. We handle unparded text here.
25 * The Resource objects belong to a collection
26 * It is used to support the fn:collection() and fn:uri-collection() functions.
27 *
28 * @since 9.7
29 */
30 public class UnparsedTextResource implements Resource {
31 private String contentType;
32 private String encoding;
33 private String href = null;
34 private String unparsedText = null;
35 private InputStream inputStream;
36
37 /**
38 * Create an UnparsedTextResource
39 * @param href the URI of the resource
40 * @param details the input stream giving access to the content of the resource, plus encoding info
41 */
42
43 public UnparsedTextResource(String href, AbstractResourceCollection.InputDetails details) {
44 this.href = href;
45 this.inputStream = details.inputStream;
46 this.contentType = details.contentType;
47 this.encoding = details.encoding;
48 }
49
50 public final static ResourceFactory FACTORY = new ResourceFactory() {
51 public Resource makeResource(Configuration config, String resourceURI, String contentType, AbstractResourceCollection.InputDetails details) throws XPathException {
52 return new UnparsedTextResource(resourceURI, details);
53 }
54 };
55
56 public String getResourceURI() {
57 return href;
58 }
59
60 public InputStream getInputStream() {
61 return inputStream;
62 }
63
64 public String getEncoding() {
65 return encoding;
66 }
67
68 public Item getItem(XPathContext context) throws XPathException {
69 if (unparsedText == null) {
70 StringBuilder builder = null;
71 try {
72 String enc = encoding;
73 if (enc == null) {
74 enc = StandardUnparsedTextResolver.inferStreamEncoding(inputStream, null);
75 }
76 builder = CatalogCollection.makeStringBuilderFromStream(inputStream, enc);
77 } catch (FileNotFoundException e) {
78 throw new XPathException(e);
79 } catch (IOException e) {
80 throw new XPathException(e);
81 }
82 unparsedText = builder.toString();
83 }
84 return new StringValue(unparsedText);
85 }
86
87 /**
88 * Get the media type (MIME type) of the resource if known
89 * @return the string "text/plain"
90 */
91
92 public String getContentType() {
93 return contentType == null ? "text/plain" : contentType;
94 }
95
96
97 }