# This file is part of NIT ( http://www.nitlanguage.org ). # # This file is free software, which comes along with NIT. This software is # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A # PARTICULAR PURPOSE. You can modify it is you want, provided this header # is kept unaltered, and a notification of the changes is added. # You are allowed to redistribute it and sell it, alone or is a part of # another product. # Basic blocks for DOM-XML representation # # DOM entities are defined in this module, specifically: # # * `XMLEntity`: Abstract kind of XML-related node # * `XMLDocument`: A well-formed XML document, root of the tree # * `PCDATA`: Raw XML-escaped character data # * `CDATA`: Raw data, may contain invalid XML escape characters # * `XMLTag`: Abstract XML tag element # * `XMLAttrTag`: Abstract XML element, they may contain attributes # * `XMLOnelinerTag`: Any tag contained on one-line only # * `XMLStartTag`: A tag starting a new hierarchy level in the tree # * `XMLPrologTag`: A tag containing meta-information on the document, must start with " end # A Tag is a node in a DOM tree abstract class XMLTag super XMLEntity # The name of the tag var tag_name: String end # Any kind of XML tag with attributes abstract class XMLAttrTag super XMLTag # List of attributes in a Tag var attributes: Array[XMLAttribute] end # One-liner XML Tag (Ends with />) class XMLOnelinerTag super XMLAttrTag redef fun to_s do var s = "<{tag_name}" if not attributes.is_empty then s += " " s += attributes.join(" ") end s += "/>" return s end end # A (potentially) multi-line spanning XML Tag start class XMLStartTag super XMLAttrTag # Optional matching tag, must be matched for the document to be well-formed var matching: nullable XMLEndTag redef fun to_s do var s = "<{tag_name}" if not attributes.is_empty then s += " " s += attributes.join(" ") end s += ">" for i in children do s += i.to_s var matching = self.matching if matching != null then s += matching.to_s return s end end # Any prolog style-Tag (starting with """ end # Processing instructions start with " end # An end Tag (starting with " end # An XML comment tag class XMLCommentTag super XMLTag redef fun to_s do return "" end # A DOCTYPE Tag class XMLDoctypeTag super XMLTag # Raw content var content: String redef fun to_s do return "" end # A Special Tag (starts with !) # # TODO: Support the remaining ! tags class XMLSpecialTag super XMLTag redef fun to_s do return "" end # Attributes are contained in tags, they provide meta-information on a tag abstract class XMLAttribute super XMLEntity # Name of the attribute var name: String end # An attribute with a String value class XMLStringAttr super XMLAttribute # Value of the attribute without the double quotes var value: String # Type of delimiter (can be either " or ') var delimiter: Char redef fun to_s do return "{name}={delimiter}{value}{delimiter}" end # Badly formed XML attribute class BadXMLAttribute super XMLAttribute redef fun to_s do return name end # Internal use only, shows the end of an attribute block private class XMLAttributeEnd super XMLAttribute end # Any XML Error that happens when parsing class XMLError super XMLEntity # Error message reported by the parser var message: String redef fun to_s do var l = self.location if l == null then return "XML Error: {message}" else return "XML Error: {message} at {l}" end end end