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