Source Code Cross Referenced for XStreamer.java in  » XML » xstream-1.3 » com » thoughtworks » xstream » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » XML » xstream 1.3 » com.thoughtworks.xstream 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (C) 2006, 2007 XStream Committers.
003:         * All rights reserved.
004:         *
005:         * The software in this package is published under the terms of the BSD
006:         * style license a copy of which has been included with this distribution in
007:         * the LICENSE.txt file.
008:         * 
009:         * Created on 13. April 2006 by Joerg Schaible
010:         */
011:        package com.thoughtworks.xstream;
012:
013:        import com.thoughtworks.xstream.converters.ConversionException;
014:        import com.thoughtworks.xstream.io.HierarchicalStreamDriver;
015:        import com.thoughtworks.xstream.io.HierarchicalStreamReader;
016:        import com.thoughtworks.xstream.io.xml.XppDriver;
017:
018:        import java.io.IOException;
019:        import java.io.ObjectInputStream;
020:        import java.io.ObjectOutputStream;
021:        import java.io.ObjectStreamException;
022:        import java.io.Reader;
023:        import java.io.StringReader;
024:        import java.io.StringWriter;
025:        import java.io.Writer;
026:
027:        /**
028:         * Self-contained XStream generator. The class is a utility to write XML streams that contain 
029:         * additionally the XStream that was used to serialize the object graph. Such a stream can
030:         * be unmarshalled using this embedded XStream instance, that kept any settings.
031:         * 
032:         * @author Jörg Schaible
033:         * @since 1.2
034:         */
035:        public class XStreamer {
036:
037:            /**
038:             * Serialize an object including the XStream to a pretty-printed XML String.
039:             * 
040:             * @throws ObjectStreamException if the XML contains non-serializable elements
041:             * @throws com.thoughtworks.xstream.XStreamException if the object cannot be serialized
042:             * @since 1.2
043:             * @see #toXML(XStream, Object, Writer)
044:             */
045:            public String toXML(XStream xstream, Object obj)
046:                    throws ObjectStreamException {
047:                Writer writer = new StringWriter();
048:                try {
049:                    toXML(xstream, obj, writer);
050:                } catch (ObjectStreamException e) {
051:                    throw e;
052:                } catch (IOException e) {
053:                    throw new ConversionException(
054:                            "Unexpeced IO error from a StringWriter", e);
055:                }
056:                return writer.toString();
057:            }
058:
059:            /**
060:             * Serialize an object including the XStream to the given Writer as pretty-printed XML.
061:             * <p>
062:             * Warning: XStream will serialize itself into this XML stream. To read such an XML code, you
063:             * should use {@link XStreamer#fromXML(Reader)} or one of the other overloaded
064:             * methods. Since a lot of internals are written into the stream, you cannot expect to use such
065:             * an XML to work with another XStream version or with XStream running on different JDKs and/or
066:             * versions. We have currently no JDK 1.3 support, nor will the PureReflectionConverter work
067:             * with a JDK less than 1.5.
068:             * </p>
069:             * 
070:             * @throws IOException if an error occurs reading from the Writer.
071:             * @throws com.thoughtworks.xstream.XStreamException if the object cannot be serialized
072:             * @since 1.2
073:             */
074:            public void toXML(XStream xstream, Object obj, Writer out)
075:                    throws IOException {
076:                XStream outer = new XStream();
077:                ObjectOutputStream oos = outer.createObjectOutputStream(out);
078:                try {
079:                    oos.writeObject(xstream);
080:                    oos.flush();
081:                    xstream.toXML(obj, out);
082:                } finally {
083:                    oos.close();
084:                }
085:            }
086:
087:            /**
088:             * Deserialize a self-contained XStream with object from a String. The method will use
089:             * internally an XppDriver to load the contained XStream instance.
090:             * 
091:             * @throws ClassNotFoundException if a class in the XML stream cannot be found
092:             * @throws ObjectStreamException if the XML contains non-deserializable elements
093:             * @throws com.thoughtworks.xstream.XStreamException if the object cannot be deserialized
094:             * @since 1.2
095:             * @see #toXML(XStream, Object, Writer)
096:             */
097:            public Object fromXML(String xml) throws ClassNotFoundException,
098:                    ObjectStreamException {
099:                try {
100:                    return fromXML(new StringReader(xml));
101:                } catch (ObjectStreamException e) {
102:                    throw e;
103:                } catch (IOException e) {
104:                    throw new ConversionException(
105:                            "Unexpeced IO error from a StringReader", e);
106:                }
107:            }
108:
109:            /**
110:             * Deserialize a self-contained XStream with object from a String.
111:             * 
112:             * @throws ClassNotFoundException if a class in the XML stream cannot be found
113:             * @throws ObjectStreamException if the XML contains non-deserializable elements
114:             * @throws com.thoughtworks.xstream.XStreamException if the object cannot be deserialized
115:             * @since 1.2
116:             * @see #toXML(XStream, Object, Writer)
117:             */
118:            public Object fromXML(HierarchicalStreamDriver driver, String xml)
119:                    throws ClassNotFoundException, ObjectStreamException {
120:                try {
121:                    return fromXML(driver, new StringReader(xml));
122:                } catch (ObjectStreamException e) {
123:                    throw e;
124:                } catch (IOException e) {
125:                    throw new ConversionException(
126:                            "Unexpeced IO error from a StringReader", e);
127:                }
128:            }
129:
130:            /**
131:             * Deserialize a self-contained XStream with object from an XML Reader. The method will use
132:             * internally an XppDriver to load the contained XStream instance.
133:             * 
134:             * @throws IOException if an error occurs reading from the Reader.
135:             * @throws ClassNotFoundException if a class in the XML stream cannot be found
136:             * @throws com.thoughtworks.xstream.XStreamException if the object cannot be deserialized
137:             * @since 1.2
138:             * @see #toXML(XStream, Object, Writer)
139:             */
140:            public Object fromXML(Reader xml) throws IOException,
141:                    ClassNotFoundException {
142:                return fromXML(new XppDriver(), xml);
143:            }
144:
145:            /**
146:             * Deserialize a self-contained XStream with object from an XML Reader.
147:             * 
148:             * @throws IOException if an error occurs reading from the Reader.
149:             * @throws ClassNotFoundException if a class in the XML stream cannot be found
150:             * @throws com.thoughtworks.xstream.XStreamException if the object cannot be deserialized
151:             * @since 1.2
152:             */
153:            public Object fromXML(HierarchicalStreamDriver driver, Reader xml)
154:                    throws IOException, ClassNotFoundException {
155:                XStream outer = new XStream(driver);
156:                HierarchicalStreamReader reader = driver.createReader(xml);
157:                ObjectInputStream configIn = outer
158:                        .createObjectInputStream(reader);
159:                try {
160:                    XStream configured = (XStream) configIn.readObject();
161:                    ObjectInputStream in = configured
162:                            .createObjectInputStream(reader);
163:                    try {
164:                        return in.readObject();
165:                    } finally {
166:                        in.close();
167:                    }
168:                } finally {
169:                    configIn.close();
170:                }
171:            }
172:
173:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.