Project

Profile

Help

Revision 74bb5cdd

Added by Michael Kay over 1 year ago

Fix bug #4476

View differences:

.gitattributes
6571 6571
latest9.9/hej/net/sf/saxon/resource/JarCollection.java svn_eol_002dstyle=native
6572 6572
latest9.9/hej/net/sf/saxon/resource/MetadataResource.java svn_eol_002dstyle=native
6573 6573
latest9.9/hej/net/sf/saxon/resource/StandardCollectionFinder.java svn_eol_002dstyle=native
6574
latest9.9/hej/net/sf/saxon/resource/UnknownResource.java svn_eol_002dstyle=native
6574 6575
latest9.9/hej/net/sf/saxon/resource/UnparsedTextResource.java svn_eol_002dstyle=native
6575 6576
latest9.9/hej/net/sf/saxon/resource/XmlResource.java svn_eol_002dstyle=native
6576 6577
latest9.9/hej/net/sf/saxon/resource/package.html svn_eol_002dstyle=native
latest9.9/hej/net/sf/saxon/Configuration.java
530 530
        registerFileExtension("MF", "text/plain");
531 531
        registerFileExtension("class", "application/java");
532 532
        registerFileExtension("json", "application/json");
533
        registerFileExtension("", "application/binary");
533
        registerFileExtension("", "application/unknown");
534 534

  
535 535
        registerMediaType("application/xml", XmlResource.FACTORY);
536 536
        registerMediaType("text/xml", XmlResource.FACTORY);
......
544 544
        registerMediaType("application/java", BinaryResource.FACTORY);
545 545
        registerMediaType("application/binary", BinaryResource.FACTORY);
546 546
        registerMediaType("application/json", JSONResource.FACTORY);
547
        registerMediaType("application/unknown", UnknownResource.FACTORY);
547 548

  
548 549
        registerFunctionAnnotationHandler(new XQueryFunctionAnnotationHandler());
549 550
    }
latest9.9/hej/net/sf/saxon/resource/JSONResource.java
43 43

  
44 44
    public JSONResource(AbstractResourceCollection.InputDetails details) {
45 45
        this.href = details.resourceUri;
46
        if (details.encoding == null) {
47
            details.encoding = "UTF-8";
48
        }
46 49
        this.details = details;
47 50
    }
48 51

  
latest9.9/hej/net/sf/saxon/resource/UnknownResource.java
1
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright (c) 2018-2020 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.om.Item;
15
import net.sf.saxon.trans.XPathException;
16

  
17
import java.io.ByteArrayInputStream;
18
import java.io.IOException;
19
import java.io.InputStream;
20
import java.net.URLConnection;
21

  
22
/**
23
 * The class is an implementation of the generic Resource object (typically an item in a collection)
24
 * representing a resource whose type is not yet known - typically because it uses an unregistered
25
 * file extension. We attempt to establish a type for the resource when it is opened, by "sniffing" the content.
26
 */
27

  
28
public class UnknownResource implements Resource {
29

  
30
    private Configuration config;
31
    private AbstractResourceCollection.InputDetails details;
32

  
33
    public static final ResourceFactory FACTORY = UnknownResource::new;
34

  
35
    public UnknownResource(Configuration config, AbstractResourceCollection.InputDetails details) {
36
        this.config = config;
37
        this.details = details;
38
    }
39

  
40
    public String getResourceURI() {
41
        return details.resourceUri;
42
    }
43

  
44
    /**
45
     * Get an item representing the resource: in this case a document node for the XML document.
46
     *
47
     * @param context the XPath evaluation context
48
     * @return the document; or null if there is an error and the error is to be ignored
49
     * @throws XPathException if (for example) XML parsing fails
50
     */
51

  
52
    public Item getItem(XPathContext context) throws XPathException {
53
        InputStream stream;
54
        if (details.binaryContent != null) {
55
            stream = new ByteArrayInputStream(details.binaryContent);
56
        } else {
57
            try {
58
                stream = details.getInputStream();
59
            } catch (IOException e) {
60
                throw new XPathException(e);
61
            }
62
        }
63
        if (stream == null) {
64
            throw new XPathException("Unable to dereference resource URI " + details.resourceUri);
65
        }
66
        String mediaType;
67
        try {
68
            mediaType = URLConnection.guessContentTypeFromStream(stream);
69
        } catch (IOException e) {
70
            mediaType = null;
71
        }
72
        if (mediaType == null) {
73
            mediaType = config.getMediaTypeForFileExtension("");
74
        }
75
        if (mediaType == null || mediaType.equals("application/unknown")) {
76
            mediaType = "application/binary";
77
        }
78
        details.contentType = mediaType;
79
        details.binaryContent = BinaryResource.readBinaryFromStream(stream, details.resourceUri);
80
        ResourceFactory delegee = config.getResourceFactoryForMediaType(mediaType);
81
        Resource actual = delegee.makeResource(config, details);
82
        return actual.getItem(context);
83
    }
84

  
85
    /**
86
     * Get the media type (MIME type) of the resource if known
87
     *
88
     * @return the string "application/xml"
89
     */
90

  
91
    public String getContentType() {
92
        return "application/xml";
93
    }
94

  
95

  
96
}

Also available in: Unified diff