📄 xmlcodec.java
字号:
sb.append(ET).append(ENVELOPE_TAG).append(CT);
return sb.toString();
}
// ***************************************************
// * Decoding methods *
// ***************************************************
/** This method is called when start the document XML*/
public void startDocument() {
env = new Envelope();
}
/** This method is called at the end of parsing */
public void endDocument() {
//Put the ro object in to envelope
//env.setReceived(ro);
}
/** This method is called when jmp event of begin element.*/
public void startElement(String uri, String localName, String rawName, Attributes attributes) {
//Detection of the begin of to or from tags
//Update the acumulator
accumulator = new StringBuffer();
if (TO_TAG.equalsIgnoreCase(localName)) {
aid = new AID();
env.addTo(aid);
}
else if (FROM_TAG.equalsIgnoreCase(localName)) {
aid = new AID();
env.setFrom(aid);
}
else if (INTENDED_TAG.equalsIgnoreCase(localName)) {
aid = new AID();
env.addIntendedReceiver(aid);
}
else if (RECEIVED_TAG.equalsIgnoreCase(localName)) {
ro = new ReceivedObject();
env.addStamp(ro);
}
else if (RECEIVED_BY.equalsIgnoreCase(localName)) {
ro.setBy(attributes.getValue(RECEIVED_ATTR));
}
else if (RECEIVED_FROM.equalsIgnoreCase(localName)) {
ro.setFrom(attributes.getValue(RECEIVED_ATTR));
}
else if (RECEIVED_DATE.equalsIgnoreCase(localName)) {
ro.setDate(new BasicFipaDateTime(attributes.getValue(RECEIVED_ATTR)).getTime());
}
else if (RECEIVED_ID.equalsIgnoreCase(localName)) {
ro.setId(attributes.getValue(RECEIVED_ATTR));
}
else if (RECEIVED_VIA.equalsIgnoreCase(localName)) {
ro.setVia(attributes.getValue(RECEIVED_ATTR));
}
else if (PROP_TAG.equalsIgnoreCase(localName)) {
prop = new Property();
env.addProperties(prop);
prop.setName(attributes.getValue(PROP_ATTR));
propType = attributes.getValue(PROP_ATTR_TYPE);
}
}
/** This method is called the end of element */
public void endElement(String namespaceURL, String localName, String qname) {
//Capture the value the attributes of class
if (AID_NAME.equalsIgnoreCase(localName)) {
aid.setName(accumulator.toString());
}
else if (AID_ADDRESS.equalsIgnoreCase(localName)) {
aid.addAddresses(accumulator.toString());
}
else if (COMMENTS_TAG.equalsIgnoreCase(localName)) {
env.setComments(accumulator.toString());
}
else if (REPRESENTATION_TAG.equalsIgnoreCase(localName)) {
env.setAclRepresentation(accumulator.toString());
}
else if (LENGTH_TAG.equalsIgnoreCase(localName)) {
env.setPayloadLength(new Long(accumulator.toString()));
if(logger.isLoggable(Logger.WARNING))
logger.log(Logger.FINE,"Length: "+env.getPayloadLength());
}
else if (ENCODING_TAG.equalsIgnoreCase(localName)) {
env.setPayloadEncoding(accumulator.toString());
}
else if (DATE_TAG.equalsIgnoreCase(localName)) {
env.setDate(new BasicFipaDateTime(accumulator.toString()).getTime());
}
else if (PROP_TAG.equalsIgnoreCase(localName)) {
decodeProp(accumulator, prop);
//prop.setValue(accumulator.toString());
}
/*
// Not sure it is still in FIPA
else if (ENCRYPTED_TAG.equalsIgnoreCase(localName)) {
env.addEncrypted(accumulator.toString());
}
*/
}
/** This method is called when exist characters in the elements*/
public void characters(char[] buffer, int start, int length) {
accumulator.append(buffer, start, length);
}
/** This method is called when warning occur*/
//#DOTNET_EXCLUDE_BEGIN
public void warning(SAXParseException exception) {
//#DOTNET_EXCLUDE_END
/*#DOTNET_INCLUDE_BEGIN
public void warning(ParseException exception) {
#DOTNET_INCLUDE_END*/
if(logger.isLoggable(Logger.WARNING))
//#DOTNET_EXCLUDE_BEGIN
logger.log(Logger.WARNING," line " + exception.getLineNumber() + ": "+
exception.getMessage());
//#DOTNET_EXCLUDE_END
/*#DOTNET_INCLUDE_BEGIN
logger.log(Logger.WARNING," line " + exception.getError() + ": " +
exception.get_Message());
#DOTNET_INCLUDE_END*/
}
/** This method is called when errors occur*/
//#DOTNET_EXCLUDE_BEGIN
public void error(SAXParseException exception) {
//#DOTNET_EXCLUDE_END
/*#DOTNET_INCLUDE_BEGIN
public void error(ParseException exception) {
#DOTNET_INCLUDE_END*/
if(logger.isLoggable(Logger.WARNING))
//#DOTNET_EXCLUDE_BEGIN
logger.log(Logger.WARNING,"ERROR: line " + exception.getLineNumber() + ": " +
exception.getMessage());
//#DOTNET_EXCLUDE_END
/*#DOTNET_INCLUDE_BEGIN
logger.log(Logger.WARNING,"ERROR: line " + exception.getError() + ": " +
exception.get_Message());
#DOTNET_INCLUDE_END*/
}
/** This method is called when non-recoverable errors occur.*/
//#DOTNET_EXCLUDE_BEGIN
public void fatalError(SAXParseException exception) throws SAXException{
//#DOTNET_EXCLUDE_END
/*#DOTNET_INCLUDE_BEGIN
public void fatalError(ParseException exception) {
#DOTNET_INCLUDE_END*/
if(logger.isLoggable(Logger.WARNING))
//#DOTNET_EXCLUDE_BEGIN
logger.log(Logger.SEVERE,"FATAL: line " + exception.getLineNumber() + ": " +
exception.getMessage());
throw exception;
//#DOTNET_EXCLUDE_END
/*#DOTNET_INCLUDE_BEGIN
logger.log(Logger.SEVERE,"FATAL: line " + exception.getError() + ": " +
exception.get_Message());
#DOTNET_INCLUDE_END*/
}
//#DOTNET_EXCLUDE_BEGIN
// Main method
public Envelope parse(Reader in) throws MTPException
{
try
{
parser.parse(new InputSource(in));
return env;
}
catch (Exception ex) {
throw new MTPException(ex.getMessage());
}
}
//#DOTNET_EXCLUDE_END
/*#DOTNET_INCLUDE_BEGIN
// Main method
public Envelope parse(System.IO.TextReader in) throws MTPException
{
try
{
//Inizialization of variables used
ArrayList elemList = new ArrayList();
String textStr = "";
env = new Envelope();
recObj = new ReceivedObject();
parser = new XmlTextReader(in);
while ( parser.Read() )
{
XmlNodeType myNType = parser.get_NodeType();
if (myNType.Equals( XmlNodeType.Element ))
{
elemList.Add( parser.get_Name() );
if ( parser.get_HasAttributes() )
{
while( parser.MoveToNextAttribute() )
{
compare (parser.get_Name(), parser.get_Value());
}//End WHILE block
}//End IF block
parser.MoveToElement();
}
else if (myNType.Equals( XmlNodeType.Text ))
{
textStr = parser.ReadString();
int counter = elemList.get_Count()-1;
found = false;
while (counter >= 0 && !found)
{
compare(elemList.get_Item(counter).ToString(), textStr);
counter--;
}
if (counter < 0)
//Console.WriteLine("ERRORE");
System.out.println("ERRORE");
else
{
found = false;
elemList.RemoveAt(elemList.get_Count()-1);
}
}
else if ( myNType.Equals( XmlNodeType.EndElement ) )
{
int idx = elemList.LastIndexOf(parser.get_Name());
elemList.RemoveAt(idx);
if (elemList.get_Count() > 0)
compare(elemList.get_Item(elemList.get_Count()-1).ToString(), "");
}
}//End WHILE block
parser.Close();
}
catch (XmlException e)
{
env = new Envelope();
throw new MTPException("Exception during parsing XML file.\nNested exception is: "+e.get_Message());
}
finally
{
return env;
}
}
public void compare(String s, String r) throws MTPException
{
try
{
this.found = true;
if (s.equalsIgnoreCase(this.AID_NAME))
{
aidStr = r.Trim(badChars);
}
else if (s.equalsIgnoreCase(this.TO_TAG))
{
//aidStr = convertHostname(aidStr);
AID newAID = new AID(aidStr, AID.ISGUID);
newAID.addAddresses( addressStr );
env.addTo( newAID );
//reset values
aidStr = "";
addressStr = "";
}
else if (s.equalsIgnoreCase(this.FROM_TAG))
{
AID newAID = new AID(aidStr, AID.ISGUID);
newAID.addAddresses( addressStr );
env.setFrom( newAID );
//reset values
aidStr = "";
addressStr = "";
}
else if (s.equalsIgnoreCase(this.AID_ADDRESS))
{
if (r != "")
addressStr = r.Trim(badChars);
}
else if (s.equalsIgnoreCase(this.AID_ADDRESSES))
{
if (r != "")
addressStr = r.Trim(badChars);
}
else if (s.equalsIgnoreCase(this.REPRESENTATION_TAG))
{
String aclStr = r.Trim(badChars);
env.setAclRepresentation( aclStr );
}
else if (s.equalsIgnoreCase(this.DATE_TAG))
{
String dateStr = r.Trim(badChars);
BasicFipaDateTime bfdt = new BasicFipaDateTime(dateStr);
env.setDate( bfdt.getTime() );
}
else if (s.equalsIgnoreCase(this.INTENDED_TAG))
{
//aidStr = convertHostname(aidStr);
AID newAID = new AID(aidStr, AID.ISGUID);
newAID.addAddresses( addressStr );
env.addIntendedReceiver( newAID );
//reset values
aidStr = "";
addressStr = "";
}
else if (s.equalsIgnoreCase(this.ENCODING_TAG))
{
String payStr = r.Trim(badChars);
env.setPayloadEncoding( payStr );
}
else if (s.equalsIgnoreCase(this.LENGTH_TAG))
{
String payStr = r.Trim(badChars);
env.setPayloadLength( new java.lang.Long(payStr) );
}
else if (s.equalsIgnoreCase(this.COMMENTS_TAG))
{
String cmtStr = r.Trim(badChars);
env.setComments( cmtStr );
}
else if (s.equalsIgnoreCase(this.RECEIVED_TAG))
{
env.setReceived( recObj );
}
else if (s.equalsIgnoreCase(this.RECEIVED_VIA))
{
String via = r.Trim(badChars);
recObj.setVia( via );
}
else if (s.equalsIgnoreCase(this.RECEIVED_BY))
{
String mts = r.Trim(badChars);
recObj.setBy( mts );
}
else if (s.equalsIgnoreCase(this.RECEIVED_DATE))
{
String aDate = r.Trim(badChars);
BasicFipaDateTime bfdt = new BasicFipaDateTime(aDate);
recObj.setDate( bfdt.getTime() );
}
else if (s.equalsIgnoreCase(this.RECEIVED_FROM))
{
String from = r.Trim(badChars);
recObj.setFrom( from );
}
else if (s.equalsIgnoreCase(this.RECEIVED_ID))
{
String id = r.Trim(badChars);
recObj.setId( id );
}
else if (s.equalsIgnoreCase(this.INDEX))
{
String index = r.Trim(badChars);
Property prop = new Property();
prop.setName(s);
prop.setValue(index);
env.addProperties( prop );
}
else
found = false;
}
catch (Exception e)
{
throw new MTPException("Error during tags comparison");
}
}
#DOTNET_INCLUDE_END*/
}//End of XMLCodec class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -