View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2001-2004 Open World Ltd
4   // Copyright (C) 2004-2005 Kevin Rogers
5   //
6   // This file is part of MillScript.
7   //
8   // MillScript is free software; you can redistribute it and/or modify it under
9   // the terms of the GNU General Public License as published by the Free
10  // Software Foundation; either version 2 of the License, or (at your option)
11  // any later version.
12  //
13  // MillScript is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  // more details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // MillScript; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA  02111-1307  USA
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  }