01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.jdt.internal.core.search;
11:
12: import org.eclipse.core.resources.IFile;
13: import org.eclipse.core.resources.ResourcesPlugin;
14: import org.eclipse.core.runtime.CoreException;
15: import org.eclipse.core.runtime.IPath;
16: import org.eclipse.core.runtime.Path;
17: import org.eclipse.jdt.core.JavaModelException;
18: import org.eclipse.jdt.core.search.IJavaSearchScope;
19: import org.eclipse.jdt.core.search.SearchDocument;
20: import org.eclipse.jdt.core.search.SearchParticipant;
21: import org.eclipse.jdt.internal.core.search.processing.JobManager;
22: import org.eclipse.jdt.internal.core.util.Util;
23:
24: public class JavaSearchDocument extends SearchDocument {
25:
26: private IFile file;
27: protected byte[] byteContents;
28: protected char[] charContents;
29:
30: public JavaSearchDocument(String documentPath,
31: SearchParticipant participant) {
32: super (documentPath, participant);
33: }
34:
35: public JavaSearchDocument(java.util.zip.ZipEntry zipEntry,
36: IPath zipFilePath, byte[] contents,
37: SearchParticipant participant) {
38: super (zipFilePath + IJavaSearchScope.JAR_FILE_ENTRY_SEPARATOR
39: + zipEntry.getName(), participant);
40: this .byteContents = contents;
41: }
42:
43: public byte[] getByteContents() {
44: if (this .byteContents != null)
45: return this .byteContents;
46: try {
47: return Util.getResourceContentsAsByteArray(getFile());
48: } catch (JavaModelException e) {
49: if (BasicSearchEngine.VERBOSE || JobManager.VERBOSE) { // used during search and during indexing
50: e.printStackTrace();
51: }
52: return null;
53: }
54: }
55:
56: public char[] getCharContents() {
57: if (this .charContents != null)
58: return this .charContents;
59: try {
60: return Util.getResourceContentsAsCharArray(getFile());
61: } catch (JavaModelException e) {
62: if (BasicSearchEngine.VERBOSE || JobManager.VERBOSE) { // used during search and during indexing
63: e.printStackTrace();
64: }
65: return null;
66: }
67: }
68:
69: public String getEncoding() {
70: // Return the encoding of the associated file
71: IFile resource = getFile();
72: if (resource != null) {
73: try {
74: return resource.getCharset();
75: } catch (CoreException ce) {
76: try {
77: return ResourcesPlugin.getWorkspace().getRoot()
78: .getDefaultCharset();
79: } catch (CoreException e) {
80: // use no encoding
81: }
82: }
83: }
84: return null;
85: }
86:
87: private IFile getFile() {
88: if (this .file == null)
89: this .file = ResourcesPlugin.getWorkspace().getRoot()
90: .getFile(new Path(getPath()));
91: return this .file;
92: }
93:
94: public String toString() {
95: return "SearchDocument for " + getPath(); //$NON-NLS-1$
96: }
97: }
|