|
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 // |
|
15 |
|
16 |
|
17 |
|
18 package com.symbian.smt.gui; |
|
19 |
|
20 import java.io.File; |
|
21 import java.io.IOException; |
|
22 import java.util.Locale; |
|
23 import java.util.ResourceBundle; |
|
24 |
|
25 import javax.xml.XMLConstants; |
|
26 import javax.xml.parsers.DocumentBuilder; |
|
27 import javax.xml.parsers.DocumentBuilderFactory; |
|
28 import javax.xml.parsers.ParserConfigurationException; |
|
29 import javax.xml.validation.SchemaFactory; |
|
30 |
|
31 import org.w3c.dom.Document; |
|
32 import org.xml.sax.ErrorHandler; |
|
33 import org.xml.sax.SAXException; |
|
34 import org.xml.sax.SAXParseException; |
|
35 |
|
36 /** |
|
37 * @author barbararosi-schwartz |
|
38 * |
|
39 */ |
|
40 public class XmlFileValidator { |
|
41 |
|
42 protected static String smgFolder = ""; // The location of the System Model |
|
43 // Generator |
|
44 |
|
45 public XmlFileValidator() { |
|
46 final ResourceBundle resourceBundle = ResourceBundle.getBundle( |
|
47 "location", Locale.getDefault(), this.getClass() |
|
48 .getClassLoader()); |
|
49 |
|
50 smgFolder = resourceBundle.getString("location"); |
|
51 } |
|
52 |
|
53 protected Document createDocument(final String filePath) |
|
54 throws ParserConfigurationException, SAXException, IOException { |
|
55 DocumentBuilderFactory domFactory = DocumentBuilderFactory |
|
56 .newInstance(); |
|
57 domFactory.setNamespaceAware(true); |
|
58 domFactory.setValidating(false); |
|
59 |
|
60 SchemaFactory schemaFactory = SchemaFactory |
|
61 .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); |
|
62 File schemaFile = getSchemaFile(); |
|
63 |
|
64 // schemaFile could be null if there is no schema for a specific file |
|
65 // (as is the case for the System Definition xml file) |
|
66 if (schemaFile != null && schemaFile.exists()) { |
|
67 domFactory.setSchema(schemaFactory.newSchema(getSchemaFile())); |
|
68 } |
|
69 |
|
70 DocumentBuilder builder = domFactory.newDocumentBuilder(); |
|
71 |
|
72 ErrorHandler errorHandler = new ErrorHandler() { |
|
73 public void error(SAXParseException exception) |
|
74 throws SAXParseException { |
|
75 throw exception; |
|
76 } |
|
77 |
|
78 public void fatalError(SAXParseException exception) |
|
79 throws SAXParseException { |
|
80 throw exception; |
|
81 } |
|
82 |
|
83 public void warning(SAXParseException exception) |
|
84 throws SAXParseException { |
|
85 throw exception; |
|
86 } |
|
87 }; |
|
88 |
|
89 builder.setErrorHandler(errorHandler); |
|
90 |
|
91 Document doc = builder.parse(filePath); |
|
92 |
|
93 return doc; |
|
94 } |
|
95 |
|
96 /** |
|
97 * The default implementation of this method |
|
98 * returns null, meaning that this Validator |
|
99 * is not aware of any schema definition for the |
|
100 * associated resource. |
|
101 * |
|
102 * @return the appropriate schema definition file |
|
103 * or null if there is no schema. |
|
104 */ |
|
105 protected File getSchemaFile() { |
|
106 return null; |
|
107 } |
|
108 |
|
109 public String validateXml(String filePath) { |
|
110 String errorMessage = null; |
|
111 |
|
112 try { |
|
113 createDocument(filePath); |
|
114 } catch (ParserConfigurationException e) { |
|
115 errorMessage = e.getMessage(); |
|
116 } catch (SAXException e) { |
|
117 errorMessage = e.getMessage(); |
|
118 } catch (IOException e) { |
|
119 errorMessage = e.getMessage(); |
|
120 } |
|
121 |
|
122 return errorMessage; |
|
123 } |
|
124 } |