1 package org.catacomb.druid.load;
2
3
4 import java.util.ArrayList;
5
6 import org.catacomb.report.E;
7 import org.catacomb.druid.manifest.DecFile;
8 import org.catacomb.druid.manifest.DecManifest;
9
10 import java.io.File;
11 import java.util.HashMap;
12
13
14 public class XMLStore {
15
16 private final static String MULTICODE = "___MULTI";
17
18 private HashMap<String, String> shortToFullID;
19 private HashMap<String, DecFile> fullIDToSource;
20
21 private HashMap<String, ArrayList<String>> shortToFullMulti;
22
23
24 public XMLStore() {
25 shortToFullID = new HashMap<String, String>();
26 fullIDToSource = new HashMap<String, DecFile>();
27 shortToFullMulti = new HashMap<String, ArrayList<String>>();
28 }
29
30
31
32 public void addClasspathManifest(DecManifest manifest) {
33 for (DecFile df : manifest.getFiles()) {
34 df.setClasspathAccess();
35 addSource(df);
36 }
37 }
38
39
40
41 public void addFileSystemManifest(DecManifest manifest, File fdir) {
42 String cproot = manifest.getRootPath();
43 for (DecFile df : manifest.getFiles()) {
44 df.setFileSystemAccess(fdir, cproot);
45 addSource(df);
46 }
47 }
48
49
50 public boolean containsSource(String fnm) {
51 boolean ret = false;
52 if (shortToFullID.containsKey(fnm) ||
53 fullIDToSource.containsKey(fnm)) {
54 ret = true;
55 }
56 return ret;
57 }
58
59
60 public boolean hasMultipleSources(String locator) {
61 boolean ret = false;
62 if (shortToFullID.containsKey(locator)) {
63 String v = shortToFullID.get(locator);
64 if (v.equals(MULTICODE)) {
65 ret = true;
66 }
67 }
68 return ret;
69 }
70
71
72 public ArrayList<DecFile> getSources(String locator) {
73 ArrayList<DecFile> ret = new ArrayList<DecFile>();
74 for (String path : shortToFullMulti.get(locator)) {
75 ret.add(fullIDToSource.get(path));
76 }
77 return ret;
78 }
79
80
81
82 public DecFile getSource(String locator) {
83 DecFile ret = null;
84
85 if (shortToFullMulti.containsKey(locator)) {
86 E.error("multiple possible sources for " + locator + " use getSources");
87 return null;
88 }
89
90 if (shortToFullID.containsKey(locator)) {
91 String path = shortToFullID.get(locator);
92 ret = fullIDToSource.get(path);
93
94 } else if (fullIDToSource.containsKey(locator)) {
95 ret = fullIDToSource.get(locator);
96
97 } else {
98 E.error("cant find resource at " + locator);
99 dumpStore();
100 }
101 return ret;
102 }
103
104
105 private void dumpStore() {
106 E.info("Known names:");
107 for (String sk : shortToFullID.keySet()) {
108 E.info(" " + sk);
109 }
110 E.info("Known names:");
111 for (String sk : shortToFullID.keySet()) {
112 E.info(" " + sk);
113 }
114 }
115
116
117
118
119 public void addSource(DecFile decFile) {
120 String dfnm = decFile.getName();
121 String fullid = decFile.getFullID();
122
123 if (shortToFullID.containsKey(dfnm)) {
124 if (shortToFullMulti.containsKey(dfnm)) {
125 shortToFullMulti.get(dfnm).add(fullid);
126
127 } else {
128 ArrayList<String> al = new ArrayList<String>();
129 al.add(shortToFullID.get(dfnm));
130 al.add(fullid);
131 shortToFullMulti.put(dfnm, al);
132
133 shortToFullID.put(dfnm, MULTICODE);
134 }
135
136 } else {
137 shortToFullID.put(dfnm, fullid);
138 }
139 fullIDToSource.put(fullid, decFile);
140 }
141
142
143
144 public void newSourceFile(File f, File rootFolder) {
145 DecFile df = new DecFile(f, rootFolder);
146 addSource(df);
147 }
148
149
150 }