View Javadoc

1   /*
2    * Copyright (C) 2007 ETH Zurich
3    *
4    * This file is part of Fosstrak (www.fosstrak.org).
5    *
6    * Fosstrak is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License version 2.1, as published by the Free Software Foundation.
9    *
10   * Fosstrak is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with Fosstrak; if not, write to the Free
17   * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18   * Boston, MA  02110-1301  USA
19   */
20  
21  package org.fosstrak.capturingapp.util;
22  
23  import java.util.LinkedList;
24  import java.util.List;
25  
26  import org.fosstrak.ale.xsd.ale.epcglobal.ECReaderStat;
27  import org.fosstrak.ale.xsd.ale.epcglobal.ECReport;
28  import org.fosstrak.ale.xsd.ale.epcglobal.ECReportGroup;
29  import org.fosstrak.ale.xsd.ale.epcglobal.ECReportGroupListMember;
30  import org.fosstrak.ale.xsd.ale.epcglobal.ECReports;
31  import org.fosstrak.ale.xsd.ale.epcglobal.ECTagStat;
32  import org.fosstrak.ale.xsd.epcglobal.EPC;
33  
34  /**
35   * helper class to perform transformations on ECReports.
36   * @author sawielan
37   *
38   */
39  public class Util {
40  	
41  	/**
42  	 * Simple interface that allows you to select an EPC programmatically 
43  	 *  (eg. raw-hex, tag, ...);
44  	 * @author sawielan
45  	 *
46  	 */
47  	public interface EPCSelector {
48  		public EPC select(ECReportGroupListMember member);
49  	}
50  	
51  	/** selector that returns the raw hex. */
52  	public static final  EPCSelector selectRawHex  = new EPCSelector() {
53  		public EPC select(ECReportGroupListMember member) {
54  			return member.getRawHex();
55  		}
56  	};
57  	
58  	/** selector that returns the raw decimal. */
59  	public static final  EPCSelector selectRawDecimal  = new EPCSelector() {
60  		public EPC select(ECReportGroupListMember member) {
61  			return member.getRawDecimal();
62  		}
63  	};
64  	
65  	/** selector that returns the tag. */
66  	public static final  EPCSelector selectTag  = new EPCSelector() {
67  		public EPC select(ECReportGroupListMember member) {
68  			return member.getTag();
69  		}
70  	};
71  	
72  	/** selector that returns the epc . */
73  	public static final  EPCSelector selectEPC  = new EPCSelector() {
74  		public EPC select(ECReportGroupListMember member) {
75  			return member.getEpc();
76  		}
77  	};
78  	
79  	/** the default selector. */
80  	public static final EPCSelector DEFAULT_SELECTOR = selectRawHex;
81  	
82  	/**
83  	 * checks if a member was retrieved by a given reader.
84  	 * @param readerName the name of the reader to check.
85  	 * @param member the member to inspect.
86  	 * @return true if read from the reader, false otherwise.
87  	 */
88  	public static boolean fromReader(String readerName, ECReportGroupListMember member) {
89  		try {
90  			for (ECTagStat stat : member.getExtension().getStats().getStat()) {
91  				for (ECReaderStat rstat : stat.getStatBlocks().getStatBlock()) {
92  					if (rstat.getReaderName().equals(readerName)) {
93  						return true;
94  					}
95  				}
96  			}
97  		} catch (Exception e) {
98  			return false;
99  		}
100 		return false;
101 	}
102 	
103 	/**
104 	 * extracts all the members containing the EPCs from the reports.
105 	 * @param reports the report to "digest". 
106 	 * @return a list of members providing access to the EPC data.
107 	 */
108 	public static List<ECReportGroupListMember> extractReportMembers(ECReports reports) {
109 		List<ECReportGroupListMember> members = 
110 			new LinkedList<ECReportGroupListMember> ();
111 			
112 		if (null == reports) return members;
113 		if (null == reports.getReports()) return members;
114 		for (ECReport report : reports.getReports().getReport()) {
115 			members.addAll(extractReportMembers(report));
116 		}
117 		return members;
118 	}
119 	
120 	/**
121 	 * extracts all the members containing the EPCs from the report.
122 	 * @param report the report to "digest". 
123 	 * @return a list of members providing access to the EPC data.
124 	 */
125 	public static List<ECReportGroupListMember> extractReportMembers(ECReport report) {
126 		List<ECReportGroupListMember> members = 
127 			new LinkedList<ECReportGroupListMember> ();
128 			
129 		if (null == report) return members;
130 		if (null == report.getGroup()) return members;
131 		for (ECReportGroup group : report.getGroup()) {
132 			if ((null != group) && (null != group.getGroupList()) && 
133 					(null != group.getGroupList().getMember())){
134 						
135 				for (ECReportGroupListMember member : group.getGroupList().getMember()) {
136 					members.add(member);
137 				}
138 			}
139 		}
140 		
141 		return members;
142 	}
143 	
144 	/**
145 	 * extracts EPC values from the reports. when the selector is set to 
146 	 * <code>null</code>, then the default selector will be chosen.
147 	 * @param selector the selector selecting the matching EPCs.
148 	 * @param reports the reports from where to select the EPCs.
149 	 * @return returns a list of selected EPCs. 
150 	 */
151 	public static List<EPC> extractEPC(EPCSelector selector, ECReports reports) {
152 		List<EPC> epcs = new LinkedList<EPC> ();
153 		if (null == reports.getReports()) return epcs;
154 		for (ECReport report : reports.getReports().getReport()) {
155 			epcs.addAll(extractEPC(selector, report));
156 		}
157 		return epcs;
158 	}
159 	
160 	/**
161 	 * extracts EPC values from the report. when the selector is set to 
162 	 * <code>null</code>, then the default selector will be chosen.
163 	 * @param selector the selector selecting the matching EPCs.
164 	 * @param report the report from where to select the EPCs.
165 	 * @return returns a list of selected EPCs.
166 	 */
167 	public static List<EPC> extractEPC(EPCSelector selector, ECReport report) {
168 		List<EPC> epcs = new LinkedList<EPC> ();
169 		for (ECReportGroupListMember member : extractReportMembers(report)) {
170 			EPCSelector s = selector;
171 			if (null == selector) s = DEFAULT_SELECTOR;
172 			
173 			EPC epc = s.select(member);			
174 			if (null != epc) epcs.add(epc);
175 		}
176 		return epcs;
177 	}
178 	
179 	/**
180 	 * helper to prepare a nice pretty print of a whole ECReport.
181 	 * @param report the ECReport to print.
182 	 * @return a nice pretty print.
183 	 */
184 	public static String printReport(ECReport report) {
185 		StringBuffer buffer = new StringBuffer(String.format("name: %n", 
186 				report.getReportName()));
187 		if (null != report.getGroup()) {
188 			for (ECReportGroup group : report.getGroup()) {
189 				if (null != group.getGroupList()) {
190 					for (ECReportGroupListMember member : group.getGroupList().getMember()) {
191 						buffer.append(printGroupMember(member));
192 					}
193 				}
194 			}
195 		}
196 		return buffer.toString();
197 	}
198 	
199 	/**
200 	 * helper to prepare a nice pretty print of a group member of a ECReport.
201 	 * @param member the member to print.
202 	 * @return a nice pretty print.
203 	 */
204 	public static String printGroupMember(ECReportGroupListMember member) {
205 		StringBuffer b = new StringBuffer();
206 		if (null != member.getEpc()) {
207 			b.append(String.format("epc: %s\n", member.getEpc().getValue()));
208 		}
209 		if (null != member.getTag()) {
210 			b.append(String.format("tag: %s\n", member.getTag().getValue()));
211 		}
212 		if (null != member.getRawDecimal()) {
213 			b.append(String.format("decimal: %s\n", member.getRawDecimal()
214 					.getValue()));
215 		}
216 		if (null != member.getRawHex()) {
217 			b.append(String.format("hex: %s\n", member.getRawHex().getValue()));
218 		}
219 		b.append("\n");
220 
221 		return b.toString();
222 	}
223 }