/*
Uiml.Net: a .Net UIML renderer (http://research.edm.uhasselt.be/kris/research/uiml.net)
Copyright (C) 2004 Kris Luyten (kris.luyten@uhasselt.be)
Expertise Centre for Digital Media (http://edm.uhasselt.be)
Hasselt University
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation; either version 2.1
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
namespace Uiml{
using System;
using System.Xml;
using System.Collections;
///
///Implementation for the template element
/// <!ELEMENT template
/// (behavior|constant|content|d-class|d-component|
/// interface |logic|part|peers|presentation|property|
/// restructure|rule |script|structure|style)>
/// <!ATTLIST template id NMTOKEN #IMPLIED>
///
/// Templates that are resolved only if they are "sourced" by an element.
/// This means we have to execute an open file operation each time an element
/// sources a template.
///
public class Template : IUimlElement{
public String m_identifier;
public IUimlElement m_top;
public Template()
{
}
public Template(string identifier)
{
m_identifier = identifier;
//TODO: remove UimlTool reference
//LoadTemplate(identifier, UimlTool.FileName);
}
public Template(Uri turi)
{
m_identifier = turi.Fragment;
if (m_identifier != "" && m_identifier[0] == '#')
m_identifier = m_identifier.Substring(1); // cut off '#'
//split the identifier and the actual uri
LoadTemplate(m_identifier, turi.GetLeftPart(UriPartial.Path));
}
public Template(XmlNode n) : this()
{
Process(n);
}
private void LoadTemplate(string tidentifier, string path)
{
if(TemplateRepository.Instance.Query(tidentifier)==null)
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
//search for the template with identifier tidentifier
//and process it accordingly
XmlNode n = SearchTemplate(doc, tidentifier);
if(n==null)
Console.WriteLine("Template {0} could not be found in {1}",tidentifier, path);
else
Process(n);
}
else
throw new TemplateAlreadyProcessedException(tidentifier);
}
///
///Does a recursive search for a template with the identifier tidentifier.
///
private XmlNode SearchTemplate(XmlNode xmlNode, String tidentifier)
{
XmlNode found=null;
XmlAttributeCollection attr = xmlNode.Attributes;
if((xmlNode.Name == IAM) && (attr.GetNamedItem(ID) != null))
{
if(attr.GetNamedItem(ID).Value == tidentifier)
return xmlNode;
}
if(xmlNode.HasChildNodes)
{
XmlNodeList xnl = xmlNode.ChildNodes;
for(int i=0; i