- Main Page
- Namespaces
- Classes
- Files
- Related Pages

# /Users/fpy/titan-1-5-MBARI/Mers/Utils/Io/Xml/Stream/XmlStreamReader.cpp

    00001
    00002 //
    00003 // Copyright (c) 2004           Model-based Embedded and Robotic Systems Group
    00004 // All Rights Reserved          Massachusetts Institute of Technology
    00005 //
    00006 // This software is provided as is. Model-based Embedded and Robotic Systems
    00007 // (MERS) group does not assume any responsibility.
    00008 //
    00009 // Organization:        Model-based Embedded and Robotic Systems Group
    00010 //                                      Massachusetts Institute of Technology
    00011 //
    00013
    00014 #include "XmlStreamReader.h"
    00015 #include "InvalidTagException.h"
    00016 #include <Mers/Utils/Exceptions/NoSuchElementException.h>
    00017
    00018 using namespace Mers::Utils::Io::Xml::Stream;
    00019 using std::string;
    00020 using Mers::Utils::Exceptions::NoSuchElementException;
    00021
    00023 // Get functions
    00025
    00026 const string& XmlStreamReader::getElementText() {
    00027   if(getEventType() != START_ELEMENT) {
    00028     throw XmlStreamException("Parser must be on START_ELEMENT for "
    00029                              "getElementText()", getLocation());
    00030   }
    00031   EventType eventType = next();
    00032   elementText = "";
    00033   while(eventType != END_ELEMENT) {
    00034     if(eventType == CHARACTERS
    00035        || eventType == CDATA
    00036        || eventType == SPACE
    00037        || eventType == ENTITY_REFERENCE) {
    00038       elementText += getText();
    00039     }
    00040     else if(eventType == PROCESSING_INSTRUCTION || eventType == COMMENT) {
    00041       // skip
    00042     }
    00043     else if(eventType == END_DOCUMENT) {
    00044       throw XmlStreamException("Unexpected end of the document when "
    00045                                "reading element text content", getLocation());
    00046     } else if(eventType == START_ELEMENT) {
    00047       throw XmlStreamException("Element text content may not contain "
    00048                                "START_ELEMENT", getLocation());
    00049     } else {
    00050       throw XmlStreamException("Unexpected event type "+eventType,
    00051                                getLocation());
    00052     }
    00053     eventType = next();
    00054   }
    00055   return elementText;
    00056 }
    00057
    00059
    00060 double XmlStreamReader::getElementTextAsDouble()
    00061 {
    00062   double dValue;
    00063   if(sscanf(getElementText().c_str(), "%lf", &dValue)==1) {
    00064     return dValue;
    00065   }
    00066   else {
    00067     throw XmlStreamException("Expected a double value", getLocation());
    00068   }
    00069 }
    00070
    00072
    00073 int XmlStreamReader::getElementTextAsInt()
    00074 {
    00075   int iValue;
    00076   if(sscanf(getElementText().c_str(), "%i", &iValue)==1) {
    00077     return iValue;
    00078   }
    00079   else {
    00080     throw XmlStreamException("Expected an integer value", getLocation());
    00081   }
    00082 }
    00083
    00085 // Event type functions
    00087
    00088 bool XmlStreamReader::isCharacters() const
    00089 {
    00090   return getEventType() == CHARACTERS;
    00091 }
    00092
    00094
    00095 bool XmlStreamReader::isStartElement() const
    00096 {
    00097   return getEventType() == START_ELEMENT;
    00098 }
    00099
    00101
    00102 bool XmlStreamReader::isEndElement() const
    00103 {
    00104   return getEventType() == END_ELEMENT;
    00105 }
    00106
    00108
    00109 // this may not be completely right - what if it's a data event (e.g. CDATA)
    00110 // that's completely empty? check reference implementation
    00111 bool XmlStreamReader::isWhiteSpace() const
    00112 {
    00113   return getEventType() == SPACE;
    00114 }
    00115
    00117 // Stream navigation functions
    00119
    00120 XmlStreamReader::EventType XmlStreamReader::nextTag()
    00121 {
    00122   EventType eventType = next();
    00123   while(isWhiteSpace() // skip whitespace
    00124         || eventType == SPACE // not sure if this is needed - check specs
    00125         || eventType == PROCESSING_INSTRUCTION
    00126         || eventType == COMMENT) {
    00127     eventType = next();
    00128   }
    00129
    00130   if (eventType != START_ELEMENT && eventType != END_ELEMENT) {
    00131     throw XmlStreamException("Invalid event; expected a start or an end tag",
    00132                              getLocation());
    00133   }
    00134   return eventType;
    00135 }
    00136
    00138
    00139 XmlStreamReader::EventType XmlStreamReader::nextSiblingTag()
    00140 {
    00141   EventType eventType = nextSibling(); // will check that not EndElement
    00142
    00143   while(isWhiteSpace() // skip whitespace
    00144         || eventType == SPACE // not sure if this is needed - check specs
    00145         || eventType == PROCESSING_INSTRUCTION
    00146         || eventType == COMMENT) {
    00147     eventType = nextSibling();
    00148   }
    00149
    00150   if (eventType != START_ELEMENT && eventType != END_ELEMENT) {
    00151     throw XmlStreamException("Invalid event; expected a start or an end tag",
    00152                              getLocation());
    00153   }
    00154   return eventType;
    00155 }
    00156
    00158
    00159 void XmlStreamReader::checkHasNext() const {
    00160   if(!hasNext()) {
    00161     throw NoSuchElementException("End of the XML stream reached");
    00162   }
    00163 }
    00164
    00166