| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 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 |
|
| 39 |
|
| 40 |
|
| 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 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public String getContentType() { |
| 93 |
return contentType == null ? "text/plain" : contentType; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
} |