diff options
author | Gurmeen Bindra <gurmeen.bindra@isode.com> | 2012-04-17 09:29:05 (GMT) |
---|---|---|
committer | Kevin Smith <git@kismith.co.uk> | 2012-04-19 10:45:53 (GMT) |
commit | d9d9451156ee8a4315de30571131d114af205fa1 (patch) | |
tree | 24d0dfb36be867651c39ff6898c944ed035e7f5b /src/com/isode/stroke/parser/tree | |
parent | b54bc5406f95902ce2438ac64a19390ae0f2f409 (diff) | |
download | stroke-d9d9451156ee8a4315de30571131d114af205fa1.zip stroke-d9d9451156ee8a4315de30571131d114af205fa1.tar.bz2 |
Port MUC Payload Parsers from Swiften to Stroke
This patch ports the MUC Payload parsers from swiften to stroke.
Test-information:
ported junits work fine
Diffstat (limited to 'src/com/isode/stroke/parser/tree')
-rw-r--r-- | src/com/isode/stroke/parser/tree/NullParserElement.java | 29 | ||||
-rw-r--r-- | src/com/isode/stroke/parser/tree/ParserElement.java | 128 | ||||
-rw-r--r-- | src/com/isode/stroke/parser/tree/TreeReparser.java | 65 |
3 files changed, 222 insertions, 0 deletions
diff --git a/src/com/isode/stroke/parser/tree/NullParserElement.java b/src/com/isode/stroke/parser/tree/NullParserElement.java new file mode 100644 index 0000000..83f8b6f --- /dev/null +++ b/src/com/isode/stroke/parser/tree/NullParserElement.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2012, Isode Limited, London, England. + * All rights reserved. + */ +/* + * Copyright (c) 2011, Kevin Smith + * All rights reserved. + */ +package com.isode.stroke.parser.tree; + +import com.isode.stroke.parser.AttributeMap; + +/** + * Class representing a Null Parser element + * + */ +public class NullParserElement extends ParserElement{ + /** + * Create the object + */ + public NullParserElement() { + super("", "", new AttributeMap()); + } + + /** + * Empty/Null Parser element + */ + public static NullParserElement element = new NullParserElement(); +} diff --git a/src/com/isode/stroke/parser/tree/ParserElement.java b/src/com/isode/stroke/parser/tree/ParserElement.java new file mode 100644 index 0000000..1a43696 --- /dev/null +++ b/src/com/isode/stroke/parser/tree/ParserElement.java @@ -0,0 +1,128 @@ +/* + * Copyright (c) 2012, Isode Limited, London, England. + * All rights reserved. + */ +/* + * Copyright (c) 2011, Kevin Smith + * All rights reserved. + */ +package com.isode.stroke.parser.tree; + +import java.util.Vector; +import com.isode.stroke.parser.AttributeMap; + +/** + * Class representing a parser for nodes in the stanza + * + */ +public class ParserElement { + private Vector<ParserElement> children_ = new Vector<ParserElement>(); + private String name_ = ""; + private String xmlns_ =""; + private AttributeMap attributes_ = new AttributeMap(); + private String text_ = ""; + + /** + * Create the parser. + * @param name name of the parser node, e.g. query, reason, not null + * @param xmlns XML namespace, e.g. http://jabber.org/protocol/muc#user, not null + * @param attributes attributes of the node, not null + */ + public ParserElement(String name, String xmlns, AttributeMap attributes){ + this.name_ = name; + this.xmlns_ = xmlns; + this.attributes_ = attributes; + } + + /** + * Get the text data of the node + * @return not null, can be empty + */ + public String getText() { + return text_; + } + + /** + * Get the name of the node, not null + * @return name of node + */ + public String getName() { + return name_; + } + + /** + * Get the name space of the XML node, not null + * @return xml namespace + */ + public String getNamespace() { + return xmlns_; + } + + /** + * Get the attributes of the XML node + * @return XMP node attributes, not null + */ + public AttributeMap getAttributes() { + return attributes_; + } + + /** + * Add child of the XML node + * @param name name of child node, not null + * @param xmlns XML namespace, not null + * @param attributes attributes, not null + * @return child node, not null + */ + public ParserElement addChild(String name, String xmlns, AttributeMap attributes){ + ParserElement child = new ParserElement(name, xmlns, attributes); + children_.add(child); + return child; + } + + /** + * Append data to the text of the node + * @param data data to be appended, not null + */ + public void appendCharacterData(String data) { + text_ += data; + } + + /** + * Get the children of the node which have the given name + * and namespace value + * @param name name of node, not null + * @param xmlns namespace of node, not null + * @return list of children, not null but can be empty + */ + public Vector<ParserElement> getChildren(String name, String xmlns) { + Vector<ParserElement> result = new Vector<ParserElement>(); + for(ParserElement child : children_) { + if(child.name_.equals(name) && child.xmlns_.equals(xmlns)) { + result.add(child); + } + } + return result; + } + + /** + * Get all the child nodes of XML element + * @return list of all children, not null but can be empty + */ + public Vector<ParserElement> getAllChildren() { + return children_; + } + + /** + * Get the first child of the node which have the given name + * and namespace value + * @param name name of node, not null + * @param xmlns namespace of node, not null + * @return child node if exists or a parser element {@link NullParserElement} + * representing null value + */ + public ParserElement getChild(String name, String xmlns) { + Vector<ParserElement> results = getChildren(name, xmlns); + ParserElement result = results.isEmpty() ? NullParserElement.element : results.get(0); + return result; + } +} diff --git a/src/com/isode/stroke/parser/tree/TreeReparser.java b/src/com/isode/stroke/parser/tree/TreeReparser.java new file mode 100644 index 0000000..647ab9e --- /dev/null +++ b/src/com/isode/stroke/parser/tree/TreeReparser.java @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2012, Isode Limited, London, England. + * All rights reserved. + */ +/* + * Copyright (c) 2011, Kevin Smith + * All rights reserved. + */ +package com.isode.stroke.parser.tree; + +import java.util.LinkedList; + +import com.isode.stroke.elements.Payload; +import com.isode.stroke.parser.PayloadParser; +import com.isode.stroke.parser.PayloadParserFactoryCollection; + +/** + * Class for parsing elements of a parser element + * + */ +public class TreeReparser { + /** + * Class representing an element and its handling state + * + */ + private static class ElementState { + ParserElement pe; + boolean state; + public ElementState(ParserElement pe, boolean state) { + super(); + this.pe = pe; + this.state = state; + } + } + + /** + * Parse the elements under the given root node + * @param root root node, not null + * @param collection payload parser factories, not null + * @return payload created from the elements of the given node, not null + */ + public static Payload parseTree(ParserElement root, PayloadParserFactoryCollection collection) { + PayloadParser parser = collection.getPayloadParserFactory(root.getName(), root.getNamespace(), + root.getAttributes()).createPayloadParser(); + LinkedList<ElementState> stack = new LinkedList<ElementState>(); + stack.addLast(new ElementState(root, true)); + while (!stack.isEmpty()) { + ElementState current = stack.getLast(); + stack.removeLast(); + if (current.state) { + stack.addLast(new ElementState(current.pe, false)); + parser.handleStartElement(current.pe.getName(), current.pe.getNamespace(), current.pe.getAttributes()); + for(ParserElement child : current.pe.getAllChildren()) { + stack.addLast(new ElementState(child, true)); + } + } else { + parser.handleCharacterData(current.pe.getText()); + parser.handleEndElement(current.pe.getName(), current.pe.getNamespace()); + } + } + + Payload payload = parser.getPayload(); + return payload; + } +} |