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

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

simplify/instrument SAX code

  • Property svn:eol-style set to native
File size: 44.3 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.Iterator;
43import java.util.List;
44import java.util.concurrent.ExecutorService;
45import java.util.concurrent.Executors;
46import java.util.regex.Matcher;
47import java.util.regex.Pattern;
48import java.util.zip.GZIPInputStream;
49import java.util.zip.ZipEntry;
50import java.util.zip.ZipFile;
51import java.util.zip.ZipInputStream;
52
53import javax.xml.XMLConstants;
54import javax.xml.parsers.ParserConfigurationException;
55import javax.xml.parsers.SAXParser;
56import javax.xml.parsers.SAXParserFactory;
57
58import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
59import org.openstreetmap.josm.Main;
60import org.openstreetmap.josm.data.Version;
61import org.xml.sax.InputSource;
62import org.xml.sax.SAXException;
63import org.xml.sax.helpers.DefaultHandler;
64
65/**
66 * Basic utils, that can be useful in different parts of the program.
67 */
68public final class Utils {
69
70 public static final Pattern WHITE_SPACES_PATTERN = Pattern.compile("\\s+");
71
72 private Utils() {
73 // Hide default constructor for utils classes
74 }
75
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
81 public static final String URL_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;=%";
82
83 /**
84 * Tests whether {@code predicate} applies to at least one elements from {@code collection}.
85 */
86 public static <T> boolean exists(Iterable<? extends T> collection, Predicate<? super T> predicate) {
87 for (T item : collection) {
88 if (predicate.evaluate(item))
89 return true;
90 }
91 return false;
92 }
93
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
101 public static <T> boolean exists(Iterable<T> collection, Class<? extends T> klass) {
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
117 @SuppressWarnings("unchecked")
118 public static <T> T find(Iterable<? super T> collection, Class<? extends T> klass) {
119 for (Object item : collection) {
120 if (klass.isInstance(item))
121 return (T) item;
122 }
123 return null;
124 }
125
126 public static <T> Collection<T> filter(Collection<? extends T> collection, Predicate<? super T> predicate) {
127 return new FilteredCollection<>(collection, predicate);
128 }
129
130 /**
131 * Returns the first element from {@code items} which is non-null, or null if all elements are null.
132 * @param items the items to look for
133 * @return first non-null item if there is one
134 */
135 @SafeVarargs
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
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) {
150 return new SubclassFilteredCollection<>(collection, new Predicate<S>() {
151 @Override
152 public boolean evaluate(S o) {
153 return klass.isInstance(o);
154 }
155 });
156 }
157
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
168 /**
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}.
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 {
181 if (a < c)
182 return a;
183 return c;
184 }
185 }
186
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 */
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
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 */
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
217 /**
218 * return the modulus in the range [0, n)
219 */
220 public static int mod(int a, int n) {
221 if (n <= 0)
222 throw new IllegalArgumentException("n must be <= 0 but is "+n);
223 int res = a % n;
224 if (res < 0) {
225 res += n;
226 }
227 return res;
228 }
229
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) {
239 CheckParameterUtil.ensureParameterNotNull(sep, "sep");
240 if (values == null)
241 return null;
242 StringBuilder s = null;
243 for (Object a : values) {
244 if (a == null) {
245 a = "";
246 }
247 if (s != null) {
248 s.append(sep).append(a.toString());
249 } else {
250 s = new StringBuilder(a.toString());
251 }
252 }
253 return s != null ? s.toString() : "";
254 }
255
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) {
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
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 }
283
284 /**
285 * convert float range 0 &lt;= x &lt;= 1 to integer range 0..255
286 * when dealing with colors and color alpha value
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
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 /**
299 * convert integer range 0..255 to float range 0 &lt;= x &lt;= 1
300 * when dealing with colors and color alpha value
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
310 public static Color complement(Color clr) {
311 return new Color(255 - clr.getRed(), 255 - clr.getGreen(), 255 - clr.getBlue(), clr.getAlpha());
312 }
313
314 /**
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 }
326
327 /**
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 }
339
340 /**
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 /**
354 * Simple file copy function that will overwrite the target file.
355 * @param in The source file
356 * @param out The destination file
357 * @return the path to the target file
358 * @throws IOException if any I/O error occurs
359 * @throws IllegalArgumentException if {@code in} or {@code out} is {@code null}
360 * @since 7003
361 */
362 public static Path copyFile(File in, File out) throws IOException {
363 CheckParameterUtil.ensureParameterNotNull(in, "in");
364 CheckParameterUtil.ensureParameterNotNull(out, "out");
365 return Files.copy(in.toPath(), out.toPath(), StandardCopyOption.REPLACE_EXISTING);
366 }
367
368 /**
369 * Recursive directory copy function
370 * @param in The source directory
371 * @param out The destination directory
372 * @throws IOException if any I/O error ooccurs
373 * @throws IllegalArgumentException if {@code in} or {@code out} is {@code null}
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 }
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 }
391 }
392 }
393 }
394
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 */
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
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 */
419 public static boolean deleteDirectory(File path) {
420 if( path.exists() ) {
421 File[] files = path.listFiles();
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 }
429 }
430 }
431 }
432 return path.delete();
433 }
434
435 /**
436 * <p>Utility method for closing a {@link java.io.Closeable} object.</p>
437 *
438 * @param c the closeable object. May be null.
439 */
440 public static void close(Closeable c) {
441 if (c == null) return;
442 try {
443 c.close();
444 } catch (IOException e) {
445 Main.warn(e);
446 }
447 }
448
449 /**
450 * <p>Utility method for closing a {@link java.util.zip.ZipFile}.</p>
451 *
452 * @param zip the zip file. May be null.
453 */
454 public static void close(ZipFile zip) {
455 if (zip == null) return;
456 try {
457 zip.close();
458 } catch (IOException e) {
459 Main.warn(e);
460 }
461 }
462
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 }
479
480 private static final double EPSILON = 1e-11;
481
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 */
488 public static boolean equalsEpsilon(double a, double b) {
489 return Math.abs(a - b) <= EPSILON;
490 }
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) {
507 Main.error(ex);
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() {
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);
522 } catch (IllegalStateException e) {
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) {
527 Main.warn("InterruptedException in "+Utils.class.getSimpleName()+" while getting clipboard content");
528 }
529 }
530 }
531 try {
532 if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
533 return (String) t.getTransferData(DataFlavor.stringFlavor);
534 }
535 } catch (UnsupportedFlavorException | IOException ex) {
536 Main.error(ex);
537 return null;
538 }
539 return null;
540 }
541
542 /**
543 * Calculate MD5 hash of a string and output in hexadecimal format.
544 * @param data arbitrary String
545 * @return MD5 hash of data, string of length 32 with characters in range [0-9a-f]
546 */
547 public static String md5Hex(String data) {
548 byte[] byteData = data.getBytes(StandardCharsets.UTF_8);
549 MessageDigest md = null;
550 try {
551 md = MessageDigest.getInstance("MD5");
552 } catch (NoSuchAlgorithmException e) {
553 throw new RuntimeException(e);
554 }
555 byte[] byteDigest = md.digest(byteData);
556 return toHexString(byteDigest);
557 }
558
559 private static final char[] HEX_ARRAY = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
560
561 /**
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
567 */
568 public static String toHexString(byte[] bytes) {
569
570 if (bytes == null) {
571 return "";
572 }
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 }
585 return new String(hexChars);
586 }
587
588 /**
589 * Topological sort.
590 *
591 * @param dependencies contains mappings (key -&gt; value). In the final list of sorted objects, the key will come
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) {
597 MultiMap<T,T> deps = new MultiMap<>();
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();
607 List<T> sorted = new ArrayList<>();
608 for (int i=0; i<size; ++i) {
609 T parentless = null;
610 for (T key : deps.keySet()) {
611 if (deps.get(key).isEmpty()) {
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 }
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
645 * applies the {@link org.openstreetmap.josm.tools.Utils.Function} {@code f} on each element upon access.
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 */
652 public static <A, B> Collection<B> transform(final Collection<? extends A> c, final Function<A, B> f) {
653 return new AbstractCollection<B>() {
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
664 private Iterator<? extends A> it = c.iterator();
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 }
682 };
683 }
684
685 /**
686 * Transforms the list {@code l} into an unmodifiable list and
687 * applies the {@link org.openstreetmap.josm.tools.Utils.Function} {@code f} on each element upon access.
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>() {
696
697 @Override
698 public int size() {
699 return l.size();
700 }
701
702 @Override
703 public B get(int index) {
704 return f.apply(l.get(index));
705 }
706 };
707 }
708
709 private static final Pattern HTTP_PREFFIX_PATTERN = Pattern.compile("https?");
710
711 /**
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
715 * @throws java.io.IOException if an I/O exception occurs.
716 * @since 5587
717 */
718 public static HttpURLConnection openHttpConnection(URL httpURL) throws IOException {
719 if (httpURL == null || !HTTP_PREFFIX_PATTERN.matcher(httpURL.getProtocol()).matches()) {
720 throw new IllegalArgumentException("Invalid HTTP url");
721 }
722 if (Main.isDebugEnabled()) {
723 Main.debug("Opening HTTP connection to "+httpURL.toExternalForm());
724 }
725 HttpURLConnection connection = (HttpURLConnection) httpURL.openConnection();
726 connection.setRequestProperty("User-Agent", Version.getInstance().getFullAgentString());
727 connection.setUseCaches(false);
728 return connection;
729 }
730
731 /**
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
735 * @throws java.io.IOException if an I/O exception occurs.
736 * @since 5867
737 */
738 public static InputStream openURL(URL url) throws IOException {
739 return openURLAndDecompress(url, false);
740 }
741
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
745 * @param decompress whether to wrap steam in a {@link GZIPInputStream} or {@link BZip2CompressorInputStream}
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());
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 }
763 }
764 return in;
765 }
766
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
772 * @since 7867
773 */
774 public static BZip2CompressorInputStream getBZip2InputStream(InputStream in) throws IOException {
775 if (in == null) {
776 return null;
777 }
778 return new BZip2CompressorInputStream(in, /* see #9537 */ true);
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
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
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)
834 * @throws java.io.IOException if an I/O exception occurs.
835 * @since 5868
836 */
837 public static BufferedReader openURLReader(URL url) throws IOException {
838 return openURLReaderAndDecompress(url, false);
839 }
840
841 /**
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
844 * @param decompress whether to wrap steam in a {@link GZIPInputStream} or {@link BZip2CompressorInputStream}
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 {
851 return new BufferedReader(new InputStreamReader(openURLAndDecompress(url, decompress), StandardCharsets.UTF_8));
852 }
853
854 /**
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://)
857 * @param keepAlive whether not to set header {@code Connection=close}
858 * @return An open HTTP connection to the given URL
859 * @throws java.io.IOException if an I/O exception occurs.
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 }
867 if (Main.isDebugEnabled()) {
868 try {
869 Main.debug("REQUEST: "+ connection.getRequestProperties());
870 } catch (IllegalStateException e) {
871 Main.warn(e);
872 }
873 }
874 return connection;
875 }
876
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>
880 * @see <a href="https://bugs.openjdk.java.net/browse/JDK-4080617">JDK bug 4080617</a>
881 * @param str The string to strip
882 * @return <code>str</code>, without leading and trailing characters, according to
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);
894 // '\u200B' (ZERO WIDTH SPACE character) needs to be handled manually because of change in Unicode 6.0 (Java 7, see #8918)
895 // same for '\uFEFF' (ZERO WIDTH NO-BREAK SPACE)
896 leadingWhite = (Character.isWhitespace(c) || Character.isSpaceChar(c) || c == '\u200B' || c == '\uFEFF');
897 if (leadingWhite) {
898 start++;
899 }
900 }
901 boolean trailingWhite = true;
902 while (trailingWhite && end > start+1) {
903 char c = str.charAt(end-1);
904 trailingWhite = (Character.isWhitespace(c) || Character.isSpaceChar(c) || c == '\u200B' || c == '\uFEFF');
905 if (trailingWhite) {
906 end--;
907 }
908 }
909 return str.substring(start, end);
910 }
911
912 /**
913 * Runs an external command and returns the standard output.
914 *
915 * The program is expected to execute fast.
916 *
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 {
922 if (Main.isDebugEnabled()) {
923 Main.debug(join(" ", command));
924 }
925 Process p = new ProcessBuilder(command).start();
926 try (BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream(), StandardCharsets.UTF_8))) {
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 }
936 }
937 return all != null ? all.toString() : null;
938 }
939 }
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");
952 if (!josmTmpDir.exists() && !josmTmpDir.mkdirs()) {
953 Main.warn("Unable to create temp directory "+josmTmpDir);
954 }
955 return josmTmpDir;
956 }
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
961 * @return A human readable string for the given duration
962 * @throws IllegalArgumentException if elapsedTime is &lt; 0
963 * @since 6354
964 */
965 public static String getDurationString(long elapsedTime) {
966 if (elapsedTime < 0) {
967 throw new IllegalArgumentException("elapsedTime must be >= 0");
968 }
969 // Is it less than 1 second ?
970 if (elapsedTime < MILLIS_OF_SECOND) {
971 return String.format("%d %s", elapsedTime, tr("ms"));
972 }
973 // Is it less than 1 minute ?
974 if (elapsedTime < MILLIS_OF_MINUTE) {
975 return String.format("%.1f %s", elapsedTime / (float) MILLIS_OF_SECOND, tr("s"));
976 }
977 // Is it less than 1 hour ?
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"));
981 }
982 // Is it less than 1 day ?
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"));
986 }
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"));
989 }
990
991 /**
992 * Returns a human readable representation of a list of positions.
993 * <p>
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 /**
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()) {
1033 List<String> result = new ArrayList<>(m.groupCount() + 1);
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 }
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 */
1051 @SuppressWarnings("unchecked")
1052 public static <T> T cast(Object o, Class<T> klass) {
1053 if (klass.isInstance(o)) {
1054 return (T) o;
1055 }
1056 return null;
1057 }
1058
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();
1069 while (cause != null && !cause.equals(result)) {
1070 result = cause;
1071 cause = result.getCause();
1072 }
1073 }
1074 return result;
1075 }
1076
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 }
1089
1090 /**
1091 * If the string {@code s} is longer than {@code maxLength}, the string is cut and "..." is appended.
1092 * @param s String to shorten
1093 * @param maxLength maximum number of characters to keep (not including the "...")
1094 * @return the shortened string
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 }
1103
1104 /**
1105 * Fixes URL with illegal characters in the query (and fragment) part by
1106 * percent encoding those characters.
1107 *
1108 * special characters like &amp; and # are not encoded
1109 *
1110 * @param url the URL that should be fixed
1111 * @return the repaired URL
1112 */
1113 public static String fixURLQuery(String url) {
1114 if (url.indexOf('?') == -1)
1115 return url;
1116
1117 String query = url.substring(url.indexOf('?') + 1);
1118
1119 StringBuilder sb = new StringBuilder(url.substring(0, url.indexOf('?') + 1));
1120
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 {
1126 sb.append(encodeUrl(c));
1127 }
1128 }
1129 return sb.toString();
1130 }
1131
1132 /**
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 /**
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 }
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 }
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 }
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 }
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 }
1253}
Note: See TracBrowser for help on using the repository browser.