| 1 |
package xtpipes; |
| 2 |
/* |
| 3 |
InputObject.java (2009-01-27-22:19) |
| 4 |
*/ |
| 5 |
import java.io.PrintWriter; |
| 6 |
import java.net.URL; |
| 7 |
import java.net.URLConnection; |
| 8 |
import java.io.ByteArrayInputStream; |
| 9 |
import java.io.File; |
| 10 |
import java.io.InputStream; |
| 11 |
|
| 12 |
|
| 13 |
public class InputObject{ |
| 14 |
InputStream inputStream = null; |
| 15 |
URLConnection connection = null; |
| 16 |
String filename = null; |
| 17 |
static PrintWriter log; |
| 18 |
String dtdRoot = null, |
| 19 |
publicId = null, |
| 20 |
systemId = null, |
| 21 |
xtpipes = null, |
| 22 |
url = null, |
| 23 |
metaType = null, |
| 24 |
contentType = null, |
| 25 |
root = null; |
| 26 |
|
| 27 |
public InputObject( String filename, PrintWriter log ){ |
| 28 |
InputObject.log = log; |
| 29 |
filename = filename.trim(); |
| 30 |
try{ |
| 31 |
inputStream = getInputStream(filename); |
| 32 |
} catch (Exception exp0){ |
| 33 |
if( !filename.startsWith( "http://" ) ){ |
| 34 |
try{ |
| 35 |
String name = "http://" + filename; |
| 36 |
inputStream = getInputStream( name ); |
| 37 |
filename = name; |
| 38 |
} catch (Exception exp1){ |
| 39 |
try{ |
| 40 |
String name = FileInfo.cleanPath(filename); |
| 41 |
inputStream = getInputStream( name ); |
| 42 |
filename = name; |
| 43 |
} catch (Exception exp2){ inputStream = null; } |
| 44 |
} } } |
| 45 |
this.filename = filename; |
| 46 |
} |
| 47 |
public InputObject( byte [] bytes, PrintWriter log ){ |
| 48 |
InputObject.log = log; |
| 49 |
inputStream = new ByteArrayInputStream( bytes ); |
| 50 |
} |
| 51 |
|
| 52 |
private java.io.InputStream getInputStream( |
| 53 |
String filename ) |
| 54 |
throws java.io.IOException{ |
| 55 |
if( filename == null ){ return null; } |
| 56 |
URL url; |
| 57 |
java.io.InputStream inputStream = null; |
| 58 |
// String loadingError = "Failed to get requested file."; |
| 59 |
try { |
| 60 |
url = new File(filename).toURI().toURL(); |
| 61 |
inputStream = getInputStream( url ); |
| 62 |
} catch (Exception ie) { |
| 63 |
try { |
| 64 |
url = new URL(filename); |
| 65 |
inputStream = getInputStream( url ); |
| 66 |
} catch (java.io.FileNotFoundException ife) { |
| 67 |
throw new java.io.IOException( |
| 68 |
"File not found: " + filename); |
| 69 |
} catch (Exception ife) { |
| 70 |
throw new java.io.IOException(ife + "\n" + ie); |
| 71 |
} } |
| 72 |
return inputStream; |
| 73 |
} |
| 74 |
|
| 75 |
private java.io.InputStream getInputStream( URL url ) |
| 76 |
throws java.io.FileNotFoundException, |
| 77 |
java.io.IOException { |
| 78 |
java.io.InputStream inputStream = null; |
| 79 |
String errMssg = ""; |
| 80 |
try{ |
| 81 |
connection = null; |
| 82 |
connection = url.openConnection(); |
| 83 |
connection.setRequestProperty("User-Agent", |
| 84 |
"[" |
| 85 |
+ System.getProperty("os.name") |
| 86 |
+ " / " |
| 87 |
+ System.getProperty("os.arch") |
| 88 |
+ "]" |
| 89 |
+ "[" |
| 90 |
+ System.getProperty("java.version") |
| 91 |
+ " - " |
| 92 |
+ System.getProperty("java.vendor") |
| 93 |
+ "]" |
| 94 |
|
| 95 |
); |
| 96 |
inputStream = connection.getInputStream(); |
| 97 |
} catch(java.io.FileNotFoundException ve){ |
| 98 |
errMssg = "File not found: " + url; |
| 99 |
throw new java.io.FileNotFoundException( |
| 100 |
"--- Ml2xml input error --- " + errMssg ); |
| 101 |
} catch (javax.net.ssl.SSLHandshakeException ve){ |
| 102 |
errMssg = "SSL Handshake Exception: " + ve.getMessage(); |
| 103 |
throw new javax.net.ssl.SSLHandshakeException( |
| 104 |
"--- Ml2xml input error --- " + errMssg ); |
| 105 |
} catch (java.net.UnknownHostException ve){ |
| 106 |
errMssg = "Unknown Host Exception: " + ve.getMessage(); |
| 107 |
throw new java.net.UnknownHostException( |
| 108 |
"--- Ml2xml input error --- " + errMssg ); |
| 109 |
} |
| 110 |
return inputStream; |
| 111 |
} |
| 112 |
|
| 113 |
public void buildProfile( boolean trace ){ |
| 114 |
if( trace ){ |
| 115 |
log.println( |
| 116 |
"xtpipes (2009-01-27-22:19)" |
| 117 |
+ "\n java.version: " + System.getProperty("java.version") |
| 118 |
+ "\n java.class.path: " + System.getProperty("java.class.path") |
| 119 |
+ "\n os.name: " + System.getProperty("os.name") |
| 120 |
+ "\n user.home: " + System.getProperty("user.home") |
| 121 |
+ "\n user.dir: " + System.getProperty("user.dir") |
| 122 |
); |
| 123 |
} |
| 124 |
if( connection != null ){ |
| 125 |
contentType = connection . getContentType(); |
| 126 |
url = connection . getURL() . toString(); |
| 127 |
|
| 128 |
} |
| 129 |
int max = 8192; |
| 130 |
int buffSize = 4096; |
| 131 |
byte [] buff = new byte [ buffSize ]; |
| 132 |
int m = 0; |
| 133 |
int length = 0; |
| 134 |
int ch; |
| 135 |
int type = 0 |
| 136 |
; |
| 137 |
String token = null; |
| 138 |
while( m < max ){ |
| 139 |
try{ |
| 140 |
int k = Math.min( max - m, buffSize ); |
| 141 |
length = inputStream.read( buff, 0, k ); |
| 142 |
if( length == -1 ){ break; } |
| 143 |
if( length == 0 ){ continue; } |
| 144 |
} catch (java.io.IOException e){ |
| 145 |
System.err.println( "--- xtpipes error --- : " + e ); |
| 146 |
break; |
| 147 |
} |
| 148 |
for(int i = 0 ; i < length; i++ ){ |
| 149 |
switch( ch = buff[i] ){ |
| 150 |
case '<': token = ""; |
| 151 |
type = 1 |
| 152 |
; |
| 153 |
break; |
| 154 |
case '>': if( token != null ){ |
| 155 |
token = token . replaceAll( "\\s+", " "); |
| 156 |
if( type == 9 |
| 157 |
){ |
| 158 |
if( xtpipes == null ){ |
| 159 |
int n = token.length(); |
| 160 |
if( (n > 1) && (token.charAt( n - 1 ) == '?') |
| 161 |
&& (token.startsWith("xtpipes") ) ){ |
| 162 |
String s = token . substring(7,n-1) . replaceAll( "\\s+", ""); |
| 163 |
n = s.length(); |
| 164 |
if( (n>6) && (s.startsWith("file=")) |
| 165 |
&& (s.charAt(5) == s.charAt(n-1)) ){ |
| 166 |
xtpipes = s.substring(6,n-1); |
| 167 |
} } } |
| 168 |
} else if( type == 11 |
| 169 |
){ |
| 170 |
if( metaType == null ){ |
| 171 |
token = token . replaceAll( "\\s+", ""); |
| 172 |
int k = token.indexOf("http-equiv"); |
| 173 |
int n = token.indexOf("content"); |
| 174 |
if( (k != -1) && (n != -1) ){ |
| 175 |
if( token.length() > (Math.max(k,n)+3) ){ |
| 176 |
if( token.substring(k+12).startsWith("Content-Type") ){ |
| 177 |
token = token.substring(n+9); |
| 178 |
n = token.indexOf(";"); |
| 179 |
if( n !=-1 ){ metaType = token.substring(0,n); } |
| 180 |
} } } } |
| 181 |
} else if( (type == 2 |
| 182 |
) && (root == null) ){ |
| 183 |
root = token; |
| 184 |
} |
| 185 |
|
| 186 |
token = null; |
| 187 |
} |
| 188 |
break; |
| 189 |
case '\n': |
| 190 |
case ' ': if( token != null ){ |
| 191 |
if( type == 2 |
| 192 |
){ |
| 193 |
if( token.equals("meta") ){ |
| 194 |
if( metaType == null ){ |
| 195 |
type = 11 |
| 196 |
; |
| 197 |
token = " "; |
| 198 |
} else { |
| 199 |
token = null; |
| 200 |
} |
| 201 |
} else { |
| 202 |
if( root == null ){ |
| 203 |
root = token; |
| 204 |
} |
| 205 |
token = null; |
| 206 |
} |
| 207 |
} else if( type == 4 |
| 208 |
){ |
| 209 |
if( token.equals("DOCTYPE") ){ |
| 210 |
type = 5 |
| 211 |
; |
| 212 |
token = " "; |
| 213 |
} else { token = null; } |
| 214 |
} else if( type == 5 |
| 215 |
){ |
| 216 |
if( !token.trim().equals("") ){ |
| 217 |
dtdRoot = token.trim(); |
| 218 |
token = " "; |
| 219 |
type = 6 |
| 220 |
; |
| 221 |
} else { token = null; } |
| 222 |
} else if( type == 6 |
| 223 |
){ |
| 224 |
if( !token.trim().equals("") ){ |
| 225 |
token = token.trim(); |
| 226 |
if( token.equals("PUBLIC") ){ |
| 227 |
type = 7 |
| 228 |
; |
| 229 |
token = ""; |
| 230 |
} else if( token.equals("SYSTEM") ){ |
| 231 |
type = 8 |
| 232 |
; |
| 233 |
token = ""; |
| 234 |
} else { token = null; } |
| 235 |
} |
| 236 |
} else { token += ' '; } |
| 237 |
|
| 238 |
} |
| 239 |
break; |
| 240 |
case '"': |
| 241 |
case '\'': if( token == null ){ break; } |
| 242 |
if( !token.trim().equals("") ){ |
| 243 |
if( token.trim().charAt(0) == ch ){ |
| 244 |
if( type == 7 |
| 245 |
){ |
| 246 |
publicId = token.trim().substring(1); |
| 247 |
type = 8 |
| 248 |
; |
| 249 |
token = ""; |
| 250 |
break; |
| 251 |
} |
| 252 |
else if( type == 8 |
| 253 |
){ |
| 254 |
systemId = token.trim().substring(1); |
| 255 |
token = null; |
| 256 |
break; |
| 257 |
} |
| 258 |
} } |
| 259 |
|
| 260 |
default: if( token != null ){ |
| 261 |
if( type == 3 |
| 262 |
){ |
| 263 |
if( ch == 'D' ){ |
| 264 |
type = 4 |
| 265 |
; |
| 266 |
token += (char) ch; |
| 267 |
} else { token = null; type = 0 |
| 268 |
; } |
| 269 |
} |
| 270 |
else |
| 271 |
if( token.equals("") && (type == 1 |
| 272 |
) ){ |
| 273 |
switch( ch ){ |
| 274 |
case '!': type = 3 |
| 275 |
; |
| 276 |
break; |
| 277 |
case '?': type = 9 |
| 278 |
; |
| 279 |
break; |
| 280 |
default: if( Character.isLetter(ch) |
| 281 |
&& ((root == null) || (metaType == null)) ){ |
| 282 |
type = 2 |
| 283 |
; |
| 284 |
token += (char) ch; |
| 285 |
} else { token = null; } |
| 286 |
} |
| 287 |
|
| 288 |
} else { token += (char) ch; } |
| 289 |
} } |
| 290 |
|
| 291 |
m++; |
| 292 |
} } |
| 293 |
|
| 294 |
if( trace ){ |
| 295 |
log.println( |
| 296 |
" url = " + url |
| 297 |
+ "\n contentType = " + contentType |
| 298 |
+ "\n publicId = " + publicId |
| 299 |
+ "\n systemId = " + systemId |
| 300 |
+ "\n xtpipes = " + xtpipes |
| 301 |
+ "\n root = " + root |
| 302 |
+ "\n dtdRoot = " + dtdRoot |
| 303 |
); |
| 304 |
} } |
| 305 |
|
| 306 |
public InputStream getInputStream(){ return inputStream; } |
| 307 |
public String getFilename(){ |
| 308 |
return (url == null)? |
| 309 |
( (connection == null)? filename |
| 310 |
: |
| 311 |
connection . getURL() . toString() |
| 312 |
) |
| 313 |
: url; |
| 314 |
} |
| 315 |
public String getContentType(){ return contentType; } |
| 316 |
public String getMetaType(){ return metaType; } |
| 317 |
public String getPublicId(){ return publicId; } |
| 318 |
public String getSystemId(){ return systemId; } |
| 319 |
public String getXtpipes(){ return xtpipes; } |
| 320 |
public String getRoot(){ return root; } |
| 321 |
public String getDtdRoot(){ return dtdRoot; } |
| 322 |
} |
| 323 |
|