xpathutil.java
来自「jakarta-taglibs」· Java 代码 · 共 888 行 · 第 1/3 页
JAVA
888 行
if (prefix == null || prefix.equals("")) {
return notNull(
pageContext.findAttribute(localName),
prefix,
localName);
} else if (prefix.equals(PAGE_P)) {
return notNull(
pageContext.getAttribute(localName,PageContext.PAGE_SCOPE),
prefix,
localName);
} else if (prefix.equals(REQUEST_P)) {
return notNull(
pageContext.getAttribute(localName,
PageContext.REQUEST_SCOPE),
prefix,
localName);
} else if (prefix.equals(SESSION_P)) {
return notNull(
pageContext.getAttribute(localName,
PageContext.SESSION_SCOPE),
prefix,
localName);
} else if (prefix.equals(APP_P)) {
return notNull(
pageContext.getAttribute(localName,
PageContext.APPLICATION_SCOPE),
prefix,
localName);
} else if (prefix.equals(PARAM_P)) {
return notNull(
pageContext.getRequest().getParameter(localName),
prefix,
localName);
} else if (prefix.equals(INITPARAM_P)) {
return notNull(
pageContext.getServletContext().
getInitParameter(localName),
prefix,
localName);
} else if (prefix.equals(HEADER_P)) {
HttpServletRequest hsr =
(HttpServletRequest) pageContext.getRequest();
return notNull(
hsr.getHeader(localName),
prefix,
localName);
} else if (prefix.equals(COOKIE_P)) {
HttpServletRequest hsr =
(HttpServletRequest) pageContext.getRequest();
Cookie[] c = hsr.getCookies();
for (int i = 0; i < c.length; i++)
if (c[i].getName().equals(localName))
return c[i].getValue();
throw new UnresolvableException("$" + prefix + ":" + localName);
} else {
throw new UnresolvableException("$" + prefix + ":" + localName);
}
}
/**
* Validate that the Object returned is not null. If it is
* null, throw an exception.
*/
private Object notNull(Object o, String prefix, String localName)
throws UnresolvableException {
if (o == null) {
throw new UnresolvableException("$" + (prefix==null?"":prefix+":") + localName);
}
//p("resolved to: " + o);
return o;
}
}
//*********************************************************************
// Support for XPath evaluation
private PageContext pageContext;
private static HashMap exprCache;
private static JSTLPrefixResolver jstlPrefixResolver = null;
/** Initialize globally useful data. */
private synchronized static void staticInit() {
if (jstlPrefixResolver == null) {
// register supported namespaces
jstlPrefixResolver = new JSTLPrefixResolver();
jstlPrefixResolver.addNamespace("pageScope", PAGE_NS_URL);
jstlPrefixResolver.addNamespace("requestScope", REQUEST_NS_URL);
jstlPrefixResolver.addNamespace("sessionScope", SESSION_NS_URL);
jstlPrefixResolver.addNamespace("applicationScope", APP_NS_URL);
jstlPrefixResolver.addNamespace("param", PARAM_NS_URL);
jstlPrefixResolver.addNamespace("initParam", INITPARAM_NS_URL);
jstlPrefixResolver.addNamespace("header", HEADER_NS_URL);
jstlPrefixResolver.addNamespace("cookie", COOKIE_NS_URL);
// create a HashMap to cache the expressions
exprCache = new HashMap();
}
}
static DocumentBuilderFactory dbf = null;
static DocumentBuilder db = null;
static Document d = null;
static Document getDummyDocument( ) {
try {
if ( dbf == null ) {
dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware( true );
dbf.setValidating( false );
}
db = dbf.newDocumentBuilder();
DOMImplementation dim = db.getDOMImplementation();
d = dim.createDocument("http://java.sun.com/jstl", "dummyroot", null);
//d = db.newDocument();
return d;
} catch ( Exception e ) {
e.printStackTrace();
}
return null;
}
static Document getDummyDocumentWithoutRoot( ) {
try {
if ( dbf == null ) {
dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware( true );
dbf.setValidating( false );
}
db = dbf.newDocumentBuilder();
d = db.newDocument();
return d;
} catch ( Exception e ) {
e.printStackTrace();
}
return null;
}
private static Document getDocumentForNode(Node node) {
Document doc = getDummyDocumentWithoutRoot();
Node importedNode = doc.importNode(node, true);
doc.appendChild(importedNode);
return doc;
}
// The following variable is used for holding the modified xpath string
// when adapting parameter for Xalan XPath engine, where we need to have
// a Non null context node.
String modifiedXPath = null;
/**
* Evaluate an XPath expression to a String value.
*/
public String valueOf(Node n, String xpath) throws JspTagException {
//p("******** valueOf(" + n + ", " + xpath + ")");
staticInit();
// @@@ but where do we set the Pag4eContext for the varaiblecontext?
JstlVariableContext vs = new JstlVariableContext();
XPathContext xpathSupport = new XPathContext();
xpathSupport.setVarStack( vs);
Vector varVector = fillVarStack(vs, xpathSupport);
Node contextNode = adaptParamsForXalan( vs, n, xpath.trim() );
xpath = modifiedXPath;
//p("******** valueOf: modified xpath: " + xpath);
XObject result = JSTLXPathAPI.eval( contextNode, xpath,
jstlPrefixResolver,xpathSupport, varVector);
//p("******Result TYPE => " + result.getTypeString() );
String resultString = result.str();
//p("******** valueOf: after eval: " + resultString);
return resultString;
}
/**
* Evaluate an XPath expression to a boolean value.
*/
public boolean booleanValueOf(Node n, String xpath)
throws JspTagException {
staticInit();
JstlVariableContext vs = new JstlVariableContext();
XPathContext xpathSupport = new XPathContext();
xpathSupport.setVarStack( vs);
Vector varVector = fillVarStack(vs, xpathSupport);
Node contextNode = adaptParamsForXalan( vs, n, xpath.trim() );
xpath = modifiedXPath;
XObject result = JSTLXPathAPI.eval( contextNode, xpath,
jstlPrefixResolver, xpathSupport, varVector);
try {
return result.bool();
} catch (TransformerException ex) {
throw new JspTagException(
Resources.getMessage("XPATH_ERROR_XOBJECT", ex.toString()), ex);
}
}
/**
* Evaluate an XPath expression to a List of nodes.
*/
public List selectNodes(Node n, String xpath) throws JspTagException {
staticInit();
JstlVariableContext vs = new JstlVariableContext();
XPathContext xpathSupport = new XPathContext();
xpathSupport.setVarStack( vs);
Vector varVector = fillVarStack(vs, xpathSupport);
Node contextNode = adaptParamsForXalan( vs, n, xpath.trim() );
xpath = modifiedXPath;
XObject result = JSTLXPathAPI.eval( contextNode, xpath,
jstlPrefixResolver,xpathSupport, varVector);
try {
NodeList nl= JSTLXPathAPI.getNodeList(result);
return new JSTLNodeList( nl );
} catch ( JspTagException e ) {
try {
//If result can't be converted to NodeList we receive exception
// In this case we may have single primitive value as the result
// Populating List with this value ( String, Boolean or Number )
//System.out.println("JSTLXPathAPI.getNodeList thrown exception:"+ e);
Vector vector = new Vector();
Object resultObject = null;
if ( result.getType()== XObject.CLASS_BOOLEAN ) {
resultObject = new Boolean( result.bool());
} else if ( result.getType()== XObject.CLASS_NUMBER ) {
resultObject = new Double( result.num());
} else if ( result.getType()== XObject.CLASS_STRING ) {
resultObject = result.str();
}
vector.add( resultObject );
return new JSTLNodeList ( vector );
} catch ( TransformerException te ) {
throw new JspTagException(te.toString(), te);
}
}
}
/**
* Evaluate an XPath expression to a single node.
*/
public Node selectSingleNode(Node n, String xpath)
throws JspTagException {
//p("selectSingleNode of XPathUtil = passed node:" +
// "xpath => " + n + " : " + xpath );
staticInit();
JstlVariableContext vs = new JstlVariableContext();
XPathContext xpathSupport = new XPathContext();
xpathSupport.setVarStack( vs);
Vector varVector = fillVarStack(vs, xpathSupport);
Node contextNode = adaptParamsForXalan( vs, n, xpath.trim() );
xpath = modifiedXPath;
return (Node) JSTLXPathAPI.selectSingleNode( contextNode, xpath,
jstlPrefixResolver,xpathSupport );
}
/** Returns a locally appropriate context given a node. */
private VariableStack getLocalContext() {
// set up instance-specific contexts
VariableStack vc = new JstlVariableContext();
return vc;
}
//*********************************************************************
// Adapt XPath expression for integration with Xalan
/**
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?