comparison src/main/java/de/mpiwg/indexmeta/IndexMetaParser.java @ 0:dfce13a5f5f9

nit project!
author Jorge Urzua <jurzua@mpiwg-berlin.mpg.de>
date Thu, 11 Apr 2013 15:25:26 +0200
parents
children 7d231e4e86e5
comparison
equal deleted inserted replaced
-1:000000000000 0:dfce13a5f5f9
1 package de.mpiwg.indexmeta;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import javax.xml.parsers.DocumentBuilder;
8 import javax.xml.parsers.DocumentBuilderFactory;
9 import javax.xml.xpath.XPath;
10 import javax.xml.xpath.XPathConstants;
11 import javax.xml.xpath.XPathExpression;
12 import javax.xml.xpath.XPathFactory;
13
14 import org.w3c.dom.Document;
15 import org.w3c.dom.Element;
16 import org.w3c.dom.Node;
17 import org.w3c.dom.NodeList;
18
19 import de.mpiwg.indexmeta.bo.Contextualization;
20
21 public class IndexMetaParser {
22
23
24 public static List<Contextualization> getCtxItems(String filePath){
25 List<Contextualization> rs = new ArrayList<Contextualization>();
26
27 try {
28 File file = new File("/Users/jurzua/Projects/workspace/contextualization/data/index.meta/01index.meta.anno.xml");
29
30 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
31 DocumentBuilder builder = factory.newDocumentBuilder();
32 Document doc = builder.parse(file);
33
34 List<Node> nodes = getNodeList(doc, Contextualization.AUTHOR);
35 for(Node node : nodes){
36 Contextualization ctx = getCtx(node, "XX");
37 if(ctx != null){
38 rs.add(ctx);
39 }
40 }
41
42 nodes = getNodeList(doc, Contextualization.CITY);
43 for(Node node : nodes){
44 Contextualization ctx = getCtx(node, "XX");
45 if(ctx != null){
46 rs.add(ctx);
47 }
48 }
49
50 for(Contextualization ctx : rs){
51 System.out.println(ctx.toString());
52 }
53
54 } catch (Exception e) {
55 e.printStackTrace();
56 }
57
58 return rs;
59 }
60
61 public static List<Node> getNodeList(Document doc, String tagName){
62 List<Node> rs = new ArrayList<Node>();
63
64 try {
65 XPathFactory xPathfactory = XPathFactory.newInstance();
66 XPath xpath = xPathfactory.newXPath();
67 XPathExpression expr = xpath.compile("//" + tagName);
68
69 Object result = expr.evaluate(doc, XPathConstants.NODESET);
70
71 NodeList nodes = (NodeList) result;
72 for (int i = 0; i < nodes.getLength(); i++) {
73 Node node = nodes.item(i);
74 rs.add(node);
75 }
76 } catch (Exception e) {
77 e.printStackTrace();
78 }
79
80 return rs;
81
82 }
83
84 public static Contextualization getCtx(Node node, String indexMetaId){
85 if(node instanceof Element){
86 try{
87 Element elem = (Element)node;
88 Contextualization ctx = new Contextualization();
89 ctx.setIndexMetaId(indexMetaId);
90 ctx.setType(elem.getNodeName());
91 ctx.setElementId(elem.getAttribute("context-id"));
92
93 Node child = elem.getFirstChild();
94 if(child != null){
95 ctx.setContent(child.getNodeValue());
96 }
97 return ctx;
98 }catch (Exception e) {
99 e.printStackTrace();
100 }
101 }
102
103
104 return null;
105 }
106
107 public static void main(String[] args){
108
109 getCtxItems("/Users/jurzua/Projects/workspace/contextualization/data/index.meta/01index.meta");
110 }
111
112 public static void printXpathResult(Object result){
113 NodeList nodes = (NodeList) result;
114 for (int i = 0; i < nodes.getLength(); i++) {
115 Node node = nodes.item(i);
116 if(node instanceof Element){
117 Element e = (Element)node;
118
119 System.out.println("Local Name= " + node.getLocalName());
120 System.out.println("Value= " + node.getNodeValue());
121 System.out.println("Name= " + node.getNodeName());
122 System.out.println("getFirstChild value= " + node.getFirstChild().getNodeValue());
123 System.out.println(node);
124 }
125 }
126 }
127 }