| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package org.mozilla.javascript.tools; |
| 6 |
|
| 7 |
import java.io.File; |
| 8 |
import java.io.FileInputStream; |
| 9 |
import java.io.IOException; |
| 10 |
import java.io.InputStream; |
| 11 |
import java.net.MalformedURLException; |
| 12 |
import java.net.URL; |
| 13 |
import java.net.URLConnection; |
| 14 |
|
| 15 |
import org.mozilla.javascript.Kit; |
| 16 |
import org.mozilla.javascript.commonjs.module.provider.ParsedContentType; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
public class SourceReader |
| 23 |
{ |
| 24 |
public static URL toUrl(String path) { |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
if (path.indexOf(':') >= 2) { |
| 29 |
try { |
| 30 |
return new URL(path); |
| 31 |
} catch (MalformedURLException ex) { |
| 32 |
|
| 33 |
} |
| 34 |
} |
| 35 |
return null; |
| 36 |
} |
| 37 |
|
| 38 |
public static Object readFileOrUrl(String path, boolean convertToString, |
| 39 |
String defaultEncoding) throws IOException |
| 40 |
{ |
| 41 |
URL url = toUrl(path); |
| 42 |
InputStream is = null; |
| 43 |
int capacityHint = 0; |
| 44 |
String encoding; |
| 45 |
final String contentType; |
| 46 |
byte[] data; |
| 47 |
try { |
| 48 |
if (url == null) { |
| 49 |
File file = new File(path); |
| 50 |
contentType = encoding = null; |
| 51 |
capacityHint = (int)file.length(); |
| 52 |
is = new FileInputStream(file); |
| 53 |
} else { |
| 54 |
URLConnection uc = url.openConnection(); |
| 55 |
is = uc.getInputStream(); |
| 56 |
if(convertToString) { |
| 57 |
ParsedContentType pct = new ParsedContentType(uc.getContentType()); |
| 58 |
contentType = pct.getContentType(); |
| 59 |
encoding = pct.getEncoding(); |
| 60 |
} |
| 61 |
else { |
| 62 |
contentType = encoding = null; |
| 63 |
} |
| 64 |
capacityHint = uc.getContentLength(); |
| 65 |
|
| 66 |
if (capacityHint > (1 << 20)) { |
| 67 |
capacityHint = -1; |
| 68 |
} |
| 69 |
} |
| 70 |
if (capacityHint <= 0) { |
| 71 |
capacityHint = 4096; |
| 72 |
} |
| 73 |
|
| 74 |
data = Kit.readStream(is, capacityHint); |
| 75 |
} finally { |
| 76 |
if(is != null) { |
| 77 |
is.close(); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
Object result; |
| 82 |
if (!convertToString) { |
| 83 |
result = data; |
| 84 |
} else { |
| 85 |
if(encoding == null) { |
| 86 |
|
| 87 |
|
| 88 |
if(data.length > 3 && data[0] == -1 && data[1] == -2 && data[2] == 0 && data[3] == 0) { |
| 89 |
encoding = "UTF-32LE"; |
| 90 |
} |
| 91 |
else if(data.length > 3 && data[0] == 0 && data[1] == 0 && data[2] == -2 && data[3] == -1) { |
| 92 |
encoding = "UTF-32BE"; |
| 93 |
} |
| 94 |
else if(data.length > 2 && data[0] == -17 && data[1] == -69 && data[2] == -65) { |
| 95 |
encoding = "UTF-8"; |
| 96 |
} |
| 97 |
else if(data.length > 1 && data[0] == -1 && data[1] == -2) { |
| 98 |
encoding = "UTF-16LE"; |
| 99 |
} |
| 100 |
else if(data.length > 1 && data[0] == -2 && data[1] == -1) { |
| 101 |
encoding = "UTF-16BE"; |
| 102 |
} |
| 103 |
else { |
| 104 |
|
| 105 |
encoding = defaultEncoding; |
| 106 |
if(encoding == null) { |
| 107 |
|
| 108 |
if(url == null) { |
| 109 |
|
| 110 |
encoding = System.getProperty("file.encoding"); |
| 111 |
} |
| 112 |
else if(contentType != null && contentType.startsWith("application/")) { |
| 113 |
|
| 114 |
encoding = "UTF-8"; |
| 115 |
} |
| 116 |
else { |
| 117 |
|
| 118 |
encoding = "US-ASCII"; |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
String strResult = new String(data, encoding); |
| 124 |
|
| 125 |
if(strResult.length() > 0 && strResult.charAt(0) == '\uFEFF') |
| 126 |
{ |
| 127 |
strResult = strResult.substring(1); |
| 128 |
} |
| 129 |
result = strResult; |
| 130 |
} |
| 131 |
return result; |
| 132 |
} |
| 133 |
} |