net.sf.freecol.common.networking
Class Message

java.lang.Object
  extended by net.sf.freecol.common.networking.Message
Direct Known Subclasses:
BuildColonyMessage, BuyMessage, BuyPropositionMessage, CashInTreasureTrainMessage, ChatMessage, ClaimLandMessage, CloseTransactionMessage, DebugForeignColonyMessage, DeclareIndependenceMessage, DeliverGiftMessage, DiplomacyMessage, DisembarkMessage, EmigrateUnitMessage, GetTransactionMessage, GiveIndependenceMessage, GoodsForSaleMessage, JoinColonyMessage, RenameMessage, SellMessage, SellPropositionMessage, SetDestinationMessage, SpySettlementMessage, StatisticsMessage, UpdateCurrentStopMessage

public class Message
extends java.lang.Object

Class for parsing raw message data into an XML-tree and for creating new XML-trees.


Constructor Summary
Message(org.w3c.dom.Document document)
          Constructs a new Message with data from the given XML-document.
Message(java.io.InputStream inputStream)
          Constructs a new Message with data from the given InputStream.
Message(java.lang.String msg)
          Constructs a new Message with data from the given String.
 
Method Summary
static org.w3c.dom.Element clientError(java.lang.String message)
          Creates an error message in response to bad client data.
static org.w3c.dom.Element createError(java.lang.String messageID, java.lang.String message)
          Creates an error message.
static void createError(javax.xml.stream.XMLStreamWriter out, java.lang.String messageID, java.lang.String message)
          Creates an error message.
static org.w3c.dom.Document createNewDocument()
          Creates and returns a new XML-document.
static org.w3c.dom.Element createNewRootElement(java.lang.String tagName)
          Creates a new root element.
 java.lang.String getAttribute(java.lang.String key)
          Gets an attribute from the root element.
static org.w3c.dom.Element getChildElement(org.w3c.dom.Element element, java.lang.String tagName)
          Convenience method: returns the first child element with the specified tagname.
 org.w3c.dom.Document getDocument()
          Gets the Document holding the message data.
static java.lang.String getFreeColProtocolVersion()
          Gets the current version of the FreeCol protocol.
 java.lang.String getType()
          Gets the type of this Message.
 boolean hasAttribute(java.lang.String attribute)
          Checks if an attribute is set on the root element.
 void insertAsRoot(org.w3c.dom.Element newRoot)
          Inserts newRoot as the new root element and appends the old root element.
 boolean isType(java.lang.String type)
          Checks if this message is of a given type.
 void setAttribute(java.lang.String key, int value)
          Sets an attribute on the root element.
 void setAttribute(java.lang.String key, java.lang.String value)
          Sets an attribute on the root element.
 java.lang.String toString()
          Returns the String representation of the message.
 org.w3c.dom.Element toXMLElement()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Message

public Message(java.lang.String msg)
        throws org.xml.sax.SAXException,
               java.io.IOException
Constructs a new Message with data from the given String. The constructor to use if this is an INCOMING message.

Parameters:
msg - The raw message data.
Throws:
java.io.IOException - should not be thrown.
org.xml.sax.SAXException - if thrown during parsing.

Message

public Message(java.io.InputStream inputStream)
        throws org.xml.sax.SAXException,
               java.io.IOException
Constructs a new Message with data from the given InputStream. The constructor to use if this is an INCOMING message.

Parameters:
inputStream - The InputStream to get the XML-data from.
Throws:
java.io.IOException - if thrown by the InputStream.
org.xml.sax.SAXException - if thrown during parsing.

Message

public Message(org.w3c.dom.Document document)
Constructs a new Message with data from the given XML-document.

Parameters:
document - The document representing an XML-message.
Method Detail

getFreeColProtocolVersion

public static java.lang.String getFreeColProtocolVersion()
Gets the current version of the FreeCol protocol.

Returns:
The version of the FreeCol protocol.

createNewDocument

public static org.w3c.dom.Document createNewDocument()
Creates and returns a new XML-document.

Returns:
the new XML-document.

createNewRootElement

public static org.w3c.dom.Element createNewRootElement(java.lang.String tagName)
Creates a new root element. This is done by creating a new document and using this document to create the root element.

Parameters:
tagName - The tag name of the root element beeing created,
Returns:
the new root element.

createError

public static org.w3c.dom.Element createError(java.lang.String messageID,
                                              java.lang.String message)
Creates an error message.

Parameters:
messageID - Identifies the "i18n"-keyname. Not specified in the message if null.
message - The error in plain text. Not specified in the message if null.
Returns:
The root Element of the error message.

createError

public static void createError(javax.xml.stream.XMLStreamWriter out,
                               java.lang.String messageID,
                               java.lang.String message)
Creates an error message.

Parameters:
out - The output stream for the message.
messageID - Identifies the "i18n"-keyname. Not specified in the message if null.
message - The error in plain text. Not specified in the message if null.

clientError

public static org.w3c.dom.Element clientError(java.lang.String message)
Creates an error message in response to bad client data.

Parameters:
message - The error in plain text.
Returns:
The root Element of the error message.

getDocument

public org.w3c.dom.Document getDocument()
Gets the Document holding the message data.

Returns:
The Document holding the message data.

getType

public java.lang.String getType()
Gets the type of this Message.

Returns:
The type of this Message.

isType

public boolean isType(java.lang.String type)
Checks if this message is of a given type.

Parameters:
type - The type you wish to test against.
Returns:
true if the type of this message equals the given type and false otherwise.

setAttribute

public void setAttribute(java.lang.String key,
                         java.lang.String value)
Sets an attribute on the root element.

Parameters:
key - The key of the attribute.
value - The value of the attribute.

setAttribute

public void setAttribute(java.lang.String key,
                         int value)
Sets an attribute on the root element.

Parameters:
key - The key of the attribute.
value - The value of the attribute.

getAttribute

public java.lang.String getAttribute(java.lang.String key)
Gets an attribute from the root element.

Parameters:
key - The key of the attribute.
Returns:
The value of the attribute with the given key.

hasAttribute

public boolean hasAttribute(java.lang.String attribute)
Checks if an attribute is set on the root element.

Parameters:
attribute - The attribute in which to verify the existence of.
Returns:
true if the root element has the given attribute.

insertAsRoot

public void insertAsRoot(org.w3c.dom.Element newRoot)
Inserts newRoot as the new root element and appends the old root element.

Parameters:
newRoot - The new root element.

getChildElement

public static org.w3c.dom.Element getChildElement(org.w3c.dom.Element element,
                                                  java.lang.String tagName)
Convenience method: returns the first child element with the specified tagname.

Parameters:
element - The Element to search for the child element.
tagName - The tag name of the child element to be found.
Returns:
The first child element with the given name.

toXMLElement

public org.w3c.dom.Element toXMLElement()

toString

public java.lang.String toString()
Returns the String representation of the message. This is what actually gets transmitted to the other peer.

Overrides:
toString in class java.lang.Object
Returns:
The String representation of the message.