影视仓 v5.0.24版本的 MD5 值为:620341689ccdc375a3b86527e140060e
以下内容为反编译后的 SaxWriter.java 源代码,内容仅作参考
package com.thoughtworks.xstream.io.xml;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.StreamException;
import com.thoughtworks.xstream.io.naming.NameCoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.xml.sax.ContentHandler;
import org.xml.sax.DTDHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.AttributesImpl;
public final class SaxWriter extends AbstractXmlWriter implements XMLReader {
public static final String CONFIGURED_XSTREAM_PROPERTY = "http://com.thoughtworks.xstream/sax/property/configured-xstream";
public static final String SOURCE_OBJECT_LIST_PROPERTY = "http://com.thoughtworks.xstream/sax/property/source-object-list";
private final AttributesImpl attributeList;
private char[] buffer;
private ContentHandler contentHandler;
private int depth;
private DTDHandler dtdHandler;
private List elementStack;
private EntityResolver entityResolver;
private ErrorHandler errorHandler;
private Map features;
private final boolean includeEnclosingDocument;
private final Map properties;
private boolean startTagInProgress;
public SaxWriter(NameCoder nameCoder) {
this(true, nameCoder);
}
private void endDocument(boolean z) {
int i = this.depth;
if (i == 0 || (i == 1 && z)) {
this.contentHandler.endDocument();
this.depth = 0;
}
}
private void flushStartTag() {
if (this.startTagInProgress) {
String str = (String) this.elementStack.get(0);
this.contentHandler.startElement("", str, str, this.attributeList);
this.attributeList.clear();
this.startTagInProgress = false;
}
}
private void startDocument(boolean z) {
if (this.depth == 0) {
this.contentHandler.startDocument();
if (z) {
this.depth++;
}
}
}
@Override
public void addAttribute(String str, String str2) {
if (this.startTagInProgress) {
String escapeXmlName = escapeXmlName(str);
this.attributeList.addAttribute("", escapeXmlName, escapeXmlName, "CDATA", str2);
return;
}
throw new StreamException(new IllegalStateException("No startElement being processed"));
}
@Override
public void close() {
}
@Override
public void endNode() {
try {
flushStartTag();
String str = (String) this.elementStack.remove(0);
this.contentHandler.endElement("", str, str);
int i = this.depth - 1;
this.depth = i;
if (i == 0 && this.includeEnclosingDocument) {
endDocument(false);
}
} catch (SAXException e) {
throw new StreamException(e);
}
}
@Override
public void flush() {
}
@Override
public ContentHandler getContentHandler() {
return this.contentHandler;
}
@Override
public DTDHandler getDTDHandler() {
return this.dtdHandler;
}
@Override
public EntityResolver getEntityResolver() {
return this.entityResolver;
}
@Override
public ErrorHandler getErrorHandler() {
return this.errorHandler;
}
@Override
public boolean getFeature(String str) {
if (!str.equals("http://xml.org/sax/features/namespaces") && !str.equals("http://xml.org/sax/features/namespace-prefixes")) {
throw new SAXNotRecognizedException(str);
}
Boolean bool = (Boolean) this.features.get(str);
if (bool == null) {
bool = Boolean.FALSE;
}
return bool.booleanValue();
}
@Override
public Object getProperty(String str) {
if (!str.equals(CONFIGURED_XSTREAM_PROPERTY) && !str.equals(SOURCE_OBJECT_LIST_PROPERTY)) {
throw new SAXNotRecognizedException(str);
}
return this.properties.get(str);
}
@Override
public void parse(String str) {
parse();
}
@Override
public void setContentHandler(ContentHandler contentHandler) {
if (contentHandler != null) {
this.contentHandler = contentHandler;
return;
}
throw new NullPointerException("handler");
}
@Override
public void setDTDHandler(DTDHandler dTDHandler) {
if (dTDHandler != null) {
this.dtdHandler = dTDHandler;
return;
}
throw new NullPointerException("handler");
}
@Override
public void setEntityResolver(EntityResolver entityResolver) {
if (entityResolver != null) {
this.entityResolver = entityResolver;
return;
}
throw new NullPointerException("resolver");
}
@Override
public void setErrorHandler(ErrorHandler errorHandler) {
if (errorHandler != null) {
this.errorHandler = errorHandler;
return;
}
throw new NullPointerException("handler");
}
@Override
public void setFeature(String str, boolean z) {
Boolean bool;
if (!str.equals("http://xml.org/sax/features/namespaces") && !str.equals("http://xml.org/sax/features/namespace-prefixes")) {
throw new SAXNotRecognizedException(str);
}
Map map = this.features;
if (z) {
bool = Boolean.TRUE;
} else {
bool = Boolean.FALSE;
}
map.put(str, bool);
}
@Override
public void setProperty(String str, Object obj) {
if (str.equals(CONFIGURED_XSTREAM_PROPERTY)) {
if (!(obj instanceof XStream)) {
throw new SAXNotSupportedException("Value for property \"http://com.thoughtworks.xstream/sax/property/configured-xstream\" must be a non-null XStream object");
}
} else if (str.equals(SOURCE_OBJECT_LIST_PROPERTY)) {
if (obj instanceof List) {
List list = (List) obj;
if (!list.isEmpty()) {
obj = Collections.unmodifiableList(new ArrayList(list));
} else {
throw new SAXNotSupportedException("Value for property \"http://com.thoughtworks.xstream/sax/property/source-object-list\" shall not be an empty list");
}
} else {
throw new SAXNotSupportedException("Value for property \"http://com.thoughtworks.xstream/sax/property/source-object-list\" must be a non-null List object");
}
} else {
throw new SAXNotRecognizedException(str);
}
this.properties.put(str, obj);
}
@Override
public void setValue(String str) {
try {
flushStartTag();
int length = str.length();
if (length > this.buffer.length) {
this.buffer = new char[length];
}
str.getChars(0, length, this.buffer, 0);
this.contentHandler.characters(this.buffer, 0, length);
} catch (SAXException e) {
throw new StreamException(e);
}
}
@Override
public void startNode(String str) {
try {
if (this.depth != 0) {
flushStartTag();
} else if (this.includeEnclosingDocument) {
startDocument(false);
}
this.elementStack.add(0, escapeXmlName(str));
this.startTagInProgress = true;
this.depth++;
} catch (SAXException e) {
throw new StreamException(e);
}
}
public SaxWriter(boolean z, NameCoder nameCoder) {
super(nameCoder);
this.entityResolver = null;
this.dtdHandler = null;
this.contentHandler = null;
this.errorHandler = null;
this.features = new HashMap();
this.properties = new HashMap();
this.depth = 0;
this.elementStack = new LinkedList();
this.buffer = new char[128];
this.startTagInProgress = false;
this.attributeList = new AttributesImpl();
this.includeEnclosingDocument = z;
}
@Override
public void parse(InputSource inputSource) {
parse();
}
private void parse() {
XStream xStream = (XStream) this.properties.get(CONFIGURED_XSTREAM_PROPERTY);
if (xStream == null) {
xStream = new XStream();
}
List<Object> list = (List) this.properties.get(SOURCE_OBJECT_LIST_PROPERTY);
if (list != null && !list.isEmpty()) {
try {
startDocument(true);
for (Object obj : list) {
xStream.marshal(obj, this);
}
endDocument(true);
return;
} catch (StreamException e) {
if (e.getCause() instanceof SAXException) {
throw ((SAXException) e.getCause());
}
throw new SAXException(e);
}
}
throw new SAXException("Missing or empty source object list. Setting property \"http://com.thoughtworks.xstream/sax/property/source-object-list\" is mandatory");
}
public SaxWriter(XmlFriendlyReplacer xmlFriendlyReplacer) {
this(true, xmlFriendlyReplacer);
}
public SaxWriter(boolean z, XmlFriendlyReplacer xmlFriendlyReplacer) {
this(z, (NameCoder) xmlFriendlyReplacer);
}
public SaxWriter(boolean z) {
this(z, new XmlFriendlyNameCoder());
}
public SaxWriter() {
this(true);
}
}