Mercurial > hg > IndexMetaContextualizer
comparison src/main/java/de/mpiwg/indexmeta/IndexMetaParser.java @ 8:9ce7979fd037
Implementation Data Provider
author | Jorge Urzua <jurzua@mpiwg-berlin.mpg.de> |
---|---|
date | Wed, 24 Apr 2013 10:34:23 +0200 |
parents | 7d231e4e86e5 |
children |
comparison
equal
deleted
inserted
replaced
7:bc57f2660b0f | 8:9ce7979fd037 |
---|---|
1 package de.mpiwg.indexmeta; | 1 package de.mpiwg.indexmeta; |
2 | 2 |
3 import java.io.File; | 3 import java.io.File; |
4 import java.io.FilenameFilter; | |
4 import java.util.ArrayList; | 5 import java.util.ArrayList; |
5 import java.util.List; | 6 import java.util.List; |
6 | 7 |
7 import javax.xml.parsers.DocumentBuilder; | 8 import javax.xml.parsers.DocumentBuilder; |
8 import javax.xml.parsers.DocumentBuilderFactory; | 9 import javax.xml.parsers.DocumentBuilderFactory; |
9 import javax.xml.xpath.XPath; | 10 import javax.xml.xpath.XPath; |
10 import javax.xml.xpath.XPathConstants; | 11 import javax.xml.xpath.XPathConstants; |
11 import javax.xml.xpath.XPathExpression; | 12 import javax.xml.xpath.XPathExpression; |
12 import javax.xml.xpath.XPathFactory; | 13 import javax.xml.xpath.XPathFactory; |
13 | 14 |
15 import org.apache.commons.lang.StringUtils; | |
14 import org.w3c.dom.Document; | 16 import org.w3c.dom.Document; |
15 import org.w3c.dom.Element; | 17 import org.w3c.dom.Element; |
16 import org.w3c.dom.Node; | 18 import org.w3c.dom.Node; |
17 import org.w3c.dom.NodeList; | 19 import org.w3c.dom.NodeList; |
18 | 20 |
19 import de.mpiwg.indexmeta.bo.Contextualization; | 21 import de.mpiwg.indexmeta.bo.Contextualization; |
20 | 22 |
21 public class IndexMetaParser { | 23 public class IndexMetaParser { |
22 | 24 |
23 | 25 public static String indexMetaFile = "index.meta"; |
26 public static String annotatedIndexMetaFile = "index.meta.annot"; | |
27 | |
28 /* | |
24 public static List<Contextualization> getCtxItems(String filePath){ | 29 public static List<Contextualization> getCtxItems(String filePath){ |
25 List<Contextualization> rs = new ArrayList<Contextualization>(); | 30 List<Contextualization> rs = new ArrayList<Contextualization>(); |
26 | 31 |
27 try { | 32 try { |
28 File file = new File("/Users/jurzua/Projects/workspace/contextualization/data/index.meta/01index.meta.anno.xml"); | 33 File file = new File("/Users/jurzua/Projects/workspace/contextualization/data/index.meta/01index.meta.anno.xml"); |
29 | 34 |
35 if(file.isFile()){ | |
36 rs = getCtxItemsFromFile(file, rs); | |
37 }else if(file.isDirectory()){ | |
38 rs = getCtxItemsFromDirectory(file, rs); | |
39 } | |
40 | |
41 } catch (Exception e) { | |
42 e.printStackTrace(); | |
43 } | |
44 | |
45 return rs; | |
46 } | |
47 */ | |
48 | |
49 public static List<Contextualization> getCtxFromDirectory(String directory){ | |
50 List<Contextualization> rs = new ArrayList<Contextualization>(); | |
51 | |
52 try { | |
53 File file = new File(directory); | |
54 rs = getCtxItemsFromDirectory(file, rs); | |
55 } catch (Exception e) { | |
56 e.printStackTrace(); | |
57 } | |
58 | |
59 return rs; | |
60 } | |
61 | |
62 public static List<Contextualization> getCtxFromResource(String indexMetaResource){ | |
63 List<Contextualization> rs = new ArrayList<Contextualization>(); | |
64 | |
65 try { | |
66 File file = new File(indexMetaResource); | |
67 rs = getCtxItemsFromIndexMetaResource(file, rs); | |
68 } catch (Exception e) { | |
69 e.printStackTrace(); | |
70 } | |
71 | |
72 return rs; | |
73 } | |
74 | |
75 | |
76 | |
77 private static List<Contextualization> getCtxItemsFromDirectory(File directory, List<Contextualization> rs) throws Exception{ | |
78 String canonicalPath = directory.getCanonicalPath(); | |
79 System.out.println(canonicalPath); | |
80 | |
81 File[] files = directory.listFiles(); | |
82 | |
83 for(File indexMetaResource : files){ | |
84 | |
85 if(indexMetaResource.isDirectory()){ | |
86 rs = getCtxItemsFromIndexMetaResource(indexMetaResource, rs); | |
87 } | |
88 } | |
89 return rs; | |
90 } | |
91 | |
92 private static List<Contextualization> getCtxItemsFromIndexMetaResource(File indexMetaResource, List<Contextualization> rs){ | |
93 | |
94 System.out.print("Working on: " + indexMetaResource.getName()); | |
95 | |
96 File[] list0 = indexMetaResource.listFiles(indexMetaFilter); | |
97 File annotatedFile = null; | |
98 | |
99 if(list0.length != 0){ | |
100 System.out.print("\tFound: " + indexMetaFile); | |
101 File[] list1 = indexMetaResource.listFiles(annotatedIndexMetaFilter); | |
102 if(list1.length == 0){ | |
103 //create annotated file | |
104 System.out.print("\tAnnotated no found"); | |
105 try{ | |
106 annotatedFile = AnnotateIndexMeta.xmlParse(list0[0].getAbsolutePath(), null); | |
107 }catch (Exception e) { | |
108 e.printStackTrace(); | |
109 } | |
110 | |
111 }else{ | |
112 annotatedFile = list1[0]; | |
113 } | |
114 } | |
115 rs = getCtxItemsFromFile(annotatedFile, rs, indexMetaResource.getName()); | |
116 | |
117 System.out.println(); | |
118 | |
119 return rs; | |
120 } | |
121 | |
122 private static List<Contextualization> getCtxItemsFromFile(File file, List<Contextualization> rs, String indexMetaId){ | |
123 | |
124 try { | |
30 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | 125 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
31 DocumentBuilder builder = factory.newDocumentBuilder(); | 126 DocumentBuilder builder = factory.newDocumentBuilder(); |
32 Document doc = builder.parse(file); | 127 Document doc = builder.parse(file); |
33 | 128 |
129 | |
130 for(String ctxElement : Contextualization.contextualizableList){} | |
131 | |
34 List<Node> nodes = getNodeList(doc, Contextualization.AUTHOR); | 132 List<Node> nodes = getNodeList(doc, Contextualization.AUTHOR); |
35 for(Node node : nodes){ | 133 for(Node node : nodes){ |
36 Contextualization ctx = getCtx(node, "XX"); | 134 Contextualization ctx = getCtx(node, indexMetaId); |
37 if(ctx != null){ | 135 if(ctx != null){ |
38 rs.add(ctx); | 136 rs.add(ctx); |
39 } | 137 } |
40 } | 138 } |
41 | 139 |
42 nodes = getNodeList(doc, Contextualization.CITY); | 140 nodes = getNodeList(doc, Contextualization.CITY); |
43 for(Node node : nodes){ | 141 for(Node node : nodes){ |
44 Contextualization ctx = getCtx(node, "XX"); | 142 Contextualization ctx = getCtx(node, indexMetaId); |
45 if(ctx != null){ | 143 if(ctx != null){ |
46 rs.add(ctx); | 144 rs.add(ctx); |
47 } | 145 } |
48 } | 146 } |
49 | 147 } catch (Exception e) { |
50 for(Contextualization ctx : rs){ | 148 e.printStackTrace(); |
51 System.out.println(ctx.toString()); | 149 } |
52 } | 150 |
53 | 151 |
54 } catch (Exception e) { | 152 return rs; |
55 e.printStackTrace(); | 153 } |
56 } | 154 |
57 | |
58 return rs; | |
59 } | |
60 | 155 |
61 public static List<Node> getNodeList(Document doc, String tagName){ | 156 public static List<Node> getNodeList(Document doc, String tagName){ |
62 List<Node> rs = new ArrayList<Node>(); | 157 List<Node> rs = new ArrayList<Node>(); |
63 | 158 |
64 try { | 159 try { |
104 return null; | 199 return null; |
105 } | 200 } |
106 | 201 |
107 public static void main(String[] args){ | 202 public static void main(String[] args){ |
108 | 203 |
109 getCtxItems("/Users/jurzua/Projects/workspace/contextualization/data/index.meta/01index.meta"); | 204 //--/Volumes/online_permanent/library |
205 //getCtxItems("/Users/jurzua/Projects/workspace/contextualization/data/index.meta/01index.meta"); | |
206 List<Contextualization> rs = getCtxFromDirectory("/Users/jurzua/Projects/max-planck/index_meta/library"); | |
207 //List<Contextualization> rs = getCtxFromResource("/Users/jurzua/Projects/max-planck/index_meta/library/BB1RH90M"); | |
208 | |
209 for(Contextualization ctx : rs){ | |
210 System.out.println(ctx.toString()); | |
211 } | |
212 | |
110 } | 213 } |
111 | 214 |
112 public static void printXpathResult(Object result){ | 215 public static void printXpathResult(Object result){ |
113 NodeList nodes = (NodeList) result; | 216 NodeList nodes = (NodeList) result; |
114 for (int i = 0; i < nodes.getLength(); i++) { | 217 for (int i = 0; i < nodes.getLength(); i++) { |
122 System.out.println("getFirstChild value= " + node.getFirstChild().getNodeValue()); | 225 System.out.println("getFirstChild value= " + node.getFirstChild().getNodeValue()); |
123 System.out.println(node); | 226 System.out.println(node); |
124 } | 227 } |
125 } | 228 } |
126 } | 229 } |
230 | |
231 private static FilenameFilter indexMetaFilter = new FilenameFilter() { | |
232 public boolean accept(File directory, String fileName) { | |
233 return StringUtils.equals(fileName, indexMetaFile); | |
234 } | |
235 }; | |
236 | |
237 private static FilenameFilter annotatedIndexMetaFilter = new FilenameFilter() { | |
238 public boolean accept(File directory, String fileName) { | |
239 return StringUtils.equals(fileName, annotatedIndexMetaFile); | |
240 } | |
241 }; | |
127 } | 242 } |