Unrecognized elements in custom config files

Here’s one of those nagging quirks that you may never even consider the solution for, unless it was by sheer dumb luck.

The problem is as follows :
You have created a custom configuration section to put all your application settings and created the appropriate classes to read from them. But for some reason, you keep getting “Unrecognized element ‘clear'” or “Unrecognized element ‘add'” errors and every one of your custom elements are rejected as well.

I have the following in my root web.config file inside the <configuration> tag.

<configSections>
	<sectionGroup name="Section12">
		<section name="ForumSettings" 
				 type="Section12.AppSettings.ForumSection"
				 requirePermission="false" 
				 restartOnExternalChanges="true"/>
	</sectionGroup>
</configSections>

<!-- Custom section group with an external forum config file -->
<Section12>
	<ForumSettings configSource="App_Data\Configurations\Forum.config" />
</Section12>

And I have the following in that custom Forum.config file :

<ForumSettings>
	<clear/>
	<add name="ForumTopicsPerPage" value="20" />
	<add name="ForumPostsPerPage" value="10" />
	<!-- ... And so on. You get the idea... -->
</ForumSettings>

The reason for the seperate file is because I wanted to be able to change the app settings at runtime. Editing XML in-app is far simpler, I think, than hitting a database for your settings. But this problem happens even if you don’t use a seperate file for your configurations.

This class is handling my custom forum settings (Section12.AppSettings.ForumSection).

public class ForumSection : ConfigurationSection
{
	[ConfigurationProperty("ForumSettings", IsRequired = true)]
	[ConfigurationCollection(typeof(SettingsCollection), 
		AddItemName="add",
		ClearItemsName="clear",
		RemoveItemName="remove")]
	public SettingsCollection ForumConfig
	{
		get
		{
			SettingsCollection set = (SettingsCollection)base["ForumSettings"];
			return set;
		}
	}
}

And I have the following two classes that handle the settings collection and each setting element

public class SettingsCollection : ConfigurationElementCollection
{
	public SettingsCollection()
	{
		SettingElement key = (SettingElement)CreateNewElement();
		Add(key);
	}

	public override ConfigurationElementCollectionType CollectionType
	{
		get { return ConfigurationElementCollectionType.AddRemoveClearMap; }
	}

	protected override ConfigurationElement CreateNewElement()
	{
		return new SettingElement();
	}

	protected override Object GetElementKey(ConfigurationElement element)
	{
		return ((SettingElement)element).Name;
	}

		public SettingElement this[int index]
	{
		get { return (SettingElement)BaseGet(index); }
		set
		{
			if (BaseGet(index) != null)
				BaseRemoveAt(index);
			
			BaseAdd(index, value);
		}
	}

	new public SettingElement this[string Name]
	{
		get { return (SettingElement)BaseGet(Name); }
	}

	public int IndexOf(SettingElement key)
	{
		return BaseIndexOf(key);
	}

	public void Add(SettingElement key)
	{
		BaseAdd(key);
	}

	protected override void BaseAdd(ConfigurationElement element)
	{
		BaseAdd(element, false);
	}

	public void Remove(SettingElement key)
	{
		if (BaseIndexOf(key) >= 0)
			BaseRemove(key.Name);
	}

	public void RemoveAt(int index)
	{
		BaseRemoveAt(index);
	}

	public void Remove(string name)
	{
		BaseRemove(name);
	}

	public void Clear()
	{
		BaseClear();
	}

}


// Settings element class...
public class SettingElement : ConfigurationElement
{
	public SettingElement() {
		this.Name = "NoName";
		this.Value = "NoValue";
		this.Perm = 0;
	}

	public SettingElement(string name, string value)
	{
		this.Name = name;
		this.Value = value;
	}

	[ConfigurationProperty("name", DefaultValue = "", IsRequired = true, IsKey = true)]
	public string Name
	{
		get { return (string)this["name"]; }
		set { this["name"] = value; }
	}

	[ConfigurationProperty("value", DefaultValue = "", IsRequired = true)]
	public string Value
	{
		get { return (string)this["value"]; }
		set { this["value"] = value; }
	}

	[ConfigurationProperty("perm", DefaultValue = "0", IsRequired = true)]
	[IntegerValidator(MinValue=0, MaxValue=1, ExcludeRange=false)]
	public int Perm
	{
		get { return (int)this["perm"]; }
		set { this["perm"] = value; }
	}
}

Now everything seemed fine and dandy, but it was when I actually tried to run this that I realized I need to stock up on Asprin.

Someone kill me please!

I was scratching my head to no end, until I found a truly WTF?! solution to it. I had to wrap the ForumSettings tag inside another ForumSettings tag!!!

<ForumSettings>
	<ForumSettings>
		<clear/>
		<add name="ForumTopicsPerPage" value="20"></add>
		<add name="ForumPostsPerPage" value="10"></add>
	</ForumSettings>
</ForumSettings>

Maybe I’m too tired, too strung up or too stupid, but there has to be a better solution to this. I hate to blame everything on Microsoft, but considering how hyped C# 4.0 and MVC is right now, it’s pretty silly that this is the only workaround for this problem.

If anyone has a better idea on how to fix this, I’m all ears.

GPL vs BSD

Here we go again!!

I know I will probably stir up the the sediment all over again, but this has to be clarified. If anything because this came up at work yet again. And It’s quite frightful at how ill informed people are on these licenses.

(This will be long and tedious, so feel free to browse away now)

Let’s clarify a few important points right here…

The GPL is not about “freedom”

In fact, the word “freedom” doesn’t even belong in there. The problem is that the GPL, and indeed many software projects that use it, has been turned a religion. And like all religions, it contradicts itself in sentiment… repeatedly.

I don’t have any problem with someone using the GPL in their work, and I’ve been happy using GPL’d products. But I will not treat this license as something it isn’t. That is, a doctrine on freedoms. A license is a license, and by definition all licenses carry restrictions. The GPL carries a particularly ridiculous combination of doctrine, philosphy, and law. Sounds like religion to me…

“The licenses for most software and other practical works are designed to take away your freedom to share and change the works”

The GPL does exactly the same, in that developers have to release every bit of derived or modified code under GPL or face legal action. That is a restriction placed upon the developers and distributors.

“To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others.”

 
So you’re forcing the code to be free because you recieved it free?
You’re denying some of the developers’ rights to give more rights to users?
That’s a lot like “peace… through superior firepower”.

Where’s the developers’ freedom to not distribute the code when distributing the product? Or does that get swept under user rights? I didn’t realise there were different standards for developers and users when it came to “rights”.

The freedom to distribute code or programs is, and has always been, a choice. Much like selling cake vs selling the cake with the recipie. A baker may choose to do both or only one, however, the customer is not forced to do the same by eating the cake thereby entering into a binding contract with the baker. If the customers decide to make cake using improvements to the same recipie, then they’re automatically forced to sell it with the improvements in the recipie?

That’s what the GPL really is. A binding contract : That is a set of restrictions on those who use, develop or modify content licensed under it. It is not now or has ever been a formula on “freedom”. The GPL is not the definition of “generocity” that is giving without expecting any return. I hope all you GPL advocates would stop treating it as such and call it what it is. A license and a binding contract. Nothing more.

If you try to pass it off as anything other than that, then you have problems.

I’m well aware of the motivation of using the GPL and, in theory, it is a noble cause. You’re trying to ensure that quality code remains free and any quality modifications are returned to the community without becoming invisible. While distributing products for free or profit, you also want to ensure the sources are available to the community.
But let’s not call this “freedom”. Free code is just that… free code.

If someone modifies some code and chooses not to distirbute it for free or profit, it still becomes invisible to the community. So this is only for those who choose to distribute their work anyway.

The BSD is not about “freedom” either

It’s about not getting involved in what the end user chooses to do with their copy of the sources and protecting the developers from harm. It’s none of their duty nor concern to police the actions of users.

Specifically, the ISC License…
The only usage restrictions are the disclaimer and copyright.

Copyright (c) Year(s), Company or Person’s Name

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

That’s it!
That’s the whole license.

Notice anything about “freedom” or philosophical ramblings?
Notice anything other than what the license is about and the criteria?
NO!

Unlike the GPL, the BSD license doesn’t pretend to be something it isn’t and users of BSD license are well aware that, like all licenses, it is a binding contract between developers, distributors, and users. They have no delusions about how much “freedom” both licenses afford however the BSD still being a license it still has usage restrictions. Namely the copyright and disclaimer.

Developers using the BSD license don’t care nor want to police the actions of users once the source is copied. They’re not interested in “freedom” through coersion, which is actually slavery. They just want to make sure their products and sources are available from them regardless of need or future availability. If the users want to share their own modifications, then more power to them. But they’ll be damned if it’s by force.

—————

That said, I don’t use the BSD license in examples I’ve posted here. I would have placed them in the public domain if not for one indemnity clause. That’s the only reason every single line of code/HTML/CSS I’ve posted on this blog isn’t in the public domain.

I don’t want to get in trouble when someone using my work didn’t have things go over so well. Except for that, everyone is free to do whatever they please with it. Use it in personal  or professional projects, buy, sell, modify, reverse engineer, give me credit or not give me credit… whatever! I pretty much wash my hands of any responsibility after it’s posted. I don’t expect anything in return.

I’m not forcing anyone who uses any of my examples to treat their work as I do mine. In fact, I’m not forcing anyone to do anything at all with it or while using it. With the exception of the indemnity clause, there is no “law” involved in the source.

I only require anyone using my examples to agree to the following disclaimer :
THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

That’s it! That’s the sum total of the “restrictions” I place on anyone using my examples. Keen observers will note, it’s identical to the disclaimer in the ISC license as I want to make sure that I don’t tread around on someone else’s needs while protecting myself.

In that aspect, it’s the closest I could come to placing everything in the public domain.

Public Domain : That’s freedom, folks.

A utopia by choice is heaven. A utopia by force is hell.

[insert name] Language sucks

So it isn’t just me then. There are many people out there who have their own favorite hatred of [insert name] programming, scripting or query language for various reasons.

Here’s a small list courtesy of Google CodeSearch :

Of course some of the problems mentioned could have been alleviated by using programming best practices from the onset of the project. A lot of the frustration I see is a result of trying to force and wrangle the language to make it do something in a way that could have been accomplished with a different method.

Why fight it when you can maneuver it instead?

Plagiarism today!

Stealing code can be a lot of fun, as long as you don’t get caught. Enter Mr. Karthikeyan.

A while ago, a WordPress user by the name of 0xscore posted a code snippet on his blog, which he has (sadly) deleted. Even though the blog is gone, I will not remove it from my link list out of principle. Hopefully, he will create another blog and start posting.

(Edit_  Thank you Google cache!)

Mr. Karthikeyan found it appropriate to “appropriate” this code and post it on his own blog with no credit to the original author… I was going to post this a while ago, but didn’t get the chance to do so. I will correct this injustice now.

As you can see, in this comment, I politely pointed out to Mr. Karthikeyan that this code had been posted on the previous day. Unsurprisingly, he did not approve it. Instead, he saw it fit to quietly delete the whole post.

Shame on you, Mr. Karthikeyan. Either, you should have posted a link to the original author’s blog or corrected your error and admit that you didn’t orignally include the author’s name. Tsk, tsk.

0xscore, if you get a chance to read this, please come back! You had some interesting code posted and some helpful info. It would be a shame to lose that. I’m sorry your first WordPress experience didn’t go well, but you shouldn’t let it get to you.

Update:

Surprise! Mr. Karthikeyan is at it again. I wouldn’t be surprised if this post were to disappear as well.

This time, the victim is Wikipedia. Granted Wikipedia does allow reproducing its content under the terms of the GPL, however that license also mentions something about citations. Don’t bother searching for a citation or any mention of Wikipedia as the source on that post, other than the links to other terms, you won’t find any.

Update Update…

Hello! we now have the Wikipedia link for the code. Now we will wait and see if the India vs Australia story will have a similar link.

The Wikipedia page :

Mr. Karthikeyan’s “adaptation” :

He didn’t even bother to change a few words around as many other plagiarists would be inclined to do.

The actual code from Wikipedia :

Mr. Karthikeyan’s unaltered version :

Most people tend to copy unknown or semi-obscure sources to protect themselves from this type of identification. Apparently Wikipedia being one of the most popular sites on Earth seems to have slipped his mind.

Here’s a fun activity for the whole family….

Take a peek at his front page as it is now, then Google the term “Sir Edmund Hillary’s most famous quote” in quotation marks.