1   package org.daisy.pipeline.tts.config;
2   
3   import java.net.URI;
4   import java.util.ArrayList;
5   import java.util.Collection;
6   import java.util.Collections;
7   import java.util.HashMap;
8   import java.util.List;
9   import java.util.Map;
10  
11  import net.sf.saxon.s9api.QName;
12  import net.sf.saxon.s9api.XdmNode;
13  
14  import org.slf4j.Logger;
15  import org.slf4j.LoggerFactory;
16  
17  public class AnnotationsConfigExtension implements ConfigReader.Extension {
18  
19  	private static final Logger logger = LoggerFactory.getLogger(AnnotationsConfigExtension.class);
20  
21  	private final Map<String,List<XdmNode>> annotations = new HashMap<>();
22  
23  	@Override
24  	public boolean parseNode(XdmNode node, URI documentURI, ConfigReader parent) {
25  		String name = node.getNodeName().getLocalName();
26  		if ("annotations".equalsIgnoreCase(name)) {
27  			String href = node.getAttributeValue(new QName(null, "href"));
28  			String type = node.getAttributeValue(new QName(null, "type"));
29  			if (type == null) {
30  				logger.debug("missing attribute @type in node <annotations> of the config file");
31  				return false;
32  			}
33  			if (!annotations.containsKey(type))
34  				annotations.put(type, new ArrayList<XdmNode>());
35  			if (href != null) {
36  				XdmNode external = parent.parseXML(href, documentURI);
37  				if (external != null) {
38  					logger.info("custom annotations read from " + external.getBaseURI());
39  					annotations.get(type).add(external);
40  				}
41  			} else {
42  				logger.info("custom embedded annotations read from " + documentURI);
43  				annotations.get(type).add(node);
44  			}
45  			return true;
46  		}
47  		return false;
48  	}
49  
50  	public Collection<XdmNode> getAnnotations(String mediatype) {
51  		Collection<XdmNode> res = annotations.get(mediatype);
52  		if (res != null)
53  			return res;
54  		return Collections.EMPTY_LIST;
55  	}
56  }