KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bluecubs > xinco > index > XincoDocument


1 /**
2  *Copyright 2004 blueCubs.com
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  *************************************************************
17  * This project supports the blueCubs vision of giving back
18  * to the community in exchange for free software!
19  * More information on: http://www.bluecubs.org
20  *************************************************************
21  *
22  * Name: XincoDocument
23  *
24  * Description: convert XincoCoreData to Lucene Documents
25  *
26  * Original Author: Alexander Manes
27  * Date: 2004/10/31
28  *
29  * Modifications:
30  *
31  * Who? When? What?
32  * - - -
33  *
34  *************************************************************
35  */

36
37 package com.bluecubs.xinco.index;
38
39 import java.io.File JavaDoc;
40 import java.io.Reader JavaDoc;
41 import java.io.FileInputStream JavaDoc;
42 import org.apache.lucene.document.Document;
43 import org.apache.lucene.document.Field;
44 import com.bluecubs.xinco.core.*;
45 import com.bluecubs.xinco.add.*;
46 import com.bluecubs.xinco.core.server.*;
47 import com.bluecubs.xinco.index.filetypes.*;
48
49 /** A utility for making Lucene Documents from a File. */
50
51 public class XincoDocument {
52
53     public static Document getXincoDocument(XincoCoreData d, boolean index_content, XincoDBManager dbm) throws java.io.FileNotFoundException JavaDoc {
54      
55         int i, j, l;
56         int i2, j2;
57         short k, k2;
58         FileInputStream JavaDoc is = null;
59         Document doc = null;
60         Document temp_doc = null;
61         int file_type = 0;
62         int file_ext_index = 0;
63         String JavaDoc file_ext = "";
64         
65         doc = new Document();
66         
67         //add XincoCoreData information
68
doc.add(new Field("id", (new Integer JavaDoc(d.getId())).toString(), true, true, false));
69         doc.add(Field.Text("designation", d.getDesignation()));
70         doc.add(new Field("language", (new Integer JavaDoc(d.getXinco_core_language().getId())).toString(), true, true, false));
71         
72         //add content of file
73
if (index_content) {
74             if ((d.getXinco_core_data_type().getId() == 1) && (d.getStatus_number() != 3)) { //process non-archived file
75
//extract file extension from file name
76
file_ext_index =((XincoAddAttribute)d.getXinco_add_attributes().elementAt(0)).getAttrib_varchar().lastIndexOf(".");
77                 if (file_ext_index == -1) {
78                     file_ext = "";
79                 } else {
80                     if (file_ext_index >= ((XincoAddAttribute)d.getXinco_add_attributes().elementAt(0)).getAttrib_varchar().length() - 1) {
81                         file_ext = "";
82                     } else {
83                         file_ext = ((XincoAddAttribute)d.getXinco_add_attributes().elementAt(0)).getAttrib_varchar().substring(file_ext_index + 1);
84                     }
85                 }
86                 //check which indexer to use for file extension
87
file_type = 0; // default: index as TEXT
88
for (l=0;l<dbm.config.FileIndexerCount;l++) {
89                     for (i=0;i<((String JavaDoc[])dbm.config.IndexFileTypesExt.elementAt(l)).length;i++) {
90                         if (((String JavaDoc[])dbm.config.IndexFileTypesExt.elementAt(l))[i].compareTo(file_ext) == 0) {
91                             file_type = l+1; // file-type specific indexing
92
break;
93                         }
94                     }
95                     if (file_type > 0) {
96                         break;
97                     }
98                 }
99                 if (file_type == 0) {
100                     for (i=0;i<dbm.config.IndexNoIndex.length;i++) {
101                         if (dbm.config.IndexNoIndex[i].compareTo(file_ext) == 0) {
102                             file_type = -1; // NO indexing
103
break;
104                         }
105                     }
106                 }
107                 // call actual indexing classes
108
XincoIndexFileType xift = null;
109                 Reader JavaDoc ContentReader = null;
110                 String JavaDoc ContentString = null;
111                 if (file_type == 0) {
112                     // index as TEXT
113
xift = new XincoIndexText();
114                     doc.add(Field.Text("file", xift.getFileContentReader(new File JavaDoc(dbm.config.FileRepositoryPath + (new Integer JavaDoc(d.getId())).toString()))));
115                 } else if (file_type > 0) {
116                     // file-type specific indexing
117
try {
118                         xift = (XincoIndexFileType)Class.forName((String JavaDoc)dbm.config.IndexFileTypesClass.elementAt(file_type-1)).newInstance();
119                         ContentReader = xift.getFileContentReader(new File JavaDoc(dbm.config.FileRepositoryPath + (new Integer JavaDoc(d.getId())).toString()));
120                         if (ContentReader != null) {
121                             doc.add(Field.Text("file", ContentReader));
122                         } else {
123                             ContentString = xift.getFileContentString(new File JavaDoc(dbm.config.FileRepositoryPath + (new Integer JavaDoc(d.getId())).toString()));
124                             if (ContentString != null) {
125                                 doc.add(Field.Text("file", ContentString));
126                             }
127                         }
128                     } catch (Exception JavaDoc ie) {
129                     }
130                 }
131                 
132             }
133         }
134         
135         //add attributes
136
for (i=0;i<d.getXinco_add_attributes().size();i++) {
137             if (((XincoCoreDataTypeAttribute)d.getXinco_core_data_type().getXinco_core_data_type_attributes().elementAt(i)).getData_type().toLowerCase().compareTo("int") == 0) {
138                 doc.add(Field.Text(((XincoCoreDataTypeAttribute)d.getXinco_core_data_type().getXinco_core_data_type_attributes().elementAt(i)).getDesignation(), "" + ((XincoAddAttribute)d.getXinco_add_attributes().elementAt(i)).getAttrib_int()));
139             }
140             if (((XincoCoreDataTypeAttribute)d.getXinco_core_data_type().getXinco_core_data_type_attributes().elementAt(i)).getData_type().toLowerCase().compareTo("unsignedint") == 0) {
141                 doc.add(Field.Text(((XincoCoreDataTypeAttribute)d.getXinco_core_data_type().getXinco_core_data_type_attributes().elementAt(i)).getDesignation(), "" + ((XincoAddAttribute)d.getXinco_add_attributes().elementAt(i)).getAttrib_unsignedint()));
142             }
143             if (((XincoCoreDataTypeAttribute)d.getXinco_core_data_type().getXinco_core_data_type_attributes().elementAt(i)).getData_type().toLowerCase().compareTo("double") == 0) {
144                 doc.add(Field.Text(((XincoCoreDataTypeAttribute)d.getXinco_core_data_type().getXinco_core_data_type_attributes().elementAt(i)).getDesignation(), "" + ((XincoAddAttribute)d.getXinco_add_attributes().elementAt(i)).getAttrib_double()));
145             }
146             if (((XincoCoreDataTypeAttribute)d.getXinco_core_data_type().getXinco_core_data_type_attributes().elementAt(i)).getData_type().toLowerCase().compareTo("varchar") == 0) {
147                 doc.add(Field.Text(((XincoCoreDataTypeAttribute)d.getXinco_core_data_type().getXinco_core_data_type_attributes().elementAt(i)).getDesignation(), ((XincoAddAttribute)d.getXinco_add_attributes().elementAt(i)).getAttrib_varchar()));
148             }
149             if (((XincoCoreDataTypeAttribute)d.getXinco_core_data_type().getXinco_core_data_type_attributes().elementAt(i)).getData_type().toLowerCase().compareTo("text") == 0) {
150                 doc.add(Field.Text(((XincoCoreDataTypeAttribute)d.getXinco_core_data_type().getXinco_core_data_type_attributes().elementAt(i)).getDesignation(), ((XincoAddAttribute)d.getXinco_add_attributes().elementAt(i)).getAttrib_text()));
151             }
152             if (((XincoCoreDataTypeAttribute)d.getXinco_core_data_type().getXinco_core_data_type_attributes().elementAt(i)).getData_type().toLowerCase().compareTo("datetime") == 0) {
153                 doc.add(Field.Text(((XincoCoreDataTypeAttribute)d.getXinco_core_data_type().getXinco_core_data_type_attributes().elementAt(i)).getDesignation(), "" + ((XincoAddAttribute)d.getXinco_add_attributes().elementAt(i)).getAttrib_datetime()));
154             }
155         }
156         
157         return doc;
158     }
159
160     private XincoDocument() {}
161 }
162
Popular Tags