0
|
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 |
// ${file_name}
|
|
15 |
//
|
|
16 |
//
|
|
17 |
|
|
18 |
package com.symbian.smt.gui.smtwidgets;
|
|
19 |
|
|
20 |
import java.io.File;
|
|
21 |
import java.io.IOException;
|
|
22 |
import java.io.InputStream;
|
|
23 |
import java.net.MalformedURLException;
|
|
24 |
import java.net.URL;
|
|
25 |
import java.net.URLConnection;
|
|
26 |
import java.util.List;
|
|
27 |
|
|
28 |
import javax.xml.parsers.DocumentBuilder;
|
|
29 |
import javax.xml.parsers.DocumentBuilderFactory;
|
|
30 |
import javax.xml.parsers.ParserConfigurationException;
|
|
31 |
|
|
32 |
import org.w3c.dom.Document;
|
|
33 |
import org.xml.sax.ErrorHandler;
|
|
34 |
import org.xml.sax.SAXException;
|
|
35 |
import org.xml.sax.SAXParseException;
|
|
36 |
|
|
37 |
import com.symbian.smt.gui.Logger;
|
|
38 |
import com.symbian.smt.gui.XmlFileValidator;
|
|
39 |
|
|
40 |
/**
|
|
41 |
* @author barbararosi-schwartz
|
|
42 |
*
|
|
43 |
*/
|
|
44 |
public class SystemDefinitionFileSelectionValidator extends
|
|
45 |
XmlFileValidator implements IXmlFileInputValidator {
|
|
46 |
|
|
47 |
private List<String> sysdefFilenames;
|
|
48 |
private InputStream urlInputStream;
|
|
49 |
|
|
50 |
public SystemDefinitionFileSelectionValidator(List<String> filenames) {
|
|
51 |
super();
|
|
52 |
|
|
53 |
this.sysdefFilenames = filenames;
|
|
54 |
}
|
|
55 |
|
|
56 |
private void closeInputStream() {
|
|
57 |
if (urlInputStream != null) {
|
|
58 |
try {
|
|
59 |
urlInputStream.close();
|
|
60 |
} catch (IOException ignore) {
|
|
61 |
Logger.log(ignore.getMessage(), ignore);
|
|
62 |
}
|
|
63 |
urlInputStream = null;
|
|
64 |
}
|
|
65 |
}
|
|
66 |
|
|
67 |
protected Document createDocument(final String filePath)
|
|
68 |
throws ParserConfigurationException, SAXException, IOException {
|
|
69 |
DocumentBuilderFactory domFactory = DocumentBuilderFactory
|
|
70 |
.newInstance();
|
|
71 |
domFactory.setNamespaceAware(true);
|
|
72 |
domFactory.setValidating(false);
|
|
73 |
|
|
74 |
DocumentBuilder builder = domFactory.newDocumentBuilder();
|
|
75 |
|
|
76 |
ErrorHandler errorHandler = new ErrorHandler() {
|
|
77 |
public void error(SAXParseException exception)
|
|
78 |
throws SAXParseException {
|
|
79 |
throw exception;
|
|
80 |
}
|
|
81 |
|
|
82 |
public void fatalError(SAXParseException exception)
|
|
83 |
throws SAXParseException {
|
|
84 |
throw exception;
|
|
85 |
}
|
|
86 |
|
|
87 |
public void warning(SAXParseException exception)
|
|
88 |
throws SAXParseException {
|
|
89 |
throw exception;
|
|
90 |
}
|
|
91 |
};
|
|
92 |
|
|
93 |
builder.setErrorHandler(errorHandler);
|
|
94 |
Document doc = null;
|
|
95 |
|
|
96 |
try {
|
|
97 |
if (isUrl(filePath)) {
|
|
98 |
if (urlInputStream != null) {
|
|
99 |
doc = builder.parse(urlInputStream);
|
|
100 |
closeInputStream();
|
|
101 |
}
|
|
102 |
} else {
|
|
103 |
doc = builder.parse(filePath);
|
|
104 |
}
|
|
105 |
} catch (InvalidPathException e) {
|
|
106 |
// This cannot happen as it has already been checked while the user
|
|
107 |
// was typing in
|
|
108 |
}
|
|
109 |
|
|
110 |
return doc;
|
|
111 |
}
|
|
112 |
|
|
113 |
private String isFilePathValid(String filePath) {
|
|
114 |
return isFileReadable(filePath);
|
|
115 |
}
|
|
116 |
|
|
117 |
/* (non-Javadoc)
|
|
118 |
* @see com.symbian.smt.gui.smtwidgets.IFileInputValidator#isFileReadable(java.lang.String)
|
|
119 |
*/
|
|
120 |
public String isFileReadable(String filePath) {
|
|
121 |
String errorMessage = null;
|
|
122 |
File inFile = new File(filePath);
|
|
123 |
|
|
124 |
if (!inFile.canRead()) {
|
|
125 |
return "Selected file cannot be read.";
|
|
126 |
}
|
|
127 |
|
|
128 |
return errorMessage;
|
|
129 |
}
|
|
130 |
|
|
131 |
/* (non-Javadoc)
|
|
132 |
* @see com.symbian.smt.gui.smtwidgets.IFileInputValidator#isUrl(java.lang.String)
|
|
133 |
*/
|
|
134 |
public boolean isUrl(String filePath) throws InvalidPathException {
|
|
135 |
int index = filePath.indexOf(':');
|
|
136 |
|
|
137 |
if (index == -1 || index == 1) {
|
|
138 |
return false;
|
|
139 |
} else if (index > 1) {
|
|
140 |
return true;
|
|
141 |
} else {
|
|
142 |
throw new InvalidPathException("Unexpected file path format.");
|
|
143 |
}
|
|
144 |
}
|
|
145 |
|
|
146 |
/* (non-Javadoc)
|
|
147 |
* @see com.symbian.smt.gui.smtwidgets.IFileInputValidator#isUrlResourceReadable(java.lang.String)
|
|
148 |
*/
|
|
149 |
public String isUrlResourceReadable(String filePath) {
|
|
150 |
String errorMessage = null;
|
|
151 |
|
|
152 |
try {
|
|
153 |
URL fileURL = new URL(filePath.trim());
|
|
154 |
URLConnection connection = fileURL.openConnection();
|
|
155 |
String contentType = connection.getContentType();
|
|
156 |
|
|
157 |
if (contentType == null) {
|
|
158 |
return "System definition resource at specified URL cannot be read.";
|
|
159 |
}
|
|
160 |
|
|
161 |
if (!contentType.endsWith("xml")) {
|
|
162 |
return "Specified URL is not an XML document.";
|
|
163 |
}
|
|
164 |
|
|
165 |
urlInputStream = connection.getInputStream();
|
|
166 |
|
|
167 |
if (urlInputStream == null) {
|
|
168 |
errorMessage = "System definition resource at specified URL cannot be read.";
|
|
169 |
}
|
|
170 |
} catch (IllegalArgumentException e) {
|
|
171 |
closeInputStream();
|
|
172 |
errorMessage = "System definition resource at specified URL cannot be read.";
|
|
173 |
} catch (MalformedURLException e) {
|
|
174 |
closeInputStream();
|
|
175 |
errorMessage = "Specified URL is not a valid URL.";
|
|
176 |
} catch (IOException e) {
|
|
177 |
closeInputStream();
|
|
178 |
errorMessage = "System definition resource at specified URL cannot be reached.";
|
|
179 |
}
|
|
180 |
|
|
181 |
return errorMessage;
|
|
182 |
}
|
|
183 |
|
|
184 |
private String isUrlValid(String url) {
|
|
185 |
String errorMessage = null;
|
|
186 |
|
|
187 |
try {
|
|
188 |
URL inputUrl = new URL(url);
|
|
189 |
inputUrl.openConnection();
|
|
190 |
} catch (MalformedURLException e) {
|
|
191 |
closeInputStream();
|
|
192 |
errorMessage = "Specified URL is not a valid URL.";
|
|
193 |
} catch (IOException e) {
|
|
194 |
closeInputStream();
|
|
195 |
errorMessage = "Resource at specified URL cannot be reached.";
|
|
196 |
}
|
|
197 |
|
|
198 |
return errorMessage;
|
|
199 |
}
|
|
200 |
|
|
201 |
/*
|
|
202 |
* @see org.eclipse.jface.dialogs.IInputValidator#isValid(java.lang.String)
|
|
203 |
*/
|
|
204 |
public String isValid(String filePath) {
|
|
205 |
if (filePath == null || filePath.length() == 0) {
|
|
206 |
return "";
|
|
207 |
} else {
|
|
208 |
return validateResourceWhileUserTypes(filePath);
|
|
209 |
}
|
|
210 |
}
|
|
211 |
|
|
212 |
/* (non-Javadoc)
|
|
213 |
* @see com.symbian.smt.gui.smtwidgets.IXmlFileInputValidator#isXmlValid(java.lang.String)
|
|
214 |
*/
|
|
215 |
public String isXmlValid(String filePath) {
|
|
216 |
return validateXml(filePath);
|
|
217 |
}
|
|
218 |
|
|
219 |
private String validateResourceWhileUserTypes(String filePath) {
|
|
220 |
String errorMessage = null;
|
|
221 |
|
|
222 |
// First check path is not UNC
|
|
223 |
if (filePath.startsWith(File.separator)) {
|
|
224 |
return "UNC paths are not compatible with the System Model Manager";
|
|
225 |
}
|
|
226 |
|
|
227 |
// Then check that filename (path) is not duplicate
|
|
228 |
// by checking name against table
|
|
229 |
if ((sysdefFilenames != null) && (sysdefFilenames.contains(filePath))) {
|
|
230 |
return "The selected file has already been assigned.";
|
|
231 |
}
|
|
232 |
|
|
233 |
// Then check that path is appropriate
|
|
234 |
try {
|
|
235 |
if (isUrl(filePath)) {
|
|
236 |
errorMessage = isUrlValid(filePath);
|
|
237 |
} else {
|
|
238 |
errorMessage = isFilePathValid(filePath);
|
|
239 |
}
|
|
240 |
} catch (InvalidPathException e) {
|
|
241 |
errorMessage = e.getMessage();
|
|
242 |
}
|
|
243 |
|
|
244 |
return errorMessage;
|
|
245 |
}
|
|
246 |
}
|