// Copyright 2003-2004 The Apache Software Foundation
//
// 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 Apache.Avalon.Composition.Data
{
using System;
///
A target is a tagged configuration fragment. The tag is a path
/// seperated by "/" charaters qualifying the component that the target
/// configuration is to be applied to.
///
///
/// Stephen McConnell
///
/// $Revision: 1.2 $ $Date: 2004/02/28 22:15:36 $
///
public class Targets
{
//========================================================================
// state
//========================================================================
/// The set of targets.
private TargetDirective[] m_targets;
//========================================================================
// constructors
//========================================================================
/// Create an empty Targets instance.
public Targets()
{
m_targets = new TargetDirective[0];
}
/// Create a new Targets instance.
///
///
/// the set of targets
///
public Targets(TargetDirective[] targets)
{
m_targets = targets;
}
//========================================================================
// implementation
//========================================================================
/// Return all targets.
///
///
/// all the targets in this targets instance.
///
public virtual TargetDirective[] getTargets()
{
return m_targets;
}
/// Return a matching target.
///
///
/// the target path to lookup
///
/// the target or null if no matching target
///
public virtual TargetDirective getTarget(System.String path)
{
System.String key = getKey(path);
for (int i = 0; i < m_targets.Length; i++)
{
TargetDirective target = m_targets[i];
if (target.Path.Equals(key))
{
return target;
}
}
return null;
}
/// Return a set of targets relative to the supplied path.
///
///
/// the base path to match against
///
/// the set of relative targets
///
public virtual Targets getTargets(System.String path)
{
System.String key = getKey(path);
System.Collections.ArrayList list = new System.Collections.ArrayList();
for (int i = 0; i < m_targets.Length; i++)
{
TargetDirective target = m_targets[i];
if (target.Path.StartsWith(key))
{
System.String name = target.Path.Substring(key.Length);
if (name.Length > 0)
{
list.Add(new TargetDirective(getKey(name), target.Configuration, target.CategoriesDirective));
}
}
}
return new Targets( (TargetDirective[]) list.ToArray( typeof(TargetDirective[]) ) );
}
/// Convert the supplied path to a valid path.
/// the path to convert
///
/// a good path value
///
private System.String getKey(System.String path)
{
if (!path.StartsWith("/"))
{
return "/" + path;
}
return path;
}
/// Return a string representation of the target.
/// a string representing the target instance
///
public override System.String ToString()
{
System.Text.StringBuilder buffer = new System.Text.StringBuilder("[targets: ");
for (int i = 0; i < m_targets.Length; i++)
{
buffer.Append(m_targets[i]);
if (i < (m_targets.Length - 1))
{
buffer.Append(", ");
}
}
buffer.Append(" ]");
return buffer.ToString();
}
}
}