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.files.parsers;
034:
035: import org.libresource.core.FileData;
036:
037: import org.xml.sax.EntityResolver;
038: import org.xml.sax.InputSource;
039: import org.xml.sax.XMLReader;
040:
041: import java.io.File;
042: import java.io.FileInputStream;
043: import java.io.FileOutputStream;
044: import java.io.InputStream;
045:
046: import java.util.zip.ZipEntry;
047: import java.util.zip.ZipFile;
048:
049: import javax.xml.parsers.SAXParser;
050: import javax.xml.parsers.SAXParserFactory;
051:
052: public class OOOContentParser implements FileContentParser {
053: protected static final String DEFAULT_PARSER_NAME = "org.apache.xerces.parsers.SAXParser";
054:
055: public String parse(FileData fileData) {
056: // temporary zip file
057: File temp = null;
058: InputStream is;
059: FileOutputStream os = null;
060: int readCount = -1;
061: byte[] buffer = new byte[1000000];
062:
063: try {
064: temp = File.createTempFile("OOOFile", ".zip");
065: is = fileData.getInputStream();
066: os = new FileOutputStream(temp);
067:
068: while ((readCount = is.read(buffer)) > 0) {
069: os.write(buffer, 0, readCount);
070: }
071: } catch (Exception e) {
072: return null;
073: } finally {
074: try {
075: os.close();
076: } catch (Exception e) {
077: //
078: }
079: }
080:
081: // get "content.xml" entry
082: ZipFile zipFile;
083: File temp2;
084: InputStream is2;
085: FileOutputStream os2 = null;
086:
087: try {
088: zipFile = new ZipFile(temp, ZipFile.OPEN_READ);
089:
090: ZipEntry zipEntry = zipFile.getEntry("content.xml");
091: temp2 = File.createTempFile("OOOFileContent", ".xml");
092: is2 = zipFile.getInputStream(zipEntry);
093: os2 = new FileOutputStream(temp2);
094:
095: int readCount2 = -1;
096: byte[] buffer2 = new byte[1000000];
097:
098: while ((readCount2 = is2.read(buffer2)) > 0) {
099: os2.write(buffer2, 0, readCount2);
100: }
101: } catch (Exception e1) {
102: return null;
103: } finally {
104: try {
105: os2.close();
106: } catch (Exception e) {
107: //
108: }
109: }
110:
111: // parse the xml file
112: XMLContentHandler handler = new XMLContentHandler();
113: SAXParserFactory saxParserFactory = SAXParserFactory
114: .newInstance();
115: EntityResolver entityResolver = new EmptyDtdEntityResolver();
116:
117: try {
118: SAXParser saxParser = saxParserFactory.newSAXParser();
119: XMLReader xmlReader = saxParser.getXMLReader();
120: xmlReader.setEntityResolver(entityResolver);
121: xmlReader.setContentHandler(handler);
122: xmlReader
123: .parse(new InputSource(new FileInputStream(temp2)));
124:
125: return handler.getBuffer();
126: } catch (Exception e) {
127: return null;
128: }
129: }
130: }
|