2
0
Эх сурвалжийг харах

Merge pull request #4709 from sebthom/xml-parser-position-patch

report line number and char position on xml parsing errors
Nicolas Cannasse 9 жил өмнө
parent
commit
2249beb462
1 өөрчлөгдсөн 76 нэмэгдсэн , 21 устгасан
  1. 76 21
      std/haxe/xml/Parser.hx

+ 76 - 21
std/haxe/xml/Parser.hx

@@ -46,6 +46,59 @@ extern private class S {
 	public static inline var ESCAPE			= 18;
 }
 
+class XmlParserException
+{
+	/**
+	 * the XML parsing error message
+	 */
+	public var message:String;
+	
+	/**
+	 * the line number at which the XML parsing error occured
+	 */
+	public var lineNumber:Int;
+	
+	/**
+	 * the character position in the reported line at which the parsing error occured
+	 */
+	public var positionAtLine:Int;
+	
+	/**
+	 * the character position in the XML string at which the parsing error occured
+	 */
+	public var position:Int;
+	
+	/**
+	 * the invalid XML string
+	 */
+	public var xml:String;
+	
+	public function new(message:String, xml:String, position:Int)
+	{
+		this.xml = xml;
+		this.message = message;
+		this.position = position;
+		lineNumber = 1;
+		positionAtLine = 0;
+		
+		for( i in 0...position)
+		{
+			var c = xml.fastCodeAt(i);
+			if (c == '\n'.code) {
+				lineNumber++;
+				positionAtLine = 0;
+			} else {
+				if (c != '\r'.code) positionAtLine++;
+			}
+		}
+	}
+	
+	public function toString():String
+	{
+		return Type.getClassName(Type.getClass(this)) + ": " + message + " at line " + lineNumber + " char " + positionAtLine;
+	}
+}
+
 class Parser
 {
 	static var escapes = {
@@ -59,8 +112,10 @@ class Parser
 	}
 
 	/**
-		Parses the String into an XML Document. Set strict parsing to true in order to enable a strict check of XML attributes and entities.
-	**/
+	 * Parses the String into an XML Document. Set strict parsing to true in order to enable a strict check of XML attributes and entities.
+	 * 
+	 * @throws haxe.xml.XmlParserException
+	 */
 	static public function parse(str:String, strict = false)
 	{
 		var doc = Xml.createDocument();
@@ -144,7 +199,7 @@ class Parser
 							{
 								p += 2;
 								if (str.substr(p, 6).toUpperCase() != "CDATA[")
-									throw("Expected <![CDATA[");
+									throw new XmlParserException("Expected <![CDATA[", str, p);
 								p += 5;
 								state = S.CDATA;
 								start = p + 1;
@@ -152,13 +207,13 @@ class Parser
 							else if (str.fastCodeAt(p + 1) == 'D'.code || str.fastCodeAt(p + 1) == 'd'.code)
 							{
 								if(str.substr(p + 2, 6).toUpperCase() != "OCTYPE")
-									throw("Expected <!DOCTYPE");
+									throw new XmlParserException("Expected <!DOCTYPE", str, p);
 								p += 8;
 								state = S.DOCTYPE;
 								start = p + 1;
 							}
 							else if( str.fastCodeAt(p + 1) != '-'.code || str.fastCodeAt(p + 2) != '-'.code )
-								throw("Expected <!--");
+								throw new XmlParserException("Expected <!--", str, p);
 							else
 							{
 								p += 2;
@@ -170,7 +225,7 @@ class Parser
 							start = p;
 						case '/'.code:
 							if( parent == null )
-								throw("Expected node name");
+								throw new XmlParserException("Expected node name", str, p);
 							start = p + 1;
 							state = S.IGNORE_SPACES;
 							next = S.CLOSE;
@@ -183,7 +238,7 @@ class Parser
 					if (!isValidChar(c))
 					{
 						if( p == start )
-							throw("Expected node name");
+							throw new XmlParserException("Expected node name", str, p);
 						xml = Xml.createElement(str.substr(start, p - start));
 						addChild(xml);
 						state = S.IGNORE_SPACES;
@@ -207,11 +262,11 @@ class Parser
 					{
 						var tmp;
 						if( start == p )
-							throw("Expected attribute name");
+							throw new XmlParserException("Expected attribute name", str, p);
 						tmp = str.substr(start,p-start);
 						aname = tmp;
 						if( xml.exists(aname) )
-							throw("Duplicate attribute");
+							throw new XmlParserException("Duplicate attribute [" + aname + "]", str, p);
 						state = S.IGNORE_SPACES;
 						next = S.EQUALS;
 						continue;
@@ -223,7 +278,7 @@ class Parser
 							state = S.IGNORE_SPACES;
 							next = S.ATTVAL_BEGIN;
 						default:
-							throw("Expected =");
+							throw new XmlParserException("Expected =", str, p);
 					}
 				case S.ATTVAL_BEGIN:
 					switch(c)
@@ -234,7 +289,7 @@ class Parser
 							start = p + 1;
 							attrValQuote = c;
 						default:
-							throw("Expected \"");
+							throw new XmlParserException("Expected \"", str, p);
 					}
 				case S.ATTRIB_VAL:
 					switch (c) {
@@ -245,7 +300,7 @@ class Parser
 							start = p + 1;
 						case '>'.code | '<'.code if( strict ):
 							// HTML allows these in attributes values
-							throw "Invalid unescaped " + String.fromCharCode(c) + " in attribute value";
+							throw new XmlParserException("Invalid unescaped " + String.fromCharCode(c) + " in attribute value", str, p);
 						case _ if (c == attrValQuote):
 							buf.addSub(str, start, p - start);
 							var val = buf.toString();
@@ -264,7 +319,7 @@ class Parser
 						case '>'.code:
 							state = S.BEGIN;
 						default :
-							throw("Expected >");
+							throw new XmlParserException("Expected >", str, p);
 					}
 				case S.WAIT_END_RET:
 					switch(c)
@@ -274,17 +329,17 @@ class Parser
 								parent.addChild(Xml.createPCData(""));
 							return p;
 						default :
-							throw("Expected >");
+							throw new XmlParserException("Expected >", str, p);
 					}
 				case S.CLOSE:
 					if (!isValidChar(c))
 					{
 						if( start == p )
-							throw("Expected node name");
+							throw new XmlParserException("Expected node name", str, p);
 
 						var v = str.substr(start,p - start);
 						if (v != parent.nodeName)
-							throw "Expected </" +parent.nodeName + ">";
+							throw new XmlParserException("Expected </" +parent.nodeName + ">", str, p);
 
 						state = S.IGNORE_SPACES;
 						next = S.WAIT_END_RET;
@@ -339,13 +394,13 @@ class Parser
 									buf.addChar(0x80 | ((c >> 6) & 63));
 									buf.addChar(0x80 | (c & 63));
 								} else
-									throw "Cannot encode UTF8-char " + c;
+									throw new XmlParserException("Cannot encode UTF8-char " + c, str, p);
 							} else
 							#end
 							buf.addChar(c);
 						} else if (!escapes.exists(s)) {
 							if( strict )
-								throw 'Undefined entity: $s';
+								throw new XmlParserException("Undefined entity: " + s, str, p);
 							buf.add('&$s;');
 						} else {
 							buf.add(escapes.get(s));
@@ -354,7 +409,7 @@ class Parser
 						state = escapeNext;
 					} else if (!isValidChar(c) && c != "#".code) {
 						if( strict )
-							throw 'Invalid character in entity: ' + String.fromCharCode(c);
+							throw new XmlParserException("Invalid character in entity: " + String.fromCharCode(c), str, p);
 						buf.addChar("&".code);
 						buf.addSub(str, start, p - start);
 						p--;
@@ -387,10 +442,10 @@ class Parser
 			return p;
 		}
 
-		throw "Unexpected end";
+		throw new XmlParserException("Unexpected end", str, p);
 	}
 
 	static inline function isValidChar(c) {
 		return (c >= 'a'.code && c <= 'z'.code) || (c >= 'A'.code && c <= 'Z'.code) || (c >= '0'.code && c <= '9'.code) || c == ':'.code || c == '.'.code || c == '_'.code || c == '-'.code;
 	}
-}
+}