📄 xpathapi.java
字号:
/**
* @(#)$RCSfile: XPathAPI.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.xpath;
import java.util.*;
import org.kxml.kdom.*;
import com.reaxion.tequila.syncml.xml.*;
import com.reaxion.tequila.syncml.xml.node.*;
import com.reaxion.tequila.syncml.util.*;
/**
* This class is responsible for finding a node in whole XML document
* using UID with name "NAME" and unique value per document
*
* @version $1.0$
* @author Oleg A. Oksyuk
*/
public class XPathAPI
{
private final static String UID = "UID";
/**
* Finds child element of contextNode with attribute "NAME" equals str
*
* return a found node or null if there is no child with such UID
*/
private static IXMLNode getUID(IXMLNode contextNode, String str)
{
// Log.println(" getUID("+contextNode.getAttribute(UID).getValue()+")");
if (str.equals(contextNode.getAttribute(UID).getValue())) {
return contextNode;
}
Enumeration en = contextNode.getChildren();
IXMLNode res = null;
IXMLNode child;
while (en.hasMoreElements()) {
child = (IXMLNode) en.nextElement();
res = getUID(child, str);
if (null != res) {
return res;
}
}
return null;
}
/**
* Finds node or atribute in the document.
*
* @param contextNode root to find in. It could be an instance of class
* Document
* @param str path in format: UID or UID?AttributeName
* @return found node or attribute
*
* @exception util.XMLDocStructureException if can not find
* a node or attribute
*/
public static IXMLNodeAttr selectSingleXmlNode(IXMLNode contextNode, String str)
{
Log.println("selectSingleXmlNode("+contextNode.getAttribute(UID).getValue()+")");
int n = str.indexOf("?");
String uidName;
String attrName;
if (n < 0) {
uidName = str;
attrName = null;
} else {
uidName = str.substring(0, n);
attrName = str.substring(n+1);
}
IXMLNode uid = getUID(contextNode, uidName);
Log.println("uid=("+uid+")");
if (null == uid) {
throw new XMLDocStructureException("Can not find node with UID="+uidName);
}
if (null == attrName) {
return uid;
}
IXMLAttr attr = uid.getAttribute(attrName);
Log.println("attr=("+attr+")");
if (null == attr) {
throw new XMLDocStructureException("Node with UID="+uidName+" has no attribute "+attrName);
}
return attr;
}
}
/* -----------------------------------------------------------------------------
* Change log:
* -----------------------------------------------------------------------------
* $Log: XPathAPI.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/27 07:46:16 OlegO
* no message
*
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -