153 lines
6.1 KiB
Java
153 lines
6.1 KiB
Java
package com.thoughtworks.xstream.io.xml;
|
|
|
|
import com.thoughtworks.xstream.converters.reflection.ObjectAccessException;
|
|
import com.thoughtworks.xstream.core.JVM;
|
|
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
|
|
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
|
|
import com.thoughtworks.xstream.io.StreamException;
|
|
import com.thoughtworks.xstream.io.naming.NameCoder;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.OutputStream;
|
|
import java.io.OutputStreamWriter;
|
|
import java.io.Reader;
|
|
import java.io.UnsupportedEncodingException;
|
|
import java.io.Writer;
|
|
import java.lang.reflect.InvocationTargetException;
|
|
import java.net.URL;
|
|
import javax.xml.parsers.DocumentBuilder;
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
import javax.xml.parsers.FactoryConfigurationError;
|
|
import javax.xml.parsers.ParserConfigurationException;
|
|
import org.xml.sax.InputSource;
|
|
import org.xml.sax.SAXException;
|
|
|
|
/* loaded from: classes.dex */
|
|
public class DomDriver extends AbstractXmlDriver {
|
|
static /* synthetic */ Class class$java$lang$String;
|
|
static /* synthetic */ Class class$javax$xml$parsers$DocumentBuilderFactory;
|
|
private DocumentBuilderFactory documentBuilderFactory;
|
|
private final String encoding;
|
|
|
|
public DomDriver() {
|
|
this(null);
|
|
}
|
|
|
|
static /* synthetic */ Class class$(String str) {
|
|
try {
|
|
return Class.forName(str);
|
|
} catch (ClassNotFoundException e) {
|
|
throw new NoClassDefFoundError().initCause(e);
|
|
}
|
|
}
|
|
|
|
protected DocumentBuilderFactory createDocumentBuilderFactory() {
|
|
Class cls;
|
|
Class<?> cls2;
|
|
DocumentBuilderFactory newInstance = DocumentBuilderFactory.newInstance();
|
|
if (JVM.isVersion(5)) {
|
|
try {
|
|
if (class$javax$xml$parsers$DocumentBuilderFactory == null) {
|
|
cls = class$("javax.xml.parsers.DocumentBuilderFactory");
|
|
class$javax$xml$parsers$DocumentBuilderFactory = cls;
|
|
} else {
|
|
cls = class$javax$xml$parsers$DocumentBuilderFactory;
|
|
}
|
|
Class<?>[] clsArr = new Class[2];
|
|
if (class$java$lang$String == null) {
|
|
cls2 = class$("java.lang.String");
|
|
class$java$lang$String = cls2;
|
|
} else {
|
|
cls2 = class$java$lang$String;
|
|
}
|
|
clsArr[0] = cls2;
|
|
clsArr[1] = Boolean.TYPE;
|
|
cls.getMethod("setFeature", clsArr).invoke(newInstance, "http://apache.org/xml/features/disallow-doctype-decl", Boolean.TRUE);
|
|
} catch (IllegalAccessException e) {
|
|
throw new ObjectAccessException("Cannot set feature of DocumentBuilderFactory.", e);
|
|
} catch (NoSuchMethodException unused) {
|
|
} catch (InvocationTargetException e2) {
|
|
Throwable cause = e2.getCause();
|
|
if (JVM.isVersion(6) || ((cause instanceof ParserConfigurationException) && cause.getMessage().indexOf("disallow-doctype-decl") < 0)) {
|
|
throw new StreamException(cause);
|
|
}
|
|
}
|
|
}
|
|
newInstance.setExpandEntityReferences(false);
|
|
return newInstance;
|
|
}
|
|
|
|
@Override // com.thoughtworks.xstream.io.HierarchicalStreamDriver
|
|
public HierarchicalStreamReader createReader(Reader reader) {
|
|
return createReader(new InputSource(reader));
|
|
}
|
|
|
|
@Override // com.thoughtworks.xstream.io.HierarchicalStreamDriver
|
|
public HierarchicalStreamWriter createWriter(Writer writer) {
|
|
return new PrettyPrintWriter(writer, getNameCoder());
|
|
}
|
|
|
|
public DomDriver(String str) {
|
|
this(str, new XmlFriendlyNameCoder());
|
|
}
|
|
|
|
@Override // com.thoughtworks.xstream.io.HierarchicalStreamDriver
|
|
public HierarchicalStreamReader createReader(InputStream inputStream) {
|
|
return createReader(new InputSource(inputStream));
|
|
}
|
|
|
|
@Override // com.thoughtworks.xstream.io.HierarchicalStreamDriver
|
|
public HierarchicalStreamWriter createWriter(OutputStream outputStream) {
|
|
try {
|
|
return createWriter(this.encoding != null ? new OutputStreamWriter(outputStream, this.encoding) : new OutputStreamWriter(outputStream));
|
|
} catch (UnsupportedEncodingException e) {
|
|
throw new StreamException(e);
|
|
}
|
|
}
|
|
|
|
public DomDriver(String str, NameCoder nameCoder) {
|
|
super(nameCoder);
|
|
this.encoding = str;
|
|
}
|
|
|
|
@Override // com.thoughtworks.xstream.io.AbstractDriver, com.thoughtworks.xstream.io.HierarchicalStreamDriver
|
|
public HierarchicalStreamReader createReader(URL url) {
|
|
return createReader(new InputSource(url.toExternalForm()));
|
|
}
|
|
|
|
@Override // com.thoughtworks.xstream.io.AbstractDriver, com.thoughtworks.xstream.io.HierarchicalStreamDriver
|
|
public HierarchicalStreamReader createReader(File file) {
|
|
return createReader(new InputSource(file.toURI().toASCIIString()));
|
|
}
|
|
|
|
public DomDriver(String str, XmlFriendlyReplacer xmlFriendlyReplacer) {
|
|
this(str, (NameCoder) xmlFriendlyReplacer);
|
|
}
|
|
|
|
private HierarchicalStreamReader createReader(InputSource inputSource) {
|
|
try {
|
|
if (this.documentBuilderFactory == null) {
|
|
synchronized (this) {
|
|
if (this.documentBuilderFactory == null) {
|
|
this.documentBuilderFactory = createDocumentBuilderFactory();
|
|
}
|
|
}
|
|
}
|
|
DocumentBuilder newDocumentBuilder = this.documentBuilderFactory.newDocumentBuilder();
|
|
if (this.encoding != null) {
|
|
inputSource.setEncoding(this.encoding);
|
|
}
|
|
return new DomReader(newDocumentBuilder.parse(inputSource), getNameCoder());
|
|
} catch (IOException e) {
|
|
throw new StreamException(e);
|
|
} catch (FactoryConfigurationError e2) {
|
|
throw new StreamException(e2);
|
|
} catch (ParserConfigurationException e3) {
|
|
throw new StreamException(e3);
|
|
} catch (SAXException e4) {
|
|
throw new StreamException(e4);
|
|
}
|
|
}
|
|
}
|