1 | /*
|
---|
2 | * 11/13/2008
|
---|
3 | *
|
---|
4 | * URLFileLocation.java - The location of a file at a (remote) URL.
|
---|
5 | *
|
---|
6 | * This library is distributed under a modified BSD license. See the included
|
---|
7 | * RSyntaxTextArea.License.txt file for details.
|
---|
8 | */
|
---|
9 | package org.fife.ui.rsyntaxtextarea;
|
---|
10 |
|
---|
11 | import java.io.InputStream;
|
---|
12 | import java.io.IOException;
|
---|
13 | import java.io.OutputStream;
|
---|
14 | import java.net.URL;
|
---|
15 |
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * The location of a file at a (remote) URL.
|
---|
19 | *
|
---|
20 | * @author Robert Futrell
|
---|
21 | * @version 1.0
|
---|
22 | */
|
---|
23 | class URLFileLocation extends FileLocation {
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * URL of the remote file.
|
---|
27 | */
|
---|
28 | private URL url;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * A prettied-up full path of the URL (password removed, etc.).
|
---|
32 | */
|
---|
33 | private String fileFullPath;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * A prettied-up filename (leading slash, and possibly "<code>%2F</code>",
|
---|
37 | * removed).
|
---|
38 | */
|
---|
39 | private String fileName;
|
---|
40 |
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Constructor.
|
---|
44 | *
|
---|
45 | * @param url The URL of the file.
|
---|
46 | */
|
---|
47 | URLFileLocation(URL url) {
|
---|
48 | this.url = url;
|
---|
49 | fileFullPath = createFileFullPath();
|
---|
50 | fileName = createFileName();
|
---|
51 | }
|
---|
52 |
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Creates a "prettied-up" URL to use. This will be stripped of
|
---|
56 | * sensitive information such as passwords.
|
---|
57 | *
|
---|
58 | * @return The full path to use.
|
---|
59 | */
|
---|
60 | private String createFileFullPath() {
|
---|
61 | String fullPath = url.toString();
|
---|
62 | fullPath = fullPath.replaceFirst("://([^:]+)(?:.+)@", "://$1@");
|
---|
63 | return fullPath;
|
---|
64 | }
|
---|
65 |
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Creates the "prettied-up" filename to use.
|
---|
69 | *
|
---|
70 | * @return The base name of the file of this URL.
|
---|
71 | */
|
---|
72 | private String createFileName() {
|
---|
73 | String fileName = url.getPath();
|
---|
74 | if (fileName.startsWith("/%2F/")) { // Absolute path
|
---|
75 | fileName = fileName.substring(4);
|
---|
76 | }
|
---|
77 | else if (fileName.startsWith("/")) { // All others
|
---|
78 | fileName = fileName.substring(1);
|
---|
79 | }
|
---|
80 | return fileName;
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Returns the last time this file was modified, or
|
---|
86 | * {@link TextEditorPane#LAST_MODIFIED_UNKNOWN} if this value cannot be
|
---|
87 | * computed (such as for a remote file).
|
---|
88 | *
|
---|
89 | * @return The last time this file was modified. This will always be
|
---|
90 | * {@link TextEditorPane#LAST_MODIFIED_UNKNOWN} for URL's.
|
---|
91 | */
|
---|
92 | protected long getActualLastModified() {
|
---|
93 | return TextEditorPane.LAST_MODIFIED_UNKNOWN;
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * {@inheritDoc}
|
---|
99 | */
|
---|
100 | public String getFileFullPath() {
|
---|
101 | return fileFullPath;
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * {@inheritDoc}
|
---|
107 | */
|
---|
108 | public String getFileName() {
|
---|
109 | return fileName;
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * {@inheritDoc}
|
---|
115 | */
|
---|
116 | protected InputStream getInputStream() throws IOException {
|
---|
117 | return url.openStream();
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * {@inheritDoc}
|
---|
123 | */
|
---|
124 | protected OutputStream getOutputStream() throws IOException {
|
---|
125 | return url.openConnection().getOutputStream();
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Returns whether this file location is a local file.
|
---|
131 | *
|
---|
132 | * @return Whether this is a local file.
|
---|
133 | * @see #isLocalAndExists()
|
---|
134 | */
|
---|
135 | public boolean isLocal() {
|
---|
136 | return "file".equalsIgnoreCase(url.getProtocol());
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Returns whether this file location is a local file and already
|
---|
142 | * exists. This method always returns <code>false</code> since we
|
---|
143 | * cannot check this value easily.
|
---|
144 | *
|
---|
145 | * @return <code>false</code> always.
|
---|
146 | * @see #isLocal()
|
---|
147 | */
|
---|
148 | public boolean isLocalAndExists() {
|
---|
149 | return false;
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | } |
---|