source: josm/trunk/src/org/openstreetmap/josm/tools/Utils.java@ 8384

Last change on this file since 8384 was 8384, checked in by Don-vip, 9 years ago

squid:S1244 - Floating point numbers should not be tested for equality

  • Property svn:eol-style set to native
File size: 44.3 KB
RevLine 
[3504]1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
[6354]4import static org.openstreetmap.josm.tools.I18n.tr;
5import static org.openstreetmap.josm.tools.I18n.trn;
6
[3859]7import java.awt.Color;
[4380]8import java.awt.Toolkit;
9import java.awt.datatransfer.Clipboard;
10import java.awt.datatransfer.ClipboardOwner;
11import java.awt.datatransfer.DataFlavor;
12import java.awt.datatransfer.StringSelection;
13import java.awt.datatransfer.Transferable;
14import java.awt.datatransfer.UnsupportedFlavorException;
[5868]15import java.io.BufferedReader;
[5874]16import java.io.Closeable;
[4065]17import java.io.File;
18import java.io.IOException;
19import java.io.InputStream;
[5868]20import java.io.InputStreamReader;
[4065]21import java.io.OutputStream;
[6972]22import java.io.UnsupportedEncodingException;
[5587]23import java.net.HttpURLConnection;
[6615]24import java.net.MalformedURLException;
[5587]25import java.net.URL;
[5868]26import java.net.URLConnection;
[8304]27import java.net.URLDecoder;
[6972]28import java.net.URLEncoder;
[7082]29import java.nio.charset.StandardCharsets;
[7003]30import java.nio.file.Files;
31import java.nio.file.Path;
32import java.nio.file.StandardCopyOption;
[4403]33import java.security.MessageDigest;
34import java.security.NoSuchAlgorithmException;
[4069]35import java.text.MessageFormat;
[5578]36import java.util.AbstractCollection;
37import java.util.AbstractList;
[4668]38import java.util.ArrayList;
[6221]39import java.util.Arrays;
[3848]40import java.util.Collection;
[6652]41import java.util.Collections;
[4816]42import java.util.Iterator;
[4668]43import java.util.List;
[7423]44import java.util.concurrent.ExecutorService;
45import java.util.concurrent.Executors;
[6538]46import java.util.regex.Matcher;
[6823]47import java.util.regex.Pattern;
[6421]48import java.util.zip.GZIPInputStream;
[7119]49import java.util.zip.ZipEntry;
[5874]50import java.util.zip.ZipFile;
[7119]51import java.util.zip.ZipInputStream;
[3848]52
[8287]53import javax.xml.XMLConstants;
54import javax.xml.parsers.ParserConfigurationException;
55import javax.xml.parsers.SAXParser;
56import javax.xml.parsers.SAXParserFactory;
57
[7867]58import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
[5868]59import org.openstreetmap.josm.Main;
[5587]60import org.openstreetmap.josm.data.Version;
[8347]61import org.xml.sax.InputSource;
[8287]62import org.xml.sax.SAXException;
[8347]63import org.xml.sax.helpers.DefaultHandler;
[5587]64
[4069]65/**
66 * Basic utils, that can be useful in different parts of the program.
67 */
[6362]68public final class Utils {
[3504]69
[6823]70 public static final Pattern WHITE_SPACES_PATTERN = Pattern.compile("\\s+");
71
[6360]72 private Utils() {
73 // Hide default constructor for utils classes
74 }
75
[6806]76 private static final int MILLIS_OF_SECOND = 1000;
77 private static final int MILLIS_OF_MINUTE = 60000;
78 private static final int MILLIS_OF_HOUR = 3600000;
79 private static final int MILLIS_OF_DAY = 86400000;
80
[6999]81 public static final String URL_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;=%";
82
[6772]83 /**
84 * Tests whether {@code predicate} applies to at least one elements from {@code collection}.
85 */
[3836]86 public static <T> boolean exists(Iterable<? extends T> collection, Predicate<? super T> predicate) {
87 for (T item : collection) {
88 if (predicate.evaluate(item))
[3504]89 return true;
90 }
91 return false;
92 }
[3674]93
[6772]94 /**
95 * Tests whether {@code predicate} applies to all elements from {@code collection}.
96 */
97 public static <T> boolean forAll(Iterable<? extends T> collection, Predicate<? super T> predicate) {
98 return !exists(collection, Predicates.not(predicate));
99 }
100
[4065]101 public static <T> boolean exists(Iterable<T> collection, Class<? extends T> klass) {
[3836]102 for (Object item : collection) {
103 if (klass.isInstance(item))
104 return true;
105 }
106 return false;
107 }
108
109 public static <T> T find(Iterable<? extends T> collection, Predicate<? super T> predicate) {
110 for (T item : collection) {
111 if (predicate.evaluate(item))
112 return item;
113 }
114 return null;
115 }
116
[4065]117 @SuppressWarnings("unchecked")
118 public static <T> T find(Iterable<? super T> collection, Class<? extends T> klass) {
[3836]119 for (Object item : collection) {
[4065]120 if (klass.isInstance(item))
121 return (T) item;
[3836]122 }
123 return null;
124 }
[4408]125
[5159]126 public static <T> Collection<T> filter(Collection<? extends T> collection, Predicate<? super T> predicate) {
[7005]127 return new FilteredCollection<>(collection, predicate);
[5159]128 }
[4668]129
[6610]130 /**
131 * Returns the first element from {@code items} which is non-null, or null if all elements are null.
[7019]132 * @param items the items to look for
133 * @return first non-null item if there is one
[6610]134 */
[7019]135 @SafeVarargs
[5159]136 public static <T> T firstNonNull(T... items) {
137 for (T i : items) {
138 if (i != null) {
139 return i;
140 }
141 }
142 return null;
143 }
144
[4100]145 /**
146 * Filter a collection by (sub)class.
147 * This is an efficient read-only implementation.
148 */
149 public static <S, T extends S> SubclassFilteredCollection<S, T> filteredCollection(Collection<S> collection, final Class<T> klass) {
[7005]150 return new SubclassFilteredCollection<>(collection, new Predicate<S>() {
[4100]151 @Override
152 public boolean evaluate(S o) {
153 return klass.isInstance(o);
154 }
155 });
156 }
[3836]157
[3869]158 public static <T> int indexOf(Iterable<? extends T> collection, Predicate<? super T> predicate) {
159 int i = 0;
160 for (T item : collection) {
161 if (predicate.evaluate(item))
162 return i;
163 i++;
164 }
165 return -1;
166 }
167
[3674]168 /**
[7867]169 * Returns the minimum of three values.
170 * @param a an argument.
171 * @param b another argument.
172 * @param c another argument.
173 * @return the smaller of {@code a}, {@code b} and {@code c}.
[3674]174 */
175 public static int min(int a, int b, int c) {
176 if (b < c) {
177 if (a < b)
178 return a;
179 return b;
180 } else {
[4065]181 if (a < c)
[3674]182 return a;
183 return c;
184 }
185 }
[3711]186
[7867]187 /**
188 * Returns the greater of four {@code int} values. That is, the
189 * result is the argument closer to the value of
190 * {@link Integer#MAX_VALUE}. If the arguments have the same value,
191 * the result is that same value.
192 *
193 * @param a an argument.
194 * @param b another argument.
195 * @param c another argument.
196 * @param d another argument.
197 * @return the larger of {@code a}, {@code b}, {@code c} and {@code d}.
198 */
[3836]199 public static int max(int a, int b, int c, int d) {
200 return Math.max(Math.max(a, b), Math.max(c, d));
201 }
202
[7867]203 /**
204 * Ensures a logical condition is met. Otherwise throws an assertion error.
205 * @param condition the condition to be met
206 * @param message Formatted error message to raise if condition is not met
207 * @param data Message parameters, optional
208 * @throws AssertionError if the condition is not met
209 */
[4069]210 public static void ensure(boolean condition, String message, Object...data) {
211 if (!condition)
212 throw new AssertionError(
213 MessageFormat.format(message,data)
214 );
215 }
216
[3796]217 /**
[3711]218 * return the modulus in the range [0, n)
219 */
220 public static int mod(int a, int n) {
221 if (n <= 0)
[7864]222 throw new IllegalArgumentException("n must be <= 0 but is "+n);
[3711]223 int res = a % n;
224 if (res < 0) {
225 res += n;
226 }
227 return res;
228 }
229
[3848]230 /**
231 * Joins a list of strings (or objects that can be converted to string via
232 * Object.toString()) into a single string with fields separated by sep.
233 * @param sep the separator
234 * @param values collection of objects, null is converted to the
235 * empty string
236 * @return null if values is null. The joined string otherwise.
237 */
238 public static String join(String sep, Collection<?> values) {
[7864]239 CheckParameterUtil.ensureParameterNotNull(sep, "sep");
[3848]240 if (values == null)
241 return null;
242 StringBuilder s = null;
243 for (Object a : values) {
244 if (a == null) {
245 a = "";
246 }
[4197]247 if (s != null) {
[8376]248 s.append(sep).append(a);
[3848]249 } else {
250 s = new StringBuilder(a.toString());
251 }
252 }
[7867]253 return s != null ? s.toString() : "";
[3848]254 }
[3859]255
[6524]256 /**
257 * Converts the given iterable collection as an unordered HTML list.
258 * @param values The iterable collection
259 * @return An unordered HTML list
260 */
261 public static String joinAsHtmlUnorderedList(Iterable<?> values) {
[5132]262 StringBuilder sb = new StringBuilder(1024);
263 sb.append("<ul>");
264 for (Object i : values) {
265 sb.append("<li>").append(i).append("</li>");
266 }
267 sb.append("</ul>");
268 return sb.toString();
269 }
270
[3859]271 /**
272 * convert Color to String
273 * (Color.toString() omits alpha value)
274 */
275 public static String toString(Color c) {
276 if (c == null)
277 return "null";
278 if (c.getAlpha() == 255)
279 return String.format("#%06x", c.getRGB() & 0x00ffffff);
280 else
281 return String.format("#%06x(alpha=%d)", c.getRGB() & 0x00ffffff, c.getAlpha());
282 }
[3865]283
284 /**
[6830]285 * convert float range 0 &lt;= x &lt;= 1 to integer range 0..255
[3865]286 * when dealing with colors and color alpha value
[3879]287 * @return null if val is null, the corresponding int if val is in the
288 * range 0...1. If val is outside that range, return 255
[3865]289 */
290 public static Integer color_float2int(Float val) {
291 if (val == null)
292 return null;
293 if (val < 0 || val > 1)
294 return 255;
295 return (int) (255f * val + 0.5f);
296 }
297
298 /**
[6830]299 * convert integer range 0..255 to float range 0 &lt;= x &lt;= 1
[6749]300 * when dealing with colors and color alpha value
[3865]301 */
302 public static Float color_int2float(Integer val) {
303 if (val == null)
304 return null;
305 if (val < 0 || val > 255)
306 return 1f;
307 return ((float) val) / 255f;
308 }
309
[4069]310 public static Color complement(Color clr) {
311 return new Color(255 - clr.getRed(), 255 - clr.getGreen(), 255 - clr.getBlue(), clr.getAlpha());
312 }
[4065]313
[5874]314 /**
[6221]315 * Copies the given array. Unlike {@link Arrays#copyOf}, this method is null-safe.
316 * @param array The array to copy
317 * @return A copy of the original array, or {@code null} if {@code array} is null
318 * @since 6221
319 */
320 public static <T> T[] copyArray(T[] array) {
321 if (array != null) {
322 return Arrays.copyOf(array, array.length);
323 }
324 return null;
325 }
[6792]326
[6221]327 /**
[6222]328 * Copies the given array. Unlike {@link Arrays#copyOf}, this method is null-safe.
329 * @param array The array to copy
330 * @return A copy of the original array, or {@code null} if {@code array} is null
331 * @since 6222
332 */
333 public static char[] copyArray(char[] array) {
334 if (array != null) {
335 return Arrays.copyOf(array, array.length);
336 }
337 return null;
338 }
[6792]339
[6222]340 /**
[7436]341 * Copies the given array. Unlike {@link Arrays#copyOf}, this method is null-safe.
342 * @param array The array to copy
343 * @return A copy of the original array, or {@code null} if {@code array} is null
344 * @since 7436
345 */
346 public static int[] copyArray(int[] array) {
347 if (array != null) {
348 return Arrays.copyOf(array, array.length);
349 }
350 return null;
351 }
352
353 /**
[7835]354 * Simple file copy function that will overwrite the target file.
[5874]355 * @param in The source file
356 * @param out The destination file
[7003]357 * @return the path to the target file
[8291]358 * @throws IOException if any I/O error occurs
359 * @throws IllegalArgumentException if {@code in} or {@code out} is {@code null}
[7003]360 * @since 7003
[5874]361 */
[7835]362 public static Path copyFile(File in, File out) throws IOException {
[7003]363 CheckParameterUtil.ensureParameterNotNull(in, "in");
364 CheckParameterUtil.ensureParameterNotNull(out, "out");
365 return Files.copy(in.toPath(), out.toPath(), StandardCopyOption.REPLACE_EXISTING);
[5874]366 }
[6070]367
[7835]368 /**
369 * Recursive directory copy function
370 * @param in The source directory
371 * @param out The destination directory
[8291]372 * @throws IOException if any I/O error ooccurs
373 * @throws IllegalArgumentException if {@code in} or {@code out} is {@code null}
[7835]374 * @since 7835
375 */
376 public static void copyDirectory(File in, File out) throws IOException {
377 CheckParameterUtil.ensureParameterNotNull(in, "in");
378 CheckParameterUtil.ensureParameterNotNull(out, "out");
379 if (!out.exists() && !out.mkdirs()) {
380 Main.warn("Unable to create directory "+out.getPath());
381 }
[8308]382 File[] files = in.listFiles();
383 if (files != null) {
384 for (File f : files) {
385 File target = new File(out, f.getName());
386 if (f.isDirectory()) {
387 copyDirectory(f, target);
388 } else {
389 copyFile(f, target);
390 }
[7835]391 }
392 }
393 }
394
[7867]395 /**
396 * Copy data from source stream to output stream.
397 * @param source source stream
398 * @param destination target stream
399 * @return number of bytes copied
400 * @throws IOException if any I/O error occurs
401 */
[4065]402 public static int copyStream(InputStream source, OutputStream destination) throws IOException {
403 int count = 0;
404 byte[] b = new byte[512];
405 int read;
406 while ((read = source.read(b)) != -1) {
407 count += read;
408 destination.write(b, 0, read);
409 }
410 return count;
411 }
412
[7835]413 /**
414 * Deletes a directory recursively.
415 * @param path The directory to delete
416 * @return <code>true</code> if and only if the file or directory is
417 * successfully deleted; <code>false</code> otherwise
418 */
[4065]419 public static boolean deleteDirectory(File path) {
420 if( path.exists() ) {
421 File[] files = path.listFiles();
[8308]422 if (files != null) {
423 for (File file : files) {
424 if (file.isDirectory()) {
425 deleteDirectory(file);
426 } else if (!file.delete()) {
427 Main.warn("Unable to delete file: "+file.getPath());
428 }
[4065]429 }
430 }
431 }
[7835]432 return path.delete();
[4065]433 }
[4087]434
435 /**
[6823]436 * <p>Utility method for closing a {@link java.io.Closeable} object.</p>
[4668]437 *
[5874]438 * @param c the closeable object. May be null.
[4087]439 */
[5874]440 public static void close(Closeable c) {
441 if (c == null) return;
[4087]442 try {
[5874]443 c.close();
[6268]444 } catch (IOException e) {
445 Main.warn(e);
[4087]446 }
447 }
[6070]448
[4087]449 /**
[6823]450 * <p>Utility method for closing a {@link java.util.zip.ZipFile}.</p>
[4668]451 *
[5874]452 * @param zip the zip file. May be null.
[4087]453 */
[5874]454 public static void close(ZipFile zip) {
455 if (zip == null) return;
[4087]456 try {
[5874]457 zip.close();
[6268]458 } catch (IOException e) {
459 Main.warn(e);
[4087]460 }
461 }
[6792]462
[6615]463 /**
464 * Converts the given file to its URL.
465 * @param f The file to get URL from
466 * @return The URL of the given file, or {@code null} if not possible.
467 * @since 6615
468 */
469 public static URL fileToURL(File f) {
470 if (f != null) {
471 try {
472 return f.toURI().toURL();
473 } catch (MalformedURLException ex) {
474 Main.error("Unable to convert filename " + f.getAbsolutePath() + " to URL");
475 }
476 }
477 return null;
478 }
[4087]479
[6889]480 private static final double EPSILON = 1e-11;
[4272]481
[6268]482 /**
483 * Determines if the two given double values are equal (their delta being smaller than a fixed epsilon)
484 * @param a The first double value to compare
485 * @param b The second double value to compare
486 * @return {@code true} if {@code abs(a - b) <= 1e-11}, {@code false} otherwise
487 */
[4272]488 public static boolean equalsEpsilon(double a, double b) {
[6268]489 return Math.abs(a - b) <= EPSILON;
[4272]490 }
[4380]491
492 /**
493 * Copies the string {@code s} to system clipboard.
494 * @param s string to be copied to clipboard.
495 * @return true if succeeded, false otherwise.
496 */
497 public static boolean copyToClipboard(String s) {
498 try {
499 Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(s), new ClipboardOwner() {
500
501 @Override
502 public void lostOwnership(Clipboard clpbrd, Transferable t) {
503 }
504 });
505 return true;
506 } catch (IllegalStateException ex) {
[6642]507 Main.error(ex);
[4380]508 return false;
509 }
510 }
511
512 /**
513 * Extracts clipboard content as string.
514 * @return string clipboard contents if available, {@code null} otherwise.
515 */
516 public static String getClipboardContent() {
[5221]517 Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
518 Transferable t = null;
519 for (int tries = 0; t == null && tries < 10; tries++) {
520 try {
521 t = clipboard.getContents(null);
[6070]522 } catch (IllegalStateException e) {
[5221]523 // Clipboard currently unavailable. On some platforms, the system clipboard is unavailable while it is accessed by another application.
524 try {
525 Thread.sleep(1);
526 } catch (InterruptedException ex) {
[6313]527 Main.warn("InterruptedException in "+Utils.class.getSimpleName()+" while getting clipboard content");
[5221]528 }
529 }
530 }
[4380]531 try {
532 if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
[6792]533 return (String) t.getTransferData(DataFlavor.stringFlavor);
[4380]534 }
[7004]535 } catch (UnsupportedFlavorException | IOException ex) {
[6642]536 Main.error(ex);
[4380]537 return null;
538 }
539 return null;
540 }
[4403]541
542 /**
543 * Calculate MD5 hash of a string and output in hexadecimal format.
[5589]544 * @param data arbitrary String
545 * @return MD5 hash of data, string of length 32 with characters in range [0-9a-f]
[4403]546 */
547 public static String md5Hex(String data) {
[7082]548 byte[] byteData = data.getBytes(StandardCharsets.UTF_8);
[4403]549 MessageDigest md = null;
550 try {
551 md = MessageDigest.getInstance("MD5");
552 } catch (NoSuchAlgorithmException e) {
[6798]553 throw new RuntimeException(e);
[4403]554 }
555 byte[] byteDigest = md.digest(byteData);
556 return toHexString(byteDigest);
557 }
558
[6175]559 private static final char[] HEX_ARRAY = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
560
[4403]561 /**
[5589]562 * Converts a byte array to a string of hexadecimal characters.
563 * Preserves leading zeros, so the size of the output string is always twice
564 * the number of input bytes.
565 * @param bytes the byte array
566 * @return hexadecimal representation
[4403]567 */
568 public static String toHexString(byte[] bytes) {
[6175]569
570 if (bytes == null) {
571 return "";
[4403]572 }
[6175]573
574 final int len = bytes.length;
575 if (len == 0) {
576 return "";
577 }
578
579 char[] hexChars = new char[len * 2];
580 for (int i = 0, j = 0; i < len; i++) {
581 final int v = bytes[i];
582 hexChars[j++] = HEX_ARRAY[(v & 0xf0) >> 4];
583 hexChars[j++] = HEX_ARRAY[v & 0xf];
584 }
[4403]585 return new String(hexChars);
586 }
[4668]587
588 /**
589 * Topological sort.
590 *
[6830]591 * @param dependencies contains mappings (key -&gt; value). In the final list of sorted objects, the key will come
[4668]592 * after the value. (In other words, the key depends on the value(s).)
593 * There must not be cyclic dependencies.
594 * @return the list of sorted objects
595 */
596 public static <T> List<T> topologicalSort(final MultiMap<T,T> dependencies) {
[7005]597 MultiMap<T,T> deps = new MultiMap<>();
[4668]598 for (T key : dependencies.keySet()) {
599 deps.putVoid(key);
600 for (T val : dependencies.get(key)) {
601 deps.putVoid(val);
602 deps.put(key, val);
603 }
604 }
605
606 int size = deps.size();
[7005]607 List<T> sorted = new ArrayList<>();
[4668]608 for (int i=0; i<size; ++i) {
609 T parentless = null;
610 for (T key : deps.keySet()) {
[6093]611 if (deps.get(key).isEmpty()) {
[4668]612 parentless = key;
613 break;
614 }
615 }
616 if (parentless == null) throw new RuntimeException();
617 sorted.add(parentless);
618 deps.remove(parentless);
619 for (T key : deps.keySet()) {
620 deps.remove(key, parentless);
621 }
622 }
623 if (sorted.size() != size) throw new RuntimeException();
624 return sorted;
625 }
[4816]626
627 /**
628 * Represents a function that can be applied to objects of {@code A} and
629 * returns objects of {@code B}.
630 * @param <A> class of input objects
631 * @param <B> class of transformed objects
632 */
633 public static interface Function<A, B> {
634
635 /**
636 * Applies the function on {@code x}.
637 * @param x an object of
638 * @return the transformed object
639 */
640 B apply(A x);
641 }
642
643 /**
644 * Transforms the collection {@code c} into an unmodifiable collection and
[6823]645 * applies the {@link org.openstreetmap.josm.tools.Utils.Function} {@code f} on each element upon access.
[4816]646 * @param <A> class of input collection
647 * @param <B> class of transformed collection
648 * @param c a collection
649 * @param f a function that transforms objects of {@code A} to objects of {@code B}
650 * @return the transformed unmodifiable collection
651 */
[5132]652 public static <A, B> Collection<B> transform(final Collection<? extends A> c, final Function<A, B> f) {
[5578]653 return new AbstractCollection<B>() {
[4816]654
655 @Override
656 public int size() {
657 return c.size();
658 }
659
660 @Override
661 public Iterator<B> iterator() {
662 return new Iterator<B>() {
663
[5132]664 private Iterator<? extends A> it = c.iterator();
[4816]665
666 @Override
667 public boolean hasNext() {
668 return it.hasNext();
669 }
670
671 @Override
672 public B next() {
673 return f.apply(it.next());
674 }
675
676 @Override
677 public void remove() {
678 throw new UnsupportedOperationException();
679 }
680 };
681 }
[5578]682 };
683 }
[4816]684
[5578]685 /**
686 * Transforms the list {@code l} into an unmodifiable list and
[6823]687 * applies the {@link org.openstreetmap.josm.tools.Utils.Function} {@code f} on each element upon access.
[5578]688 * @param <A> class of input collection
689 * @param <B> class of transformed collection
690 * @param l a collection
691 * @param f a function that transforms objects of {@code A} to objects of {@code B}
692 * @return the transformed unmodifiable list
693 */
694 public static <A, B> List<B> transform(final List<? extends A> l, final Function<A, B> f) {
695 return new AbstractList<B>() {
[4816]696
697 @Override
[5578]698 public int size() {
699 return l.size();
[4816]700 }
701
702 @Override
[5578]703 public B get(int index) {
704 return f.apply(l.get(index));
[4816]705 }
706 };
707 }
[5577]708
[6823]709 private static final Pattern HTTP_PREFFIX_PATTERN = Pattern.compile("https?");
710
[5577]711 /**
[5587]712 * Opens a HTTP connection to the given URL and sets the User-Agent property to JOSM's one.
713 * @param httpURL The HTTP url to open (must use http:// or https://)
714 * @return An open HTTP connection to the given URL
[6823]715 * @throws java.io.IOException if an I/O exception occurs.
[5587]716 * @since 5587
717 */
718 public static HttpURLConnection openHttpConnection(URL httpURL) throws IOException {
[6823]719 if (httpURL == null || !HTTP_PREFFIX_PATTERN.matcher(httpURL.getProtocol()).matches()) {
[5587]720 throw new IllegalArgumentException("Invalid HTTP url");
721 }
[7473]722 if (Main.isDebugEnabled()) {
723 Main.debug("Opening HTTP connection to "+httpURL.toExternalForm());
724 }
[5587]725 HttpURLConnection connection = (HttpURLConnection) httpURL.openConnection();
[5868]726 connection.setRequestProperty("User-Agent", Version.getInstance().getFullAgentString());
[6123]727 connection.setUseCaches(false);
[5587]728 return connection;
729 }
[6070]730
[5587]731 /**
[5868]732 * Opens a connection to the given URL and sets the User-Agent property to JOSM's one.
733 * @param url The url to open
734 * @return An stream for the given URL
[6823]735 * @throws java.io.IOException if an I/O exception occurs.
[5868]736 * @since 5867
737 */
738 public static InputStream openURL(URL url) throws IOException {
[6421]739 return openURLAndDecompress(url, false);
[5868]740 }
741
[6421]742 /**
743 * Opens a connection to the given URL, sets the User-Agent property to JOSM's one, and decompresses stream if necessary.
744 * @param url The url to open
[7867]745 * @param decompress whether to wrap steam in a {@link GZIPInputStream} or {@link BZip2CompressorInputStream}
[6421]746 * if the {@code Content-Type} header is set accordingly.
747 * @return An stream for the given URL
748 * @throws IOException if an I/O exception occurs.
749 * @since 6421
750 */
751 public static InputStream openURLAndDecompress(final URL url, final boolean decompress) throws IOException {
752 final URLConnection connection = setupURLConnection(url.openConnection());
[7119]753 final InputStream in = connection.getInputStream();
754 if (decompress) {
755 switch (connection.getHeaderField("Content-Type")) {
756 case "application/zip":
757 return getZipInputStream(in);
758 case "application/x-gzip":
759 return getGZipInputStream(in);
760 case "application/x-bzip2":
761 return getBZip2InputStream(in);
762 }
[6421]763 }
[7119]764 return in;
[6421]765 }
766
[7119]767 /**
768 * Returns a Bzip2 input stream wrapping given input stream.
769 * @param in The raw input stream
770 * @return a Bzip2 input stream wrapping given input stream, or {@code null} if {@code in} is {@code null}
771 * @throws IOException if the given input stream does not contain valid BZ2 header
[7867]772 * @since 7867
[7119]773 */
[7867]774 public static BZip2CompressorInputStream getBZip2InputStream(InputStream in) throws IOException {
[7119]775 if (in == null) {
776 return null;
777 }
[7869]778 return new BZip2CompressorInputStream(in, /* see #9537 */ true);
[7119]779 }
780
781 /**
782 * Returns a Gzip input stream wrapping given input stream.
783 * @param in The raw input stream
784 * @return a Gzip input stream wrapping given input stream, or {@code null} if {@code in} is {@code null}
785 * @throws IOException if an I/O error has occurred
786 * @since 7119
787 */
788 public static GZIPInputStream getGZipInputStream(InputStream in) throws IOException {
789 if (in == null) {
790 return null;
791 }
792 return new GZIPInputStream(in);
793 }
794
795 /**
796 * Returns a Zip input stream wrapping given input stream.
797 * @param in The raw input stream
798 * @return a Zip input stream wrapping given input stream, or {@code null} if {@code in} is {@code null}
799 * @throws IOException if an I/O error has occurred
800 * @since 7119
801 */
802 public static ZipInputStream getZipInputStream(InputStream in) throws IOException {
803 if (in == null) {
804 return null;
805 }
806 ZipInputStream zis = new ZipInputStream(in, StandardCharsets.UTF_8);
807 // Positions the stream at the beginning of first entry
808 ZipEntry ze = zis.getNextEntry();
809 if (ze != null && Main.isDebugEnabled()) {
810 Main.debug("Zip entry: "+ze.getName());
811 }
812 return zis;
813 }
814
[5887]815 /***
816 * Setups the given URL connection to match JOSM needs by setting its User-Agent and timeout properties.
817 * @param connection The connection to setup
818 * @return {@code connection}, with updated properties
819 * @since 5887
820 */
821 public static URLConnection setupURLConnection(URLConnection connection) {
822 if (connection != null) {
823 connection.setRequestProperty("User-Agent", Version.getInstance().getFullAgentString());
824 connection.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect",15)*1000);
825 connection.setReadTimeout(Main.pref.getInteger("socket.timeout.read",30)*1000);
826 }
827 return connection;
828 }
829
[5868]830 /**
831 * Opens a connection to the given URL and sets the User-Agent property to JOSM's one.
832 * @param url The url to open
833 * @return An buffered stream reader for the given URL (using UTF-8)
[6823]834 * @throws java.io.IOException if an I/O exception occurs.
[5881]835 * @since 5868
[5868]836 */
837 public static BufferedReader openURLReader(URL url) throws IOException {
[6421]838 return openURLReaderAndDecompress(url, false);
[5868]839 }
840
841 /**
[6421]842 * Opens a connection to the given URL and sets the User-Agent property to JOSM's one.
843 * @param url The url to open
[7867]844 * @param decompress whether to wrap steam in a {@link GZIPInputStream} or {@link BZip2CompressorInputStream}
[6421]845 * if the {@code Content-Type} header is set accordingly.
846 * @return An buffered stream reader for the given URL (using UTF-8)
847 * @throws IOException if an I/O exception occurs.
848 * @since 6421
849 */
850 public static BufferedReader openURLReaderAndDecompress(final URL url, final boolean decompress) throws IOException {
[7082]851 return new BufferedReader(new InputStreamReader(openURLAndDecompress(url, decompress), StandardCharsets.UTF_8));
[6421]852 }
853
854 /**
[5587]855 * Opens a HTTP connection to the given URL, sets the User-Agent property to JOSM's one and optionnaly disables Keep-Alive.
856 * @param httpURL The HTTP url to open (must use http:// or https://)
[6421]857 * @param keepAlive whether not to set header {@code Connection=close}
[5587]858 * @return An open HTTP connection to the given URL
[6823]859 * @throws java.io.IOException if an I/O exception occurs.
[5587]860 * @since 5587
861 */
862 public static HttpURLConnection openHttpConnection(URL httpURL, boolean keepAlive) throws IOException {
863 HttpURLConnection connection = openHttpConnection(httpURL);
864 if (!keepAlive) {
865 connection.setRequestProperty("Connection", "close");
866 }
[6852]867 if (Main.isDebugEnabled()) {
868 try {
869 Main.debug("REQUEST: "+ connection.getRequestProperties());
870 } catch (IllegalStateException e) {
871 Main.warn(e);
872 }
873 }
[5587]874 return connection;
875 }
[6070]876
[5772]877 /**
878 * An alternative to {@link String#trim()} to effectively remove all leading and trailing white characters, including Unicode ones.
879 * @see <a href="http://closingbraces.net/2008/11/11/javastringtrim/">Java’s String.trim has a strange idea of whitespace</a>
[6617]880 * @see <a href="https://bugs.openjdk.java.net/browse/JDK-4080617">JDK bug 4080617</a>
[5772]881 * @param str The string to strip
[6070]882 * @return <code>str</code>, without leading and trailing characters, according to
[5772]883 * {@link Character#isWhitespace(char)} and {@link Character#isSpaceChar(char)}.
884 * @since 5772
885 */
886 public static String strip(String str) {
887 if (str == null || str.isEmpty()) {
888 return str;
889 }
890 int start = 0, end = str.length();
891 boolean leadingWhite = true;
892 while (leadingWhite && start < end) {
893 char c = str.charAt(start);
[6094]894 // '\u200B' (ZERO WIDTH SPACE character) needs to be handled manually because of change in Unicode 6.0 (Java 7, see #8918)
[6410]895 // same for '\uFEFF' (ZERO WIDTH NO-BREAK SPACE)
896 leadingWhite = (Character.isWhitespace(c) || Character.isSpaceChar(c) || c == '\u200B' || c == '\uFEFF');
[5791]897 if (leadingWhite) {
[5772]898 start++;
899 }
900 }
901 boolean trailingWhite = true;
902 while (trailingWhite && end > start+1) {
903 char c = str.charAt(end-1);
[6410]904 trailingWhite = (Character.isWhitespace(c) || Character.isSpaceChar(c) || c == '\u200B' || c == '\uFEFF');
[5791]905 if (trailingWhite) {
[5772]906 end--;
907 }
908 }
909 return str.substring(start, end);
910 }
[6103]911
912 /**
913 * Runs an external command and returns the standard output.
[6792]914 *
[6103]915 * The program is expected to execute fast.
[6792]916 *
[6103]917 * @param command the command with arguments
918 * @return the output
919 * @throws IOException when there was an error, e.g. command does not exist
920 */
921 public static String execOutput(List<String> command) throws IOException {
[6962]922 if (Main.isDebugEnabled()) {
923 Main.debug(join(" ", command));
924 }
[6103]925 Process p = new ProcessBuilder(command).start();
[7082]926 try (BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream(), StandardCharsets.UTF_8))) {
[7037]927 StringBuilder all = null;
928 String line;
929 while ((line = input.readLine()) != null) {
930 if (all == null) {
931 all = new StringBuilder(line);
932 } else {
933 all.append("\n");
934 all.append(line);
935 }
[6103]936 }
[7037]937 return all != null ? all.toString() : null;
[6103]938 }
939 }
[6245]940
941 /**
942 * Returns the JOSM temp directory.
943 * @return The JOSM temp directory ({@code <java.io.tmpdir>/JOSM}), or {@code null} if {@code java.io.tmpdir} is not defined
944 * @since 6245
945 */
946 public static File getJosmTempDir() {
947 String tmpDir = System.getProperty("java.io.tmpdir");
948 if (tmpDir == null) {
949 return null;
950 }
951 File josmTmpDir = new File(tmpDir, "JOSM");
[6883]952 if (!josmTmpDir.exists() && !josmTmpDir.mkdirs()) {
953 Main.warn("Unable to create temp directory "+josmTmpDir);
[6245]954 }
955 return josmTmpDir;
956 }
[6354]957
958 /**
959 * Returns a simple human readable (hours, minutes, seconds) string for a given duration in milliseconds.
960 * @param elapsedTime The duration in milliseconds
[6661]961 * @return A human readable string for the given duration
[6830]962 * @throws IllegalArgumentException if elapsedTime is &lt; 0
[6354]963 * @since 6354
964 */
[7867]965 public static String getDurationString(long elapsedTime) {
[6354]966 if (elapsedTime < 0) {
[7321]967 throw new IllegalArgumentException("elapsedTime must be >= 0");
[6354]968 }
969 // Is it less than 1 second ?
[6661]970 if (elapsedTime < MILLIS_OF_SECOND) {
[6354]971 return String.format("%d %s", elapsedTime, tr("ms"));
972 }
973 // Is it less than 1 minute ?
[6661]974 if (elapsedTime < MILLIS_OF_MINUTE) {
[8384]975 return String.format("%.1d %s", elapsedTime / (double) MILLIS_OF_SECOND, tr("s"));
[6354]976 }
977 // Is it less than 1 hour ?
[6661]978 if (elapsedTime < MILLIS_OF_HOUR) {
979 final long min = elapsedTime / MILLIS_OF_MINUTE;
980 return String.format("%d %s %d %s", min, tr("min"), (elapsedTime - min * MILLIS_OF_MINUTE) / MILLIS_OF_SECOND, tr("s"));
[6354]981 }
982 // Is it less than 1 day ?
[6661]983 if (elapsedTime < MILLIS_OF_DAY) {
984 final long hour = elapsedTime / MILLIS_OF_HOUR;
985 return String.format("%d %s %d %s", hour, tr("h"), (elapsedTime - hour * MILLIS_OF_HOUR) / MILLIS_OF_MINUTE, tr("min"));
[6354]986 }
[6661]987 long days = elapsedTime / MILLIS_OF_DAY;
988 return String.format("%d %s %d %s", days, trn("day", "days", days), (elapsedTime - days * MILLIS_OF_DAY) / MILLIS_OF_HOUR, tr("h"));
[6354]989 }
[6538]990
991 /**
[6652]992 * Returns a human readable representation of a list of positions.
[6830]993 * <p>
[6652]994 * For instance, {@code [1,5,2,6,7} yields "1-2,5-7
995 * @param positionList a list of positions
996 * @return a human readable representation
997 */
998 public static String getPositionListString(List<Integer> positionList) {
999 Collections.sort(positionList);
1000 final StringBuilder sb = new StringBuilder(32);
1001 sb.append(positionList.get(0));
1002 int cnt = 0;
1003 int last = positionList.get(0);
1004 for (int i = 1; i < positionList.size(); ++i) {
1005 int cur = positionList.get(i);
1006 if (cur == last + 1) {
1007 ++cnt;
1008 } else if (cnt == 0) {
1009 sb.append(",").append(cur);
1010 } else {
1011 sb.append("-").append(last);
1012 sb.append(",").append(cur);
1013 cnt = 0;
1014 }
1015 last = cur;
1016 }
1017 if (cnt >= 1) {
1018 sb.append("-").append(last);
1019 }
1020 return sb.toString();
1021 }
1022
1023
1024 /**
[6538]1025 * Returns a list of capture groups if {@link Matcher#matches()}, or {@code null}.
1026 * The first element (index 0) is the complete match.
1027 * Further elements correspond to the parts in parentheses of the regular expression.
1028 * @param m the matcher
1029 * @return a list of capture groups if {@link Matcher#matches()}, or {@code null}.
1030 */
1031 public static List<String> getMatches(final Matcher m) {
1032 if (m.matches()) {
[7005]1033 List<String> result = new ArrayList<>(m.groupCount() + 1);
[6538]1034 for (int i = 0; i <= m.groupCount(); i++) {
1035 result.add(m.group(i));
1036 }
1037 return result;
1038 } else {
1039 return null;
1040 }
1041 }
[6578]1042
1043 /**
1044 * Cast an object savely.
1045 * @param <T> the target type
1046 * @param o the object to cast
1047 * @param klass the target class (same as T)
1048 * @return null if <code>o</code> is null or the type <code>o</code> is not
1049 * a subclass of <code>klass</code>. The casted value otherwise.
1050 */
[6623]1051 @SuppressWarnings("unchecked")
[6578]1052 public static <T> T cast(Object o, Class<T> klass) {
1053 if (klass.isInstance(o)) {
[6623]1054 return (T) o;
[6578]1055 }
1056 return null;
1057 }
1058
[6642]1059 /**
1060 * Returns the root cause of a throwable object.
1061 * @param t The object to get root cause for
1062 * @return the root cause of {@code t}
1063 * @since 6639
1064 */
1065 public static Throwable getRootCause(Throwable t) {
1066 Throwable result = t;
1067 if (result != null) {
1068 Throwable cause = result.getCause();
[7867]1069 while (cause != null && !cause.equals(result)) {
[6642]1070 result = cause;
1071 cause = result.getCause();
1072 }
1073 }
1074 return result;
1075 }
[6792]1076
[6717]1077 /**
1078 * Adds the given item at the end of a new copy of given array.
1079 * @param array The source array
1080 * @param item The item to add
1081 * @return An extended copy of {@code array} containing {@code item} as additional last element
1082 * @since 6717
1083 */
1084 public static <T> T[] addInArrayCopy(T[] array, T item) {
1085 T[] biggerCopy = Arrays.copyOf(array, array.length + 1);
1086 biggerCopy[array.length] = item;
1087 return biggerCopy;
1088 }
[6742]1089
1090 /**
1091 * If the string {@code s} is longer than {@code maxLength}, the string is cut and "..." is appended.
[7867]1092 * @param s String to shorten
1093 * @param maxLength maximum number of characters to keep (not including the "...")
1094 * @return the shortened string
[6742]1095 */
1096 public static String shortenString(String s, int maxLength) {
1097 if (s != null && s.length() > maxLength) {
1098 return s.substring(0, maxLength - 3) + "...";
1099 } else {
1100 return s;
1101 }
1102 }
[7019]1103
[6972]1104 /**
[7019]1105 * Fixes URL with illegal characters in the query (and fragment) part by
[6972]1106 * percent encoding those characters.
[7019]1107 *
[6972]1108 * special characters like &amp; and # are not encoded
[7019]1109 *
[6972]1110 * @param url the URL that should be fixed
1111 * @return the repaired URL
1112 */
1113 public static String fixURLQuery(String url) {
[7019]1114 if (url.indexOf('?') == -1)
[6972]1115 return url;
[7019]1116
[6972]1117 String query = url.substring(url.indexOf('?') + 1);
[7019]1118
[6972]1119 StringBuilder sb = new StringBuilder(url.substring(0, url.indexOf('?') + 1));
[7019]1120
[6972]1121 for (int i=0; i<query.length(); i++) {
1122 String c = query.substring(i, i+1);
1123 if (URL_CHARS.contains(c)) {
1124 sb.append(c);
1125 } else {
[8304]1126 sb.append(encodeUrl(c));
[6972]1127 }
1128 }
1129 return sb.toString();
1130 }
[7019]1131
[7356]1132 /**
[8304]1133 * Translates a string into <code>application/x-www-form-urlencoded</code>
1134 * format. This method uses UTF-8 encoding scheme to obtain the bytes for unsafe
1135 * characters.
1136 *
1137 * @param s <code>String</code> to be translated.
1138 * @return the translated <code>String</code>.
1139 * @see #decodeUrl(String)
1140 * @since 8304
1141 */
1142 public static String encodeUrl(String s) {
1143 final String enc = StandardCharsets.UTF_8.name();
1144 try {
1145 return URLEncoder.encode(s, enc);
1146 } catch (UnsupportedEncodingException e) {
1147 Main.error(e);
1148 return null;
1149 }
1150 }
1151
1152 /**
1153 * Decodes a <code>application/x-www-form-urlencoded</code> string.
1154 * UTF-8 encoding is used to determine
1155 * what characters are represented by any consecutive sequences of the
1156 * form "<code>%<i>xy</i></code>".
1157 *
1158 * @param s the <code>String</code> to decode
1159 * @return the newly decoded <code>String</code>
1160 * @see #encodeUrl(String)
1161 * @since 8304
1162 */
1163 public static String decodeUrl(String s) {
1164 final String enc = StandardCharsets.UTF_8.name();
1165 try {
1166 return URLDecoder.decode(s, enc);
1167 } catch (UnsupportedEncodingException e) {
1168 Main.error(e);
1169 return null;
1170 }
1171 }
1172
1173 /**
[7356]1174 * Determines if the given URL denotes a file on a local filesystem.
1175 * @param url The URL to test
1176 * @return {@code true} if the url points to a local file
1177 * @since 7356
1178 */
1179 public static boolean isLocalUrl(String url) {
1180 if (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("resource://"))
1181 return false;
1182 return true;
1183 }
[7423]1184
1185 /**
1186 * Returns a pair containing the number of threads (n), and a thread pool (if n > 1) to perform
1187 * multi-thread computation in the context of the given preference key.
1188 * @param pref The preference key
1189 * @return a pair containing the number of threads (n), and a thread pool (if n > 1, null otherwise)
1190 * @since 7423
1191 */
1192 public static Pair<Integer, ExecutorService> newThreadPool(String pref) {
1193 int noThreads = Main.pref.getInteger(pref, Runtime.getRuntime().availableProcessors());
1194 ExecutorService pool = noThreads <= 1 ? null : Executors.newFixedThreadPool(noThreads);
1195 return new Pair<>(noThreads, pool);
1196 }
[7894]1197
1198 /**
1199 * Updates a given system property.
1200 * @param key The property key
1201 * @param value The property value
1202 * @return the previous value of the system property, or {@code null} if it did not have one.
1203 * @since 7894
1204 */
1205 public static String updateSystemProperty(String key, String value) {
1206 if (value != null) {
1207 String old = System.setProperty(key, value);
1208 if (!key.toLowerCase().contains("password")) {
1209 Main.debug("System property '"+key+"' set to '"+value+"'. Old value was '"+old+"'");
1210 } else {
1211 Main.debug("System property '"+key+"' changed.");
1212 }
1213 return old;
1214 }
1215 return null;
1216 }
[8287]1217
1218 /**
1219 * Returns a new secure SAX parser, supporting XML namespaces.
1220 * @return a new secure SAX parser, supporting XML namespaces
1221 * @throws ParserConfigurationException if a parser cannot be created which satisfies the requested configuration.
1222 * @throws SAXException for SAX errors.
1223 * @since 8287
1224 */
1225 public static SAXParser newSafeSAXParser() throws ParserConfigurationException, SAXException {
1226 SAXParserFactory parserFactory = SAXParserFactory.newInstance();
1227 parserFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
1228 parserFactory.setNamespaceAware(true);
1229 return parserFactory.newSAXParser();
1230 }
[8347]1231
1232 /**
1233 * Parse the content given {@link org.xml.sax.InputSource} as XML using the specified {@link org.xml.sax.helpers.DefaultHandler}.
1234 * This method uses a secure SAX parser, supporting XML namespaces.
1235 *
1236 * @param is The InputSource containing the content to be parsed.
1237 * @param dh The SAX DefaultHandler to use.
1238 * @throws ParserConfigurationException if a parser cannot be created which satisfies the requested configuration.
1239 * @throws SAXException for SAX errors.
1240 * @throws IOException if any IO errors occur.
1241 * @since 8347
1242 */
1243 public static void parseSafeSAX(InputSource is, DefaultHandler dh) throws ParserConfigurationException, SAXException, IOException {
1244 long start = System.currentTimeMillis();
1245 if (Main.isDebugEnabled()) {
1246 Main.debug("Starting SAX parsing of "+is+" using "+dh);
1247 }
1248 newSafeSAXParser().parse(is, dh);
1249 if (Main.isDebugEnabled()) {
1250 Main.debug("SAX parsing done in " + getDurationString(System.currentTimeMillis()-start));
1251 }
1252 }
[4069]1253}
Note: See TracBrowser for help on using the repository browser.