Wolves Themes v1.0版本的 MD5 值为:cddc92b47f6b7c4a195c9625507eb51c
以下内容为反编译后的 XmlTools.java 源代码,内容仅作参考
package org.nexage.sourcekit.util;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class XmlTools {
private static String TAG = "XmlTools";
public static String getElementValue(Node node) {
NodeList childNodes = node.getChildNodes();
String str = null;
int i = 0;
while (true) {
if (i >= childNodes.getLength()) {
break;
}
str = ((CharacterData) childNodes.item(i)).getData().trim();
if (str.length() != 0) {
String str2 = TAG;
VASTLog.v(str2, "getElementValue: " + str);
break;
}
i++;
}
return str;
}
public static void logXmlDocument(Document document) {
VASTLog.d(TAG, "logXmlDocument");
try {
Transformer newTransformer = TransformerFactory.newInstance().newTransformer();
newTransformer.setOutputProperty("omit-xml-declaration", "no");
newTransformer.setOutputProperty("method", "xml");
newTransformer.setOutputProperty("indent", "yes");
newTransformer.setOutputProperty("encoding", "UTF-8");
newTransformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
StringWriter stringWriter = new StringWriter();
newTransformer.transform(new DOMSource(document), new StreamResult(stringWriter));
VASTLog.d(TAG, stringWriter.toString());
} catch (Exception e) {
VASTLog.e(TAG, e.getMessage(), e);
}
}
public static String stringFromStream(InputStream inputStream) {
VASTLog.d(TAG, "stringFromStream");
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] bArr = new byte[1024];
while (true) {
int read = inputStream.read(bArr);
if (read == -1) {
return new String(byteArrayOutputStream.toByteArray(), "UTF-8");
}
byteArrayOutputStream.write(bArr, 0, read);
}
}
public static Document stringToDocument(String str) {
VASTLog.d(TAG, "stringToDocument");
try {
DocumentBuilder newDocumentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource inputSource = new InputSource();
inputSource.setCharacterStream(new StringReader(str));
return newDocumentBuilder.parse(inputSource);
} catch (Exception e) {
VASTLog.e(TAG, e.getMessage(), e);
return null;
}
}
public static String xmlDocumentToString(Document document) {
VASTLog.d(TAG, "xmlDocumentToString");
try {
Transformer newTransformer = TransformerFactory.newInstance().newTransformer();
newTransformer.setOutputProperty("omit-xml-declaration", "no");
newTransformer.setOutputProperty("method", "xml");
newTransformer.setOutputProperty("indent", "yes");
newTransformer.setOutputProperty("encoding", "UTF-8");
newTransformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
StringWriter stringWriter = new StringWriter();
newTransformer.transform(new DOMSource(document), new StreamResult(stringWriter));
return stringWriter.toString();
} catch (Exception e) {
VASTLog.e(TAG, e.getMessage(), e);
return null;
}
}
public static String xmlDocumentToString(Node node) {
VASTLog.d(TAG, "xmlDocumentToString");
try {
Transformer newTransformer = TransformerFactory.newInstance().newTransformer();
newTransformer.setOutputProperty("omit-xml-declaration", "yes");
newTransformer.setOutputProperty("method", "xml");
newTransformer.setOutputProperty("indent", "yes");
newTransformer.setOutputProperty("encoding", "UTF-8");
newTransformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
StringWriter stringWriter = new StringWriter();
newTransformer.transform(new DOMSource(node), new StreamResult(stringWriter));
return stringWriter.toString();
} catch (Exception e) {
VASTLog.e(TAG, e.getMessage(), e);
return null;
}
}
}