001 package com.croftsoft.core.beans; 002 003 import java.beans.*; 004 import java.io.*; 005 006 import com.croftsoft.core.io.Encoder; 007 import com.croftsoft.core.io.Parser; 008 009 /********************************************************************* 010 * Encodes and decodes objects using XMLEncoder and XMLDecoder. 011 * 012 * @see 013 * java.beans.XMLEncoder 014 * @see 015 * java.beans.XMLDecoder 016 * 017 * @version 018 * $Id: XmlBeanCoder.java,v 1.2 2006/01/24 22:20:29 croft Exp $ 019 * @since 020 * 2003-04-07 021 * @author 022 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 023 *********************************************************************/ 024 025 public final class XmlBeanCoder 026 implements Encoder, Parser 027 ////////////////////////////////////////////////////////////////////// 028 ////////////////////////////////////////////////////////////////////// 029 { 030 031 public static final XmlBeanCoder INSTANCE = new XmlBeanCoder ( ); 032 033 ////////////////////////////////////////////////////////////////////// 034 // static methods 035 ////////////////////////////////////////////////////////////////////// 036 037 public static Object decodeFromXml ( byte [ ] bytes ) 038 ////////////////////////////////////////////////////////////////////// 039 { 040 return decodeFromXml ( new ByteArrayInputStream ( bytes ) ); 041 } 042 043 public static Object decodeFromXml ( InputStream inputStream ) 044 ////////////////////////////////////////////////////////////////////// 045 { 046 XMLDecoder xmlDecoder = new XMLDecoder ( inputStream ); 047 048 try 049 { 050 return xmlDecoder.readObject ( ); 051 } 052 finally 053 { 054 xmlDecoder.close ( ); 055 } 056 } 057 058 public static void encodeAsXml ( 059 Object o, 060 OutputStream outputStream ) 061 ////////////////////////////////////////////////////////////////////// 062 { 063 XMLEncoder xmlEncoder = new XMLEncoder ( outputStream ); 064 065 try 066 { 067 xmlEncoder.writeObject ( o ); 068 } 069 finally 070 { 071 xmlEncoder.close ( ); 072 } 073 } 074 075 public static byte [ ] encodeAsXml ( Object o ) 076 ////////////////////////////////////////////////////////////////////// 077 { 078 ByteArrayOutputStream byteArrayOutputStream 079 = new ByteArrayOutputStream ( ); 080 081 encodeAsXml ( o, byteArrayOutputStream ); 082 083 return byteArrayOutputStream.toByteArray ( ); 084 } 085 086 public static Object loadFromXmlFile ( File file ) 087 throws IOException 088 ////////////////////////////////////////////////////////////////////// 089 { 090 return decodeFromXml ( 091 new BufferedInputStream ( new FileInputStream ( file ) ) ); 092 } 093 094 public static Object loadFromXmlFile ( String filename ) 095 throws IOException 096 ////////////////////////////////////////////////////////////////////// 097 { 098 return decodeFromXml ( 099 new BufferedInputStream ( new FileInputStream ( filename ) ) ); 100 } 101 102 public static void saveToXmlFile ( 103 Object object, 104 File file ) 105 throws IOException 106 ////////////////////////////////////////////////////////////////////// 107 { 108 encodeAsXml ( 109 object, 110 new BufferedOutputStream ( new FileOutputStream ( file ) ) ); 111 } 112 113 public static void saveToXmlFile ( 114 Object object, 115 String filename ) 116 throws IOException 117 ////////////////////////////////////////////////////////////////////// 118 { 119 encodeAsXml ( 120 object, 121 new BufferedOutputStream ( new FileOutputStream ( filename ) ) ); 122 } 123 124 ////////////////////////////////////////////////////////////////////// 125 // interface Encoder and Parser methods 126 ////////////////////////////////////////////////////////////////////// 127 128 public byte [ ] encode ( Object object ) 129 throws IOException 130 ////////////////////////////////////////////////////////////////////// 131 { 132 return encodeAsXml ( object ); 133 } 134 135 public Object parse ( 136 InputStream inputStream, 137 int contentLength ) 138 throws IOException 139 ////////////////////////////////////////////////////////////////////// 140 { 141 return decodeFromXml ( inputStream ); 142 } 143 144 ////////////////////////////////////////////////////////////////////// 145 // private constructor method 146 ////////////////////////////////////////////////////////////////////// 147 148 private XmlBeanCoder ( ) { } 149 150 ////////////////////////////////////////////////////////////////////// 151 ////////////////////////////////////////////////////////////////////// 152 }