view src/main/java/de/mpiwg/indexmeta/IndexMetaParser.java @ 5:7d231e4e86e5

environment setting
author Jorge Urzua <jurzua@mpiwg-berlin.mpg.de>
date Fri, 12 Apr 2013 14:28:32 +0200
parents dfce13a5f5f9
children 9ce7979fd037
line wrap: on
line source

package de.mpiwg.indexmeta;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import de.mpiwg.indexmeta.bo.Contextualization;

public class IndexMetaParser {

	
	public static List<Contextualization> getCtxItems(String filePath){
		List<Contextualization> rs = new ArrayList<Contextualization>();
		
		try {
			File file = new File("/Users/jurzua/Projects/workspace/contextualization/data/index.meta/01index.meta.anno.xml");
			
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document doc = builder.parse(file);
			
			List<Node> nodes = getNodeList(doc, Contextualization.AUTHOR);
			for(Node node : nodes){
				Contextualization ctx = getCtx(node, "XX");
				if(ctx != null){
					rs.add(ctx);
				}				
			}

			nodes = getNodeList(doc, Contextualization.CITY);
			for(Node node : nodes){
				Contextualization ctx = getCtx(node, "XX");
				if(ctx != null){
					rs.add(ctx);
				}
			}
			
			for(Contextualization ctx : rs){
				System.out.println(ctx.toString());
			}			
	        
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return rs;
	} 
	
	public static List<Node> getNodeList(Document doc, String tagName){
		List<Node> rs = new ArrayList<Node>();
		
		try {
			XPathFactory xPathfactory = XPathFactory.newInstance();
			XPath xpath = xPathfactory.newXPath();
			XPathExpression expr = xpath.compile("//" + tagName);
			
	        Object result = expr.evaluate(doc, XPathConstants.NODESET);
	        
	        NodeList nodes = (NodeList) result;
	        for (int i = 0; i < nodes.getLength(); i++) {
	        	Node node = nodes.item(i);
	        	rs.add(node);
	        }
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return rs;
		
	}
	
	public static Contextualization getCtx(Node node, String indexMetaId){
		if(node instanceof Element){
			try{
				Element elem = (Element)node;
				Contextualization ctx = new Contextualization();
				ctx.setIndexMetaId(indexMetaId);
				ctx.setType(elem.getNodeName());
				ctx.setElementId(elem.getAttribute("context-id"));
				
				Node child = elem.getFirstChild();
				if(child != null){
					ctx.setContent(child.getNodeValue());
				}
				return ctx;
			}catch (Exception e) {
				e.printStackTrace();
			}	
		}
			
		
		return null;
	}
	
	public static void main(String[] args){

		getCtxItems("/Users/jurzua/Projects/workspace/contextualization/data/index.meta/01index.meta");
	}
	
    public static void printXpathResult(Object result){
        NodeList nodes = (NodeList) result;
        for (int i = 0; i < nodes.getLength(); i++) {
        	Node node = nodes.item(i);
        	if(node instanceof Element){
            	Element e = (Element)node;
            	
                System.out.println("Local Name= " + node.getLocalName());
                System.out.println("Value= " + node.getNodeValue());
                System.out.println("Name= " + node.getNodeName());
                System.out.println("getFirstChild value= " + node.getFirstChild().getNodeValue());
                System.out.println(node);
        	}
        }
    }
}