Project

General

Profile

1
package eu.dnetlib.dhp.wf.importer.mdrecord;
2

    
3
import java.util.Stack;
4

    
5
import org.xml.sax.Attributes;
6
import org.xml.sax.SAXException;
7
import org.xml.sax.helpers.DefaultHandler;
8

    
9
import eu.dnetlib.dhp.common.InfoSpaceConstants;
10

    
11
/**
12
 * MDRecord handler extracting record identifier.
13
 * 
14
 * Notice: writer is not being closed by handler. Created outside, let it be closed outside as well.
15
 * @author mhorst
16
 *
17
 */
18
public class MDRecordHandler extends DefaultHandler {
19

    
20
    public static final String ELEM_OBJ_IDENTIFIER = "objIdentifier";
21
    
22
    private static final String ELEM_HEADER = "header";
23
    
24
    private Stack<String> parents;
25
    
26
    private StringBuilder currentValue = new StringBuilder();
27
    
28
    private String recordId;
29
    
30
    
31
    // ------------------------ LOGIC --------------------------
32
    
33
    @Override
34
    public void startDocument() throws SAXException {
35
        parents = new Stack<String>();
36
        recordId = null;
37
    }
38

    
39
    @Override
40
    public void startElement(String uri, String localName, String qName,
41
            Attributes attributes) throws SAXException {
42
        if (this.recordId == null) {
43
            if (isWithinElement(localName, ELEM_OBJ_IDENTIFIER, ELEM_HEADER)) {
44
//              identifierType attribute is mandatory
45
                this.currentValue = new StringBuilder();
46
            }
47
            this.parents.push(localName);            
48
        }
49
    }
50

    
51
    @Override
52
    public void endElement(String uri, String localName, String qName)
53
            throws SAXException {
54
        if (this.recordId == null) {
55
            this.parents.pop();
56
            if (isWithinElement(localName, ELEM_OBJ_IDENTIFIER, ELEM_HEADER)) {
57
                this.recordId = InfoSpaceConstants.ROW_PREFIX_RESULT + this.currentValue.toString().trim();
58
            }
59
//          resetting current value;
60
            this.currentValue = null;
61
        }
62
    }
63

    
64
    @Override
65
    public void endDocument() throws SAXException {
66
        parents.clear();
67
        parents = null;
68
    }
69

    
70
    @Override
71
    public void characters(char[] ch, int start, int length)
72
            throws SAXException {
73
        if (this.currentValue!=null) {
74
            this.currentValue.append(ch, start, length);
75
        }
76
    }
77

    
78
    /**
79
     * @return record identifier
80
     */
81
    public String getRecordId() {
82
        return recordId;
83
    }
84
    
85
    // ------------------------ PRIVATE --------------------------
86
    
87
    private boolean isWithinElement(String localName, String expectedElement, String expectedParent) {
88
        return localName.equals(expectedElement) && !this.parents.isEmpty() && 
89
                expectedParent.equals(this.parents.peek());
90
    }
91

    
92
    
93
}
94

    
(1-1/3)