Project

General

Profile

1
package eu.dnetlib.data.mdstore.modular;
2

    
3
import java.io.StringReader;
4
import java.util.HashMap;
5
import java.util.Map;
6

    
7
import javax.xml.xpath.XPath;
8
import javax.xml.xpath.XPathFactory;
9

    
10
import org.apache.commons.logging.Log;
11
import org.apache.commons.logging.LogFactory;
12
import org.xml.sax.InputSource;
13

    
14
import static eu.dnetlib.data.mdstore.modular.MDStoreConstants.*;
15

    
16
/**
17
 * Terrible implementation of a record parser.
18
 * 
19
 * @author marko
20
 *
21
 */
22
public class SimpleRecordParser implements RecordParser {
23
	static final Log log = LogFactory.getLog(SimpleRecordParser.class); // NOPMD by marko on 11/24/08 5:02 PM
24

    
25
	private long ts;
26

    
27
	@Override
28
	public Map<String, String> parseRecord(String record) {
29
		Map<String, String> props = new HashMap<String, String>();
30
		props.put(TIMESTAMP, String.valueOf(getTimestamp()));
31

    
32
		try {
33
//			DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
34
			XPath xpath = XPathFactory.newInstance().newXPath();
35

    
36
//			Document doc = builder.parse(new InputSource(new StringReader(record)));
37
			InputSource doc = new InputSource(new StringReader(record));
38
			
39
			props.put(ID, xpath.evaluate("//*[local-name()='objIdentifier']", doc));
40
			props.put("originalId", xpath.evaluate("//*[local-name()='efgEntity']/*/*[local-name()='identifier']", doc));
41
			
42
//			String date = xpath.evaluate("//*[local-name()='dateOfCollection'][1]", doc);
43
//			props.put("date", new Date(date).getTime());	
44
				
45
		} catch (Exception e) {
46
			log.warn("got exception while parsing document", e);
47
			log.warn("record is:");
48
			log.warn(record);
49
			log.warn("------------");
50
		}
51
		return props;
52

    
53
	}
54

    
55
	@Override
56
	public void setTimestamp(final long ts) {
57
		this.ts = ts;
58
	}
59

    
60
	@Override
61
	public long getTimestamp() {
62
		return ts;
63
	}
64

    
65
}
(12-12/13)