Debian Codegraph @

src/com/drew/tools/ProcessUrlUtility.java

raw symbols SCIP
1 /*
2 * Copyright 2002-2019 Drew Noakes and contributors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * More information about this project is available at:
17 *
18 * https://drewnoakes.com/code/exif/
19 * https://github.com/drewnoakes/metadata-extractor
20 */
21
22 package com.drew.tools;
23
24 import com.drew.imaging.ImageMetadataReader;
25 import com.drew.imaging.ImageProcessingException;
26 import com.drew.imaging.jpeg.JpegProcessingException;
27 import com.drew.metadata.Directory;
28 import com.drew.metadata.Metadata;
29 import com.drew.metadata.Tag;
30
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.net.URL;
34 import java.net.URLConnection;
35
36 /**
37 * Utility that extracts metadata found at a given URL.
38 *
39 * @author Drew Noakes https://drewnoakes.com
40 */
41 public class ProcessUrlUtility
42 {
43 public static void main(String[] args) throws IOException, JpegProcessingException
44 {
45 if (args.length == 0) {
46 System.err.println("Expects one or more URLs as arguments.");
47 System.exit(1);
48 }
49
50 for (String url : args)
51 processUrl(new URL(url));
52
53 System.out.println("Completed.");
54 }
55
56 private static void processUrl(URL url) throws IOException
57 {
58 URLConnection con = url.openConnection();
59 // con.setConnectTimeout(connectTimeout);
60 // con.setReadTimeout(readTimeout);
61 InputStream in = con.getInputStream();
62
63 // Read metadata
64 final Metadata metadata;
65 try {
66 metadata = ImageMetadataReader.readMetadata(in);
67 } catch (ImageProcessingException e) {
68 // this is an error in the Jpeg segment structure. we're looking for bad handling of
69 // metadata segments. in this case, we didn't even get a segment.
70 System.err.printf("%s: %s [Error Extracting Metadata]%n\t%s%n", e.getClass().getName(), url, e.getMessage()); return;
71 } catch (Throwable t) {
72 // general, uncaught exception during processing of jpeg segments
73 System.err.printf("%s: %s [Error Extracting Metadata]%n", t.getClass().getName(), url);
74 t.printStackTrace(System.err);
75 return;
76 }
77
78 if (metadata.hasErrors()) {
79 System.err.println(url);
80 for (Directory directory : metadata.getDirectories()) {
81 if (!directory.hasErrors())
82 continue;
83 for (String error : directory.getErrors()) {
84 System.err.printf("\t[%s] %s%n", directory.getName(), error);
85 }
86 }
87 }
88
89 // Iterate through all values
90 for (Directory directory : metadata.getDirectories()) {
91 for (Tag tag : directory.getTags()) {
92 String tagName = tag.getTagName();
93 String directoryName = directory.getName();
94 String description = tag.getDescription();
95
96 // truncate the description if it's too long
97 if (description != null && description.length() > 1024) {
98 description = description.substring(0, 1024) + "...";
99 }
100
101 System.out.printf("[%s] %s = %s%n", directoryName, tagName, description);
102 }
103 }
104
105 // if (processedCount > 0)
106 // System.out.println(String.format("Processed %,d files (%,d bytes) with %,d exceptions and %,d file errors in %s", processedCount, byteCount, exceptionCount, errorCount, path));
107 }
108 }