1 | private void scanStrings(BibtexDatabase inMem, BibtexDatabase onTmp, BibtexDatabase onDisk) { | | 1 | private BibtexString findString(BibtexDatabase base, String name, HashSet<Object> used) { |
2 | int nTmp = onTmp.getStringCount(), | | 2 | if (!base.hasStringLabel(name)) |
3 | nDisk = onDisk.getStringCount(); | | 3 | return null; |
4 | if ((nTmp == 0) && (nDisk == 0)) | | 4 | for (Iterator<String> i=base.getStringKeySet().iterator(); i.hasNext();) { |
5 | return; | | 5 | String key = i.next(); |
6 | | | 6 | BibtexString bs = base.getString(key); |
7 | HashSet<Object> used = new HashSet<Object>(); | | 7 | if (bs.getName().equals(name) && !used.contains(key)) { |
8 | HashSet<Object> usedInMem = new HashSet<Object>(); | | 8 | used.add(key); |
9 | HashSet<String> notMatched = new HashSet<String>(onTmp.getStringCount()); | | 9 | return bs; |
10 | | | 10 | } |
11 | // First try to match by string names. | | 11 | } |
12 | //int piv2 = -1; | | 12 | return null; |
13 | mainLoop: for (String key : onTmp.getStringKeySet()){ | | 13 | } |
14 | BibtexString tmp = onTmp.getString(key); | | | |
15 | | | | |
16 | // for (int j=piv2+1; j<nDisk; j++) | | | |
17 | for (String diskId : onDisk.getStringKeySet()){ | | | |
18 | if (!used.contains(diskId)) { | | | |
19 | BibtexString disk = onDisk.getString(diskId); | | | |
20 | if (disk.getName().equals(tmp.getName())) { | | | |
21 | // We have found a string with a matching name. | | | |
22 | if ((tmp.getContent() != null) && !tmp.getContent().equals(disk.getContent())) { | | | |
23 | // But they have nonmatching contents, so we've found a change. | | | |
24 | BibtexString mem = findString(inMem, tmp.getName(), usedInMem); | | | |
25 | if (mem != null) | | | |
26 | changes.add(new StringChange(mem, tmp, tmp.getName(), | | | |
27 | mem.getContent(), | | | |
28 | tmp.getContent(), disk.getContent())); | | | |
29 | else | | | |
30 | changes.add(new StringChange(null, tmp, tmp.getName(), null, tmp.getContent(), disk.getContent())); | | | |
31 | } | | | |
32 | used.add(diskId); | | | |
33 | //if (j==piv2) | | | |
34 | // piv2++; | | | |
35 | continue mainLoop; | | | |
36 | } | | | |
37 | | | | |
38 | } | | | |
39 | } | | | |
40 | // If we get here, there was no match for this string. | | | |
41 | notMatched.add(tmp.getId()); | | | |
42 | } | | | |
43 | | | | |
44 | // See if we can detect a name change for those entries that we couldn't match. | | | |
45 | if (notMatched.size() > 0) { | | | |
46 | for (Iterator<String> i = notMatched.iterator(); i.hasNext();){ | | | |
47 | BibtexString tmp = onTmp.getString(i.next()); | | | |
48 | | | | |
49 | // If we get to this point, we found no string with matching name. See if we | | | |
50 | // can find one with matching content. | | | |
51 | for (String diskId : onDisk.getStringKeySet()){ | | | |
52 | | | | |
53 | if (!used.contains(diskId)) { | | | |
54 | BibtexString disk = onDisk.getString(diskId); | | | |
55 | | | | |
56 | if (disk.getContent().equals(tmp.getContent())) { | | | |
57 | // We have found a string with the same content. It cannot have the same | | | |
58 | // name, or we would have found it above. | | | |
59 | | | | |
60 | // Try to find the matching one in memory: | | | |
61 | BibtexString bsMem = null; | | | |
62 | | | | |
63 | for (String memId : inMem.getStringKeySet()){ | | | |
64 | BibtexString bsMem_cand = inMem.getString(memId); | | | |
65 | if (bsMem_cand.getContent().equals(disk.getContent()) && | | | |
66 | !usedInMem.contains(memId)) { | | | |
67 | usedInMem.add(memId); | | | |
68 | bsMem = bsMem_cand; | | | |
69 | break; | | | |
70 | } | | | |
71 | } | | | |
72 | | | | |
73 | changes.add(new StringNameChange(bsMem, tmp, bsMem.getName(), | | | |
74 | tmp.getName(), disk.getName(), | | | |
75 | tmp.getContent())); | | | |
76 | i.remove(); | | | |
77 | used.add(diskId); | | | |
78 | | | | |
79 | } | | | |
80 | } | | | |
81 | } | | | |
82 | } | | | |
83 | } | | | |
84 | | | | |
85 | if (notMatched.size() > 0) { | | | |
86 | // Still one or more non-matched strings. So they must have been removed. | | | |
87 | for (Iterator<String> i = notMatched.iterator(); i.hasNext(); ) { | | | |
88 | String nmId = i.next(); | | | |
89 | BibtexString tmp = onTmp.getString(nmId); | | | |
90 | BibtexString mem = findString(inMem, tmp.getName(), usedInMem); | | | |
91 | if (mem != null) { // The removed string is not removed from the mem version. | | | |
92 | changes.add(new StringRemoveChange(tmp, tmp, mem)); | | | |
93 | } | | | |
94 | } | | | |
95 | } | | | |
96 | | | | |
97 | | | | |
98 | // Finally, see if there are remaining strings in the disk database. They | | | |
99 | // must have been added. | | | |
100 | for (Iterator<String> i=onDisk.getStringKeySet().iterator(); i.hasNext();) { | | | |
101 | String diskId = i.next(); | | | |
102 | if (!used.contains(diskId)) { | | | |
103 | BibtexString disk = onDisk.getString(diskId); | | | |
104 | //System.out.println(disk.getName()); | | | |
105 | used.add(diskId); | | | |
106 | changes.add(new StringAddChange(disk)); | | | |
107 | } | | | |
108 | } | | | |
109 | } | | | |