Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

DocumentFacadeImpl.java

Go to the documentation of this file.
00001 
00058 package com.arcle.rmt.j2se.model.domimpl;
00059 
00060 import javax.xml.parsers.DocumentBuilderFactory;
00061 import javax.xml.parsers.DocumentBuilder;
00062 import org.apache.xml.serialize.OutputFormat;
00063 import org.apache.xml.serialize.XMLSerializer;
00064 import org.apache.xml.serialize.LineSeparator;
00065 import org.w3c.dom.Document;
00066 import org.w3c.dom.Element;
00067 import org.w3c.dom.DOMException;
00068 
00069 import java.io.File;
00070 import java.io.FileWriter;
00071 import java.io.FileReader;
00072 import java.io.IOException;
00073 
00074 import com.arcle.rmt.j2se.model.RQMLDocument;
00075 import com.arcle.rmt.j2se.model.RQMLDocumentFacade;
00076 import com.arcle.rmt.j2se.model.RQMLDocumentFacadeException;
00077 
00083 public class DocumentFacadeImpl extends RQMLDocumentFacade {
00084 
00085     public DocumentFacadeImpl() {
00086         try {
00087             documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
00088         } catch(Exception e) {
00089             e.printStackTrace();
00090             throw new IllegalStateException("Cannot create DOM document builder.");
00091         }
00092     }
00093 
00097     public synchronized RQMLDocument createRQMLDocument()
00098             throws RQMLDocumentFacadeException {
00099         RQMLDocument impl = null;
00100         try {
00101             Document dom = documentBuilder.newDocument();
00102             Element element = dom.createElement("rqml");
00103             dom.appendChild(element);
00104             impl = createDocument(dom.getDocumentElement());
00105         } catch(Exception e) {
00106             rethrowException(e);
00107         }
00108         return impl;
00109     }
00110 
00111 
00115     public synchronized RQMLDocument openDocument(File rqmlDocument)
00116             throws RQMLDocumentFacadeException {
00117         RQMLDocument doc = null;
00118         try {
00119             Document dom = documentBuilder.parse(rqmlDocument);
00120             doc = createDocument(dom.getDocumentElement());
00121         } catch(Exception e) {
00122             rethrowException(e);
00123         }
00124         return doc;
00125     }
00126 
00127     public synchronized void saveDocument(RQMLDocument doc, File docFile)
00128             throws RQMLDocumentFacadeException {
00129         try {
00130             Document dom = getDOMDocument(doc);
00131 
00132             // serialize
00133             OutputFormat format = new OutputFormat((Document) dom);
00134             format.setIndenting(true);
00135             format.setLineWidth(0);
00136             format.setPreserveSpace(true);
00137             XMLSerializer serializer = new XMLSerializer (
00138                 new FileWriter(docFile), format);
00139             serializer.asDOMSerializer();
00140             serializer.serialize(dom);
00141         } catch(Exception e) {
00142             rethrowException(e);
00143         }
00144     }
00145 
00146 
00151     protected RQMLDocument createDocument(Element dom) {
00152         return new DocumentImpl(dom);
00153     }
00154 
00159     protected org.w3c.dom.Document getDOMDocument(RQMLDocument doc) {
00160         return ((DocumentImpl) doc).getRootElement().getOwnerDocument();
00161     }
00162 
00166     protected DocumentBuilder documentBuilder;
00167 }

Generated on Fri Jun 18 19:51:17 2004 for Arcle Rambutan by doxygen 1.3.5