|
1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // ResourceFileSelectionValidator |
|
15 // |
|
16 |
|
17 |
|
18 |
|
19 package com.symbian.smt.gui.smtwidgets.resources; |
|
20 |
|
21 import java.io.File; |
|
22 import java.io.IOException; |
|
23 import java.io.InputStream; |
|
24 import java.net.MalformedURLException; |
|
25 import java.net.URL; |
|
26 import java.net.URLConnection; |
|
27 import java.util.List; |
|
28 |
|
29 import com.symbian.smt.gui.Logger; |
|
30 import com.symbian.smt.gui.ResourceFileValidator; |
|
31 import com.symbian.smt.gui.ResourcesEnums; |
|
32 import com.symbian.smt.gui.smtwidgets.IXmlFileInputValidator; |
|
33 import com.symbian.smt.gui.smtwidgets.InvalidPathException; |
|
34 |
|
35 /** |
|
36 * @author barbararosi-schwartz |
|
37 * |
|
38 */ |
|
39 public class ResourceFileSelectionValidator extends ResourceFileValidator |
|
40 implements IXmlFileInputValidator { |
|
41 |
|
42 private List<CheckableResourceFilename> existingFilenames; |
|
43 |
|
44 public ResourceFileSelectionValidator(ResourcesEnums selectedResourceType, |
|
45 List<CheckableResourceFilename> filenames) { |
|
46 super(selectedResourceType); |
|
47 |
|
48 this.existingFilenames = filenames; |
|
49 } |
|
50 |
|
51 /* (non-Javadoc) |
|
52 * @see com.symbian.smt.gui.smtwidgets.IFileInputValidator#isFileReadable(java.lang.String) |
|
53 */ |
|
54 public String isFileReadable(String filePath) { |
|
55 String errorMessage = null; |
|
56 File inFile = new File(filePath); |
|
57 |
|
58 if (!inFile.canRead()) { |
|
59 return "Selected file cannot be read."; |
|
60 } |
|
61 |
|
62 return errorMessage; |
|
63 } |
|
64 |
|
65 /* (non-Javadoc) |
|
66 * @see com.symbian.smt.gui.smtwidgets.IFileInputValidator#isUrl(java.lang.String) |
|
67 */ |
|
68 public boolean isUrl(String filePath) throws InvalidPathException { |
|
69 int index = filePath.indexOf(':'); |
|
70 |
|
71 if (index == -1 || index == 1) { |
|
72 return false; |
|
73 } else if (index > 1) { |
|
74 return true; |
|
75 } else { |
|
76 throw new InvalidPathException("Unexpected file path format."); |
|
77 } |
|
78 } |
|
79 |
|
80 /* (non-Javadoc) |
|
81 * @see com.symbian.smt.gui.smtwidgets.IFileInputValidator#isUrlResourceReadable(java.lang.String) |
|
82 */ |
|
83 public String isUrlResourceReadable(String filePath) { |
|
84 String errorMessage = null; |
|
85 InputStream urlInputStream = null; |
|
86 |
|
87 try { |
|
88 URL fileURL = new URL(filePath); |
|
89 URLConnection connection = fileURL.openConnection(); |
|
90 String contentType = null; |
|
91 |
|
92 try { |
|
93 contentType = connection.getContentType(); |
|
94 } catch (Exception e) { |
|
95 return "Resource at specified URL cannot be found."; |
|
96 } |
|
97 |
|
98 if (contentType == null) { |
|
99 return "Resource at specified URL cannot be found."; |
|
100 } |
|
101 |
|
102 if (!contentType.endsWith("xml")) { |
|
103 return "Specified URL is not an XML document."; |
|
104 } |
|
105 |
|
106 urlInputStream = connection.getInputStream(); |
|
107 |
|
108 if (urlInputStream == null) { |
|
109 errorMessage = "Resource at specified URL cannot be read."; |
|
110 } |
|
111 /* |
|
112 * Snippet below is just for diagnostics else { int bytes; while |
|
113 * ((bytes = urlInputStream.available()) > 0) { byte[] b = new |
|
114 * byte[bytes]; |
|
115 * |
|
116 * urlInputStream.read(b); String s = new String(b); |
|
117 * |
|
118 * System.err.println(s); } } |
|
119 */ |
|
120 } catch (IllegalArgumentException e) { |
|
121 errorMessage = "Resource at specified URL cannot be read."; |
|
122 } catch (MalformedURLException e) { |
|
123 errorMessage = "Specified URL is not a valid URL."; |
|
124 } catch (IOException e) { |
|
125 errorMessage = "Resource at specified URL cannot be reached."; |
|
126 } finally { |
|
127 if (urlInputStream != null) { |
|
128 try { |
|
129 urlInputStream.close(); |
|
130 } catch (IOException ignore) { |
|
131 Logger.log(ignore.getMessage(), ignore); |
|
132 } |
|
133 urlInputStream = null; |
|
134 } |
|
135 } |
|
136 |
|
137 return errorMessage; |
|
138 } |
|
139 |
|
140 private String isUrlValid(String url) { |
|
141 String errorMessage = null; |
|
142 |
|
143 try { |
|
144 URL inputUrl = new URL(url); |
|
145 inputUrl.openConnection(); |
|
146 } catch (MalformedURLException e) { |
|
147 errorMessage = "Specified URL is not a valid URL."; |
|
148 } catch (IOException e) { |
|
149 errorMessage = "Resource at specified URL cannot be reached."; |
|
150 } |
|
151 |
|
152 return errorMessage; |
|
153 } |
|
154 |
|
155 /* |
|
156 * @see org.eclipse.jface.dialogs.IInputValidator#isValid(java.lang.String) |
|
157 */ |
|
158 public String isValid(String filePath) { |
|
159 if (filePath == null || filePath.length() == 0) { |
|
160 return ""; |
|
161 } else { |
|
162 return validateResourceWhileUserTypes(filePath); |
|
163 } |
|
164 } |
|
165 |
|
166 /* (non-Javadoc) |
|
167 * @see com.symbian.smt.gui.smtwidgets.IXmlFileInputValidator#isXmlValid(java.lang.String) |
|
168 */ |
|
169 public String isXmlValid(String filePath) { |
|
170 return validateXml(filePath); |
|
171 } |
|
172 |
|
173 private String validateResourceWhileUserTypes(String filePath) { |
|
174 String errorMessage = null; |
|
175 |
|
176 // First check path is not UNC |
|
177 if (filePath.startsWith(File.separator)) { |
|
178 return "UNC paths are not compatible with the System Model Manager"; |
|
179 } |
|
180 |
|
181 // Then check that filename (path) is not duplicate |
|
182 // by checking name against table |
|
183 if (existingFilenames != null) { |
|
184 if (ResourcesWidgetHelper.contains(existingFilenames, filePath)) { |
|
185 return "The selected file has already been assigned."; |
|
186 } |
|
187 } |
|
188 |
|
189 // Then check that path is appropriate |
|
190 try { |
|
191 if (isUrl(filePath)) { |
|
192 errorMessage = isUrlValid(filePath); |
|
193 } else { |
|
194 errorMessage = isFileReadable(filePath); |
|
195 } |
|
196 } catch (InvalidPathException e) { |
|
197 errorMessage = e.getMessage(); |
|
198 } |
|
199 |
|
200 return errorMessage; |
|
201 } |
|
202 } |