Source Code Cross Referenced for odtParser.java in  » Search-Engine » yacy » de » anomic » plasma » parser » odt » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Search Engine » yacy » de.anomic.plasma.parser.odt 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        //zipParser.java 
002:        //------------------------
003:        //part of YaCy
004:        //(C) by Michael Peter Christen; mc@anomic.de
005:        //first published on http://www.anomic.de
006:        //Frankfurt, Germany, 2005
007:        //
008:        //this file is contributed by Martin Thelian
009:        //last major change: 16.05.2005
010:        //
011:        //This program is free software; you can redistribute it and/or modify
012:        //it under the terms of the GNU General Public License as published by
013:        //the Free Software Foundation; either version 2 of the License, or
014:        //(at your option) any later version.
015:        //
016:        //This program is distributed in the hope that it will be useful,
017:        //but WITHOUT ANY WARRANTY; without even the implied warranty of
018:        //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
019:        //GNU General Public License for more details.
020:        //
021:        //You should have received a copy of the GNU General Public License
022:        //along with this program; if not, write to the Free Software
023:        //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
024:        //
025:        //Using this software in any meaning (reading, learning, copying, compiling,
026:        //running) means that you agree that the Author(s) is (are) not responsible
027:        //for cost, loss of data or any harm that may be caused directly or indirectly
028:        //by usage of this softare or this documentation. The usage of this software
029:        //is on your own risk. The installation and usage (starting/running) of this
030:        //software may allow other people or application to access your computer and
031:        //any attached devices and is highly dependent on the configuration of the
032:        //software which must be done by the user of the software; the author(s) is
033:        //(are) also not responsible for proper configuration and usage of the
034:        //software, even if provoked by documentation provided together with
035:        //the software.
036:        //
037:        //Any changes to this file according to the GPL as documented in the file
038:        //gpl.txt aside this file in the shipment you received can be done to the
039:        //lines that follows this copyright notice here, but changes must not be
040:        //done inside the copyright notive above. A re-distribution must contain
041:        //the intact and unchanged copyright notice.
042:        //Contributions and changes to the program code must be marked as such.
043:
044:        package de.anomic.plasma.parser.odt;
045:
046:        import java.io.ByteArrayInputStream;
047:        import java.io.File;
048:        import java.io.FileOutputStream;
049:        import java.io.InputStream;
050:        import java.io.OutputStreamWriter;
051:        import java.io.Writer;
052:        import java.util.Enumeration;
053:        import java.util.Hashtable;
054:        import java.util.zip.ZipEntry;
055:        import java.util.zip.ZipFile;
056:
057:        import com.catcode.odf.ODFMetaFileAnalyzer;
058:        import com.catcode.odf.OpenDocumentMetadata;
059:        import com.catcode.odf.OpenDocumentTextInputStream;
060:
061:        import de.anomic.http.httpc;
062:        import de.anomic.plasma.plasmaParserDocument;
063:        import de.anomic.plasma.parser.AbstractParser;
064:        import de.anomic.plasma.parser.Parser;
065:        import de.anomic.plasma.parser.ParserException;
066:        import de.anomic.server.serverCharBuffer;
067:        import de.anomic.server.serverFileUtils;
068:        import de.anomic.server.logging.serverLog;
069:        import de.anomic.yacy.yacyURL;
070:
071:        public class odtParser extends AbstractParser implements  Parser {
072:
073:            /**
074:             * a list of mime types that are supported by this parser class
075:             * @see #getSupportedMimeTypes()
076:             */
077:            public static final Hashtable<String, String> SUPPORTED_MIME_TYPES = new Hashtable<String, String>();
078:            static {
079:                SUPPORTED_MIME_TYPES.put(
080:                        "application/vnd.oasis.opendocument.text", "odt");
081:                SUPPORTED_MIME_TYPES.put(
082:                        "application/x-vnd.oasis.opendocument.text", "odt");
083:            }
084:
085:            /**
086:             * a list of library names that are needed by this parser
087:             * @see Parser#getLibxDependences()
088:             */
089:            private static final String[] LIBX_DEPENDENCIES = new String[] { "odf_utils_05_11_29.jar" };
090:
091:            public odtParser() {
092:                super (LIBX_DEPENDENCIES);
093:                this .parserName = "OASIS OpenDocument V2 Text Document Parser";
094:            }
095:
096:            public Hashtable<String, String> getSupportedMimeTypes() {
097:                return SUPPORTED_MIME_TYPES;
098:            }
099:
100:            public plasmaParserDocument parse(yacyURL location,
101:                    String mimeType, String charset, File dest)
102:                    throws ParserException, InterruptedException {
103:
104:                Writer writer = null;
105:                File writerFile = null;
106:                try {
107:                    String docDescription = null;
108:                    String docKeywordStr = null;
109:                    String docShortTitle = null;
110:                    String docLongTitle = null;
111:                    String docAuthor = null;
112:
113:                    // opening the file as zip file
114:                    ZipFile zipFile = new ZipFile(dest);
115:                    Enumeration<? extends ZipEntry> zipEnum = zipFile.entries();
116:
117:                    // looping through all containing files
118:                    while (zipEnum.hasMoreElements()) {
119:                        // check for interruption
120:                        checkInterruption();
121:
122:                        // getting the next zip file entry
123:                        ZipEntry zipEntry = (ZipEntry) zipEnum.nextElement();
124:                        String entryName = zipEntry.getName();
125:
126:                        // content.xml contains the document content in xml format
127:                        if (entryName.equals("content.xml")) {
128:                            long contentSize = zipEntry.getSize();
129:
130:                            // creating a writer for output
131:                            if ((contentSize == -1)
132:                                    || (contentSize > Parser.MAX_KEEP_IN_MEMORY_SIZE)) {
133:                                writerFile = File.createTempFile("odtParser",
134:                                        ".tmp");
135:                                writer = new OutputStreamWriter(
136:                                        new FileOutputStream(writerFile),
137:                                        "UTF-8");
138:                            } else {
139:                                writer = new serverCharBuffer();
140:                            }
141:
142:                            // extract data
143:                            InputStream zipFileEntryStream = zipFile
144:                                    .getInputStream(zipEntry);
145:                            OpenDocumentTextInputStream odStream = new OpenDocumentTextInputStream(
146:                                    zipFileEntryStream);
147:                            serverFileUtils.copy(odStream, writer, "UTF-8");
148:
149:                            // close readers and writers
150:                            odStream.close();
151:                            writer.close();
152:
153:                        } else if (entryName.equals("meta.xml")) {
154:                            //  meta.xml contains metadata about the document
155:                            InputStream zipFileEntryStream = zipFile
156:                                    .getInputStream(zipEntry);
157:                            ODFMetaFileAnalyzer metaAnalyzer = new ODFMetaFileAnalyzer();
158:                            OpenDocumentMetadata metaData = metaAnalyzer
159:                                    .analyzeMetaData(zipFileEntryStream);
160:                            docDescription = metaData.getDescription();
161:                            docKeywordStr = metaData.getKeyword();
162:                            docShortTitle = metaData.getTitle();
163:                            docLongTitle = metaData.getSubject();
164:                            docAuthor = metaData.getCreator();
165:                        }
166:                    }
167:
168:                    // if there is no title availabe we generate one
169:                    if (docLongTitle == null) {
170:                        if (docShortTitle != null) {
171:                            docLongTitle = docShortTitle;
172:                        }
173:                    }
174:
175:                    // split the keywords
176:                    String[] docKeywords = null;
177:                    if (docKeywordStr != null)
178:                        docKeywords = docKeywordStr.split(" |,");
179:
180:                    // create the parser document
181:                    plasmaParserDocument theDoc = null;
182:                    if (writer instanceof  serverCharBuffer) {
183:                        byte[] contentBytes = ((serverCharBuffer) writer)
184:                                .toString().getBytes("UTF-8");
185:                        theDoc = new plasmaParserDocument(location, mimeType,
186:                                "UTF-8", docKeywords, docLongTitle, docAuthor,
187:                                null, docDescription, contentBytes, null, null);
188:                    } else {
189:                        theDoc = new plasmaParserDocument(location, mimeType,
190:                                "UTF-8", docKeywords, docLongTitle, docAuthor,
191:                                null, docDescription, writerFile, null, null);
192:                    }
193:                    return theDoc;
194:                } catch (Exception e) {
195:                    if (e instanceof  InterruptedException)
196:                        throw (InterruptedException) e;
197:                    if (e instanceof  ParserException)
198:                        throw (ParserException) e;
199:
200:                    // close the writer
201:                    if (writer != null)
202:                        try {
203:                            writer.close();
204:                        } catch (Exception ex) {/* ignore this */
205:                        }
206:
207:                    // delete the file
208:                    if (writerFile != null)
209:                        try {
210:                            writerFile.delete();
211:                        } catch (Exception ex) {/* ignore this */
212:                        }
213:
214:                    throw new ParserException(
215:                            "Unexpected error while parsing odt file. "
216:                                    + e.getMessage(), location);
217:                }
218:            }
219:
220:            public plasmaParserDocument parse(yacyURL location,
221:                    String mimeType, String charset, InputStream source)
222:                    throws ParserException, InterruptedException {
223:                File dest = null;
224:                try {
225:                    // creating a tempfile
226:                    dest = File.createTempFile("OpenDocument", ".odt");
227:                    dest.deleteOnExit();
228:
229:                    // copying the stream into a file
230:                    serverFileUtils.copy(source, dest);
231:
232:                    // parsing the content
233:                    return parse(location, mimeType, charset, dest);
234:                } catch (Exception e) {
235:                    if (e instanceof  InterruptedException)
236:                        throw (InterruptedException) e;
237:                    if (e instanceof  ParserException)
238:                        throw (ParserException) e;
239:
240:                    throw new ParserException(
241:                            "Unexpected error while parsing odt file. "
242:                                    + e.getMessage(), location);
243:                } finally {
244:                    if (dest != null)
245:                        try {
246:                            dest.delete();
247:                        } catch (Exception e) {/* ignore this */
248:                        }
249:                }
250:            }
251:
252:            public void reset() {
253:                // Nothing todo here at the moment
254:                super .reset();
255:            }
256:
257:            public static void main(String[] args) {
258:                try {
259:                    if (args.length != 1)
260:                        return;
261:
262:                    // getting the content URL
263:                    yacyURL contentUrl = new yacyURL(args[0], null);
264:
265:                    // creating a new parser
266:                    odtParser testParser = new odtParser();
267:
268:                    // setting the parser logger
269:                    testParser.setLogger(new serverLog("PARSER.ODT"));
270:
271:                    // downloading the document content
272:                    byte[] content = httpc.singleGET(contentUrl, contentUrl
273:                            .getHost(), 10000, null, null, null, null);
274:                    ByteArrayInputStream input = new ByteArrayInputStream(
275:                            content);
276:
277:                    // parsing the document
278:                    testParser.parse(contentUrl,
279:                            "application/vnd.oasis.opendocument.text", null,
280:                            input);
281:                } catch (Exception e) {
282:                    e.printStackTrace();
283:                }
284:            }
285:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.