01: package org.obe.event;
02:
03: import org.obe.client.api.model.MIMETypes;
04: import org.obe.spi.service.ServiceManager;
05: import org.w3c.dom.Document;
06: import org.w3c.dom.DocumentType;
07: import org.w3c.dom.Element;
08:
09: import java.util.HashMap;
10: import java.util.Iterator;
11: import java.util.Map;
12: import java.util.Properties;
13: import java.util.regex.Matcher;
14: import java.util.regex.Pattern;
15:
16: /**
17: * Handles MIME <tag>text/*</tag> content.
18: *
19: * @author Adrian Price
20: */
21: public class MIMETextHandler extends AbstractContentHandler {
22: private final Map _patterns = new HashMap();
23:
24: public MIMETextHandler() {
25: }
26:
27: public void init(ServiceManager svcMgr, Properties props) {
28: init(svcMgr);
29: for (Iterator iter = props.entrySet().iterator(); iter
30: .hasNext();) {
31: Map.Entry entry = (Map.Entry) iter.next();
32: String contentType = (String) entry.getKey();
33: String pattern = (String) entry.getValue();
34: _patterns.put(Pattern.compile(pattern), contentType);
35: }
36: }
37:
38: public String getContentType(Object data) {
39: String s = (String) data;
40: for (Iterator iter = _patterns.entrySet().iterator(); iter
41: .hasNext();) {
42: Map.Entry entry = (Map.Entry) iter.next();
43: Pattern p = (Pattern) entry.getKey();
44: Matcher matcher = p.matcher(s);
45: if (matcher.matches())
46: return (String) entry.getValue();
47: }
48: return MIMETypes.TEXT_PLAIN;
49: }
50:
51: protected String getSchema(Object data, String contentType) {
52: String schema = "";
53: if (contentType.equals(MIMETypes.XML)) {
54: // Obtain DTD, document element namespace URI and tag.
55: Document document = (Document) data;
56: DocumentType docType = document.getDoctype();
57: Element docElem = document.getDocumentElement();
58: String nodeName = docElem.getLocalName();
59: String namespace;
60: if (docType != null) {
61: namespace = docType.getPublicId();
62: if (namespace == null)
63: namespace = docType.getSystemId();
64: } else {
65: namespace = docElem.getNamespaceURI();
66: }
67: schema = namespace != null ? '{' + namespace + '}'
68: + nodeName : nodeName;
69: }
70: return schema;
71: }
72: }
|