View Javadoc

1   /*
2    *  
3    *  Fosstrak LLRP Commander (www.fosstrak.org)
4    * 
5    *  Copyright (C) 2008 ETH Zurich
6    *
7    *  This program is free software: you can redistribute it and/or modify
8    *  it under the terms of the GNU General Public License as published by
9    *  the Free Software Foundation, either version 3 of the License, or
10   *  (at your option) any later version.
11   *
12   *  This program is distributed in the hope that it will be useful,
13   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   *  GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License
18   *  along with this program.  If not, see <http://www.gnu.org/licenses/> 
19   *
20   */
21  
22  package org.fosstrak.llrp.commander.editors;
23  
24  import java.io.StringReader;
25  import java.util.ArrayList;
26  
27  import org.apache.log4j.Logger;
28  import org.jdom.Document;
29  import org.llrp.ltk.generated.LLRPMessageFactory;
30  import org.llrp.ltk.types.LLRPBitList;
31  import org.llrp.ltk.types.LLRPMessage;
32  
33  
34  /**
35   * This is a helper class for LLRP Binary Message, which transform the XML message
36   * to binary bit list by calling LTK Java APIs.
37   * 
38   * @author Haoning Zhang
39   * @author sawielan
40   * @version 1.0
41   */
42  public class BinaryMessageHelper {
43  
44  	/**
45  	 * Log4J instance.
46  	 */
47  	private static Logger log = Logger.getLogger(BinaryMessageHelper.class);
48  	
49  	private LLRPBitList bitList;
50  	
51  	/** denotes the number of characters used to split the parameters into several lines. */
52  	public static final int DEFAULT_LINE_LENGTH = 64;
53  	
54  	/** denotes the length of a chunk within a line. */
55  	public static final int DEFAULT_CHUNK_LENGTH = 8;
56  	
57  	/** the default separator between two chunks. */
58  	public static final String DEFAULT_CHUNK_DELIMITER = " ";
59  	
60  	/**
61  	 * Constructor, initialize the valid Message in binary format
62  	 * 
63  	 * @param aXMLContent XML Message Format
64  	 * @throws Exception if message is not valid.
65  	 */
66  	public BinaryMessageHelper(String aXMLContent) throws Exception {
67  		log.debug("Start tranforming.");
68  		Document doc = new org.jdom.input.SAXBuilder()
69  				.build(new StringReader(aXMLContent));
70  		//XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
71  		//log.debug("Input XML Message: " + outputter.outputString(doc));
72  		LLRPMessage message = LLRPMessageFactory.createLLRPMessage(doc);
73  
74  		bitList = new LLRPBitList(message.toBinaryString());
75  		log.debug("Finish tranforming.");
76  	}
77  	
78  	/**
79  	 * Get the binary list.
80  	 * 
81  	 * @return Binary List
82  	 */
83  	public String getBinaryString() {
84  		return bitList.toString();
85  	}
86  	
87  	/**
88  	 * Get the reversed value of the message, in binary format.
89  	 * 
90  	 * @return Reserved Value
91  	 */
92  	public String getReserved() {
93  		return bitList.subList(0, 3).toString();
94  	}
95  	
96  	/**
97  	 * Get the version value of the message, in binary format.
98  	 * 
99  	 * @return Version Value
100 	 */
101 	public String getVersion() {
102 		return bitList.subList(3, 3).toString();
103 	}
104 	
105 	/**
106 	 * Get the message type value of the message, in binary format.
107 	 * 
108 	 * @return Message Type Value
109 	 */
110 	public String getMessageType() {
111 		return bitList.subList(6, 10).toString();
112 	}
113 	
114 	/**
115 	 * Get the length value of the message, in binary format.
116 	 * 
117 	 * @return Message Length Value
118 	 */
119 	public String getLength() {
120 		return bitList.subList(16, 32).toString();
121 	}
122 	
123 	/**
124 	 * Get the message ID  of the message, in binary format.
125 	 * 
126 	 * @return Message ID Value
127 	 */
128 	public String getMessageID() {
129 		return bitList.subList(48, 32).toString();
130 	}
131 	
132 	/**
133 	 * Get the ALL parameter values of the message, in binary format.
134 	 * 
135 	 * @return ALL parameter values
136 	 */
137 	public String getParameters() {
138 		int length = bitList.length() - 64;
139 		
140 		return (length > 0) ? bitList.subList(64, length).toString() : "";
141 	}
142 	
143 	/**
144 	 * create a new string that contains the delimiter ins every n characters.
145 	 * @param orig the original string.
146 	 * @param ins the delimiter to insert.
147 	 * @param n the number of characters to use between two delimiters.
148 	 * @return the resulting string.
149 	 */
150 	public String insert(String orig, String ins, int n) {
151 		StringBuffer copy = new StringBuffer();
152 		
153 		final int length = orig.length();
154 		for (int i=0; i<length; i+=n) {
155 			int up = (i+n<length-1) ? i+n : length-1;
156 			copy.append(orig.subSequence(i, up));
157 			if (up < length-1) {
158 				copy.append(ins);
159 			}
160 		}
161 		
162 		return copy.toString();
163 	}
164 	
165 	/**
166 	 * splits the parameters into an array of several strings with sub-chunks.
167 	 * @param lineLength the length of the resulting line.
168 	 * @param chunkLength the length of one chunk within the line.
169 	 * @param delimiter the delimiter between the chunks.
170 	 * @return an array encoding the parameters into a chunk of several lines.
171 	 */
172 	public String[] getArrParameters(final int lineLength, final int chunkLength, final String delimiter) {
173 		
174 		int length = bitList.length() - 64;
175 		if (0 >= length) {
176 			return new String[] { "" };
177 		}
178 		
179 		ArrayList<String> arr = new ArrayList<String>();
180 		int i=0;
181 		for (;i<length-lineLength; i+=lineLength) {
182 			arr.add(insert(bitList.subList(i+64, lineLength).toString(), delimiter, chunkLength));
183 		}
184 		if (i<length) {
185 			arr.add(insert(bitList.subList(i+64, length-i).toString(), delimiter, chunkLength));
186 		}
187 		String[] a = new String[arr.size()];
188 		arr.toArray(a);
189 		return a;
190 	}
191 }