1
2
3
4
5
6
7
8
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176