Mercurial > hg > eSciDocImport
comparison src/de/mpiwg/itgroup/eSciDoc/Tools/DRI/CheckAndAddDRIs.java @ 11:ecd66e70cb65
Tools for adding and changing DRIs in index.meta
author | dwinter |
---|---|
date | Thu, 26 Jul 2012 11:24:03 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
9:b6cf6462d709 | 11:ecd66e70cb65 |
---|---|
1 | |
2 package de.mpiwg.itgroup.eSciDoc.Tools.DRI; | |
3 | |
4 import java.awt.datatransfer.StringSelection; | |
5 import java.io.BufferedReader; | |
6 import java.io.BufferedWriter; | |
7 import java.io.DataInputStream; | |
8 import java.io.File; | |
9 import java.io.FileInputStream; | |
10 import java.io.FileNotFoundException; | |
11 import java.io.FileOutputStream; | |
12 import java.io.FileWriter; | |
13 import java.io.IOException; | |
14 import java.io.InputStreamReader; | |
15 import java.io.OutputStreamWriter; | |
16 import java.io.Writer; | |
17 import java.util.HashMap; | |
18 | |
19 import org.jdom.Document; | |
20 import org.jdom.Element; | |
21 import org.jdom.JDOMException; | |
22 import org.jdom.input.SAXBuilder; | |
23 import org.jdom.output.XMLOutputter; | |
24 import org.jdom.xpath.XPath; | |
25 | |
26 import de.mpiwg.itgroup.eSciDoc.Tools.EScidocTools; | |
27 import de.mpiwg.itgroup.eSciDoc.utils.EScidocNameSpaceContext; | |
28 | |
29 /** | |
30 * This package loads a list of filenames and the dris belonging to them as tab delimited list. | |
31 * It checks if the DRI are already in the index.meta, if not there are added, if there is wrong ID | |
32 * this is eithrer corrected or an warning message is generated | |
33 * @author dwinter | |
34 * | |
35 */ | |
36 public class CheckAndAddDRIs { | |
37 | |
38 public class DRI{ | |
39 | |
40 DRI(Document doc, String driString, Element driElement, boolean hasNameSpace){ | |
41 this.doc = doc; | |
42 this.driString=driString; | |
43 this.driElement=driElement; | |
44 this.hasNameSpace=hasNameSpace; | |
45 | |
46 | |
47 } | |
48 Document doc; | |
49 String driString; | |
50 Element driElement; | |
51 boolean hasNameSpace; | |
52 } | |
53 | |
54 HashMap<String,String> fileToDri; | |
55 String driType; | |
56 | |
57 public CheckAndAddDRIs(String fileName, String driType) throws IOException { | |
58 | |
59 this.driType=driType; | |
60 | |
61 FileInputStream fstream = new FileInputStream(fileName); | |
62 | |
63 DataInputStream in = new DataInputStream(fstream); | |
64 BufferedReader br = new BufferedReader(new InputStreamReader(in)); | |
65 String strLine; | |
66 | |
67 | |
68 // lies alle files and dris | |
69 fileToDri = new HashMap<String, String>(); | |
70 while ((strLine = br.readLine()) != null) { | |
71 String[] splitted= strLine.split(("\t")); | |
72 fileToDri.put(splitted[0], splitted[1]); | |
73 } | |
74 //Close the input stream | |
75 in.close(); | |
76 | |
77 } | |
78 | |
79 | |
80 /** | |
81 * Checks if the DRIs are in the index.meta | |
82 */ | |
83 private void doDRI(String mode) { | |
84 for (String key: fileToDri.keySet()){ | |
85 int result; | |
86 DRI dri; | |
87 try { | |
88 dri = getDRI(key); | |
89 result = checkDRI(dri,fileToDri.get(key)); | |
90 } catch (JDOMException e) { | |
91 System.err.println("jdomExecption:"+key); | |
92 continue; | |
93 | |
94 } catch (IOException e) { | |
95 System.err.println("IOError:"+key); | |
96 e.printStackTrace(); | |
97 continue; | |
98 | |
99 } | |
100 if (result==0){ | |
101 System.out.println(String.format("%s\t%s\tOK", key,fileToDri.get(key))); | |
102 } else if (result==1){ | |
103 if (mode.equals("check")){ | |
104 System.out.println(String.format("%s\t%s\tno DRI", key,fileToDri.get(key))); | |
105 } else { | |
106 try { | |
107 System.out.println(String.format("%s\t%s\tadding DRI", key,fileToDri.get(key))); | |
108 add(key,dri,fileToDri.get(key)); | |
109 } catch (JDOMException e) { | |
110 System.err.println("jdomExecption:"+key); | |
111 continue; | |
112 } catch (IOException e) { | |
113 | |
114 System.err.println("IOExecption:"+key); | |
115 } | |
116 } | |
117 } else if (result==2){ | |
118 | |
119 if (mode.equals("modify")){ | |
120 try { | |
121 modify(key,dri,fileToDri.get(key)); | |
122 } catch (IOException e) { | |
123 System.err.println("IOExecption:"+key); | |
124 } | |
125 } else { | |
126 System.out.println(String.format("%s\t%s\twrong DRI", key,fileToDri.get(key))); | |
127 } | |
128 } | |
129 | |
130 } | |
131 | |
132 | |
133 | |
134 | |
135 } | |
136 private void modify(String fileName, DRI dri, String driString) throws IOException { | |
137 dri.driElement.setText(driString); | |
138 writeIndexMeta(fileName, dri); | |
139 | |
140 } | |
141 | |
142 | |
143 private void add(String fileName, DRI dri, String driString) throws JDOMException, IOException { | |
144 XPath xp; | |
145 if (dri.hasNameSpace){ | |
146 xp = EScidocTools.getESciDocXpath("//mpiwg:meta"); | |
147 } else { | |
148 xp = EScidocTools.getESciDocXpath("//meta"); | |
149 } | |
150 Element meta = (Element)xp.selectSingleNode(dri.doc); | |
151 if (meta==null){ | |
152 System.err.println("NO metatag in:"+fileName); | |
153 return; | |
154 } | |
155 | |
156 Element el; | |
157 if (dri.hasNameSpace){ | |
158 el = new Element("dri",EScidocNameSpaceContext.MPIWG); | |
159 } | |
160 else { | |
161 el = new Element("dri"); | |
162 } | |
163 | |
164 el.setAttribute("type",this.driType); | |
165 el.addContent(driString); | |
166 meta.addContent(el); | |
167 | |
168 writeIndexMeta(fileName,dri); | |
169 } | |
170 | |
171 | |
172 private void writeIndexMeta(String fileName, DRI dri) throws IOException { | |
173 | |
174 File im = new File(fileName+"/index.meta"); | |
175 File old = new File(fileName+"/index.meta_old_DRI"); | |
176 | |
177 if (im.renameTo(old)){ | |
178 XMLOutputter out = new XMLOutputter(); | |
179 | |
180 String string = out.outputString(dri.doc); | |
181 Writer out1 = new BufferedWriter(new OutputStreamWriter( | |
182 new FileOutputStream(fileName+"/index.meta"), "UTF-8")); | |
183 | |
184 | |
185 out1.write(string); | |
186 out1.close(); | |
187 } else { | |
188 System.err.println("cannot rename:"+fileName); | |
189 } | |
190 | |
191 | |
192 | |
193 | |
194 // TODO Auto-generated method stub | |
195 | |
196 } | |
197 | |
198 | |
199 private int checkDRI(DRI dri, String driString) throws JDOMException, IOException { | |
200 | |
201 | |
202 if(dri.driString==null){ | |
203 return 1; | |
204 } else if (dri.driString.equals(driString)){ | |
205 return 0; | |
206 } | |
207 | |
208 return 2; | |
209 } | |
210 | |
211 | |
212 private DRI getDRI(String filename) throws JDOMException, IOException { | |
213 | |
214 | |
215 Document doc = readIndexMeta(filename); | |
216 | |
217 XPath xpath = EScidocTools.getESciDocXpath("//meta/dri[@type=\""+driType+"\"]"); | |
218 | |
219 | |
220 Element res = (Element)xpath.selectSingleNode(doc); | |
221 | |
222 boolean hasNameSpace=false; | |
223 //Try namespace | |
224 if (res == null){ | |
225 xpath = EScidocTools.getESciDocXpath("//mpiwg:meta/mpiwg:dri[@type=\""+driType+"\"]"); | |
226 res = (Element)xpath.selectSingleNode(doc); | |
227 | |
228 if (res==null) | |
229 return new DRI(doc,null,null,false); | |
230 else { | |
231 hasNameSpace=true; | |
232 } | |
233 } | |
234 | |
235 String txt=res.getTextTrim(); | |
236 | |
237 | |
238 | |
239 return new DRI(doc, txt, res, hasNameSpace); | |
240 } | |
241 | |
242 | |
243 private Document readIndexMeta(String filename) throws JDOMException, IOException { | |
244 SAXBuilder builder = new SAXBuilder(); | |
245 Document doc = builder.build(new File(filename+"/index.meta")); | |
246 return doc; | |
247 } | |
248 | |
249 | |
250 /** | |
251 * @param args | |
252 * @throws IOException | |
253 */ | |
254 public static void main(String[] args) throws IOException { | |
255 if (args.length <3){ | |
256 System.out.print("USAGE: command filename dri-type"); | |
257 System.exit(0); | |
258 } | |
259 | |
260 String command = args[0]; | |
261 String fileName = args[1]; | |
262 String driType = args[2]; | |
263 | |
264 final CheckAndAddDRIs ck = new CheckAndAddDRIs(fileName,driType); | |
265 | |
266 if (command.equals("check")){ | |
267 System.out.println("only checking"); | |
268 ck.doDRI("check"); | |
269 } else if (command.equals("add")){ | |
270 System.out.println("only adding"); | |
271 ck.doDRI("add"); | |
272 } else if (command.equals("modify")){ | |
273 System.out.println("adding and modify"); | |
274 ck.doDRI("modify"); | |
275 } | |
276 else { | |
277 System.out.println("Only the commands: check, add or modify are allowed"); | |
278 } | |
279 | |
280 | |
281 } | |
282 | |
283 | |
284 | |
285 | |
286 | |
287 | |
288 } |