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

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

fix #11498 - Warn about obvious misspelled tag values (modified patch by mdk) + javadoc

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