Project

General

Profile

1
package eu.dnetlib.xml.database.exist;
2

    
3
import javax.xml.xpath.XPathExpression;
4
import javax.xml.xpath.XPathExpressionException;
5
import javax.xml.xpath.XPathFactory;
6

    
7
import org.apache.commons.logging.Log;
8
import org.apache.commons.logging.LogFactory;
9
import org.exist.collections.triggers.SAXTrigger;
10
import org.exist.collections.triggers.TriggerException;
11
import org.exist.dom.persistent.DocumentImpl;
12
import org.exist.storage.DBBroker;
13
import org.exist.storage.txn.Txn;
14
import org.exist.xmldb.XmldbURI;
15

    
16
/**
17
 * simple test trigger.
18
 *
19
 * @author marko
20
 *
21
 */
22
public class TestExistTrigger extends SAXTrigger {
23
	/**
24
	 * logger.
25
	 */
26
	private static final Log log = LogFactory.getLog(TestExistTrigger.class); // NOPMD by marko on 11/24/08 5:02 PM
27
	/**
28
	 * expression.
29
	 */
30
	private transient XPathExpression expr;
31

    
32
	/**
33
	 * useless.
34
	 */
35
	public TestExistTrigger() {
36
		super();
37

    
38
		try {
39
			expr = XPathFactory.newInstance().newXPath().compile("//hello");
40
		} catch (XPathExpressionException e) {
41
			log.fatal("cannot parse xpath", e);
42
		}
43
	}
44

    
45

    
46

    
47
	@Override
48
	public void beforeCreateDocument(final DBBroker dbBroker, final Txn txn, final XmldbURI xmldbURI) throws TriggerException {
49

    
50
	}
51

    
52
	@Override
53
	public void afterCreateDocument(final DBBroker dbBroker, final Txn txn, final DocumentImpl document) throws TriggerException {
54
		try {
55
			log.debug("created new document: " + expr.evaluate(document));
56
		} catch (XPathExpressionException e) {
57
			log.fatal("xpatthing", e);
58
		}
59
	}
60

    
61
	@Override
62
	public void beforeUpdateDocument(final DBBroker dbBroker, final Txn txn, final DocumentImpl document) throws TriggerException {
63
		try {
64
			log.info("old value: " + expr.evaluate(document));
65

    
66
		} catch (XPathExpressionException e) {
67
			log.fatal("xpatthing", e);
68
		}
69
	}
70

    
71
	@Override
72
	public void afterUpdateDocument(final DBBroker dbBroker, final Txn txn, final DocumentImpl document) throws TriggerException {
73

    
74
		try {
75
			log.info("new value: " + expr.evaluate(document));
76
		} catch (XPathExpressionException e) {
77
			log.fatal("xpatthing", e);
78
		}
79

    
80
	}
81

    
82
	@Override
83
	public void beforeUpdateDocumentMetadata(final DBBroker dbBroker, final Txn txn, final DocumentImpl document) throws TriggerException {
84

    
85
	}
86

    
87
	@Override
88
	public void afterUpdateDocumentMetadata(final DBBroker dbBroker, final Txn txn, final DocumentImpl document) throws TriggerException {
89

    
90
	}
91

    
92
	@Override
93
	public void beforeCopyDocument(final DBBroker dbBroker, final Txn txn, final DocumentImpl document, final XmldbURI xmldbURI) throws TriggerException {
94

    
95
	}
96

    
97
	@Override
98
	public void afterCopyDocument(final DBBroker dbBroker, final Txn txn, final DocumentImpl document, final XmldbURI xmldbURI) throws TriggerException {
99

    
100
	}
101

    
102
	@Override
103
	public void beforeMoveDocument(final DBBroker dbBroker, final Txn txn, final DocumentImpl document, final XmldbURI xmldbURI) throws TriggerException {
104

    
105
	}
106

    
107
	@Override
108
	public void afterMoveDocument(final DBBroker dbBroker, final Txn txn, final DocumentImpl document, final XmldbURI xmldbURI) throws TriggerException {
109

    
110
	}
111

    
112
	@Override
113
	public void beforeDeleteDocument(final DBBroker dbBroker, final Txn txn, final DocumentImpl document) throws TriggerException {
114
		try {
115
			log.debug("deleted document: " + expr.evaluate(document));
116
		} catch (XPathExpressionException e) {
117
			log.fatal("xpatthing", e);
118
		}
119

    
120
	}
121

    
122
	@Override
123
	public void afterDeleteDocument(final DBBroker dbBroker, final Txn txn, final XmldbURI xmldbURI) throws TriggerException {
124

    
125
	}
126
}
(5-5/6)