View Javadoc

1   package org.rpi.sources;
2   
3   import java.io.File;
4   import java.io.FileNotFoundException;
5   import java.util.concurrent.ConcurrentHashMap;
6   
7   import javax.xml.parsers.DocumentBuilder;
8   import javax.xml.parsers.DocumentBuilderFactory;
9   import javax.xml.xpath.XPath;
10  import javax.xml.xpath.XPathFactory;
11  
12  import org.apache.log4j.Logger;
13  import org.rpi.config.Config;
14  import org.w3c.dom.Document;
15  import org.w3c.dom.Element;
16  import org.w3c.dom.Node;
17  import org.w3c.dom.NodeList;
18  
19  public class SourceReader {
20  
21  	private Logger log = Logger.getLogger(this.getClass());
22  	private ConcurrentHashMap<String, Source> sources = new ConcurrentHashMap<String, Source>();
23  	private String default_pin = "";
24  
25  	public SourceReader() {
26  		readSources();
27  	}
28  
29  	/***
30  	 * Read the InputSources.xml file and build a List of Radio Channels
31  	 * 
32  	 * @return
33  	 */
34  	private void readSources() {
35  
36  		try {
37  			sources.clear();
38  			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
39  			DocumentBuilder builder = factory.newDocumentBuilder();
40  			Document doc = builder.parse(new File("InputSources.xml"));
41  			NodeList listOfChannels = doc.getElementsByTagName("Source");
42  			String ex_columns = "/Sources/@default_pin";
43  			XPath xPath = XPathFactory.newInstance().newXPath();
44  			setDefaultPin(xPath.compile(ex_columns).evaluate(doc));
45  			int i = 1;
46  			for (int s = 0; s < listOfChannels.getLength(); s++) {
47  				boolean addToSource = true;
48  				String name = null;
49  				String type = null;
50  				String GPIO_PIN = "";
51  
52  				Node channel = listOfChannels.item(s);
53  				if (channel.getNodeType() == Node.ELEMENT_NODE) {
54  					Element element = (Element) channel;
55  					name = getElement(element, "name");
56  					type = getElement(element, "type");
57  					GPIO_PIN = getElement(element, "GPIO_PIN");
58  					Source source = new Source(name, type, GPIO_PIN);
59  					if (type.equalsIgnoreCase("RECEIVER")) {
60  						if (!Config.enableReceiver) {
61  							addToSource = false;
62  						}
63  					}else if (type.equalsIgnoreCase("UPNP"))
64  					{
65  						if(!Config.enableAVTransport)
66  						{
67  							addToSource = false;
68  						}
69  					}
70  					if (addToSource) {
71  						addSource(source);
72  					}
73  				}
74  			}
75  		} catch (Exception e) {
76  			if (e instanceof FileNotFoundException) {
77  				log.warn("ListSources.xml Not Found");
78  			} else {
79  				log.error("Error Getting Source List", e);
80  			}
81  		}
82  	}
83  
84  	private void addSource(Source source) {
85  		if (!getSources().containsKey(source.getName())) {
86  			getSources().put(source.getName(), source);
87  		}
88  	}
89  
90  	/***
91  	 * 
92  	 * @param element
93  	 * @param name
94  	 * @return
95  	 */
96  	private String getElement(Element element, String name) {
97  		String res = "";
98  		NodeList nid = element.getElementsByTagName(name);
99  		if (nid != null) {
100 			Element fid = (Element) nid.item(0);
101 			if (fid != null) {
102 				res = fid.getTextContent();
103 				// log.debug("ElementName: " + name + " Value: " + res);
104 				return res;
105 
106 			}
107 		}
108 		return res;
109 	}
110 
111 	public ConcurrentHashMap<String, Source> getSources() {
112 		return sources;
113 	}
114 
115 	private void setSources(ConcurrentHashMap<String, Source> sources) {
116 		this.sources = sources;
117 	}
118 
119 	public String getDefaultPin() {
120 		return default_pin;
121 	}
122 
123 	private void setDefaultPin(String default_pin) {
124 		this.default_pin = default_pin;
125 	}
126 
127 }