001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.xml;
034:
035: import org.libresource.Libresource;
036: import org.libresource.LibresourceException;
037:
038: import org.libresource.kernel.LibresourceSecurityException;
039: import org.libresource.kernel.interfaces.KernelService;
040:
041: import org.xml.sax.Attributes;
042: import org.xml.sax.ContentHandler;
043: import org.xml.sax.DTDHandler;
044: import org.xml.sax.EntityResolver;
045: import org.xml.sax.ErrorHandler;
046: import org.xml.sax.InputSource;
047: import org.xml.sax.SAXException;
048: import org.xml.sax.SAXNotRecognizedException;
049: import org.xml.sax.SAXNotSupportedException;
050: import org.xml.sax.XMLReader;
051: import org.xml.sax.helpers.DefaultHandler;
052:
053: import java.io.IOException;
054: import java.io.InputStream;
055: import java.io.OutputStream;
056:
057: import java.net.URI;
058:
059: import java.util.Date;
060:
061: import javax.xml.parsers.SAXParser;
062: import javax.xml.parsers.SAXParserFactory;
063: import javax.xml.transform.Transformer;
064: import javax.xml.transform.TransformerFactory;
065: import javax.xml.transform.sax.SAXSource;
066: import javax.xml.transform.stream.StreamResult;
067:
068: /**
069: * LibreSource
070: *
071: * @author <a href="mailto:bort@loria.fr">Guillaume Bort</a> - <a
072: * href="http://www.inria.fr">INRIA Lorraine</a>
073: */
074: public class LibresourceImportExport {
075: private SecurityImportHandler aclHandler;
076:
077: public LibresourceImportExport(boolean createProfile)
078: throws LibresourceException {
079: try {
080: aclHandler = new DefaultSecurityHandler(createProfile);
081: } catch (Exception e) {
082: throw new LibresourceException(e);
083: }
084: }
085:
086: public void setAclHandler(SecurityImportHandler handler) {
087: this .aclHandler = handler;
088: }
089:
090: public void exportXML(URI fromNode, OutputStream os,
091: ImportExportLogger logger) throws LibresourceException {
092: try {
093: Transformer transformer = TransformerFactory.newInstance()
094: .newTransformer();
095: XMLReader saxReader = new LibresourceSAXReader(logger);
096: SAXSource source = new SAXSource(saxReader,
097: new LibresourceInputSource(fromNode));
098: StreamResult result = new StreamResult(os);
099: transformer.transform(source, result);
100: } catch (Exception e) {
101: throw new LibresourceException("Problem during export("
102: + fromNode + ")", e);
103: }
104: }
105:
106: public void importXML(URI atNode, InputStream is,
107: ImportExportLogger logger)
108: throws LibresourceSecurityException, LibresourceException {
109: try {
110: DefaultHandler handler = new LibresourceSAXParser(atNode,
111: aclHandler, logger);
112: SAXParserFactory factory = SAXParserFactory.newInstance();
113: SAXParser saxParser = factory.newSAXParser();
114:
115: saxParser.parse(is, handler);
116: } catch (Exception e) {
117: throw new LibresourceException("Problem during import("
118: + atNode + ")", e);
119: }
120: }
121: }
122:
123: class LibresourceSAXReader implements XMLReader {
124: private ContentHandler handler;
125: private ImportExportLogger logger;
126:
127: public LibresourceSAXReader(ImportExportLogger logger) {
128: this .logger = logger;
129: }
130:
131: public ContentHandler getContentHandler() {
132: return handler;
133: }
134:
135: public DTDHandler getDTDHandler() {
136: return null;
137: }
138:
139: public EntityResolver getEntityResolver() {
140: return null;
141: }
142:
143: public ErrorHandler getErrorHandler() {
144: return null;
145: }
146:
147: public boolean getFeature(String arg0)
148: throws SAXNotRecognizedException, SAXNotSupportedException {
149: return false;
150: }
151:
152: public Object getProperty(String arg0)
153: throws SAXNotRecognizedException, SAXNotSupportedException {
154: return null;
155: }
156:
157: public void parse(InputSource inputSource) throws IOException,
158: SAXException {
159: if (!(inputSource instanceof LibresourceInputSource)) {
160: throw new IOException(
161: "LibresourceSAXReader can only parse LibresourceInputSource");
162: }
163:
164: LibresourceInputSource is = (LibresourceInputSource) inputSource;
165:
166: if (handler == null) {
167: throw new SAXException("No content handler");
168: }
169:
170: try {
171: KernelService kernelService = (KernelService) Libresource
172: .getService("Kernel");
173:
174: XmlDumpAttributes attrs = new XmlDumpAttributes();
175: attrs.put("date", Libresource.formatDate(new Date()));
176: attrs.put("original-root", kernelService
177: .normalizeAbsoluteURIPath(is.getURI()));
178: attrs.put("original-host", kernelService.getUriHost());
179:
180: XmlDumpHelper.startDocument("libresource",
181: "libresource-dump", XmlDumpHelper
182: .generateAttributesSet(attrs), handler);
183:
184: LibresourceExportHandler exportHandler = new URIExportHandler(
185: is.getURI(), is.getURI());
186: exportHandler.export(handler, logger);
187:
188: XmlDumpHelper.endDocument("libresource",
189: "libresource-dump", handler);
190: } catch (Exception e) {
191: throw new SAXException(
192: "Error in LibresourceSAXReader.parse("
193: + is.getURI() + ")", e);
194: }
195: }
196:
197: public void parse(String str) throws IOException, SAXException {
198: }
199:
200: public void setContentHandler(ContentHandler handler) {
201: this .handler = handler;
202: }
203:
204: public void setDTDHandler(DTDHandler arg0) {
205: }
206:
207: public void setEntityResolver(EntityResolver arg0) {
208: }
209:
210: public void setErrorHandler(ErrorHandler arg0) {
211: }
212:
213: public void setFeature(String arg0, boolean arg1)
214: throws SAXNotRecognizedException, SAXNotSupportedException {
215: }
216:
217: public void setProperty(String arg0, Object arg1)
218: throws SAXNotRecognizedException, SAXNotSupportedException {
219: }
220: }
221:
222: class LibresourceInputSource extends InputSource {
223: private URI uri;
224:
225: public LibresourceInputSource(URI uri) {
226: this .uri = uri;
227: }
228:
229: public URI getURI() {
230: return uri;
231: }
232: }
233:
234: class LibresourceSAXParser extends DefaultHandler {
235: private URIImportHandler handler;
236: private ImportExportLogger logger;
237:
238: public LibresourceSAXParser(URI atNode,
239: SecurityImportHandler aclImportHandler,
240: ImportExportLogger logger) {
241: handler = new URIImportHandler(aclImportHandler);
242: this .logger = logger;
243: handler.init(atNode, logger);
244: }
245:
246: public void startElement(String uri, String localName,
247: String qName, Attributes attributes) throws SAXException {
248: try {
249: if (handler != null) {
250: handler.handleBeginElement(qName, attributes, logger);
251: }
252: } catch (Exception e) {
253: throw new SAXException(e);
254: }
255: }
256:
257: public void endElement(String uri, String localName, String qName)
258: throws SAXException {
259: try {
260: if (handler != null) {
261: if (handler.handleEndElement(qName, logger)) {
262: handler = null;
263: }
264: }
265: } catch (Exception e) {
266: throw new SAXException(e);
267: }
268: }
269:
270: public void characters(char[] ch, int start, int length)
271: throws SAXException {
272: try {
273: if (handler != null) {
274: handler.handleContent(new String(ch, start, length),
275: logger);
276: }
277: } catch (Exception e) {
278: throw new SAXException(e);
279: }
280: }
281: }
|