Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7c68f37462f03dbd275020116308a941ca95bacb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*******************************************************************************
 * Copyright (c) 2006, 2015 Symbian Software Ltd. and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Andrew Ferguson (Symbian) - initial API and implementation
 *     Markus Schorn (Wind River Systems)
 *     Karsten Thoms (itemis) - Bug 471103
 *******************************************************************************/
 package org.eclipse.cdt.internal.core.pdom.dom;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.cdt.core.index.IIndexFileLocation;
import org.eclipse.cdt.core.index.IIndexLocationConverter;
import org.eclipse.cdt.internal.core.index.IndexFileLocation;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;

/**
 * The standard location converter used by the per-project PDOM
 */
public class PDOMProjectIndexLocationConverter implements IIndexLocationConverter {
	private static final String EXTERNAL = "<EXT>"; //$NON-NLS-1$
	private static final String WS = "<WS>"; //$NON-NLS-1$

	final private IWorkspaceRoot fRoot;
	final private String fFullPathPrefix;
	final private boolean fIgnoreExternal;
	// Cache for results of method fromInternalFormat (bug 471103).
	final Map<String, IIndexFileLocation> fromInternalFormatCache = new HashMap<>();

	public PDOMProjectIndexLocationConverter(IProject project) {
		this(project, false);
	}

	public PDOMProjectIndexLocationConverter(IProject project, boolean ignoreWSExternal) {
		fRoot= (IWorkspaceRoot) project.getParent();
		fFullPathPrefix= project.getFullPath().toString() + IPath.SEPARATOR;
		fIgnoreExternal= ignoreWSExternal;
	}

	@Override
	public IIndexFileLocation fromInternalFormat(String raw) {
		// Fast return when 'raw' was queried before (bug 471103).
		IIndexFileLocation cachedResult = fromInternalFormatCache.get(raw);
		if (cachedResult != null) {
			return cachedResult;
		}

		String fullPath = null;
		URI uri= null;
		if (raw.startsWith(EXTERNAL)) {
			try {
				uri = new URI(raw.substring(EXTERNAL.length()));
			} catch (URISyntaxException e) {
			}
		} else {
			if (raw.startsWith(WS)) {
				fullPath= raw.substring(WS.length());
			} else {
				fullPath= fFullPathPrefix + raw;
			}
			final IPath path= new Path(fullPath);
			if (path.segmentCount() > 1) {
				IResource member= fRoot.getFile(path);
				uri = member.getLocationURI();
			}
		}
		if (uri == null)
			return null;

		IndexFileLocation location = new IndexFileLocation(uri, fullPath);
		fromInternalFormatCache.put(raw, location);
		return location;
	}

	@Override
	public String toInternalFormat(IIndexFileLocation location) {
		String fullPath= location.getFullPath();
		if (fullPath != null) {
			if (fullPath.startsWith(fFullPathPrefix)) {
				return fullPath.substring(fFullPathPrefix.length());
			}
			return WS + fullPath;
		}

		if (fIgnoreExternal)
			return null;

		return EXTERNAL + location.getURI().toString();
	}
}

Back to the top