01: /******************************************************************************
02: * Copyright (C) Lars Ivar Almli. All rights reserved. *
03: * ---------------------------------------------------------------------------*
04: * This file is part of MActor. *
05: * *
06: * MActor is free software; you can redistribute it and/or modify *
07: * it under the terms of the GNU General Public License as published by *
08: * the Free Software Foundation; either version 2 of the License, or *
09: * (at your option) any later version. *
10: * *
11: * MActor is distributed in the hope that it will be useful, *
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14: * GNU General Public License for more details. *
15: * *
16: * You should have received a copy of the GNU General Public License *
17: * along with MActor; if not, write to the Free Software *
18: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
19: ******************************************************************************/package org.mactor.framework.spec;
20:
21: import java.io.File;
22: import java.io.FileInputStream;
23: import java.io.IOException;
24: import java.io.InputStreamReader;
25: import java.io.StringWriter;
26: import javax.xml.XMLConstants;
27: import javax.xml.transform.stream.StreamSource;
28: import javax.xml.validation.Schema;
29: import javax.xml.validation.SchemaFactory;
30: import javax.xml.validation.Validator;
31: import org.dom4j.Document;
32: import org.dom4j.Element;
33: import org.dom4j.io.OutputFormat;
34: import org.dom4j.io.SAXReader;
35: import org.dom4j.io.XMLWriter;
36: import org.mactor.framework.MactorException;
37:
38: public class XmlUtil {
39: public static Document readFromFile(String path)
40: throws MactorException {
41: return readFromFile(new File(path));
42: }
43:
44: public static Document readFromFile(File path)
45: throws MactorException {
46: if (path == null || !path.exists())
47: throw new MactorException("The file '" + path
48: + "' does not exist");
49: SAXReader reader = new SAXReader();
50: try {
51: final String sl = XMLConstants.W3C_XML_SCHEMA_NS_URI;
52: SchemaFactory factory = SchemaFactory.newInstance(sl);
53: StreamSource ss = new StreamSource(new File(
54: "resources/mactor.xsd"));
55: Schema schema = factory.newSchema(ss);
56: Validator v = schema.newValidator();
57: v.validate(new StreamSource(path));
58: Document doc = reader.read(path);
59: return doc;
60: } catch (Exception e) {
61: throw new MactorException(e);
62: }
63: }
64:
65: public static String readFile(String path) {
66: try {
67: InputStreamReader ir = new InputStreamReader(
68: new FileInputStream(path));
69: char[] buffer = new char[4000];
70: StringBuffer sb = new StringBuffer();
71: while (true) {
72: int count = ir.read(buffer);
73: if (count <= 0)
74: break;
75: sb.append(buffer, 0, count);
76: }
77: return sb.toString();
78: } catch (Exception e) {
79: e.printStackTrace();
80: }
81: return null;
82: }
83:
84: public static String getPrettyXML(Element element) {
85: StringWriter sw = new StringWriter();
86: OutputFormat format = OutputFormat.createPrettyPrint();
87: try {
88: new XMLWriter(sw, format).write(element);
89: } catch (IOException ioe) {
90: ioe.printStackTrace();
91: return null;
92: }
93: return sw.toString().trim();
94: }
95: }
|