📄 textmessageserializer.java
字号:
/*
* Copyright (C) 2007 ETH Zurich
*
* This file is part of Fosstrak (www.fosstrak.org).
*
* Fosstrak is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
* Fosstrak is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Fosstrak; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
package org.fosstrak.reader.rprm.core.msg;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.TimeZone;
import java.util.Vector;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.XMLGregorianCalendar;
import org.fosstrak.reader.rprm.core.msg.notification.Notification;
import org.fosstrak.reader.rprm.core.msg.reply.AddressReturnType;
import org.fosstrak.reader.rprm.core.msg.reply.BooleanReturnType;
import org.fosstrak.reader.rprm.core.msg.reply.DataSelectorListReturnType;
import org.fosstrak.reader.rprm.core.msg.reply.DataSelectorReply;
import org.fosstrak.reader.rprm.core.msg.reply.DataSelectorReturnType;
import org.fosstrak.reader.rprm.core.msg.reply.EpcReturnType;
import org.fosstrak.reader.rprm.core.msg.reply.ErrorType;
import org.fosstrak.reader.rprm.core.msg.reply.EventTypeListReturnValue;
import org.fosstrak.reader.rprm.core.msg.reply.EventTypeReply;
import org.fosstrak.reader.rprm.core.msg.reply.FieldNameListReturnType;
import org.fosstrak.reader.rprm.core.msg.reply.FieldNameReply;
import org.fosstrak.reader.rprm.core.msg.reply.HexStringReturnType;
import org.fosstrak.reader.rprm.core.msg.reply.IntReturnType;
import org.fosstrak.reader.rprm.core.msg.reply.NotificationChannelListReturnType;
import org.fosstrak.reader.rprm.core.msg.reply.NotificationChannelReply;
import org.fosstrak.reader.rprm.core.msg.reply.NotificationChannelReturnType;
import org.fosstrak.reader.rprm.core.msg.reply.ReadPointListReturnType;
import org.fosstrak.reader.rprm.core.msg.reply.ReadPointReply;
import org.fosstrak.reader.rprm.core.msg.reply.ReadPointReturnType;
import org.fosstrak.reader.rprm.core.msg.reply.ReadReportType;
import org.fosstrak.reader.rprm.core.msg.reply.ReaderDeviceReply;
import org.fosstrak.reader.rprm.core.msg.reply.Reply;
import org.fosstrak.reader.rprm.core.msg.reply.SourceInfoType;
import org.fosstrak.reader.rprm.core.msg.reply.SourceListReturnType;
import org.fosstrak.reader.rprm.core.msg.reply.SourceReply;
import org.fosstrak.reader.rprm.core.msg.reply.SourceReturnType;
import org.fosstrak.reader.rprm.core.msg.reply.StringListReturnType;
import org.fosstrak.reader.rprm.core.msg.reply.StringReturnType;
import org.fosstrak.reader.rprm.core.msg.reply.TagEventType;
import org.fosstrak.reader.rprm.core.msg.reply.TagFieldListReturnType;
import org.fosstrak.reader.rprm.core.msg.reply.TagFieldReply;
import org.fosstrak.reader.rprm.core.msg.reply.TagFieldReturnType;
import org.fosstrak.reader.rprm.core.msg.reply.TagFieldValueParamType;
import org.fosstrak.reader.rprm.core.msg.reply.TagSelectorListReturnType;
import org.fosstrak.reader.rprm.core.msg.reply.TagSelectorReply;
import org.fosstrak.reader.rprm.core.msg.reply.TagSelectorReturnType;
import org.fosstrak.reader.rprm.core.msg.reply.TagType;
import org.fosstrak.reader.rprm.core.msg.reply.TimeStampReturnType;
import org.fosstrak.reader.rprm.core.msg.reply.TriggerListReturnType;
import org.fosstrak.reader.rprm.core.msg.reply.TriggerReply;
import org.fosstrak.reader.rprm.core.msg.reply.TriggerReturnType;
import org.fosstrak.reader.rprm.core.msg.reply.TriggerTypeListReturnType;
import org.fosstrak.reader.rprm.core.msg.reply.TriggerTypeReply;
import org.fosstrak.reader.rprm.core.msg.reply.TriggerTypeReturnType;
import org.fosstrak.reader.rprm.core.msg.reply.TriggerValueReturnType;
import org.fosstrak.reader.rprm.core.msg.reply.NotificationChannelReply.ReadQueuedData;
import org.fosstrak.reader.rprm.core.msg.reply.ReadReportType.SourceReport;
import org.fosstrak.reader.rprm.core.msg.reply.SourceReply.RawReadIDs;
import org.fosstrak.reader.rprm.core.msg.reply.SourceReply.Read;
import org.fosstrak.reader.rprm.core.msg.reply.SourceReply.ReadIDs;
import com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl;
/**
* Serializes a JAXB object tree into a String using the text message format
* defined in the EPC Reader Protocol Specification.
*
* @author Andreas F黵er, ETH Zurich Switzerland, Winter 2005/06
*
*/
public class TextMessageSerializer implements MessageSerializer {
/* String tokens */
public static final String EXCLAMATION = "!";
public static final String TERMINATOR = ">";
public static final String COMMA = ",";
public static final String DOUBLEQUOTE = "\"";
public static final String LF = "\n";
public static final String ERR = "ERR";
public static final String OK = "OK";
public static final String GOODBYE = "GOODBYE";
public static final String ISO_8601_MILLIS_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSz"; //$NON-NLS-1$
public String serialize(Reply r) throws MessageSerializingException {
StringWriter writer = new StringWriter();
writer.write(serializeReplyHeader(r));
if (r.getReaderDevice() != null) {
ReaderDeviceReply rdReply = r.getReaderDevice();
writer.write(serializeReaderDevice(rdReply));
} else if (r.getSource() != null) {
SourceReply srcReply = r.getSource();
writer.write(serializeSource(srcReply));
} else if (r.getReadPoint() != null) {
ReadPointReply rpReply = r.getReadPoint();
writer.write(serializeReadPoint(rpReply));
} else if (r.getTagSelector() != null) {
TagSelectorReply tsReply = r.getTagSelector();
writer.write(serializeTagSelector(tsReply));
} else if (r.getDataSelector() != null) {
DataSelectorReply dsReply = r.getDataSelector();
writer.write(serializeDataSelector(dsReply));
} else if (r.getNotificationChannel() != null) {
NotificationChannelReply ncReply = r.getNotificationChannel();
writer.write(serializeNotificationChannel(ncReply));
} else if (r.getTrigger() != null) {
TriggerReply trgReply = r.getTrigger();
writer.write(serializeTrigger(trgReply));
} else if (r.getFieldName() != null) {
FieldNameReply fnReply = r.getFieldName();
writer.write(serializeFieldName(fnReply));
} else if (r.getTagField() != null) {
TagFieldReply tfReply = r.getTagField();
writer.write(serializeTagField(tfReply));
} else if (r.getError() != null) {
ErrorType error = r.getError();
writer.write(serializeError(r.getResultCode(), error));
}
writer.write(TERMINATOR);
return writer.toString();
}
public String serialize(Notification n) throws MessageSerializingException {
StringWriter writer = new StringWriter();
writer.write(serializeReplyHeader(n));
if (n.getGoodBye() != null) {
writer.write(GOODBYE);
writer.write(LF);
} else if (n.getReadReport() != null) {
writer.write(serializeNotificationReadReport(n.getReadReport()));
}
// TODO Gibts ReaderInfo in n.getReader() im Text-Format nicht?
return writer.toString();
}
/**
* Serializes the reply header
*
* @param r
* @return the reply header
*/
private String serializeReplyHeader(Reply r) {
StringBuffer sBuf = new StringBuffer();
if (r.getId() != null) {
sBuf.append(EXCLAMATION);
sBuf.append(r.getId());
sBuf.append(" ");
}
sBuf.append(OK);
sBuf.append(LF);
return sBuf.toString();
}
/**
* Serializes the notification header
*
* @param n
* the notification
* @return the notification header
*/
private String serializeReplyHeader(Notification n) {
StringBuffer sBuf = new StringBuffer();
if (n.getId() != null) {
sBuf.append(EXCLAMATION);
sBuf.append(n.getId());
sBuf.append(LF);
}
return sBuf.toString();
}
private void writeReplyLine(StringWriter writer, String val) {
writer.write(val);
writer.write(LF);
}
private void writeReplyLine(StringWriter writer, int val) {
writer.write(new Integer(val).toString());
writer.write(LF);
}
private void writeReplyLine(StringWriter writer, StringReturnType s) {
writeReplyLine(writer, s.getReturnValue());
}
private void writeReplyLine(StringWriter writer, IntReturnType i) {
writeReplyLine(writer, i.getReturnValue());
}
private void writeReplyLine(StringWriter writer, BooleanReturnType b) {
writeReplyLine(writer, (b.isReturnValue() ? "true" : "false"));
}
/**
* Writes a timestamp to the output formatted in ISO 8601 i.e.
* YYYY-MM-DDThh:mm:ss.SSSz
*
* @param writer
* The StringWriter for the output
* @param t
* The timestamp to be formatted.
*/
private void writeReplyLine(StringWriter writer, TimeStampReturnType t) {
Calendar cal = xmlGregorianCalendarToCalendar(t.getReturnValue());
writeReplyLine(writer, toISO8601(cal));
}
/**
* Converts a calendar into a ISO 8601 formatted String. i.e.
* YYYY-MM-DDThh:mm:ss.SSSz
*
* @param cal
* The Calendar
* @return String with timestamp formatted as ISO 8601
*/
private String toISO8601(Calendar cal) {
SimpleDateFormat formatter = new SimpleDateFormat();
formatter.applyPattern(ISO_8601_MILLIS_PATTERN);
return formatter.format(cal.getTime());
}
private XMLGregorianCalendar calendarToXMLGregorianCalendar(Calendar cal) {
GregorianCalendar gregorianCalendar = new GregorianCalendar(cal.getTimeZone());
gregorianCalendar.setTimeInMillis(cal.getTimeInMillis());
return new XMLGregorianCalendarImpl(gregorianCalendar);
}
private Calendar xmlGregorianCalendarToCalendar(XMLGregorianCalendar xmlCal) {
Calendar cal = new GregorianCalendar();
cal.set(xmlCal.getYear(), xmlCal.getMonth(), xmlCal.getDay(), xmlCal.getHour(), xmlCal.getMinute(), xmlCal.getSecond());
return cal;
}
private void writeReplyLine(StringWriter writer, HexStringReturnType h) {
String hexString = new String(h.getReturnValue());
writeReplyLine(writer, hexString);
}
private void writeReplyLine(StringWriter writer, AddressReturnType a) {
writeReplyLine(writer, a.getReturnValue());
}
private void writeStringListLine(StringWriter writer, List valueList) {
for (Iterator it = valueList.iterator(); it.hasNext();) {
writer.write((String) it.next());
writer.write(LF);
}
}
private void writeReplyLine(StringWriter writer,
StringListReturnType strList) {
List valueList = strList.getReturnValue().getList().getValue();
writeStringListLine(writer, valueList);
}
private void writeReplyLine(StringWriter writer,
SourceListReturnType sourceList) {
List valueList = sourceList.getReturnValue().getList().getValue();
writeStringListLine(writer, valueList);
}
private void writeReplyLine(StringWriter writer,
DataSelectorListReturnType dsList) {
List valueList = dsList.getReturnValue().getList().getValue();
writeStringListLine(writer, valueList);
}
private void writeReplyLine(StringWriter writer,
NotificationChannelListReturnType ncList) {
List valueList = ncList.getReturnValue().getList().getValue();
writeStringListLine(writer, valueList);
}
private void writeReplyLine(StringWriter writer,
TriggerListReturnType triggerList) {
List valueList = triggerList.getReturnValue().getList().getValue();
writeStringListLine(writer, valueList);
}
private void writeReplyLine(StringWriter writer,
TagSelectorListReturnType tsList) {
List valueList = tsList.getReturnValue().getList().getValue();
writeStringListLine(writer, valueList);
}
private void writeReplyLine(StringWriter writer,
TagFieldListReturnType tfList) {
List valueList = tfList.getReturnValue().getList().getValue();
writeStringListLine(writer, valueList);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -