/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace Avro
{
///
/// Class to store schema name, namespace and enclosing namespace
///
public class SchemaName
{
///
/// Name of the schema
///
public String Name { get; private set; }
///
/// Namespace specified within the schema
///
public String Space { get; private set; }
///
/// Namespace from the most tightly enclosing schema
///
public String EncSpace { get; private set; }
///
/// Namespace.Name of the schema
///
public String Fullname { get { return string.IsNullOrEmpty(Namespace) ? this.Name : Namespace + "." + this.Name; } }
///
/// Namespace of the schema
///
public String Namespace { get { return string.IsNullOrEmpty(this.Space) ? this.EncSpace : this.Space; } }
///
/// Constructor for SchemaName
///
/// name of the schema
/// namespace of the schema
/// enclosing namespace of the schema
public SchemaName(String name, String space, String encspace)
{
if (name == null)
{ // anonymous
this.Name = this.Space = null;
this.EncSpace = encspace; // need to save enclosing namespace for anonymous types, so named types within the anonymous type can be resolved
}
else if (!name.Contains("."))
{ // unqualified name
this.Space = space; // use default space
this.Name = name;
this.EncSpace = encspace;
}
else
{
string[] parts = name.Split('.');
this.Space = string.Join(".", parts, 0, parts.Length - 1);
this.Name = parts[parts.Length - 1];
this.EncSpace = encspace;
}
}
///
/// Returns the full name of the schema
///
///
public override string ToString()
{
return Fullname;
}
///
/// Writes the schema name in JSON format
///
/// JSON writer
/// list of named schemas already written
/// enclosing namespace of the schema
internal void WriteJson(Newtonsoft.Json.JsonTextWriter writer, SchemaNames names, string encspace)
{
if (null != this.Name) // write only if not anonymous
{
JsonHelper.writeIfNotNullOrEmpty(writer, "name", this.Name);
if (!String.IsNullOrEmpty(this.Space))
JsonHelper.writeIfNotNullOrEmpty(writer, "namespace", this.Space);
else if (!String.IsNullOrEmpty(this.EncSpace)) // need to put enclosing name space for code generated classes
JsonHelper.writeIfNotNullOrEmpty(writer, "namespace", this.EncSpace);
}
}
///
/// Compares two schema names
///
/// SchameName object to compare against this object
/// true or false
public override bool Equals(Object obj)
{
if (obj == this) return true;
if (obj != null && obj is SchemaName)
{
SchemaName that = (SchemaName)obj;
return areEqual(that.Name, Name) && areEqual(that.Namespace, Namespace);
}
return false;
}
///
/// Compares two objects
///
/// first object
/// second object
/// true or false
private static bool areEqual(object obj1, object obj2)
{
return obj1 == null ? obj2 == null : obj1.Equals(obj2);
}
public override int GetHashCode()
{
return string.IsNullOrEmpty(Fullname) ? 0 : 29 * Fullname.GetHashCode();
}
}
///
/// A class that contains a list of named schemas. This is used when reading or writing a schema/protocol.
/// This prevents reading and writing of duplicate schema definitions within a protocol or schema file
///
public class SchemaNames
{
///
/// Map of schema name and named schema objects
///
public IDictionary Names { get; private set; }
///
/// Constructor
///
public SchemaNames()
{
Names = new Dictionary();
}
///
/// Checks if given name is in the map
///
/// schema name
/// true or false
public bool Contains(SchemaName name)
{
if (Names.ContainsKey(name))
return true;
return false;
}
///
/// Adds a schema name to the map if it doesn't exist yet
///
/// schema name
/// schema object
/// true if schema was added to the list, false if schema is already in the list
public bool Add(SchemaName name, NamedSchema schema)
{
if (Names.ContainsKey(name))
return false;
Names.Add(name, schema);
return true;
}
///
/// Adds a named schema to the list
///
/// schema object
/// true if schema was added to the list, false if schema is already in the list
public bool Add(NamedSchema schema)
{
SchemaName name = schema.SchemaName;
return Add(name, schema);
}
///
/// Tries to get the value for the given name fields
///
/// name of the schema
/// namespace of the schema
/// enclosing namespace of the schema
/// schema object found
/// true if name is found in the map, false otherwise
public bool TryGetValue(string name, string space, string encspace, out NamedSchema schema)
{
SchemaName schemaname = new SchemaName(name, space, encspace);
return Names.TryGetValue(schemaname, out schema);
}
///
/// Returns the enumerator for the map
///
///
public IEnumerator> GetEnumerator()
{
return Names.GetEnumerator();
}
}
}