net.sf.freecol.common.networking
Class DOMMessage

java.lang.Object
  extended by net.sf.freecol.common.networking.DOMMessage
Direct Known Subclasses:
AbandonColonyMessage, AskSkillMessage, AssignTeacherMessage, AssignTradeRouteMessage, AttackMessage, BuildColonyMessage, BuyGoodsMessage, BuyMessage, BuyPropositionMessage, CashInTreasureTrainMessage, ChangeStateMessage, ChangeWorkImprovementTypeMessage, ChangeWorkTypeMessage, ChatMessage, ChooseFoundingFatherMessage, ClaimLandMessage, ClearSpecialityMessage, CloseTransactionMessage, DeclareIndependenceMessage, DeclineMoundsMessage, DeliverGiftMessage, DemandTributeMessage, DiplomacyMessage, DisbandUnitMessage, DisembarkMessage, EmbarkMessage, EmigrateUnitMessage, EquipUnitMessage, GetNationSummaryMessage, GetTransactionMessage, GoodsForSaleMessage, InciteMessage, IndianDemandMessage, JoinColonyMessage, LearnSkillMessage, LoadCargoMessage, LootCargoMessage, MissionaryMessage, MonarchActionMessage, MoveMessage, MoveToMessage, NewLandNameMessage, NewRegionNameMessage, PayArrearsMessage, PayForBuildingMessage, PutOutsideColonyMessage, RenameMessage, ScoutIndianSettlementMessage, SellGoodsMessage, SellMessage, SellPropositionMessage, ServerAPI.TrivialMessage, SetBuildQueueMessage, SetDestinationMessage, SetGoodsLevelsMessage, SetTradeRoutesMessage, SpySettlementMessage, TrainUnitInEuropeMessage, UnloadCargoMessage, UpdateCurrentStopMessage, UpdateTradeRouteMessage, WorkMessage

public class DOMMessage
extends java.lang.Object

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


Field Summary
protected  org.w3c.dom.Document document
          The actual Message data.
private static java.lang.String FREECOL_PROTOCOL_VERSION
           
private static java.lang.String INVALID_MESSAGE
           
protected static java.util.logging.Logger logger
           
 
Constructor Summary
protected DOMMessage()
           
  DOMMessage(org.w3c.dom.Document document)
          Constructs a new DOMMessage with data from the given XML-document.
private DOMMessage(org.xml.sax.InputSource inputSource)
          Constructs a new DOMMessage with data from the given InputSource.
  DOMMessage(java.io.InputStream inputStream)
          Constructs a new DOMMessage with data from the given InputStream.
  DOMMessage(java.lang.String msg)
          Constructs a new DOMMessage 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 collapseElements(java.util.List<org.w3c.dom.Element> elements)
          Collapses a list of elements into a "multiple" element with the original elements added as child nodes.
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.
static java.lang.String elementToString(org.w3c.dom.Element element)
          Convert an element to a string.
 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 DOMMessage.
 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
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

logger

protected static final java.util.logging.Logger logger

FREECOL_PROTOCOL_VERSION

private static final java.lang.String FREECOL_PROTOCOL_VERSION
See Also:
Constant Field Values

INVALID_MESSAGE

private static final java.lang.String INVALID_MESSAGE
See Also:
Constant Field Values

document

protected org.w3c.dom.Document document
The actual Message data.

Constructor Detail

DOMMessage

protected DOMMessage()

DOMMessage

public DOMMessage(java.lang.String msg)
           throws org.xml.sax.SAXException,
                  java.io.IOException
Constructs a new DOMMessage 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.

DOMMessage

public DOMMessage(java.io.InputStream inputStream)
           throws org.xml.sax.SAXException,
                  java.io.IOException
Constructs a new DOMMessage 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.

DOMMessage

private DOMMessage(org.xml.sax.InputSource inputSource)
            throws org.xml.sax.SAXException,
                   java.io.IOException
Constructs a new DOMMessage with data from the given InputSource. The constructor to use if this is an INCOMING message.

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

DOMMessage

public DOMMessage(org.w3c.dom.Document document)
Constructs a new DOMMessage 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.

collapseElements

public static org.w3c.dom.Element collapseElements(java.util.List<org.w3c.dom.Element> elements)
Collapses a list of elements into a "multiple" element with the original elements added as child nodes.

Parameters:
elements - A list of Elements to collapse.
Returns:
A new "multiple" element, or the singleton element of the list, or null if the list is empty.

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 DOMMessage.

Returns:
The type of this DOMMessage.

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.

elementToString

public static java.lang.String elementToString(org.w3c.dom.Element element)
Convert an element to a string.

Parameters:
element - The Element to convert.
Returns:
A string representation of an element.

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.