📄 xmlnodeimpl.java
字号:
/**
* @(#)$RCSfile: XmlNodeImpl.java,v $ $Revision: 1.2 $
*
* ====================================================================
* Copyright 2001, Reaxion Corp.,
* 11418 105th PL NE, Kirkland, WA, 98033, USA
* All rights reserved.
* ====================================================================
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (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.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is the Tequila SyncML.
*
* The Initial Developer of the Original Code is Reaxion Corp.
* All Rights Reserved.
*/
package com.reaxion.tequila.syncml.xml.node;
import java.util.*;
import java.io.*;
import org.kxml.kdom.*;
import org.kxml.*;
import com.reaxion.tequila.syncml.xml.*;
import com.reaxion.tequila.syncml.util.*;
/**
* IXMLNode implementation for DOM XML parser
*
* @version $1.0$
* @author Oleg A. Oksyuk
*/
public class XmlNodeImpl implements IXMLNode
{
private Element node;
public XmlNodeImpl(String data) throws IOException
{
node = XmlIO.readElementFromBytes(data.getBytes());
}
public XmlNodeImpl(Element n)
{
node = n;
}
public XmlNodeImpl(Document d)
{
node = d.getRootElement();
}
public Element getNode()
{
return node;
}
public String getName()
{
return node.getName();
}
public void add(IXMLNodeAttr nodeToAdd)
{
if (nodeToAdd.isAttribute())
{
String name = nodeToAdd.getName();
if (null != node.getAttribute(name))
{
throw new XMLDocStructureException("Element "+getName()+" already contains attribute "+name);
}
IXMLAttr attr = (IXMLAttr) nodeToAdd;
node.setValue(name, attr.getValue());
}
else
{
XmlNodeImpl el = (XmlNodeImpl) nodeToAdd;
node.addChild(Xml.ELEMENT, el.node);
}
}
public void replace(String value)
{
int len = node.getChildCount ();
for (int i = 0; i < len; i++)
{
if (node.getType(i) == Xml.TEXT)
{
node.removeChild(i);
node.addChild(i, Xml.TEXT, value);
return;
}
else if (node.getType (i) == Xml.ELEMENT)
{
throw new SyncException ("not text-only content!");
}
}
node.addChild(Xml.TEXT, value);
}
public void delete()
{
Node parent = node.getParent();
int n = parent.getChildCount();
Object childTmp;
Element childTmpEl;
for (int i = 0 ; i < n ; i++)
{
childTmp = parent.getChild(i);
if (childTmp instanceof Element)
{
childTmpEl = (Element) childTmp;
if (getName().equals(childTmpEl.getName()))
{
parent.removeChild(i);
return;
}
}
}
throw new XMLDocStructureException("Unexpected exception in delete element");
}
public boolean isAttribute()
{
return false;
}
public String getTextTrim()
{
return node.getText().trim();
}
public IXMLNode getChild(String name)
{
int n = node.getChildCount();
Object tmpChild;
Element tmpChildEl;
for (int i = 0 ; i < n ; i++)
{
tmpChild = node.getChild(i);
if (tmpChild instanceof Element)
{
tmpChildEl = (Element) tmpChild;
if (name.equals(tmpChildEl.getName()))
{
return new XmlNodeImpl(tmpChildEl);
}
}
}
return null;
}
public Enumeration getChildren()
{
int n = node.getChildCount();
Object tmpChild;
Vector res = new Vector();
for (int i = 0 ; i < n ; i++)
{
tmpChild = node.getChild(i);
if (tmpChild instanceof Element)
{
res.addElement(new XmlNodeImpl((Element) tmpChild));
}
}
return res.elements();
}
public IXMLAttr getAttribute(String name)
{
Attribute attr = node.getAttribute(name);
return (null != attr) ? new XmlAttrImpl(this, attr.getName(), attr.getValue()) : null;
}
public void removeAttribute(String name)
{
int n = node.getAttributeCount();
for (int i = 0 ; i < n ; i++)
{
if (name.equals(node.getAttribute(i).getName()))
{
node.removeAttribute(i);
return;
}
}
throw new XMLDocStructureException("Can not find attribute with name "+name);
}
public void replaceAttribute(String name, String value)
{
Attribute attr = new Attribute(name, value);
node.setAttribute(attr);
}
public Document createDocument()
{
Document doc = new Document();
doc.addChild(Xml.ELEMENT, node);
return doc;
}
public String toString()
{
return node.toString();
}
public StringBuffer getXml()
{
return new StringBuffer(toString());
}
}
/* -----------------------------------------------------------------------------
* Change log:
* -----------------------------------------------------------------------------
* $Log: XmlNodeImpl.java,v $
* Revision 1.2 2001/10/17 15:27:43 OlegO
* changed comments for better javadoc
*
* Revision 1.1.1.1 2001/10/11 13:13:32 OlegO
* no message
*
* Revision 1.1 2001/07/24 13:08:19 OlegO
* no message
*
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -