Source code: com/watsonnet/jcap/Util.java
1 package com.watsonnet.jcap;
2
3 // java:
4 import java.awt.*;
5 import java.awt.event.*;
6 import java.io.*;
7 import java.net.*;
8 import java.util.*;
9 import java.net.URL;
10
11 // swing:
12 import javax.swing.*;
13 import javax.swing.event.*;
14
15 // exif
16 import com.drew.metadata.*;
17 import com.drew.imaging.jpeg.*;
18 import com.drew.lang.*;
19
20 // watsonnet
21 import com.watsonnet.jcap.*;
22
23 public class Util {
24 public static final String DESCRIPTION_EXT = "_description.txt";
25 public static final String KEYWORDS_EXT = "_keywords.txt";
26 public static final String CAPTION_EXT = ".txt";
27 public static final String TEXT_EXT = ".txt";
28 public static final String FOLDERINFO_FILENAME = "_folderInfo.txt";
29
30 // Read the contents of a text file
31 public static String readFile(File f) {
32 try {
33 StringBuffer sb = new StringBuffer();
34 if (f.canRead()) {
35 String line;
36 BufferedReader br = new BufferedReader(new FileReader(f));
37 while ((line = br.readLine()) != null) {
38 if (sb.length() > 0) { sb.append("\n"); }
39 sb.append(line);
40 }
41 }
42 return sb.toString();
43 } catch (java.io.IOException ex) {
44 JOptionPane.showMessageDialog(null, "The file " + f.getName() + " could not be read.", "Read error", JOptionPane.WARNING_MESSAGE);
45 return("");
46 }
47 }
48
49 // Save text data to the given file
50 public static void saveFile(File f, String data) {
51 // Save data
52 try {
53 f.createNewFile();
54 if (f.canWrite()) {
55 FileWriter fw = new FileWriter(f);
56 fw.write(data);
57 fw.flush();
58 fw.close();
59 }
60 } catch (java.io.IOException ex) {
61 JOptionPane.showMessageDialog(null, "The file " + f.getName() + " could not be saved.", "Save error", JOptionPane.WARNING_MESSAGE);
62 }
63 }
64
65 // Get filenames
66 public static String getTextFileName(String filename) {
67 return(getFilenameLabel(filename) + DESCRIPTION_EXT);
68 }
69
70 // Get the filename of the txt file associated with each image
71 public static String getKeywordsFileName(String filename) {
72 return(getFilenameLabel(filename) + KEYWORDS_EXT);
73 }
74
75 // Get the filename of the txt file associated with each image
76 public static String getCaptionFileName(String filename) {
77 return(getFilenameLabel(filename) + CAPTION_EXT);
78 }
79
80 // Determines if the given base filename (without extension) has the same
81 // name as an image file in the same folder.
82 public static boolean imageExistsForFilename(String filename) {
83 String filelabel = getFilenameLabel(filename);
84 ArrayList ext = getImageTypesList();
85
86 for(int i=0; i <= ext.size()-1; i++) {
87 File f = new File(filelabel + (String)ext.get(i));
88 if (f.exists()) {
89 return(true);
90 }
91 }
92
93 return(false);
94 }
95
96 // Determines if the given filename is a supported image type
97 public static boolean isImageType(String filename) {
98 ArrayList ext = getImageTypesList();
99
100 for(int i=0; i <= ext.size()-1; i++) {
101 if (filename.toLowerCase().endsWith(((String)ext.get(i)).toLowerCase())) {
102 return(true);
103 }
104 }
105
106 return(false);
107 }
108
109 // An arraylist of valid image types
110 public static ArrayList getImageTypesList() {
111 ArrayList data = new ArrayList();
112 data.add(".jpg");
113 data.add(".jpeg");
114 data.add(".gif");
115 data.add(".png");
116 return(data);
117 }
118
119 // Gets the filename without the extension. The purpose of this is
120 // to get the raw image filename without extensions.
121 public static String getFilenameLabel(String path) {
122 String retval = path;
123 int dot = -1;
124
125 // look for special filenames first
126 if (dot == -1) { dot = path.lastIndexOf(DESCRIPTION_EXT); }
127 if (dot == -1) { dot = path.lastIndexOf(KEYWORDS_EXT); }
128
129 // if those aren't found then just look for the extension
130 if (dot == -1) { dot = path.lastIndexOf('.'); }
131
132 if (dot > -1) {
133 retval = path.substring(0, dot);
134 }
135 return retval;
136 }
137
138 // Returns true if this image type supports EXIF data
139 public static boolean supportsEXIF(String path) {
140 return(path.toLowerCase().endsWith(".jpg") || path.toLowerCase().endsWith(".jpeg"));
141 }
142 }
143