View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2004 Kevin Rogers
4   //
5   // This file is part of MillScript.
6   //
7   // MillScript is free software; you can redistribute it and/or modify it under
8   // the terms of the GNU General Public License as published by the Free
9   // Software Foundation; either version 2 of the License, or (at your option)
10  // any later version.
11  //
12  // MillScript is distributed in the hope that it will be useful, but WITHOUT
13  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  // more details.
16  //
17  // You should have received a copy of the GNU General Public License along with
18  // MillScript; if not, write to the Free Software Foundation, Inc., 59 Temple
19  // Place, Suite 330, Boston, MA  02111-1307  USA
20  ////////////////////////////////////////////////////////////////////////////////
21  package org.millscript.millscript;
22  
23  import org.millscript.millscript.alert.Alerts;
24  
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.net.URL;
28  
29  import jline.ConsoleReader;
30  import jline.ConsoleReaderInputStream;
31  
32  /**
33   * A standard input source, e.g. for reading from standard input during an
34   * interactive session.
35   */
36  public class StandardInputSource extends Source {
37  
38      /**
39       * @see org.millscript.millscript.Source#getInputStream()
40       */
41      @Override
42      public InputStream getInputStream() {
43          try {
44              final ConsoleReaderInputStream cris = new ConsoleReaderInputStream( new ConsoleReader() );
45              cris.setPrompt( ">>>" );
46              return cris;
47          } catch ( IOException ex ) {
48              throw(
49                  Alerts.compile(
50                      "Problem opening URL for reading",
51                      null
52                  ).
53                  culprit( "source", getOrigin() ).
54                  culprit( "reason", ex.getMessage() ).
55                  mishap()
56              );
57          }
58      }
59  
60      /**
61       * @see org.millscript.millscript.Source#getOrigin()
62       */
63      @Override
64      public String getOrigin() {
65          return "Standard Input";
66      }
67  
68      /**
69       * @see org.millscript.millscript.Source#getOriginURL()
70       */
71      @Override
72      public URL getOriginURL() {
73          // There is no suitable value for this.
74          throw new UnsupportedOperationException( "Cannot make URL for standard input" );
75      }
76  
77  }