1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.millscript.millscript;
23
24 import org.millscript.millscript.alert.Alerts;
25
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
28 import java.io.Reader;
29 import java.io.UnsupportedEncodingException;
30 import java.net.URL;
31
32 /**
33 * Abstract class for representing sources of information. Using instances of
34 * this instead of using the relevant Java classes aids debugging, as it
35 * contains a method to obtain the origin of this source.
36 */
37 public abstract class Source {
38
39 /**
40 * Returns an InputStream to read bytes from this source.
41 *
42 * @return an InputStream to read bytes from this source.
43 */
44 public abstract InputStream getInputStream();
45
46 /**
47 * Returns this sources origin.
48 *
49 * @return a String containing this origins source
50 */
51 public abstract String getOrigin();
52
53 /**
54 * Returns a URL for the sources origin.
55 *
56 * @return a URL for this sources origin.
57 */
58 public abstract URL getOriginURL();
59
60 /**
61 * Returns a Reader to read characters from this source. This method
62 * assumes UTF8 encoding for the character data.
63 *
64 * @return a Reader to read characters from this source.
65 */
66 public Reader getReader() {
67 try {
68 return new InputStreamReader( getInputStream(), "UTF8" );
69 } catch ( UnsupportedEncodingException ex ) {
70 throw(
71 Alerts.compile(
72 "Problem opening source for reading",
73 "Unsupported encoding"
74 ).
75 culprit( "source", getOrigin() ).
76 culprit( "reason", ex.getMessage() ).
77 mishap()
78 );
79 }
80 }
81
82 }