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
|
/*******************************************************************************
* Copyright (c) 2013 Stefan Seelmann and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Stefan Seelmann - initial API and implementation
*******************************************************************************/
package org.eclipse.mylyn.internal.wikitext.markdown.core;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.eclipse.mylyn.wikitext.core.parser.Locator;
/**
* This class tracks which link definitions are used, unused, and missing.
*
* @author Stefan Seelmann
*/
public class LinkDefinitionUsageTracker {
private final Locator locator;
private final LinkDefinitionParser linkDefinitionParser;
private final Set<String> usedLinkDefinitionIds;
private final List<Position> missingLinkDefinitionPositions;
public LinkDefinitionUsageTracker(Locator locator, LinkDefinitionParser linkDefinitionParser) {
this.locator = locator;
this.linkDefinitionParser = linkDefinitionParser;
usedLinkDefinitionIds = new HashSet<String>();
missingLinkDefinitionPositions = new ArrayList<LinkDefinitionUsageTracker.Position>();
}
public void linkDefinitionRequested(String id) {
// ignore
LinkDefinition linkDefinition = linkDefinitionParser.getLinkDefinition(id);
if (linkDefinition == null) {
int offset = locator.getDocumentOffset();
int length = locator.getLineSegmentEndOffset() - locator.getLineCharacterOffset();
Position position = new Position(id, offset, length);
missingLinkDefinitionPositions.add(position);
} else {
usedLinkDefinitionIds.add(id.toLowerCase());
}
}
public List<Position> getUnusedLinkDefinitionPositions() {
List<Position> usedLinkDefinitionPositions = new ArrayList<Position>();
Map<String, LinkDefinition> linkDefinitions = linkDefinitionParser.getLinkDefinitions();
for (Entry<String, LinkDefinition> entry : linkDefinitions.entrySet()) {
String id = entry.getKey();
if (!usedLinkDefinitionIds.contains(id.toLowerCase())) {
LinkDefinition linkDefinition = entry.getValue();
Position position = new Position(linkDefinition.getId(), linkDefinition.getOffset(),
linkDefinition.getLength());
usedLinkDefinitionPositions.add(position);
}
}
return usedLinkDefinitionPositions;
}
public List<Position> getMissingLinkDefinitionPositions() {
return missingLinkDefinitionPositions;
}
public class Position {
private final String id;
int offset;
int length;
public Position(String id, int offset, int length) {
this.id = id;
this.offset = offset;
this.length = length;
}
public String getId() {
return id;
}
public int getOffset() {
return offset;
}
public int getLength() {
return length;
}
}
}
|