001: package org.columba.core.gui.docking;
002:
003: import java.io.IOException;
004: import java.io.InputStream;
005: import java.io.OutputStream;
006: import java.io.OutputStreamWriter;
007:
008: import javax.xml.parsers.DocumentBuilder;
009: import javax.xml.parsers.DocumentBuilderFactory;
010: import javax.xml.parsers.ParserConfigurationException;
011: import javax.xml.transform.OutputKeys;
012: import javax.xml.transform.Transformer;
013: import javax.xml.transform.TransformerConfigurationException;
014: import javax.xml.transform.TransformerException;
015: import javax.xml.transform.TransformerFactory;
016: import javax.xml.transform.dom.DOMSource;
017: import javax.xml.transform.stream.StreamResult;
018:
019: import org.flexdock.docking.DockingPort;
020: import org.flexdock.docking.state.LayoutNode;
021: import org.flexdock.docking.state.PersistenceException;
022: import org.flexdock.perspective.persist.xml.ISerializer;
023: import org.flexdock.perspective.persist.xml.SerializerRegistry;
024: import org.flexdock.util.Utilities;
025: import org.w3c.dom.Document;
026: import org.w3c.dom.Element;
027: import org.xml.sax.InputSource;
028: import org.xml.sax.SAXException;
029:
030: public class XMLPersister {
031:
032: private DocumentBuilder createDocumentBuilder()
033: throws PersistenceException {
034: try {
035: DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
036: .newInstance();
037: DocumentBuilder documentBuilder = documentBuilderFactory
038: .newDocumentBuilder();
039:
040: return documentBuilder;
041: } catch (ParserConfigurationException ex) {
042: throw new PersistenceException(
043: "Unable to create document builder", ex);
044: }
045: }
046:
047: public boolean store(OutputStream os, DockingPort dockingPort)
048: throws IOException, PersistenceException {
049: DocumentBuilder documentBuilder = createDocumentBuilder();
050: Document document = documentBuilder.newDocument();
051:
052: ISerializer serializer = SerializerRegistry
053: .getSerializer(LayoutNode.class);
054: Element layoutNodeElement = serializer.serialize(document,
055: dockingPort.exportLayout());
056:
057: document.appendChild(layoutNodeElement);
058:
059: TransformerFactory transformerFactory = TransformerFactory
060: .newInstance();
061: // the indent-number attribute causes an IllegalArgumentException under 1.4
062: if (Utilities.JAVA_1_5) {
063: transformerFactory.setAttribute("indent-number",
064: new Integer(4));
065: }
066:
067: try {
068: Transformer transformer = transformerFactory
069: .newTransformer();
070: // this property is ignored under java 1.5.
071: transformer.setOutputProperty(
072: "{http://xml.apache.org/xslt}indent-amount", "4");
073: transformer.setOutputProperty(OutputKeys.INDENT, "yes");
074:
075: DOMSource source = new DOMSource(document);
076: StreamResult result = new StreamResult(
077: new OutputStreamWriter(os));
078:
079: transformer.transform(source, result);
080: } catch (TransformerConfigurationException ex) {
081: throw new PersistenceException(
082: "Unable to serialize perspectiveModel", ex);
083: } catch (TransformerException ex) {
084: throw new PersistenceException(
085: "Unable to serialize perspectiveModel", ex);
086: }
087:
088: return true;
089: }
090:
091: public void load(InputStream is, DockingPort dockingPort)
092: throws IOException, PersistenceException {
093: try {
094: InputSource inputSource = new InputSource(is);
095: DocumentBuilder documentBuilder = createDocumentBuilder();
096: Document document = documentBuilder.parse(inputSource);
097:
098: ISerializer layoutNodeSerializer = SerializerRegistry
099: .getSerializer(LayoutNode.class);
100: LayoutNode layoutNode = (LayoutNode) layoutNodeSerializer
101: .deserialize(document.getDocumentElement());
102: dockingPort.importLayout(layoutNode);
103:
104: } catch (SAXException ex) {
105: throw new PersistenceException(
106: "Unable to deserialize perspectiveModel from xml",
107: ex);
108: }
109: }
110:
111: }
|