| 1 | /* WRS 2.0 |
|---|
| 2 | * Copyright (C) 2009 Anton Patrushev |
|---|
| 3 | * |
|---|
| 4 | * This program is free software; you can redistribute it and/or modify |
|---|
| 5 | * it under the terms of the GNU General Public License as published by |
|---|
| 6 | * the Free Software Foundation; either version 3 of the License, or |
|---|
| 7 | * (at your option) any later version. |
|---|
| 8 | * |
|---|
| 9 | * This program is distributed in the hope that it will be useful, |
|---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 12 | * GNU General Public License for more details. |
|---|
| 13 | * |
|---|
| 14 | * You should have received a copy of the GNU General Public License |
|---|
| 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 16 | */ |
|---|
| 17 | |
|---|
| 18 | package util; |
|---|
| 19 | |
|---|
| 20 | import java.io.BufferedReader; |
|---|
| 21 | import java.io.File; |
|---|
| 22 | import java.io.FileReader; |
|---|
| 23 | import java.io.IOException; |
|---|
| 24 | import java.text.DecimalFormat; |
|---|
| 25 | |
|---|
| 26 | import org.w3c.dom.Document; |
|---|
| 27 | |
|---|
| 28 | import javax.xml.bind.JAXBContext; |
|---|
| 29 | import javax.xml.bind.JAXBException; |
|---|
| 30 | import javax.xml.bind.Unmarshaller; |
|---|
| 31 | import javax.xml.parsers.DocumentBuilder; |
|---|
| 32 | import javax.xml.parsers.DocumentBuilderFactory; |
|---|
| 33 | import javax.xml.parsers.ParserConfigurationException; |
|---|
| 34 | import javax.xml.transform.Source; |
|---|
| 35 | import javax.xml.transform.stream.StreamSource; |
|---|
| 36 | import javax.xml.validation.Schema; |
|---|
| 37 | import javax.xml.validation.SchemaFactory; |
|---|
| 38 | import javax.xml.validation.Validator; |
|---|
| 39 | |
|---|
| 40 | import org.xml.sax.SAXException; |
|---|
| 41 | |
|---|
| 42 | public class IOHelper |
|---|
| 43 | { |
|---|
| 44 | |
|---|
| 45 | static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema"; |
|---|
| 46 | public static final DecimalFormat FORMATTER = new DecimalFormat("##########0.######"); |
|---|
| 47 | |
|---|
| 48 | @SuppressWarnings("unchecked") |
|---|
| 49 | public static <T> T readConfig(Class<T> docClass, String fileName) |
|---|
| 50 | throws SAXException, IOException, ParserConfigurationException, |
|---|
| 51 | JAXBException |
|---|
| 52 | { |
|---|
| 53 | T conf = null; |
|---|
| 54 | |
|---|
| 55 | if (validateXML(fileName)) |
|---|
| 56 | { |
|---|
| 57 | DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory |
|---|
| 58 | .newInstance(); |
|---|
| 59 | DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); |
|---|
| 60 | Document doc = docBuilder.parse(new File(fileName)); |
|---|
| 61 | |
|---|
| 62 | // normalize text representation |
|---|
| 63 | doc.getDocumentElement().normalize(); |
|---|
| 64 | |
|---|
| 65 | JAXBContext jaxbContext = JAXBContext.newInstance(docClass |
|---|
| 66 | .getPackage().getName()); |
|---|
| 67 | Unmarshaller u = jaxbContext.createUnmarshaller(); |
|---|
| 68 | conf = (T) u.unmarshal(new File(fileName)); |
|---|
| 69 | |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | return conf; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | public static String readFile(String url) throws IOException |
|---|
| 76 | { |
|---|
| 77 | StringBuffer fileData = new StringBuffer(1000); |
|---|
| 78 | BufferedReader reader = new BufferedReader(new FileReader(url)); |
|---|
| 79 | char[] buf = new char[1024]; |
|---|
| 80 | int numRead = 0; |
|---|
| 81 | while ((numRead = reader.read(buf)) != -1) |
|---|
| 82 | { |
|---|
| 83 | fileData.append(buf, 0, numRead); |
|---|
| 84 | } |
|---|
| 85 | reader.close(); |
|---|
| 86 | return fileData.toString(); |
|---|
| 87 | |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | /** |
|---|
| 91 | * Validates XML document against a document-specified schema |
|---|
| 92 | * |
|---|
| 93 | * @param fileName |
|---|
| 94 | * XML document file name |
|---|
| 95 | * @return true if XML document is valid |
|---|
| 96 | * @throws SAXException |
|---|
| 97 | * @throws IOException |
|---|
| 98 | */ |
|---|
| 99 | private static boolean validateXML(String fileName) throws SAXException, |
|---|
| 100 | IOException |
|---|
| 101 | { |
|---|
| 102 | boolean isValid = false; |
|---|
| 103 | |
|---|
| 104 | SchemaFactory factory = SchemaFactory.newInstance(W3C_XML_SCHEMA); |
|---|
| 105 | Schema schema = factory.newSchema(); |
|---|
| 106 | |
|---|
| 107 | Validator validator = schema.newValidator(); |
|---|
| 108 | Source source = new StreamSource(fileName); |
|---|
| 109 | |
|---|
| 110 | try |
|---|
| 111 | { |
|---|
| 112 | validator.validate(source); |
|---|
| 113 | isValid = true; |
|---|
| 114 | System.out.println(fileName + " is valid."); |
|---|
| 115 | } |
|---|
| 116 | catch (SAXException ex) |
|---|
| 117 | { |
|---|
| 118 | System.out.println(fileName + " is not valid because "); |
|---|
| 119 | System.out.println(ex.getMessage()); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | return isValid; |
|---|
| 123 | } |
|---|
| 124 | } |
|---|