use of java.beans.XMLEncoder in project SQLWindowing by hbutani.
the class SerializationUtils method addAntlrPersistenceDelegates.
public static void addAntlrPersistenceDelegates(XMLEncoder e) {
e.setPersistenceDelegate(ASTNode.class, new PersistenceDelegate() {
protected Expression instantiate(Object oldInstance, Encoder out) {
return new Expression(oldInstance, oldInstance.getClass(), "new", new Object[] { ((ASTNode) oldInstance).getToken() });
}
});
e.setPersistenceDelegate(CommonTree.class, new PersistenceDelegate() {
protected Expression instantiate(Object oldInstance, Encoder out) {
return new Expression(oldInstance, oldInstance.getClass(), "new", new Object[] { ((CommonTree) oldInstance).getToken() });
}
});
e.setPersistenceDelegate(BaseTree.class, new PersistenceDelegate() {
protected Expression instantiate(Object oldInstance, Encoder out) {
return new Expression(oldInstance, oldInstance.getClass(), "new", new Object[] {});
}
@SuppressWarnings("rawtypes")
protected void initialize(Class type, Object oldInstance, Object newInstance, Encoder out) {
super.initialize(type, oldInstance, newInstance, out);
BaseTree t = (BaseTree) oldInstance;
for (int i = 0; i < t.getChildCount(); i++) {
out.writeStatement(new Statement(oldInstance, "addChild", new Object[] { t.getChild(i) }));
}
}
});
e.setPersistenceDelegate(CommonToken.class, new PersistenceDelegate() {
protected Expression instantiate(Object oldInstance, Encoder out) {
return new Expression(oldInstance, oldInstance.getClass(), "new", new Object[] { ((CommonToken) oldInstance).getType(), ((CommonToken) oldInstance).getText() });
}
});
}
use of java.beans.XMLEncoder in project SQLWindowing by hbutani.
the class SerializationUtils method serialize.
public static void serialize(OutputStream out, Object o) {
XMLEncoder e = new XMLEncoder(out);
e.setExceptionListener(new EL());
SerializationUtils.addPersistenceDelegates(e);
e.writeObject(o);
e.close();
}
use of java.beans.XMLEncoder in project hutool by looly.
the class XmlUtil method writeObjectAsXml.
/**
* 将可序列化的对象转换为XML写入文件,已经存在的文件将被覆盖<br>
* Writes serializable object to a XML file. Existing file will be overwritten
*
* @param <T> 对象类型
* @param dest 目标文件
* @param t 对象
* @throws IOException IO异常
*/
public static <T> void writeObjectAsXml(File dest, T t) throws IOException {
XMLEncoder xmlenc = null;
try {
xmlenc = new XMLEncoder(FileUtil.getOutputStream(dest));
xmlenc.writeObject(t);
} finally {
// 关闭XMLEncoder会相应关闭OutputStream
IoUtil.close(xmlenc);
}
}
use of java.beans.XMLEncoder in project jdk8u_jdk by JetBrains.
the class Test4625418 method test.
private void test(String string) {
try {
File file = new File("4625418." + this.encoding + ".xml");
FileOutputStream output = new FileOutputStream(file);
XMLEncoder encoder = new XMLEncoder(output, this.encoding, true, 0);
encoder.setExceptionListener(this);
encoder.writeObject(string);
encoder.close();
FileInputStream input = new FileInputStream(file);
XMLDecoder decoder = new XMLDecoder(input);
decoder.setExceptionListener(this);
Object object = decoder.readObject();
decoder.close();
if (!string.equals(object))
throw new Error(this.encoding + " - can't read properly");
file.delete();
} catch (FileNotFoundException exception) {
throw new Error(this.encoding + " - file not found", exception);
} catch (IllegalCharsetNameException exception) {
throw new Error(this.encoding + " - illegal charset name", exception);
} catch (UnsupportedCharsetException exception) {
throw new Error(this.encoding + " - unsupported charset", exception);
} catch (UnsupportedOperationException exception) {
throw new Error(this.encoding + " - unsupported encoder", exception);
}
}
use of java.beans.XMLEncoder in project jdk8u_jdk by JetBrains.
the class AbstractTest method test.
static void test(AbstractTest object) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
XMLEncoder encoder = new XMLEncoder(output);
encoder.setPersistenceDelegate(object.getClass(), new DefaultPersistenceDelegate(new String[] { "value" }));
encoder.writeObject(object);
encoder.close();
System.out.print(output);
ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
XMLDecoder decoder = new XMLDecoder(input);
AbstractTest result = (AbstractTest) decoder.readObject();
decoder.close();
if (object.getValue() != result.getValue())
throw new Error("Should be " + object);
}
Aggregations