| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #library('uri'); | 5 #library('uri'); |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * A parsed URI, inspired by: | 8 * A parsed URI, inspired by: |
| 9 * http://closure-library.googlecode.com/svn/docs/class_goog_Uri.html | 9 * http://closure-library.googlecode.com/svn/docs/class_goog_Uri.html |
| 10 */ | 10 */ |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 bool hasAuthority() { | 162 bool hasAuthority() { |
| 163 return (userInfo != "") || (domain != "") || (port != 0); | 163 return (userInfo != "") || (domain != "") || (port != 0); |
| 164 } | 164 } |
| 165 | 165 |
| 166 String toString() { | 166 String toString() { |
| 167 StringBuffer sb = new StringBuffer(); | 167 StringBuffer sb = new StringBuffer(); |
| 168 _addIfNonEmpty(sb, scheme, scheme, ':'); | 168 _addIfNonEmpty(sb, scheme, scheme, ':'); |
| 169 if (hasAuthority() || (scheme == "file")) { | 169 if (hasAuthority() || (scheme == "file")) { |
| 170 sb.add("//"); | 170 sb.add("//"); |
| 171 _addIfNonEmpty(sb, userInfo, userInfo, "@"); | 171 _addIfNonEmpty(sb, userInfo, userInfo, "@"); |
| 172 sb.add(domain); | 172 sb.add(domain === null ? "null" : domain); |
| 173 if (port != 0) { | 173 if (port != 0) { |
| 174 sb.add(":"); | 174 sb.add(":"); |
| 175 sb.add(port.toString()); | 175 sb.add(port.toString()); |
| 176 } | 176 } |
| 177 } | 177 } |
| 178 sb.add(path); | 178 sb.add(path === null ? "null" : path); |
| 179 _addIfNonEmpty(sb, query, "?", query); | 179 _addIfNonEmpty(sb, query, "?", query); |
| 180 _addIfNonEmpty(sb, fragment, "#", fragment); | 180 _addIfNonEmpty(sb, fragment, "#", fragment); |
| 181 return sb.toString(); | 181 return sb.toString(); |
| 182 } | 182 } |
| 183 | 183 |
| 184 static void _addIfNonEmpty(StringBuffer sb, String test, | 184 static void _addIfNonEmpty(StringBuffer sb, String test, |
| 185 String first, String second) { | 185 String first, String second) { |
| 186 if ("" != test) { | 186 if ("" != test) { |
| 187 sb.add(first); | 187 sb.add(first === null ? "null" : first); |
| 188 sb.add(second); | 188 sb.add(second === null ? "null" : second); |
| 189 } | 189 } |
| 190 } | 190 } |
| 191 } | 191 } |
| 192 | 192 |
| 193 String merge(String base, String reference) { | 193 String merge(String base, String reference) { |
| 194 if (base == "") return "/$reference"; | 194 if (base == "") return "/$reference"; |
| 195 return base.substring(0, base.lastIndexOf("/") + 1) + "$reference"; | 195 return base.substring(0, base.lastIndexOf("/") + 1) + "$reference"; |
| 196 } | 196 } |
| 197 | 197 |
| 198 String removeDotSegments(String path) { | 198 String removeDotSegments(String path) { |
| 199 List<String> output = []; | 199 List<String> output = []; |
| 200 bool appendSlash = false; | 200 bool appendSlash = false; |
| 201 for (String segment in path.split("/")) { | 201 for (String segment in path.split("/")) { |
| 202 appendSlash = false; | 202 appendSlash = false; |
| 203 if (segment == "..") { | 203 if (segment == "..") { |
| 204 if (!output.isEmpty() && | 204 if (!output.isEmpty() && |
| 205 ((output.length != 1) || (output[0] != ""))) output.removeLast(); | 205 ((output.length != 1) || (output[0] != ""))) output.removeLast(); |
| 206 appendSlash = true; | 206 appendSlash = true; |
| 207 } else if ("." == segment) { | 207 } else if ("." == segment) { |
| 208 appendSlash = true; | 208 appendSlash = true; |
| 209 } else { | 209 } else { |
| 210 output.add(segment); | 210 output.add(segment); |
| 211 } | 211 } |
| 212 } | 212 } |
| 213 if (appendSlash) output.add(""); | 213 if (appendSlash) output.add(""); |
| 214 return Strings.join(output, "/"); | 214 return Strings.join(output, "/"); |
| 215 } | 215 } |
| OLD | NEW |