Java tutorial
/* * See the NOTICE file distributed with this work for additional * information regarding copyright ownership. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. * */ package org.xwiki.store.serialization.xml.internal; import java.io.ByteArrayOutputStream; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.IOException; import org.dom4j.io.SAXReader; import org.dom4j.io.OutputFormat; import org.dom4j.Document; import org.dom4j.DocumentException; import org.xml.sax.SAXException; import org.xwiki.component.annotation.ComponentRole; import org.xwiki.store.serialization.xml.XMLSerializer; /** * A Serializer which converts objects into XML Elements and back. * * @param <R> The class of object which the serializer can serialize (what it requires). * @param <P> The class of object which will be provided by this serializer when it parses data. * @version $Id: 1be7544f8adddde6fcd5aa0f8278f77b6b5800ab $ * @since 3.0M2 */ @ComponentRole() public abstract class AbstractXMLSerializer<R, P extends R> implements XMLSerializer<R, P> { /** * {@inheritDoc} * * @see org.xwiki.store.filesystem.internal.XMLSerializer#serialize(T) */ public InputStream serialize(final R object) throws IOException { // This puts everything on the heap for now. // if size becomes a major problem, the alternitive is to fork over another thread // and use a PipedInputStream. final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final XMLWriter writer; try { // This should always be UTF-8 because it is a property of the serializer. final OutputFormat of = new OutputFormat(" ", true, "UTF-8"); writer = new XMLWriter(baos, of); writer.startDocument(); } catch (SAXException e) { throw new IOException("Could not open the XML writer."); } this.serialize(object, writer); try { writer.endDocument(); } catch (SAXException e) { throw new IOException("Could not close the XML writer."); } return new ByteArrayInputStream(baos.toByteArray()); } /** * {@inheritDoc} * * @see org.xwiki.store.filesystem.internal.XMLSerializer#parse(InputStream) */ public P parse(final InputStream stream) throws IOException { final SAXReader reader = new SAXReader(); // Remove nodes generated by indentation. reader.setStripWhitespaceText(true); reader.setMergeAdjacentText(true); final Document domdoc; try { domdoc = reader.read(stream); } catch (DocumentException e) { throw new IOException("Failed to parse XML, probably malformed input."); } return this.parse(domdoc.getRootElement()); } }