Public Member Functions | Protected Attributes | Private Member Functions | Private Attributes

XMLStorage::XMLStreamReader Class Reference

XMLStreamReader is the streaming XML file reader of XMLStorage. More...

Inheritance diagram for XMLStorage::XMLStreamReader:
Inheritance graph
[legend]
Collaboration diagram for XMLStorage::XMLStreamReader:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 XMLStreamReader (string path, XmlReaderSettings settings=null)
 XMLStreamReader (StreamReader source, XmlReaderSettings settings=null)
void Dispose ()
XMLStreamNode getNode ()
Object getNodeName ()
bool isAtNode (string nodeName)
IEnumerable< XMLStreamNodeiterator ()
 Iterates over the immediate children of the current XML node.
IEnumerable< XMLStreamNodefilterChildren (XMLStreamReader reader, string name)
 Iterates over the immediate children of the current XML node and filters by node name.
string get (string attrName)
 Return the value of an attribute in the current node or "".
string get (string attrName, string def)
 Return the value of an attribute in the current node or a default value.
bool goDown ()
 Go down to first child.
bool goDown (string name)
 Search for child and go down.
bool go (XPath xpath)
 move X-Path like to position in XML tree
bool back ()
 Move back to the next higher nesting level.
string getContent ()
override string ToString ()
string getInstructions ()
bool firstChild ()
 Move to the first child node.
bool firstChild (string nodeName)
 Move to the first child node with the specified name.
bool next ()
 Move to the next sibling node independent from its name.
bool nextSkipOthers (string nodeName)
 Move to the next sibling node with the specified name.
bool nextStopOnOthers (string nodeName)
 Move to the next sibling node with the specified name.
bool find (string nodeName)
 Find a children node by name.
bool find (string nodeName, int n)
 Find the n'th children node with tag nodeName.
bool find (string nodeName, string attrName, string attrValue)
 Search matching child node identified by key name and an attribute value.
bool find (string nodeName, string attrName, string attrValue, int n)
 Search matching child node identified by key name and an attribute value.

Protected Attributes

XmlReader _reader
XMLStreamNode _node
string _content = ""
StringBuilder _instructions = new StringBuilder()

Private Member Functions

void init (TextReader source, XmlReaderSettings settings=null)
void startElement (string nodeName)
void endElement (string nodeName)
void characters (string str)
void processingInstruction (string target, string data)
bool findNode (string targetTag, int targetLevel, int minLevel, bool stopOnOthers)
bool handlePendingEndElement (int minLevel)
bool findRelative (XPath xpath)
 XPath find function.
bool find (XPathElement xelem)
 Search matching child node identified by key name and an attribute value.

Private Attributes

TextReader _istream
int _level = 0
bool _pendingEndElement = false

Detailed Description

XMLStreamReader is the streaming XML file reader of XMLStorage.

Definition at line 51 of file XMLStreamReader.cs.


Constructor & Destructor Documentation

XMLStorage::XMLStreamReader::XMLStreamReader ( string  path,
XmlReaderSettings  settings = null 
) [inline]

Definition at line 63 of file XMLStreamReader.cs.

References _istream, and init().

    {
        _istream = new StreamReader(path);

        init(_istream, settings);
    }

Here is the call graph for this function:

XMLStorage::XMLStreamReader::XMLStreamReader ( StreamReader  source,
XmlReaderSettings  settings = null 
) [inline]

Definition at line 70 of file XMLStreamReader.cs.

References init().

    {
        init(source, settings);
    }

Here is the call graph for this function:


Member Function Documentation

bool XMLStorage::XMLStreamReader::back (  ) [inline]

Move back to the next higher nesting level.

Remark: This implementation does _not_ jump more than one level up like XMLPos.node() when using go() to move down more than one level at a time.

Returns:
true if successfull

Definition at line 192 of file XMLStreamReader.cs.

References _level, and findNode().

    {
        return findNode(null, -1, _level+1, false);
    }

Here is the call graph for this function:

void XMLStorage::XMLStreamReader::characters ( string  str ) [inline, private]

Definition at line 230 of file XMLStreamReader.cs.

References _content, and XMLStorage::XMLHelper::encodeXMLString().

Referenced by findNode().

    {
        _content += XMLHelper.encodeXMLString(str);
    }

Here is the call graph for this function:

void XMLStorage::XMLStreamReader::Dispose (  ) [inline]

Definition at line 82 of file XMLStreamReader.cs.

References _istream, and _reader.

    {
        _reader.Close();
        _reader = null;

        if (_istream != null) {
            _istream.Dispose();
            _istream = null;
        }
    }
void XMLStorage::XMLStreamReader::endElement ( string  nodeName ) [inline, private]

Definition at line 221 of file XMLStreamReader.cs.

References _content, _level, _node, and XMLStorage::XMLStreamNode::assign().

Referenced by findNode(), and handlePendingEndElement().

    {
        _node.assign(nodeName);

        _content = "";

        --_level;
    }

Here is the call graph for this function:

IEnumerable<XMLStreamNode> XMLStorage::XMLStreamReader::filterChildren ( XMLStreamReader  reader,
string  name 
) [inline]

Iterates over the immediate children of the current XML node and filters by node name.

(replaces XMLStreamFilter)

Definition at line 126 of file XMLStreamReader.cs.

References XMLStorage::XMLStreamNode::getName(), and iterator().

    {
        foreach(XMLStreamNode node in iterator())
        {
            if (node.getName() == name)
                yield return node;
        }
    }

Here is the call graph for this function:

bool XMLStorage::XMLStreamReader::find ( string  nodeName,
string  attrName,
string  attrValue,
int  n 
) [inline]

Search matching child node identified by key name and an attribute value.

Parameters:
nodeName
attrName
attrValue
n
Returns:
true on success

Definition at line 475 of file XMLStreamReader.cs.

References _node, firstChild(), XMLStorage::XMLStreamNode::get(), XMLStorage::XMLStreamNode::getName(), and nextSkipOthers().

    {
        if (firstChild(nodeName)) {
            if (_node.getName()==nodeName && _node.get(attrName)==attrValue)
                if (n-- == 0)
                    return true;

            while(nextSkipOthers(nodeName))
                if (_node.getName()==nodeName && _node.get(attrName)==attrValue)
                    if (n-- == 0)
                        return true;
        }

        return false;
    }

Here is the call graph for this function:

bool XMLStorage::XMLStreamReader::find ( XPathElement  xelem ) [inline, private]

Search matching child node identified by key name and an attribute value.

Parameters:
xelem
Returns:
true on success

Definition at line 511 of file XMLStreamReader.cs.

References XMLStorage::XPathElement::_childName, _node, firstChild(), XMLStorage::XPathElement::matches(), and nextSkipOthers().

    {
        int[] n = new int[] {0};    // index in array for call-by-reference matches()

        if (firstChild(xelem._childName)) {
            if (xelem.matches(_node, n))
                return true;

            while(nextSkipOthers(xelem._childName))
                if (xelem.matches(_node, n))
                    return true;
        }

        return false;
    }

Here is the call graph for this function:

bool XMLStorage::XMLStreamReader::find ( string  nodeName ) [inline]

Find a children node by name.

Parameters:
nodeName
Returns:
true on success

Definition at line 423 of file XMLStreamReader.cs.

References firstChild().

Referenced by findRelative().

    {
        return firstChild(nodeName);
    }

Here is the call graph for this function:

bool XMLStorage::XMLStreamReader::find ( string  nodeName,
int  n 
) [inline]

Find the n'th children node with tag nodeName.

Parameters:
nodeName
n
Returns:
true on success

Definition at line 434 of file XMLStreamReader.cs.

References firstChild(), and nextSkipOthers().

    {
        if (!firstChild(nodeName))
            return false;

        for(int i=1; i<n; ++i)
            if (!nextSkipOthers(nodeName))
                return false;

        return true;
    }

Here is the call graph for this function:

bool XMLStorage::XMLStreamReader::find ( string  nodeName,
string  attrName,
string  attrValue 
) [inline]

Search matching child node identified by key name and an attribute value.

Parameters:
nodeName
attrName
attrValue
Returns:
true on success

Definition at line 453 of file XMLStreamReader.cs.

References _node, firstChild(), XMLStorage::XMLStreamNode::get(), XMLStorage::XMLStreamNode::getName(), and nextSkipOthers().

    {
        if (firstChild(nodeName)) {
            if (_node.getName()==nodeName && _node.get(attrName)==attrValue)
                return true;

            while(nextSkipOthers(nodeName))
                if (_node.getName()==nodeName && _node.get(attrName)==attrValue)
                    return true;
        }

        return false;
    }

Here is the call graph for this function:

bool XMLStorage::XMLStreamReader::findNode ( string  targetTag,
int  targetLevel,
int  minLevel,
bool  stopOnOthers 
) [inline, private]

Definition at line 295 of file XMLStreamReader.cs.

References _content, _level, _node, _pendingEndElement, _reader, characters(), endElement(), handlePendingEndElement(), processingInstruction(), XMLStorage::XMLStreamNode::setContent(), and startElement().

Referenced by back(), firstChild(), next(), nextSkipOthers(), and nextStopOnOthers().

    {
        bool found = false;

        if (!handlePendingEndElement(minLevel))
            return false;

        do {
            switch(_reader.NodeType) {
                case XmlNodeType.Element: {
                    // stop parsing if we found the target node and its content is already processed.
                    if (found)
                        goto break_parsingLoop;

                    // check for match in target level and eventually node name
                    int level = _pendingEndElement? _level-1: _level;

                    if (level+1 == targetLevel) {
                        if (targetTag==null || _reader.Name==targetTag)
                            found = true;
                        else if (stopOnOthers)
                            return false;
                    }

                    if (_reader.IsEmptyElement) { // node without sub nodes and text content, but maybe with attribute values {
                        if (!handlePendingEndElement(minLevel))
                            return false;

                        _pendingEndElement = true;
                    }

                    startElement(_reader.Name);
                    break;}

                case XmlNodeType.EndElement: {
                    // stop parsing if we found the target node and its content is already processed.
                    if (found)
                        goto break_parsingLoop;

                    // stop before leaving the targeted level range; used by nextStopOnOthers()
                    int level = _pendingEndElement? _level-1: _level;
                    if (level == minLevel)
                        return false;

                    endElement(_reader.Name);

                    // stop searching if the current level leaves the target range; used by back()
                    if (_level < minLevel) {
                        _reader.Read();
                        return false;
                    }
                    break;}

                case XmlNodeType.Text:
                    //Console.Write(_reader.Value);
                    characters(_reader.Value);
                    break;

                case XmlNodeType.CDATA:
                    //Console.Write("<![CDATA[{0}]]>", _reader.Value);
                    characters(_reader.Value);
                    break;

                case XmlNodeType.Comment:
                    //Console.Write("<!--{0}-->", _reader.Value);
                    characters(_reader.Value);
                    break;

                case XmlNodeType.XmlDeclaration:
                    //Console.Write("<?xml version='1.0'?>");
                    break;

                case XmlNodeType.ProcessingInstruction:
                    //Console.Write("<?{0} {1}?>", _reader.Name, _reader.Value);
                    processingInstruction(_reader.Name, _reader.Value);
                    break;

                case XmlNodeType.DocumentType:
                    //Console.Write("<!DOCTYPE {0} [{1}]", _reader.Name, _reader.Value);
                    break;

                //case XmlNodeType.Document:
                //    break;

                //case XmlNodeType.EntityReference:
                //    Console.Write(reader.Name);
                //    break;
            }
        } while(_reader.Read());
    break_parsingLoop:

        if (found) {
            _node.setContent(_content);
            _content = "";
            return true;
        } else
            return false;
    }

Here is the call graph for this function:

bool XMLStorage::XMLStreamReader::findRelative ( XPath  xpath ) [inline, private]

XPath find function.

Parameters:
xpath
Returns:
true on success

Definition at line 496 of file XMLStreamReader.cs.

References find().

Referenced by go().

    {
        foreach(XPathElement xelem in xpath) {
            if (!find(xelem))
                return false;
        }

        return true;
    }

Here is the call graph for this function:

bool XMLStorage::XMLStreamReader::firstChild (  ) [inline]

Move to the first child node.

Returns:
true on success

Definition at line 250 of file XMLStreamReader.cs.

References _level, and findNode().

Referenced by find(), goDown(), and iterator().

    {
        return findNode(null, _level+1, _level, false);
    }

Here is the call graph for this function:

bool XMLStorage::XMLStreamReader::firstChild ( string  nodeName ) [inline]

Move to the first child node with the specified name.

Returns:
true on success

Definition at line 259 of file XMLStreamReader.cs.

References _level, and findNode().

    {
        return findNode(nodeName, _level+1, _level, false);
    }

Here is the call graph for this function:

string XMLStorage::XMLStreamReader::get ( string  attrName,
string  def 
) [inline]

Return the value of an attribute in the current node or a default value.

Parameters:
attrName
defdefault value
Returns:

Definition at line 152 of file XMLStreamReader.cs.

References _node, and XMLStorage::XMLStreamNode::get().

    {
        return _node.get(attrName, def);
    }

Here is the call graph for this function:

string XMLStorage::XMLStreamReader::get ( string  attrName ) [inline]

Return the value of an attribute in the current node or "".

Parameters:
attrName
Returns:

Definition at line 141 of file XMLStreamReader.cs.

References _node, and XMLStorage::XMLStreamNode::get().

    {
        return _node.get(attrName);
    }

Here is the call graph for this function:

string XMLStorage::XMLStreamReader::getContent (  ) [inline]
Returns:
content string

Definition at line 200 of file XMLStreamReader.cs.

References _node, and XMLStorage::XMLStreamNode::getContent().

    {
        return _node.getContent();
    }

Here is the call graph for this function:

string XMLStorage::XMLStreamReader::getInstructions (  ) [inline]

Definition at line 240 of file XMLStreamReader.cs.

References _instructions.

    {
        return _instructions.ToString();
    }
XMLStreamNode XMLStorage::XMLStreamReader::getNode (  ) [inline]
Object XMLStorage::XMLStreamReader::getNodeName (  ) [inline]

Definition at line 99 of file XMLStreamReader.cs.

References _node, and XMLStorage::XMLStreamNode::getName().

    {
        return _node.getName();
    }

Here is the call graph for this function:

bool XMLStorage::XMLStreamReader::go ( XPath  xpath ) [inline]

move X-Path like to position in XML tree

Definition at line 177 of file XMLStreamReader.cs.

References XMLStorage::XPath::_absolute, and findRelative().

    {
         // Absolute paths are not supported.
        if (xpath._absolute)
            return false;

        return findRelative(xpath);
    }

Here is the call graph for this function:

bool XMLStorage::XMLStreamReader::goDown (  ) [inline]

Go down to first child.

Returns:
true if successfull

Definition at line 161 of file XMLStreamReader.cs.

References firstChild().

    {
        return firstChild(null);
    }

Here is the call graph for this function:

bool XMLStorage::XMLStreamReader::goDown ( string  name ) [inline]

Search for child and go down.

Parameters:
name
Returns:

Definition at line 171 of file XMLStreamReader.cs.

References firstChild().

    {
        return firstChild(name);
    }

Here is the call graph for this function:

bool XMLStorage::XMLStreamReader::handlePendingEndElement ( int  minLevel ) [inline, private]

Definition at line 394 of file XMLStreamReader.cs.

References _level, _node, _pendingEndElement, _reader, endElement(), XMLStorage::XMLStreamNode::getName(), and XMLStorage::AbstractXMLNode::hasAttributes().

Referenced by findNode().

    {
        if (_pendingEndElement) {
            _pendingEndElement = false;

            bool withAttributes = _node.hasAttributes();

            endElement(_node.getName());

            // stop searching if the current level leaves the target range; used by back()
            if (_level < minLevel) {
                // If there have been read attributes in AbstractXMLNode.setAttributes(), XMLReader already points to the next node,
                // so don't call XmlReader.Read()
                if (!withAttributes)
                    _reader.Read();

                return false;
            }
        }

        return true;
    }

Here is the call graph for this function:

void XMLStorage::XMLStreamReader::init ( TextReader  source,
XmlReaderSettings  settings = null 
) [inline, private]

Definition at line 75 of file XMLStreamReader.cs.

References _node, and _reader.

Referenced by XMLStreamReader().

    {
        _node = new XMLStreamNode();

        _reader = XmlReader.Create(source, settings);
    }
bool XMLStorage::XMLStreamReader::isAtNode ( string  nodeName ) [inline]

Definition at line 104 of file XMLStreamReader.cs.

References _node, and XMLStorage::XMLStreamNode::getName().

    {
        return _node.getName() == nodeName;
    }

Here is the call graph for this function:

IEnumerable<XMLStreamNode> XMLStorage::XMLStreamReader::iterator (  ) [inline]

Iterates over the immediate children of the current XML node.

Definition at line 113 of file XMLStreamReader.cs.

References _node, firstChild(), and next().

Referenced by filterChildren().

    {
        if (firstChild())
            yield return _node;

        while(next())
            yield return _node;
    }

Here is the call graph for this function:

bool XMLStorage::XMLStreamReader::next (  ) [inline]

Move to the next sibling node independent from its name.

Returns:
true on success

Definition at line 269 of file XMLStreamReader.cs.

References _level, and findNode().

Referenced by iterator().

    {
        return findNode(null, _level, _level-1, false);
    }

Here is the call graph for this function:

bool XMLStorage::XMLStreamReader::nextSkipOthers ( string  nodeName ) [inline]

Move to the next sibling node with the specified name.

Returns:
true on success

Definition at line 278 of file XMLStreamReader.cs.

References _level, and findNode().

Referenced by find().

    {
        return findNode(nodeName, _level, _level-1, false);
    }

Here is the call graph for this function:

bool XMLStorage::XMLStreamReader::nextStopOnOthers ( string  nodeName ) [inline]

Move to the next sibling node with the specified name.

Returns:
true on success

Definition at line 287 of file XMLStreamReader.cs.

References _level, and findNode().

    {
        return findNode(nodeName, _level, _level-1, true);
    }

Here is the call graph for this function:

void XMLStorage::XMLStreamReader::processingInstruction ( string  target,
string  data 
) [inline, private]

Definition at line 235 of file XMLStreamReader.cs.

References _instructions.

Referenced by findNode().

    {
        _instructions.Append("<?").Append(target).Append(" ").Append(data).Append("?>");
    }
void XMLStorage::XMLStreamReader::startElement ( string  nodeName ) [inline, private]

Definition at line 211 of file XMLStreamReader.cs.

References _content, _level, _node, _reader, XMLStorage::XMLStreamNode::assign(), and XMLStorage::AbstractXMLNode::setAttributes().

Referenced by findNode().

    {
        _node.assign(nodeName);
        _node.setAttributes(_reader);

        _content = "";

        ++_level;
    }

Here is the call graph for this function:

override string XMLStorage::XMLStreamReader::ToString (  ) [inline]

Definition at line 205 of file XMLStreamReader.cs.

References _node, and XMLStorage::XMLStreamNode::ToString().

    {
        return _node.ToString();
    }

Here is the call graph for this function:


Member Data Documentation

string XMLStorage::XMLStreamReader::_content = "" [protected]

Definition at line 58 of file XMLStreamReader.cs.

Referenced by characters(), endElement(), findNode(), and startElement().

StringBuilder XMLStorage::XMLStreamReader::_instructions = new StringBuilder() [protected]

Definition at line 59 of file XMLStreamReader.cs.

Referenced by getInstructions(), and processingInstruction().

Definition at line 53 of file XMLStreamReader.cs.

Referenced by Dispose(), and XMLStreamReader().

Definition at line 293 of file XMLStreamReader.cs.

Referenced by findNode(), and handlePendingEndElement().

XmlReader XMLStorage::XMLStreamReader::_reader [protected]

Definition at line 54 of file XMLStreamReader.cs.

Referenced by Dispose(), findNode(), handlePendingEndElement(), init(), and startElement().


The documentation for this class was generated from the following file: