KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > search > JavaSearchDocument


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core.search;
12
13 import org.eclipse.core.resources.IFile;
14 import org.eclipse.core.resources.ResourcesPlugin;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IPath;
17 import org.eclipse.core.runtime.Path;
18 import org.eclipse.jdt.core.JavaModelException;
19 import org.eclipse.jdt.core.search.IJavaSearchScope;
20 import org.eclipse.jdt.core.search.SearchDocument;
21 import org.eclipse.jdt.core.search.SearchParticipant;
22 import org.eclipse.jdt.internal.core.search.processing.JobManager;
23 import org.eclipse.jdt.internal.core.util.Util;
24
25 public class JavaSearchDocument extends SearchDocument {
26     
27     private IFile file;
28     protected byte[] byteContents;
29     protected char[] charContents;
30     
31     public JavaSearchDocument(String JavaDoc documentPath, SearchParticipant participant) {
32         super(documentPath, participant);
33     }
34     public JavaSearchDocument(java.util.zip.ZipEntry JavaDoc zipEntry, IPath zipFilePath, byte[] contents, SearchParticipant participant) {
35         super(zipFilePath + IJavaSearchScope.JAR_FILE_ENTRY_SEPARATOR + zipEntry.getName(), participant);
36         this.byteContents = contents;
37     }
38
39     public byte[] getByteContents() {
40         if (this.byteContents != null) return this.byteContents;
41         try {
42             return Util.getResourceContentsAsByteArray(getFile());
43         } catch (JavaModelException e) {
44             if (BasicSearchEngine.VERBOSE || JobManager.VERBOSE) { // used during search and during indexing
45
e.printStackTrace();
46             }
47             return null;
48         }
49     }
50     public char[] getCharContents() {
51         if (this.charContents != null) return this.charContents;
52         try {
53             return Util.getResourceContentsAsCharArray(getFile());
54         } catch (JavaModelException e) {
55             if (BasicSearchEngine.VERBOSE || JobManager.VERBOSE) { // used during search and during indexing
56
e.printStackTrace();
57             }
58             return null;
59         }
60     }
61     public String JavaDoc getEncoding() {
62         // Return the encoding of the associated file
63
IFile resource = getFile();
64         if (resource != null) {
65             try {
66                 return resource.getCharset();
67             }
68             catch(CoreException ce) {
69                 try {
70                     return ResourcesPlugin.getWorkspace().getRoot().getDefaultCharset();
71                 } catch (CoreException e) {
72                     // use no encoding
73
}
74             }
75         }
76         return null;
77     }
78     private IFile getFile() {
79         if (this.file == null)
80             this.file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(getPath()));
81         return this.file;
82     }
83     public String JavaDoc toString() {
84         return "SearchDocument for " + getPath(); //$NON-NLS-1$
85
}
86 }
87
Popular Tags