package net.xoetrope.xml.nanoxml;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;
import net.n3.nanoxml.IXMLElement;
import net.n3.nanoxml.XMLElement;
import net.xoetrope.xml.XmlElement;
/**
* <p>A customization of the XmlElement to interface with NanoXML</p>
* <p>Copyright (c) Xoetrope Ltd., 1998-2003<br>
* License: see license.txt
* @version $Revision: 2.3 $
*/
public class NanoXmlElement implements XmlElement
{
/**
* The implementing object
*/
protected IXMLElement element;
/**
* Create the implementing object
*/
public NanoXmlElement()
{
element = new XMLElement();
}
/**
* ctor which passes the implementing object as a parameter
* @param e The instance of the IXMLElement which is to be used as the
* implementing object
*/
public NanoXmlElement( IXMLElement e )
{
element = e;
}
/**
* Create the implementing object with its name equal to that of the passed
* name parameter
* @param name The name to be assigned to the implementing XmlElement
*/
public NanoXmlElement( String name )
{
element = new XMLElement( name );
}
/**
* Retrieve the XmlElement at the requested position of the implementing
* object
* @param i The index of the XmlElement required
* @return The XmlElement at the specified index
*/
public XmlElement elementAt( int i )
{
return new NanoXmlElement( element.getChildAtIndex( i ));
}
/**
* Retrieve the named attribute of the implementing XmlElement
* @param name The name of the attribute to be retrieved
* @return The value of the attribute
*/
public String getAttribute( String name )
{
return element.getAttribute( name );
}
/**
* Retrieve a Vector of child XmlElements contained in the implementing
* object
* @return A Vector of child XmlElements
*/
public Vector getChildren()
{
Vector temp = new Vector();
Vector children = element.getChildren();
int numChildren = children.size();
for ( int i = 0; i < numChildren; i++ ) {
temp.addElement( new NanoXmlElement( (IXMLElement)children.elementAt( i )));
}
return temp;
}
/**
* Retrieve a Vector of child XmlElements contained in the implementing
* object at the path specified by the path parameter
* @param path The path into the implementing object from which the return
* Vector is to be made up
* @return A Vector of child XmlElements
*/
public Vector getChildren( String path )
{
Vector temp = new Vector();
Vector children = element.getChildrenNamed( path );
int numChildren = children.size();
for ( int i = 0; i < numChildren; i++ ) {
temp.addElement( new NanoXmlElement( (IXMLElement)children.elementAt( i )));
}
return temp;
}
/**
* Retrieve the name of the implementing XmlElement object
* @return The name of the implementing object
*/
public String getName()
{
return element.getName();
}
/**
* Set the name of the implementing XmlElement object
* @param newName The name to be applied to the implementing XmlElement object
*/
public void setName( String newName )
{
element.setName( newName );
}
/**
* Retrieve the content of the implementing XmlElement
* @return The implementing object's content
*/
public String getContent()
{
return element.getContent();
}
/**
* Set the content of the implementing XmlElement
* @param value the implementing object's content
*/
public void setContent( String value )
{
element.setContent( value );
}
/**
* Retrieve and Enumeration of the implementing object's attributes
* @return An Enumeration of the implementing object's attribute names
*/
public Enumeration enumerateAttributeNames()
{
return element.enumerateAttributeNames();
}
/**
* Set the value of the named attribute in the implementing XmlElement object
* @param name The name of the attribute to be set
* @param value The new value of the attribute
*/
public void setAttribute( String name, String value )
{
element.setAttribute( name, value );
}
/**
* Add the passed child to the implementing object
* @param child The XmlElement to be added to the implementing object
*/
public void addChild( XmlElement child )
{
if ( child instanceof NanoXmlElement )
element.addChild( (XMLElement)child.getImplementation() );
}
/**
* Create a basic XmlElement of the type registered in the XmlParserFactory
* @param name The name of the new element
* @return The created XmlElement
*/
public XmlElement createElement( String name )
{
IXMLElement ele = element.createElement( name );
return new NanoXmlElement( ele );
}
/**
* Retrieve the first XmlElement found at the path specified by the name
* parameter of the implementing XmlElement
* @param name The path to the element of the implementing XmlElement object's
* to be found
* @return The first instance of an XmlElement found at the specified path of
* the implementing object
*/
public XmlElement getFirstChildNamed( String name )
{
IXMLElement ele = element.getFirstChildNamed( name );
return ele == null ? null : new NanoXmlElement( ele );
}
/**
* Retrieve the namespace of the implementation object
* @return The implementation objects namespace
*/
public String getNamespace()
{
return element.getNamespace();
}
/**
* Retrieve a count of the implementing classes attributes
* @return The amount of attributes contained in the implementing object
*/
public int getAttributeCount()
{
return element.getAttributeCount();
}
/**
* Retrieve the attributes set
* @return The attributes contained in the implementing object
*/
public Properties getAttributes()
{
return element.getAttributes();
}
/**
* Retrieve the namespace of the named attribute of the implementation object
* @param name The name of the attributes namespace
* @return The name of the named attribute namespace
*/
public String getAttributeNamespace( String name )
{
return element.getAttributeNamespace( name );
}
/**
* Retrieve the XmlElement implementation object
* @return The XmlElement object
*/
public Object getImplementation()
{
return element;
}
/**
* Retrive the implementing XmlElement object
* @return the implementing XmlElement object
*/
public IXMLElement getElement()
{
return element;
}
/**
* Remove the specified child element
* @param child the child to be removed
*/
public void remove( XmlElement child )
{
element.removeChild( ((NanoXmlElement)child).getElement() );
}
/**
* Remove the specified child attribute
* @param name the name of the attribute to be removed
*/
public void removeAttribute( String name )
{
element.removeAttribute( name );
}
/**
* Produce a deep copy of this element
* @return the copy
*/
public Object clone()
{
XMLElement clone = clone( element );
return new NanoXmlElement( clone );
}
private XMLElement clone( IXMLElement e )
{
XMLElement clone = new XMLElement( e.getName());
// Setting the content seems to wipe the children LOC 04/03/2007
String content = e.getContent();
if ( content != null ) {
content.trim();
if ( content.length() > 0 )
clone.setContent( content );
}
Enumeration keys = e.getAttributes().keys();
while ( keys.hasMoreElements()) {
String attribName = keys.nextElement().toString();
clone.setAttribute( attribName, e.getAttribute( attribName ));
}
Vector kids = e.getChildren();
int numKids = kids.size();
for ( int i = 0; i < numKids; i++ ) {
XMLElement o = (XMLElement)kids.elementAt( i );
clone.addChild( clone( o ));
}
return clone;
}
}