本文整理汇总了Java中java.beans.XMLEncoder.setExceptionListener方法的典型用法代码示例。如果您正苦于以下问题:Java XMLEncoder.setExceptionListener方法的具体用法?Java XMLEncoder.setExceptionListener怎么用?Java XMLEncoder.setExceptionListener使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.beans.XMLEncoder
的用法示例。
在下文中一共展示了XMLEncoder.setExceptionListener方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: write
import java.beans.XMLEncoder; //导入方法依赖的package包/类
public @Override void write(java.io.Writer w, final Object inst) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
XMLEncoder e = new XMLEncoder(out);
e.setExceptionListener(new ExceptionListener() {
public @Override void exceptionThrown(Exception x) {
Logger.getLogger(XMLBeanConvertor.class.getName()).log(Level.INFO, "Problem writing " + inst, x);
}
});
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
try {
// XXX would inst.getClass().getClassLoader() be more appropriate?
ClassLoader ccl2 = Lookup.getDefault().lookup(ClassLoader.class);
if (ccl2 != null) {
Thread.currentThread().setContextClassLoader(ccl2);
}
e.writeObject(inst);
} finally {
Thread.currentThread().setContextClassLoader(ccl);
}
e.close();
String data = new String(out.toByteArray(), "UTF-8");
data = data.replaceFirst("<java", "<!DOCTYPE xmlbeans PUBLIC \"-//NetBeans//DTD XML beans 1.0//EN\" \"http://www.netbeans.org/dtds/xml-beans-1_0.dtd\">\n<java");
w.write(data);
}
示例2: run
import java.beans.XMLEncoder; //导入方法依赖的package包/类
public void run() {
// run thread a few time
// object stays the same but NullPointerException appears randomly
// on dual proccessor a lock is generated
for (int i = 0; i < THREAD_LENGTH; i++) {
// create XMLEncoder to ByteArrayOutputStream
// this is to exclude file locking problems
XMLEncoder encoder = new XMLEncoder(new ByteArrayOutputStream());
encoder.setExceptionListener(this);
// write the object
// will see randomly null pointer exceptions
// a bug as object is same through different encode phases
encoder.writeObject(this.object);
//close encoder
encoder.close();
}
System.out.println(Thread.currentThread().getName() + " is finished");
}
示例3: toXml
import java.beans.XMLEncoder; //导入方法依赖的package包/类
/**
* To xml.
*
* @param obj
* the obj
* @return the string
*/
// ////////////////////////////////////////////////////////////////////
public static String toXml(final Object obj) {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
// XStream x = createXStream();
// String xml = x.toXML(obj);
// return xml;
final XMLEncoder e = new XMLEncoder(out);
e.setExceptionListener(new XmlEncoderExceptionListener());
// e.setPersistenceDelegate(Object.class, new MyPersistenceDelegate());
e.writeObject(obj);
e.close();
return out.toString();
// return null;
}
示例4: getXMLEncoder
import java.beans.XMLEncoder; //导入方法依赖的package包/类
/**
* Sets up the XMLEnconder.
*/
public XMLEncoder getXMLEncoder(OutputStream out) {
XMLEncoder e = new XMLEncoder(out);
e.setExceptionListener(new ExceptionListener() {
public void exceptionThrown(Exception e1) {
logger.error("", e1);
}
});
e.setPersistenceDelegate(BeanUser.class,
new DefaultPersistenceDelegate(new String[] { "name" }));
e.setPersistenceDelegate(Key.class, new DefaultPersistenceDelegate(
new String[] { "owner", "key" }));
e.setPersistenceDelegate(HostMask.class,
new DefaultPersistenceDelegate(new String[] { "mask" }));
return e;
}
示例5: toXML
import java.beans.XMLEncoder; //导入方法依赖的package包/类
/**
* Serialize all the EventData items into an XML representation.
*
* @param map the Map to transform
* @return an XML String containing all the EventDAta items.
*/
public static String toXML(Map<String, Object> map) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
XMLEncoder encoder = new XMLEncoder(baos);
encoder.setExceptionListener(new ExceptionListener() {
public void exceptionThrown(Exception exception) {
exception.printStackTrace();
}
});
encoder.writeObject(map);
encoder.close();
return baos.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
示例6: writeXMLObject
import java.beans.XMLEncoder; //导入方法依赖的package包/类
/**
* Writes the objet and CLOSES the stream. Uses the persistence delegate registered in this class.
*
* @param os The stream to write to. Will be closed.
* @param o The object to be serialized.
* @param listener The listener to recieve the exeptions if there are any. If <code>null</code> not used.
*/
void writeXMLObject(final OutputStream os, final Object o, final ExceptionListener listener) {
final ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(LayoutUtilCommon.class.getClassLoader());
final XMLEncoder encoder = new XMLEncoder(os);
if (listener != null) {
encoder.setExceptionListener(listener);
}
encoder.writeObject(o);
encoder.close(); // Must be closed to write.
Thread.currentThread().setContextClassLoader(oldClassLoader);
}
示例7: serialize
import java.beans.XMLEncoder; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public <T> int serialize(T obj, Class<T> type, OutputStream out) throws IOException {
out = new CountingOutputStream(new BufferedOutputStream(out, getBufferSize()));
EL el = new EL();
XMLEncoder enc = new XMLEncoder(out);
try {
enc.setExceptionListener(el);
enc.writeObject(obj);
enc.flush();
} finally {
if (isCloseEnabled()) {
enc.close();
}
}
IOException ioe = el.getIOException();
if (ioe != null) {
throw ioe;
}
return ((CountingOutputStream)out).getCount();
}
示例8: writeObject
import java.beans.XMLEncoder; //导入方法依赖的package包/类
private byte[] writeObject(Object object) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
XMLEncoder encoder = new XMLEncoder(output);
encoder.setExceptionListener(this);
initialize(encoder);
encoder.writeObject(object);
encoder.close();
return output.toByteArray();
}
示例9: encode
import java.beans.XMLEncoder; //导入方法依赖的package包/类
private byte[] encode(String name) throws Exception {
Object object = loadClass(name).newInstance();
validate(object);
ByteArrayOutputStream out = new ByteArrayOutputStream();
XMLEncoder encoder = new XMLEncoder(out);
encoder.setExceptionListener(this);
encoder.writeObject(object);
encoder.close();
return out.toByteArray();
}
示例10: saveJNLP
import java.beans.XMLEncoder; //导入方法依赖的package包/类
private void saveJNLP( Serializable object ) throws Exception {
final JFrame frame = PhetApplication.getInstance().getPhetFrame();
// XML encode into a byte output stream.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLEncoder encoder = new XMLEncoder( baos );
encoder.setExceptionListener( new ExceptionListener() {
private int errors = 0;
// Report the first recoverable exception.
public void exceptionThrown( Exception e ) {
if( errors == 0 ) {
showError( QWIResources.getString( "Save.error.encode" ), e );
errors++;
}
}
} );
encoder.writeObject( object );
encoder.close();
if( object == null ) {
throw new Exception( QWIResources.getString( "XML encoding failed" ) );
}
// Convert to a byte input stream.
ByteArrayInputStream inputStream = new ByteArrayInputStream( baos.toByteArray() );
// Get the JNLP service for saving files.
FileSaveService fss = (FileSaveService)ServiceManager.lookup( "javax.jnlp.FileSaveService" );
if( fss == null ) {
throw new UnavailableServiceException( "JNLP FileSaveService is unavailable" );
}
// Save the configuration to a file.
FileContents fc = fss.saveFileDialog( null, null, inputStream, _directoryName );
if( fc != null ) {
_directoryName = getDirectoryName( fc.getName() );
}
}
示例11: testSerialization
import java.beans.XMLEncoder; //导入方法依赖的package包/类
public void testSerialization()
{
ArtifactSourceMemento src = new ArtifactSourceMemento();
src.setArtifactSourceClassName("classname");
src.setArtifactUrls(new String[]{"http://localhost:8080/Vix"});
src.setMetadataUrls(new String[]{"http://localhost:8080/Vix"});
src.setHomeCommunityId("1.2.3.4.56789.10");
src.setRepositoryId("R1");
src.setName("Fred");
ByteArrayOutputStream outStream = new ByteArrayOutputStream(4096);
XMLEncoder encoder = new XMLEncoder(outStream);
encoder.setExceptionListener(new ExceptionListener()
{
public void exceptionThrown(Exception exception)
{
logger.error(exception.getMessage());
exception.printStackTrace();
}
}
);
encoder.writeObject(src);
encoder.close();
ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());
XMLDecoder decoder = new XMLDecoder(inStream);
ArtifactSourceMemento copy = (ArtifactSourceMemento)decoder.readObject();
assertEquals(src, copy);
}
示例12: objectToContent
import java.beans.XMLEncoder; //导入方法依赖的package包/类
private byte[] objectToContent(final Object value) {
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
final XMLEncoder encoder = new XMLEncoder(byteArrayOutputStream);
encoder.setExceptionListener(new LogEncoderExceptionListener(value));
LOG.debug("START Writing Object {} with XMLEncoder", value.getClass().getName());
encoder.writeObject(value);
LOG.debug("FINISH Writing Object {} with XMLEncoder", value.getClass().getName());
encoder.flush();
encoder.close();
return byteArrayOutputStream.toByteArray();
}
示例13: initialize
import java.beans.XMLEncoder; //导入方法依赖的package包/类
@Override
protected void initialize(XMLEncoder encoder) {
encoder.setExceptionListener(null); // TODO: ignore non-public listener because of 4808251
}
示例14: saveLocal
import java.beans.XMLEncoder; //导入方法依赖的package包/类
public void saveLocal( Object object ) throws Exception {
Window frame = getFrame();
// Choose the file to save.
JFileChooser fileChooser = new JFileChooser( _directoryName );
fileChooser.setDialogTitle( QWIResources.getString( "save" ) );
int rval = fileChooser.showSaveDialog( frame );
_directoryName = fileChooser.getCurrentDirectory().getAbsolutePath();
File selectedFile = fileChooser.getSelectedFile();
if( rval == JFileChooser.CANCEL_OPTION || selectedFile == null ) {
return;
}
_directoryName = selectedFile.getParentFile().getAbsolutePath();
// If the file exists, confirm overwrite.
if( selectedFile.exists() ) {
String title = QWIResources.getString( "confirm" );
String message = QWIResources.getString( "confirm.save" );
int reply = PhetOptionPane.showYesNoDialog( frame, message, title );
if( reply != JOptionPane.YES_OPTION ) {
return;
}
}
// XML encode directly to the file.
String filename = selectedFile.getAbsolutePath();
FileOutputStream fos = new FileOutputStream( filename );
BufferedOutputStream bos = new BufferedOutputStream( fos );
XMLEncoder encoder = new XMLEncoder( bos );
encoder.setExceptionListener( new ExceptionListener() {
private int errors = 0;
// Report the first recoverable exception.
public void exceptionThrown( Exception e ) {
if( errors == 0 ) {
showError( QWIResources.getString( "Save.error.encode" ), e );
errors++;
}
}
} );
encoder.writeObject( object );
encoder.close();
}