This is a dumping ground of software development topics that I've run across and found interesting (mainly .NET development). Many topics are covered more thoroughly in other places, and much of the information is gleaned from other places, and I'll try to credit those sources when possible.

Thursday, May 03, 2007

OuterXml for Java

Java can be extremely frustrating, particularily because there always seems to be many many different ways to do a simple task. For example, I needed to get a fragment from an XML document which seems easy enough. .NET has the XmlNode class which has a property OuterXml. Done. However, no such simple equivalent exists for Java, at least in my limited experience. I don't claim to be an XML expert, so I'm not about to write a diatribe. After a couple of hours, I found this guy's blog, which I used as a basis for the method below. You can figure out how to get a org.w3c.dom.Node object, that's another painful tale...


public static String getOuterXml(Node node)
throws TransformerConfigurationException, TransformerException
{
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty("omit-xml-declaration", "yes");

StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(node), new StreamResult(writer));
return writer.toString();
}

Thursday, April 05, 2007

Windows Firewall for FTP

This is silly, but it hung me up for more than a few minutes. I was trying to FTP from a laptop to my desktop PC (running Microsoft's IIS FTP server). The client would claim it was connected, then after a few seconds, report "Connection closed by remote host." So I'm guessing it's a firewall problem, and I'm right (for a change), but it was still not easy to find. So below are the screen shots of the magical checkbox I needed to check.

Thursday, February 01, 2007

Random Password Generator

I needed to create a random password. First I found this short method, which didn't work. Then I found this guy agreed with me. His code works, but not so hot in my test program. The parameterless Random constructor uses a time-based seed value (per millisecond?) so there are duplicates. The code I ended up with reuses a single Random member.

Then there's the holy grail, which was too much for the requirements I was working with, eight alphanumeric digits only. Simplicity is divine.


private Random m_random = new Random();

public string CreateRandomPassword(int length)
{
char[] chars = new char[length];

int index = 0;
while (index < length)
{
char c = (char)m_random.Next(48, 122);
if (Char.IsLetterOrDigit(c))
{
chars[index] = c;
index++;
}
}

return new string(chars);
}

Followers