Debian Codegraph @

rhino/toolsrc/org/mozilla/javascript/tools/SourceReader.java

raw symbols SCIP
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
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 * @author Attila Szegedi
20 * @version $Id: SourceReader.java,v 1.2 2010/02/15 19:31:17 szegedia%freemail.hu Exp $
21 */
22 public class SourceReader
23 {
24 public static URL toUrl(String path) {
25 // Assume path is URL if it contains a colon and there are at least
26 // 2 characters in the protocol part. The later allows under Windows
27 // to interpret paths with driver letter as file, not URL.
28 if (path.indexOf(':') >= 2) {
29 try {
30 return new URL(path);
31 } catch (MalformedURLException ex) {
32 // not a URL
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 // Ignore insane values for Content-Length
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 // None explicitly specified in Content-type header. Use RFC-4329
87 // 4.2.2 section to autodetect
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 // No autodetect. See if we have explicit value on command line
105 encoding = defaultEncoding;
106 if(encoding == null) {
107 // No explicit encoding specification
108 if(url == null) {
109 // Local files default to system encoding
110 encoding = System.getProperty("file.encoding");
111 }
112 else if(contentType != null && contentType.startsWith("application/")) {
113 // application/* types default to UTF-8
114 encoding = "UTF-8";
115 }
116 else {
117 // text/* MIME types default to US-ASCII
118 encoding = "US-ASCII";
119 }
120 }
121 }
122 }
123 String strResult = new String(data, encoding);
124 // Skip BOM
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 }