1 /** 2 * Copyright 2014 Internet2 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 /* 17 * Copyright (C) 2006, 2007 XStream Committers. 18 * All rights reserved. 19 * 20 * The software in this package is published under the terms of the BSD 21 * style license a copy of which has been included with this distribution in 22 * the LICENSE.txt file. 23 * 24 * Created on 13. April 2006 by Joerg Schaible 25 */ 26 package edu.internet2.middleware.grouperClientExt.com.thoughtworks.xstream; 27 28 import java.io.IOException; 29 import java.io.ObjectInputStream; 30 import java.io.ObjectOutputStream; 31 import java.io.ObjectStreamException; 32 import java.io.Reader; 33 import java.io.StringReader; 34 import java.io.StringWriter; 35 import java.io.Writer; 36 37 import edu.internet2.middleware.grouperClientExt.com.thoughtworks.xstream.converters.ConversionException; 38 import edu.internet2.middleware.grouperClientExt.com.thoughtworks.xstream.io.HierarchicalStreamDriver; 39 import edu.internet2.middleware.grouperClientExt.com.thoughtworks.xstream.io.HierarchicalStreamReader; 40 import edu.internet2.middleware.grouperClientExt.com.thoughtworks.xstream.io.xml.XppDriver; 41 42 /** 43 * Self-contained XStream generator. The class is a utility to write XML streams that contain 44 * additionally the XStream that was used to serialize the object graph. Such a stream can 45 * be unmarshalled using this embedded XStream instance, that kept any settings. 46 * 47 * @author Jörg Schaible 48 * @since 1.2 49 */ 50 public class XStreamer { 51 52 /** 53 * Serialize an object including the XStream to a pretty-printed XML String. 54 * 55 * @throws ObjectStreamException if the XML contains non-serializable elements 56 * @throws edu.internet2.middleware.grouperClientExt.com.thoughtworks.xstream.XStreamException if the object cannot be serialized 57 * @since 1.2 58 * @see #toXML(XStream, Object, Writer) 59 */ 60 public String toXML(XStream xstream, Object obj) throws ObjectStreamException { 61 Writer writer = new StringWriter(); 62 try { 63 toXML(xstream, obj, writer); 64 } catch (ObjectStreamException e) { 65 throw e; 66 } catch (IOException e) { 67 throw new ConversionException("Unexpeced IO error from a StringWriter", e); 68 } 69 return writer.toString(); 70 } 71 72 /** 73 * Serialize an object including the XStream to the given Writer as pretty-printed XML. 74 * <p> 75 * Warning: XStream will serialize itself into this XML stream. To read such an XML code, you 76 * should use {@link XStreamer#fromXML(Reader)} or one of the other overloaded 77 * methods. Since a lot of internals are written into the stream, you cannot expect to use such 78 * an XML to work with another XStream version or with XStream running on different JDKs and/or 79 * versions. We have currently no JDK 1.3 support, nor will the PureReflectionConverter work 80 * with a JDK less than 1.5. 81 * </p> 82 * 83 * @throws IOException if an error occurs reading from the Writer. 84 * @throws edu.internet2.middleware.grouperClientExt.com.thoughtworks.xstream.XStreamException if the object cannot be serialized 85 * @since 1.2 86 */ 87 public void toXML(XStream xstream, Object obj, Writer out) 88 throws IOException { 89 XStreamks/xstream/XStream.html#XStream">XStream outer = new XStream(); 90 ObjectOutputStream oos = outer.createObjectOutputStream(out); 91 try { 92 oos.writeObject(xstream); 93 oos.flush(); 94 xstream.toXML(obj, out); 95 } finally { 96 oos.close(); 97 } 98 } 99 100 /** 101 * Deserialize a self-contained XStream with object from a String. The method will use 102 * internally an XppDriver to load the contained XStream instance. 103 * 104 * @throws ClassNotFoundException if a class in the XML stream cannot be found 105 * @throws ObjectStreamException if the XML contains non-deserializable elements 106 * @throws edu.internet2.middleware.grouperClientExt.com.thoughtworks.xstream.XStreamException if the object cannot be deserialized 107 * @since 1.2 108 * @see #toXML(XStream, Object, Writer) 109 */ 110 public Object fromXML(String xml) throws ClassNotFoundException, ObjectStreamException { 111 try { 112 return fromXML(new StringReader(xml)); 113 } catch (ObjectStreamException e) { 114 throw e; 115 } catch (IOException e) { 116 throw new ConversionException("Unexpeced IO error from a StringReader", e); 117 } 118 } 119 120 /** 121 * Deserialize a self-contained XStream with object from a String. 122 * 123 * @throws ClassNotFoundException if a class in the XML stream cannot be found 124 * @throws ObjectStreamException if the XML contains non-deserializable elements 125 * @throws edu.internet2.middleware.grouperClientExt.com.thoughtworks.xstream.XStreamException if the object cannot be deserialized 126 * @since 1.2 127 * @see #toXML(XStream, Object, Writer) 128 */ 129 public Object fromXML(HierarchicalStreamDriver driver, String xml) 130 throws ClassNotFoundException, ObjectStreamException { 131 try { 132 return fromXML(driver, new StringReader(xml)); 133 } catch (ObjectStreamException e) { 134 throw e; 135 } catch (IOException e) { 136 throw new ConversionException("Unexpeced IO error from a StringReader", e); 137 } 138 } 139 140 /** 141 * Deserialize a self-contained XStream with object from an XML Reader. The method will use 142 * internally an XppDriver to load the contained XStream instance. 143 * 144 * @throws IOException if an error occurs reading from the Reader. 145 * @throws ClassNotFoundException if a class in the XML stream cannot be found 146 * @throws edu.internet2.middleware.grouperClientExt.com.thoughtworks.xstream.XStreamException if the object cannot be deserialized 147 * @since 1.2 148 * @see #toXML(XStream, Object, Writer) 149 */ 150 public Object fromXML(Reader xml) 151 throws IOException, ClassNotFoundException { 152 return fromXML(new XppDriver(), xml); 153 } 154 155 /** 156 * Deserialize a self-contained XStream with object from an XML Reader. 157 * 158 * @throws IOException if an error occurs reading from the Reader. 159 * @throws ClassNotFoundException if a class in the XML stream cannot be found 160 * @throws edu.internet2.middleware.grouperClientExt.com.thoughtworks.xstream.XStreamException if the object cannot be deserialized 161 * @since 1.2 162 */ 163 public Object fromXML(HierarchicalStreamDriver driver, Reader xml) 164 throws IOException, ClassNotFoundException { 165 XStreamks/xstream/XStream.html#XStream">XStream outer = new XStream(driver); 166 HierarchicalStreamReader reader = driver.createReader(xml); 167 ObjectInputStream configIn = outer.createObjectInputStream(reader); 168 try { 169 XStream/../../../../../edu/internet2/middleware/grouperClientExt/com/thoughtworks/xstream/XStream.html#XStream">XStream configured = (XStream)configIn.readObject(); 170 ObjectInputStream in = configured.createObjectInputStream(reader); 171 try { 172 return in.readObject(); 173 } finally { 174 in.close(); 175 } 176 } finally { 177 configIn.close(); 178 } 179 } 180 181 }