00001
00002
00003
00004
00005
00006
package org.nees.rbnb;
00007
00008
00009
import org.w3c.dom.*;
00010
import javax.xml.parsers.*;
00011
import org.xml.sax.*;
00012
import org.apache.xml.serialize.*;
00013
import java.net.*;
00014
import java.io.*;
00015
00024 public class Xml
00025 {
00026
00027
public static final String XML_TAG_FOR_ERROR_MESSAGE =
"errorMessage";
00028
public static final String XML_TAG_FOR_STATUS_MESSAGE =
"statusMessage";
00029
00034 public static Document
createDocument()
00035 {
00036
try
00037 {
00038 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
00039 DocumentBuilder builder = factory.newDocumentBuilder();
00040 Document doc = builder.newDocument();
00041
00042
return doc;
00043 }
00044
catch (Exception any)
00045 {
00046
return null;
00047 }
00048
00049 }
00050
00056 public static Document
readDocument(String name)
00057 {
00058
try
00059 {
00060
00061 InputStreamReader in =
new InputStreamReader(
new FileInputStream(name),
"UTF-8");
00062 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
00063 DocumentBuilder docBuilder = dbf.newDocumentBuilder();
00064 InputSource inputSource =
new InputSource(in);
00065 Document doc = docBuilder.parse(inputSource);
00066
return doc;
00067 }
00068
catch (Exception any)
00069 {
00070
return null;
00071 }
00072
00073 }
00074
00080 public static Document
readDocumentFromString(String in)
00081 {
00082
try
00083 {
00084 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
00085 DocumentBuilder docBuilder = dbf.newDocumentBuilder();
00086 InputSource inputSource =
new InputSource(
new StringReader(in));
00087 Document doc = docBuilder.parse(inputSource);
00088
return doc;
00089 }
00090
catch (Exception any)
00091 {
00092
return null;
00093 }
00094
00095 }
00096
00102 public static void writeDocument(Document doc, String fileName)
00103
throws IOException
00104 {
00105 OutputStreamWriter out =
new OutputStreamWriter(
new FileOutputStream(fileName),
"UTF-8");
00106
00107 XMLSerializer s =
new XMLSerializer(out,
new OutputFormat(
00108
"xml",
"UTF-8",
true));
00109 s.serialize(doc);
00110 out.close();
00111
00112 }
00113
00119 public static String
writeDocumentToString(Document doc)
throws IOException
00120 {
00121 StringWriter sw =
new StringWriter();
00122
00123 XMLSerializer s =
new XMLSerializer(sw,
new OutputFormat(
00124
"xml",
"UTF-8",
true ));
00125 s.serialize(doc);
00126
00127 sw.flush();
00128
return sw.toString();
00129
00130 }
00131
00132
public static String errorXml(String problem, String reason)
00133 {
00134
return
00135
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
00136
"<" + XML_TAG_FOR_ERROR_MESSAGE +
">\n" +
00137
" <problem>" +
00138 problem +
00139
" </problem>\n" +
00140
" <reason>" +
00141 reason +
00142
" </reason>\n" +
00143
"</" + XML_TAG_FOR_ERROR_MESSAGE +
">";
00144 }
00145
00150 public static String
statusXml(String string) {
00151
return
00152
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
00153
"<" + XML_TAG_FOR_STATUS_MESSAGE +
">" +
00154 string +
00155
"</" + XML_TAG_FOR_STATUS_MESSAGE +
">\n";
00156 }
00157
00158 }
00159
00160
00161
00162
00163
00164