<commit_before>// Copyright 2004-2006 Castle Project - http://www.castleproject.org/
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Castle.Components.Binder
{
	using System;
	using System.Collections.Specialized;
	using System.Web;

	/// <summary>
	/// 
	/// </summary>
	public class TreeBuilder
	{
		// TODO: NameObjectCollection / IDictionary / HttpFileCollection
		
		public CompositeNode BuildSourceNode(NameValueCollection nameValueCollection)
		{
			CompositeNode root = new CompositeNode("root");
			
			PopulateTree(root, nameValueCollection);
			
			return root;
		}
		
		public void PopulateTree(CompositeNode root, NameValueCollection nameValueCollection)
		{
			foreach(String key in nameValueCollection.Keys)
			{
				if (key == null) continue;

				String[] values = nameValueCollection.GetValues(key);

				if (values == null) continue;
				
				if (values.Length == 1)
				{
					ProcessNode(root, typeof(String), key, values[0]);
				}
				else
				{
					ProcessNode(root, typeof(String[]), key, values);
				}
			}
		}

		public void PopulateTree(CompositeNode root, HttpFileCollection fileCollection)
		{
			foreach(String key in fileCollection.Keys)
			{
				if (key == null) continue;

				HttpPostedFile value = fileCollection.Get(key);

				if (value == null) continue;
				
				ProcessNode(root, typeof(HttpPostedFile), key, value);
			}
		}
		
		private void ProcessNode(CompositeNode node, Type type, String name, object value)
		{
			if (name == null || name == String.Empty)
			{
				// Not valid
			}
			if (name[0] == '.' || name[0] == '[' || name[0] == ']')
			{
				// Something went wrong
			}
			
			int dotIndex = name.IndexOf('.');
			int startBracketIndex = name.IndexOf('[');
				
			if (dotIndex != -1 && (startBracketIndex == -1 || dotIndex < startBracketIndex))
			{
				// Child node
					
				String childNodeName = name.Substring(0, dotIndex);
				
				CompositeNode newNode = GetOrCreateCompositeNode(node, childNodeName);
					
				ProcessNode(newNode, type, name.Substring(dotIndex + 1), value);
			}
			else if (startBracketIndex != -1)
			{
				// Indexed node
					<commit_msg>Minor adjustment: null keys are ignored. Invalid ones throw exceptions<commit_after>// Copyright 2004-2006 Castle Project - http://www.castleproject.org/
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Castle.Components.Binder
{
	using System;
	using System.Collections.Specialized;
	using System.Web;

	/// <summary>
	/// 
	/// </summary>
	public class TreeBuilder
	{
		// TODO: NameObjectCollection / IDictionary / HttpFileCollection
		
		public CompositeNode BuildSourceNode(NameValueCollection nameValueCollection)
		{
			CompositeNode root = new CompositeNode("root");
			
			PopulateTree(root, nameValueCollection);
			
			return root;
		}
		
		public void PopulateTree(CompositeNode root, NameValueCollection nameValueCollection)
		{
			foreach(String key in nameValueCollection.Keys)
			{
				if (key == null) continue;

				String[] values = nameValueCollection.GetValues(key);

				if (values == null) continue;
				
				if (values.Length == 1)
				{
					ProcessNode(root, typeof(String), key, values[0]);
				}
				else
				{
					ProcessNode(root, typeof(String[]), key, values);
				}
			}
		}

		public void PopulateTree(CompositeNode root, HttpFileCollection fileCollection)
		{
			foreach(String key in fileCollection.Keys)
			{
				if (key == null) continue;

				HttpPostedFile value = fileCollection.Get(key);

				if (value == null) continue;
				
				ProcessNode(root, typeof(HttpPostedFile), key, value);
			}
		}
		
		private void ProcessNode(CompositeNode node, Type type, String name, object value)
		{
			if (name == null || name == String.Empty)
			{
				// Ignore
				return;
			}
			if (name[0] == '.' || name[0] == '[' || name[0] == ']')
			{
				// Something went wrong
				throw new BindingException("Invalid key: " + name);
			}
			
			int dotIndex = name.IndexOf('.');
			int startBracketIndex = name.IndexOf('[');
				
			if (dotIndex != -1 && (startBracketIndex == -1 || dotIndex < startBracketIndex))
			{
				// Child node
					
				String childNodeName = name.Substring(0, dotIndex);
				
				CompositeNode newNode = GetOrCreateCompositeNode(node, childNodeName);
					
				ProcessNode(newNode, type, name.Substring(dotIndex + 1), value);
			}
			else if (startBracketIndex != -1)
			{
				// Indexed node
					
{'fraction_non_alphanumeric': 0.11532792925571113, 'fraction_numerical': 0.008106116433308769, 'mean_word_length': 3.0697151424287856, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5527073', 'n_tokens_mistral': 1949, 'n_tokens_neox': 1559, 'n_words': 472}