Deleting my Blog
It is done.
I deleted my blog. From java.blogs. Don’t get me wrong, I appreciate all of the traffic they provide, but by actively pushing my blog to their service, I give up a little bit of freedom.
Specifically, I lose the freedom to say:
If you don’t like what I say, feel free to go away.
And I can follow up with a little smiley, which makes everything all better:
See? You feel better already!
Plus, I’m writing less and less about Java, which makes me feel guilty for posting to java.blogs. Here is some Ruby code Mark and I worked on yesterday:
require 'rexml/document'
# This returns a normalized version of a given XML string.
def normalize(xml_str)
# Convert the XML string to a Document object and back to a string
# to normalize it. This changes single quotes around attribute values
# to double quotes and sorts the attributes within each element.
normalized_str = REXML::Document.new(xml_str).to_s
# Remove ignorable whitespace and carriage returns
# from each line in the XML string.
s = ""
normalized_str.split("\n").each { |line| s << line.strip }
s
end
I used this in some unit tests to compare some expected XML to the actual XML produced by a Ruby class, ignoring insignificant whitespace.
Blog On…
And there you have it. I am not forcing you to read this. These are just my opinions, and you are free to subscribe or unsubscribe at will. (unless you live in North Korea, where you probably don’t have the freedom to subscribe…sorry!)
And unless you drop the F bomb or are a blatant spammer, I’ll let you leave comments all day long. You don’t even have to give your real name.
I assume you don’t mean “ignorable whitespace” in the strict sense (strict meaning that w/o a schema, the only “ignorable” whitespace is that outside of the root element).
I’m not going anywhere!
Yeah, it’s pretty crude…probably missing all sorts of XML whitespace variations, but it seems to work for my tests. I’ve basically got a big HERE block with a chunk of expected XML, and the indentation was hosing up the test. While REXML has some pretty-print capability, I wasn’t able to coerce it into consistently indented tags. In Java, I typically use JDOM to convert to/from a Document and a String, which does seem to be more consistent about pretty-printing.
Hi Eric,
just a small suggestion to refactor your code:
normalized_str.split("\n").map { |line| line.strip }.join
You see, you don’t need intermediate variables: just apply the block (
{|line| line.strip}) to the array returned by split and then join them.joinuses its first argument as the separator, or the contents of the global variable ($,),nilby default.Have a good journey down the Ruby way ;).
-vjt