source: gs3-extensions/mat/trunk/src/org/greenstone/mat/SearchLink.java@ 21927

Last change on this file since 21927 was 21927, checked in by sjm84, 14 years ago

Renamed package to org.greenstone.mat from org.greenstone.gsdl3_extension.mat

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