1 | package org.greenstone.gsdl3_extension.mat.servlet;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.PrintWriter;
|
---|
5 | import java.util.ArrayList;
|
---|
6 | import java.util.HashMap;
|
---|
7 |
|
---|
8 | import javax.xml.parsers.DocumentBuilder;
|
---|
9 | import javax.xml.parsers.DocumentBuilderFactory;
|
---|
10 |
|
---|
11 | import org.w3c.dom.Document;
|
---|
12 | import org.w3c.dom.Element;
|
---|
13 | import org.w3c.dom.NamedNodeMap;
|
---|
14 | import org.w3c.dom.Node;
|
---|
15 | import org.w3c.dom.NodeList;
|
---|
16 |
|
---|
17 | public class SearchLink {
|
---|
18 |
|
---|
19 | private String path = "";
|
---|
20 | private boolean status = true;
|
---|
21 | protected final String DocumentNode = "Document";
|
---|
22 | protected final String ActualValueNode = "ActualValue";
|
---|
23 |
|
---|
24 | public SearchLink(String arg1){
|
---|
25 | path = arg1;
|
---|
26 | }
|
---|
27 |
|
---|
28 | public SearchLink(){}
|
---|
29 |
|
---|
30 | public boolean getStatus(){
|
---|
31 | return status;
|
---|
32 | }
|
---|
33 |
|
---|
34 | public ArrayList CreateIndentifierLinkPage(String metadataElement, String text, String identifier, String col, HashMap valueMap, HashMap linkMap){
|
---|
35 |
|
---|
36 | if(valueMap.size()==0){
|
---|
37 | status = false;
|
---|
38 | return new ArrayList();
|
---|
39 | }
|
---|
40 | else{
|
---|
41 | ArrayList alist = retrieveHASHID(valueMap,text);
|
---|
42 | if(alist.size()==0){
|
---|
43 | status = false;
|
---|
44 | return new ArrayList();
|
---|
45 | }
|
---|
46 |
|
---|
47 | if(linkMap.size()==0){
|
---|
48 | status = false;
|
---|
49 | return new ArrayList();
|
---|
50 | }
|
---|
51 | else{
|
---|
52 | ArrayList alist2 = retrieveHASHID(linkMap,alist);
|
---|
53 | return alist2;
|
---|
54 | }
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 | private Element openfile(String core_element, String collection){
|
---|
60 |
|
---|
61 | try{
|
---|
62 | DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
|
---|
63 | DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
|
---|
64 | Document doc = docBuilder.newDocument();
|
---|
65 |
|
---|
66 | doc = docBuilder.parse (new File(path+core_element+".xml"));
|
---|
67 | Element rootNode = doc.getDocumentElement();
|
---|
68 | return rootNode;
|
---|
69 | }catch (Exception e) {
|
---|
70 | System.out.println(core_element + " does not exist");
|
---|
71 | return null;
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | private ArrayList retrieveHASHID (HashMap hp, String text){
|
---|
76 | if(hp.containsKey(text)){
|
---|
77 | InternalLink il = (InternalLink) hp.get(text);
|
---|
78 | return il.retrieveList();
|
---|
79 | }
|
---|
80 | else{
|
---|
81 | return new ArrayList();
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | private ArrayList retrieveHASHID (HashMap hp, ArrayList arg){
|
---|
86 |
|
---|
87 | ArrayList alist = new ArrayList();
|
---|
88 | for(int i = 0; i<arg.size(); i++){
|
---|
89 | String text = (String)arg.get(i);
|
---|
90 | if(hp.containsKey(text)){
|
---|
91 | InternalLink il = (InternalLink) hp.get(text);
|
---|
92 | ArrayList arrayList = il.retrieveList();
|
---|
93 | if(arrayList.size()!=0){
|
---|
94 | alist.add(il.retrieveList().get(0));
|
---|
95 | }
|
---|
96 | else{
|
---|
97 | alist.add(" ");
|
---|
98 | }
|
---|
99 | }
|
---|
100 | }
|
---|
101 | return alist;
|
---|
102 | }
|
---|
103 |
|
---|
104 | public HashMap createValueMap(String core_element, String collection){
|
---|
105 |
|
---|
106 | HashMap hp = new HashMap();
|
---|
107 | Element ex = openfile(core_element,collection);
|
---|
108 | NodeList valueList = ex.getElementsByTagName(ActualValueNode);
|
---|
109 |
|
---|
110 | for(int s=0; s<valueList.getLength() ; s++){
|
---|
111 | Element docNode = (Element)valueList.item(s);
|
---|
112 | NodeList NodeIDMap = docNode.getChildNodes();
|
---|
113 | Node DocNodeID = NodeIDMap.item(0);
|
---|
114 | String actualValue = DocNodeID.getNodeValue();
|
---|
115 | String DocID = DocNodeID.getParentNode().getParentNode().getAttributes().item(0).getNodeValue();
|
---|
116 |
|
---|
117 | if(hp.containsKey(actualValue)){
|
---|
118 | InternalLink il = (InternalLink) hp.get(actualValue);
|
---|
119 | il.increaseElement(DocID);
|
---|
120 | hp.put(actualValue, il);
|
---|
121 | }
|
---|
122 | else{
|
---|
123 | InternalLink il = new InternalLink();
|
---|
124 | il.setValue(actualValue);
|
---|
125 | il.increaseElement(DocID);
|
---|
126 | hp.put(actualValue, il);
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | return hp;
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | public HashMap createLinkMap (String core_element, String collection){
|
---|
135 |
|
---|
136 | HashMap hp = new HashMap();
|
---|
137 | Element ex = openfile(core_element,collection);
|
---|
138 | NodeList listOfFrequency = ex.getElementsByTagName(DocumentNode);
|
---|
139 |
|
---|
140 | if(listOfFrequency.getLength()==0){
|
---|
141 | return hp;
|
---|
142 | }
|
---|
143 |
|
---|
144 | for(int s=0; s<listOfFrequency.getLength() ; s++){
|
---|
145 | Node docNode = listOfFrequency.item(s);
|
---|
146 | NamedNodeMap NodeIDMap = docNode.getAttributes();
|
---|
147 | Node DocNodeID = NodeIDMap.item(0);
|
---|
148 | String DocID = DocNodeID.getNodeValue();
|
---|
149 |
|
---|
150 |
|
---|
151 | if(hp.containsKey(DocID)){
|
---|
152 |
|
---|
153 | Element xNode = (Element)docNode;
|
---|
154 | NodeList valueList = xNode.getElementsByTagName(ActualValueNode);
|
---|
155 |
|
---|
156 | ArrayList alist = new ArrayList();
|
---|
157 | for(int y = 0; y<valueList.getLength(); y++){
|
---|
158 | Element valueElement = (Element)valueList.item(y);
|
---|
159 | NodeList textFNList = valueElement.getChildNodes();
|
---|
160 | String textValue = ((Node)textFNList.item(0)).getNodeValue();
|
---|
161 |
|
---|
162 | if(textValue.startsWith("http://")){
|
---|
163 | InternalLink il = (InternalLink)hp.get(DocID);
|
---|
164 | il.increaseElement(textValue);
|
---|
165 | break;
|
---|
166 | }
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | else {
|
---|
172 | InternalLink il = new InternalLink();
|
---|
173 | il.setValue(DocID);
|
---|
174 |
|
---|
175 | Element xNode = (Element)docNode;
|
---|
176 | NodeList valueList = xNode.getElementsByTagName(ActualValueNode);
|
---|
177 |
|
---|
178 | for(int y = 0; y<valueList.getLength(); y++){
|
---|
179 | Element valueElement = (Element)valueList.item(y);
|
---|
180 | NodeList textFNList = valueElement.getChildNodes();
|
---|
181 | String textValue = ((Node)textFNList.item(0)).getNodeValue();
|
---|
182 |
|
---|
183 | if(textValue.startsWith("http://")){
|
---|
184 | il.increaseElement(textValue);
|
---|
185 | break;
|
---|
186 | }
|
---|
187 | }
|
---|
188 | hp.put(DocID, il);
|
---|
189 | }
|
---|
190 | }
|
---|
191 | return hp;
|
---|
192 | }
|
---|
193 |
|
---|
194 |
|
---|
195 |
|
---|
196 |
|
---|
197 |
|
---|
198 |
|
---|
199 |
|
---|
200 |
|
---|
201 |
|
---|
202 |
|
---|
203 |
|
---|
204 |
|
---|
205 |
|
---|
206 |
|
---|
207 |
|
---|
208 |
|
---|
209 |
|
---|
210 |
|
---|
211 |
|
---|
212 |
|
---|
213 |
|
---|
214 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
---|
215 | }
|
---|