View Javadoc

1   /*
2    * $Id: UrlFile.java 125 2008-11-04 07:18:28Z arnep $
3    */
4   package net.sf.ovanttasks.ovnative.win32;
5   
6   import java.io.File;
7   import java.io.FileOutputStream;
8   import java.io.FileReader;
9   import java.io.IOException;
10  import java.io.LineNumberReader;
11  import java.io.PrintStream;
12  import java.net.URL;
13  
14  /**
15   * UrlFile reads and writes Windows Shortcut files in the .url format. 
16   * It can be used to create or read <i>url</i> files which are used by Microsoft for Shortcuts and
17   * Favorites.
18   * </p>
19   * <p>
20   * see <a href="http://www.cyanwerks.com/urlFile-format-url.html">windows shortcut
21   * url urlFile format</a> for details of the urlFile format.*
22   * </p>
23   * <p>
24   * 
25   * <pre>
26   * UrlFile sf = new UrlFile(new File(Win32
27   * 		.getSpecialDirectory(Win32.SPECIALDIRECTORY_PERSONAL_DESKTOP),
28   * 		&quot;Go to ROXES Technologies.url&quot;));
29   * sf.setUrl(new URL(&quot;http://www.roxes.com&quot;));
30   * sf.setIconFile(&quot;C:\\winnt\\explorer.exe&quot;);
31   * sf.setIconIndex(1);
32   * // create desktop shortcut
33   * sf.save();
34   * 
35   * // dump created shortcut to console
36   * System.out.println(sf);
37   * 
38   * // create favorite
39   * // save shortcut in internet explorer favorites directory 
40   * sf.save(new File(Win32
41   * 		.getSpecialDirectory(Win32.SPECIALDIRECTORY_PERSONAL_FAVORITES),
42   * 		&quot;Go to ROXES Technologies.url&quot;));
43   * </pre>
44   * 
45   * </p>
46   * 
47   * @author lars.gersmann@roxes.com
48   * @author arnep@users.sf.net
49   */
50  public class UrlFile {
51  
52      public static final String SIGNATURE = "[InternetShortcut]";
53      public static final String LINE_BREAK = "\r\n";
54      public static final int SHOWCOMMAND_NORMAL = -1;
55      public static final int SHOWCOMMAND_MAXIMIZED = 3;
56      public static final int SHOWCOMMAND_MINIMIZED = 7;
57      String workingDirectory;
58      String iconFile;
59      int iconIndex = -1;
60      URL url;
61      int showCommand = -1;
62      short hotKey = -1;
63      String modified;
64      File urlFile;
65  
66      /**
67       * Create a new <code>LnkFile</code> instance for a specified location.
68       * <p>
69       * If <code>load</code> is <code>true</code> the instance of 
70       * <code>LnkFile</code> is initialized with the values from the <code>lnkFile</code>. 
71       * <p>
72       * The native library is also loaded.
73       * 
74       * @param urlFile The lnkFile to use
75       * @param load wether or not load the data from existing file.
76       */
77      public UrlFile(File urlFile, boolean load) throws IOException {
78          if (!urlFile.getName().endsWith(".url")) {
79              throw new IllegalArgumentException("Url file name must end with \".url\"");
80          }
81          this.urlFile = urlFile;
82          if (load) {
83              load();
84          }
85      }
86  
87      public void load() throws IOException {
88          if (!urlFile.exists() || (urlFile.length() == 0)) {
89              return;
90          }
91          LineNumberReader reader = new LineNumberReader(new FileReader(urlFile));
92  
93          String line = reader.readLine();
94          if (!line.equals(SIGNATURE)) {
95              throw new Win32Exception("Shortcut Signature \"" + SIGNATURE + "\"" + " not found in file " + urlFile.getAbsolutePath());
96          }
97          while ((line = reader.readLine()) != null) {
98              int i = line.indexOf('=');
99              if (i == -1) {
100                 throw new Win32Exception("Invalid Shortcut file format (line " + reader.getLineNumber() + "): \"=\" expected in \"" + line + "\"");
101             }
102             String key = line.substring(0, i);
103             String value = line.substring(i + 1);
104 
105             if (key.equals("URL")) {
106                 url = new URL(value);
107             } else if (key.equals("WorkingDirectory")) {
108                 workingDirectory = value;
109             } else if (key.equals("IconFile")) {
110                 iconFile = value;
111             } else if (key.equals("IconIndex")) {
112                 iconIndex = Integer.parseInt(value);
113             } else if (key.equals("ShowCommand")) {
114                 showCommand = Integer.parseInt(value);
115             } else if (key.equals("Modified")) {
116                 modified = value;
117             } else if (key.equals("HotKey")) {
118                 hotKey = Short.parseShort(value);
119             } else {
120                 throw new Win32Exception("Invalid Shortcut file format (line " + reader.getLineNumber() + "): dont know key " + key);
121             }
122         }
123 
124         reader.close();
125     }
126 
127     /**
128      * Create a new <code>UrlFile</code> instance for a specified location.
129      * <p>
130      * {@link File#File(java.lang.String, java.lang.String) 
131      * If <code>load</code> is <code>true</code> the instance of 
132      * <code>UrlFile</code> is initialized with the values from the file. 
133      * <p>
134      * The native library is also loaded.
135      * 
136      * @param directory The parent pathname string
137      * @param name The child pathname
138      * @param load wether or not load the data from existing file.
139      * @throws IOException 
140      */
141     public UrlFile(String directory, String name, boolean load) throws IOException {
142         setUrlFile(directory, name);
143         if (load) {
144             load();
145         }
146     }
147 
148     public File getUrlFile() {
149         return urlFile;
150     }
151 
152     private void setUrlFile(final String directory, String name) {
153         if (!name.endsWith(".url")) {
154             name += ".url";
155         }
156         urlFile = new File(directory, name);
157         // recoursive ?? apply to LnkFile???
158         if (!urlFile.getParentFile().exists()) {
159             urlFile.getParentFile().mkdirs();
160         }
161     }
162 
163     @Override
164     public String toString() {
165         StringBuffer sb = new StringBuffer();
166         sb.append(SIGNATURE).append(LINE_BREAK);
167 
168         if (url != null) {
169             sb.append("URL=").append(url).append(LINE_BREAK);
170         }
171         if (showCommand != SHOWCOMMAND_NORMAL) {
172             sb.append("ShowCommand=").append(showCommand).append(LINE_BREAK);
173         }
174         if (workingDirectory != null) {
175             sb.append("WorkingDirectory=").append(workingDirectory).append(
176                     LINE_BREAK);
177         }
178         if (iconFile != null) {
179             sb.append("IconFile=").append(iconFile).append(LINE_BREAK);
180         }
181         if (iconIndex != -1) {
182             sb.append("IconIndex=").append(iconIndex).append(LINE_BREAK);
183         }
184         if (modified != null) {
185             sb.append("Modified=").append(modified).append(LINE_BREAK);
186         }
187         if (hotKey != -1) {
188             sb.append("HotKey=").append(hotKey).append(LINE_BREAK);
189         }
190         return sb.toString();
191     }
192 
193     /**
194      * @return iconIndex
195      */
196     public int getIconIndex() {
197         return iconIndex;
198     }
199 
200     /**
201      * @return modified
202      */
203     public String getModified() {
204         return modified;
205     }
206 
207     /**
208      * @return showCommand (see the SHOWCOMMAND_* constants)
209      */
210     public int getShowCommand() {
211         return showCommand;
212     }
213 
214     /**
215      * @return url to open
216      */
217     public URL getUrl() {
218         return url;
219     }
220 
221     /**
222      * @return working directory
223      */
224     public String getWorkingDirectory() {
225         return workingDirectory;
226     }
227 
228     /**
229      * @param string
230      *            modification date
231      */
232     public void setModified(String string) {
233         modified = string;
234     }
235 
236     /**
237      * @param i
238      *            the show command to use (see the SHOWCOMMAND_* constants)
239      */
240     public void setShowCommand(int i) {
241         showCommand = i;
242     }
243 
244     /**
245      * @param url
246      *            url to open
247      */
248     public void setUrl(URL url) {
249         this.url = url;
250     }
251 
252     /**
253      * @param string
254      *            the working directory
255      */
256     public void setWorkingDirectory(String string) {
257         workingDirectory = string;
258     }
259 
260     public void save(final String directory, final String name) throws IOException {
261         setUrlFile(directory, name);
262         save();
263     }
264 
265     public void save() throws IOException {
266         PrintStream out = new PrintStream(new FileOutputStream(urlFile));
267         out.print(toString());
268         out.close();
269     }
270 
271     public void save(File file) throws IOException {
272         this.urlFile = file;
273         save();
274     }
275 
276     public String getHotKey() {
277         return HotKeyTools.toString(hotKey);
278     }
279 
280     /**
281      * @return the icon urlFile
282      */
283     public String getIconFile() {
284         return iconFile;
285     }
286 
287     public void setHotKey(String hotKey) {
288         this.hotKey = HotKeyTools.parseString(hotKey);
289     }
290 
291     /**
292      * @param iconFile
293      *            the icon urlFile to use (valid urlFile types are exe, dll and ico)
294      * @param iconIndex 
295      */
296     public void setIcon(String iconFile, int iconIndex) {
297         this.iconFile = iconFile;
298         this.iconIndex = iconIndex;
299     }
300 }