Re: Patch for XML pretty printer

Jann Röder <[email protected]> Thu, 3 Sep 2009 23:52:16 +0200
Newsgroups gmane.comp.lang.eiffel.gobo.devel
Message-ID <[email protected]>
Ok,
I updated my patch with better feature names and comments and I made the 
use of "empty element tags" optional, disabled by default.

Jann

PS: Can anyone comment on the XML declaration issue?

Eric Bezault schrieb:
> Jocelyn wrote:
>> I haven't looked at the patch, but it might be easy to support both behavior
>> with short tag <foo/>  and <foo></foo> using a boolean attribute, or 
>> flag for the executable.
>>
>> This way, this won't break existing programs
> 
> Either that, or write a descendant of the pretty-printer class
> and let dynamic binding use one or the other form.
>

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july

_______________________________________________
gobo-eiffel-develop mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gobo-eiffel-develop
tags.patch (text/plain, 4.2 KB)
Index: xm_indent_pretty_print_filter.e
===================================================================
--- xm_indent_pretty_print_filter.e	(revision 6665)
+++ xm_indent_pretty_print_filter.e	(working copy)
@@ -70,6 +70,8 @@
 		do
 			check space_preserved_not_void: space_preserved /= Void end
 
+			flush_pending_tag_end
+
 			if not has_content then
 				if is_root then
 					is_root := False
@@ -105,14 +107,18 @@
 		do
 			depth := depth - 1
 
-			if not has_content then
-				output_indent_new_line
-				output_indent
+			if last_call_was_start_tag_finish and empty_element_tags_enabled then
+				Precursor (a_namespace, a_prefix, a_local_part)
+			else
+				if not has_content then
+					output_indent_new_line
+					output_indent
+				end
+
+				Precursor (a_namespace, a_prefix, a_local_part)
 			end
+
 			has_content := False
-
-			Precursor (a_namespace, a_prefix, a_local_part)
-
 			space_preserved.remove
 		end
 
Index: xm_pretty_print_filter.e
===================================================================
--- xm_pretty_print_filter.e	(revision 6665)
+++ xm_pretty_print_filter.e	(working copy)
@@ -45,6 +45,8 @@
 			output_constant (Space_s)
 			output (a_content)
 			output_constant (Pi_end)
+
+			last_call_was_start_tag_finish := False
 			Precursor (a_name, a_content)
 		end
 
@@ -54,6 +56,8 @@
 			output_constant (Comment_start)
 			output (a_content)
 			output_constant (Comment_end)
+
+			last_call_was_start_tag_finish := False
 			Precursor (a_content)
 		end
 
@@ -62,36 +66,57 @@
 	on_start_tag (a_namespace: STRING; a_prefix: STRING; a_local_part: STRING) is
 			-- Print start of start tag.
 		do
+			flush_pending_tag_end
+
 			output_constant (Stag_start)
 			output_name (a_prefix, a_local_part)
+
+			last_call_was_start_tag_finish := False
 			Precursor (a_namespace, a_prefix, a_local_part)
 		end
 
 	on_attribute (a_namespace: STRING; a_prefix: STRING; a_local_part: STRING; a_value: STRING) is
 			-- Print attribute.
 		do
+			flush_pending_tag_end
+
 			output_constant (Space_s)
 			output_name (a_prefix, a_local_part)
 			output_constant (Eq_s)
 			output_constant (Quot_s)
 			output_quote_escaped (a_value)
 			output_constant (Quot_s)
+
+			last_call_was_start_tag_finish := False
 			Precursor (a_namespace, a_prefix, a_local_part, a_value)
 		end
 
 	on_start_tag_finish is
 			-- Print end of start tag.
 		do
-			output_constant (Stag_end)
+			if empty_element_tags_enabled then
+				is_tag_end_pending := True
+			else
+				output_constant (stag_end)
+			end
+			last_call_was_start_tag_finish := True
+
 			Precursor
 		end
 
 	on_end_tag (a_namespace: STRING; a_prefix: STRING; a_local_part: STRING) is
 			-- Print end tag.
 		do
-			output_constant (Etag_start)
-			output_name (a_prefix, a_local_part)
-			output_constant (Etag_end)
+			if last_call_was_start_tag_finish and empty_element_tags_enabled then
+				output_constant (emptytag_end)
+				is_tag_end_pending := False
+			else
+				output_constant (Etag_start)
+				output_name (a_prefix, a_local_part)
+				output_constant (Etag_end)
+			end
+
+			last_call_was_start_tag_finish := False
 			Precursor (a_namespace, a_prefix, a_local_part)
 		end
 
@@ -102,10 +127,49 @@
 			-- NOT atomic: successive content may be different.
 			-- Default: forward event to 'next'.
 		do
+			flush_pending_tag_end
+
 			output_escaped (a_content)
+
+			last_call_was_start_tag_finish := False
 			Precursor (a_content)
 		end
 
+feature -- Settings
+
+	empty_element_tags_enabled: BOOLEAN
+			-- Do we use empty element tags
+			-- i.e. <tag/> instead of <tag><tag/>
+
+	enable_empty_element_tags is
+			-- Use empty element tags
+		do
+			empty_element_tags_enabled := True
+		end
+
+	disable_empty_element_tags is
+			-- Do not use empty element tags
+		do
+			empty_element_tags_enabled := False
+		end
+
+feature {NONE} -- Implementation
+
+	last_call_was_start_tag_finish: BOOLEAN
+			-- Was the last `on_*' feature called `on_start_tag_finished' ?
+
+	is_tag_end_pending: BOOLEAN
+			-- Do we have a pending ">" or "/>" to be written ?
+
+	flush_pending_tag_end is
+			-- Output the pending tag end, if any
+		do
+			if is_tag_end_pending then
+				output_constant (stag_end)
+				is_tag_end_pending := False
+			end
+		end
+
 feature {NONE} -- Escaped
 
 	is_escaped (a_char: INTEGER): BOOLEAN is