KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > summary > DocumentSummarizerImpl


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.summary;
17
18 import org.outerj.daisy.repository.Document;
19 import org.outerj.daisy.repository.Part;
20 import org.outerj.daisy.repository.schema.RepositorySchema;
21 import org.apache.avalon.framework.logger.AbstractLogEnabled;
22
23 import java.io.InputStream JavaDoc;
24 import java.io.BufferedInputStream JavaDoc;
25
26
27 /**
28  * @avalon.component version="1.0" name="summarizer" lifestyle="singleton"
29  * @avalon.service type="org.outerj.daisy.summary.DocumentSummarizer"
30  */

31 public class DocumentSummarizerImpl extends AbstractLogEnabled implements DocumentSummarizer {
32     private static final int SUMMARY_LENGTH = 300;
33
34     public String JavaDoc getSummary(Document document, long versionId, RepositorySchema repositorySchema) throws Exception JavaDoc {
35         Part[] parts = null;
36
37         if (versionId == -1) {
38             parts = document.getPartsInOrder().getArray();
39         } else {
40             parts = document.getVersion(versionId).getPartsInOrder().getArray();
41         }
42
43         for (int i = 0; i < parts.length; i++) {
44             Part part = parts[i];
45             if (part.getMimeType().equals("text/xml")) {
46                 boolean daisyHtml = repositorySchema.getPartTypeById(part.getTypeId(), false).isDaisyHtml();
47                 if (daisyHtml) {
48                     String JavaDoc summary;
49                     InputStream JavaDoc is = new BufferedInputStream JavaDoc(part.getDataStream());
50                     try {
51                         summary = HtmlSummarizer.extractSummary(is, SUMMARY_LENGTH);
52                     } finally {
53                         is.close();
54                     }
55                     return summary;
56                 }
57             }
58         }
59
60         return null;
61     }
62 }
63
Popular Tags