View Javadoc

1   /*$Id: IOHelper.java,v 1.5 2004/12/07 16:50:33 jdt Exp $
2    * Created on 22-Nov-2004
3    *
4    * Copyright (C) AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid 
7    * Software License version 1.2, a copy of which has been included 
8    * with this distribution in the LICENSE.txt file.  
9    *
10  **/
11  package org.astrogrid.scripting;
12  
13  import org.astrogrid.applications.parameter.protocol.DefaultProtocolLibraryFactory;
14  import org.astrogrid.applications.parameter.protocol.ExternalValue;
15  import org.astrogrid.applications.parameter.protocol.InaccessibleExternalValueException;
16  import org.astrogrid.applications.parameter.protocol.ProtocolLibrary;
17  import org.astrogrid.applications.parameter.protocol.UnrecognizedProtocolException;
18  import org.astrogrid.io.Piper;
19  
20  import java.io.ByteArrayInputStream;
21  import java.io.ByteArrayOutputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.OutputStream;
25  import java.io.Reader;
26  import java.io.StringReader;
27  import java.io.StringWriter;
28  import java.io.Writer;
29  import java.net.URI;
30  import java.net.URISyntaxException;
31  import java.net.URL;
32  import java.text.SimpleDateFormat;
33  import java.util.Date;
34  
35  /*** hellper methods for working with IO.
36   * 
37   * <p>
38   * would have liked to just extend piper - but has no public constructor.
39   * @see org.astrogrid.io.Piper
40   * @author Noel Winstanley nw@jb.man.ac.uk 22-Nov-2004
41   *@script-summary help with input / output
42   *@script-doc helper methods for working with streams and external values
43   */
44  public class IOHelper {
45  
46      /*** Construct a new IOHelper
47       * 
48       */
49      public IOHelper() {
50          super();
51      }
52     /***@script-doc pipe contents of <code>in</code> to <code>out</code> */
53      public void bufferedPipe(InputStream in, OutputStream out) throws IOException {
54          Piper.bufferedPipe(in,out);
55      }
56      /***@script-doc pipe contents of <code>in</code> to <code>out</code> */    
57      public void bufferedPipe(Reader in,Writer out) throws IOException {
58          Piper.bufferedPipe(in,out);
59      }
60      /***@script-doc pipe contents of <code>in</code> to <code>out</code> */
61      public void pipe(InputStream in, OutputStream out) throws IOException {
62          Piper.pipe(in,out);
63      }
64      /***@script-doc pipe contents of <code>in</code> to <code>out</code> */
65      public void pipe(Reader in, Writer out) throws IOException {
66          Piper.pipe(in,out);
67      }
68      /***@script-doc pipe contents of <code>in</code> to <code>out</code> */
69      public void pipe(ExternalValue in, OutputStream out) throws InaccessibleExternalValueException, IOException {
70          pipe(in.read(),out);
71      }
72      /***@script-doc pipe contents of <code>in</code> to <code>out</code> */
73      public void pipe(InputStream in,ExternalValue out) throws InaccessibleExternalValueException, IOException {
74          pipe(in,out.write());
75      }
76      /***@script-doc pipe contents of <code>in</code> to <code>out</code> */    
77      public void pipe(ExternalValue in,ExternalValue out) throws InaccessibleExternalValueException, IOException {
78          pipe(in.read(),out.write());
79      }
80      /****@script-doc returns a stream of the contents of a string */
81      public InputStream streamFromString(String content) {
82          return new ByteArrayInputStream(content.getBytes());
83      }
84      /***@script-doc returns a reader of the contents of a string */
85      public Reader readFromString(String content) {
86          return new StringReader(content);
87      }
88      /*** @script-doc read contents of a stream */
89      public String getContents(InputStream is) throws IOException {
90          ByteArrayOutputStream os = new ByteArrayOutputStream();
91          this.pipe(is,os);
92          is.close();
93          os.close();
94          return os.toString();
95      }
96      /*** @script-doc read contents of a stream */    
97      public  String getContents(Reader r) throws IOException {
98          StringWriter sw = new StringWriter();
99          this.pipe(r,sw);
100         r.close();
101         sw.close();
102         return sw.toString();             
103     }
104     /*** @script-doc read contents of an external value */
105     public String getContents(ExternalValue ext) throws InaccessibleExternalValueException, IOException {
106         return getContents(ext.read());
107     }
108     /*** @script-doc generate a date-stamp in format 'DD-MM-YY' */
109     public String dateStamp() {
110         return dateFormat.format(new Date());
111     }
112     
113     private final SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yy");
114     /*** @script-doc generate a time-stamp as a long number */
115     public String timeStamp() {
116         return String.valueOf(System.currentTimeMillis());
117     }
118     /*** @script-doc generate a date/time stamp in format 'DD-MM-YY-HHMMSS' */
119     public String dateTimeStamp() {
120         return dateTimeFormat.format(new Date());
121     }
122     
123     private final SimpleDateFormat dateTimeFormat = new SimpleDateFormat("dd-MM-yy-HHmmss");
124 
125     /*** access library object that 'knows' about a variety of IO protocols, and can construct {@link ExternalValue} objects to 
126      * read / write resources via these protocols.
127      */
128     public ProtocolLibrary getProtocolLibrary() {
129         return protocolLib;
130     }
131 
132     private final ProtocolLibrary protocolLib = (new DefaultProtocolLibraryFactory()).createLibrary();
133     
134     /*** create an external value that points to a vospace uri */
135     public ExternalValue getExternalValue(String uri) throws InaccessibleExternalValueException, UnrecognizedProtocolException, URISyntaxException {
136         return protocolLib.getExternalValue(uri);
137     }
138 
139     /*** create an external value that points to a vospace uri */    
140     public ExternalValue getExternalValue(URI uri) throws InaccessibleExternalValueException, UnrecognizedProtocolException {
141         return protocolLib.getExternalValue(uri);
142     }
143 
144     /*** create an external value that points to a vosapce uri */    
145     public ExternalValue getExternalValue(URL uri) throws InaccessibleExternalValueException, UnrecognizedProtocolException, URISyntaxException {
146         return protocolLib.getExternalValue(uri.toString());
147     }
148     
149 }
150 
151 
152 /* 
153 $Log: IOHelper.java,v $
154 Revision 1.5  2004/12/07 16:50:33  jdt
155 merges from scripting-nww-805
156 
157 Revision 1.4.2.1  2004/12/07 14:47:58  nw
158 got table manipulation working.
159 
160 Revision 1.4  2004/12/06 20:03:03  clq2
161 nww_807a
162 
163 Revision 1.3.2.1  2004/12/06 13:27:47  nw
164 fixes to improvide integration with external values and starTables.
165 
166 Revision 1.3  2004/11/30 15:39:56  clq2
167 scripting-nww-777
168 
169 Revision 1.1.2.1.2.1  2004/11/26 15:38:16  nw
170 improved some names, added some missing methods.
171 
172 Revision 1.1.2.1  2004/11/22 15:54:51  nw
173 deprecated existing scripting interface (which includes service lists).
174 produced new scripting interface, with more helpler objects.
175  
176 */