wwxml.java
来自「world wind java sdk 源码」· Java 代码 · 共 1,246 行 · 第 1/4 页
JAVA
1,246 行
/** * Returns the element identified by an XPath expression. * * @param context the context from which to start the XPath search. * @param path the XPath expression. * @param xpath an {@link XPath} object to use for the search. This allows the caller to re-use XPath objects when * performing multiple searches. May be null. * * @return the element matching the XPath expression, or null if no element matches. * * @throws IllegalArgumentException if the context or XPath expression are null. */ public static Element getElement(Element context, String path, XPath xpath) { if (context == null) { String message = Logging.getMessage("nullValue.ContextIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } if (path == null) { String message = Logging.getMessage("nullValue.PathIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } if (xpath == null) xpath = makeXPath(); try { Node node = (Node) xpath.evaluate(path, context, XPathConstants.NODE); if (node == null) return null; return node instanceof Element ? (Element) node : null; } catch (XPathExpressionException e) { return null; } } /** * Returns all elements identified by an XPath expression. * * @param context the context from which to start the XPath search. * @param path the XPath expression. * @param xpath an {@link XPath} object to use for the search. This allows the caller to re-use XPath objects when * performing multiple searches. May be null. * * @return an array containing the elements matching the XPath expression. * * @throws IllegalArgumentException if the context or XPath expression are null. */ public static Element[] getElements(Element context, String path, XPath xpath) { if (context == null) { String message = Logging.getMessage("nullValue.ContextIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } if (path == null) { String message = Logging.getMessage("nullValue.PathIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } if (xpath == null) xpath = makeXPath(); try { NodeList nodes = (NodeList) xpath.evaluate(path, context, XPathConstants.NODESET); if (nodes == null || nodes.getLength() == 0) return null; Element[] elements = new Element[nodes.getLength()]; for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (node instanceof Element) elements[i] = (Element) node; } return elements; } catch (XPathExpressionException e) { return null; } } /** * Returns the unique elements identified by an XPath expression and a sub-expression. * * @param context the context from which to start the XPath search. * @param path the XPath expression. * @param uniqueTag an XPath expression to match with the elements matched with the above expression. * @param xpath an {@link XPath} object to use for the search. This allows the caller to re-use XPath objects * when performing multiple searches. May be null. * * @return an array containing the unique elements matching the XPath expression and subexpression. If multiple * elements have the same content only the first one found is returned. Returns null if no matching element * is found. * * @throws IllegalArgumentException if either the context, XPath expression or XPath sub-expression are null. */ public static Element[] getUniqueElements(Element context, String path, String uniqueTag, XPath xpath) { if (context == null) { String message = Logging.getMessage("nullValue.ContextIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } if (path == null) { String message = Logging.getMessage("nullValue.PathIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } if (uniqueTag == null) { String message = Logging.getMessage("nullValue.UniqueTagIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } if (xpath == null) xpath = makeXPath(); Element[] elements = getElements(context, path, xpath); if (elements == null) return null; HashMap<String, Element> styles = new HashMap<String, Element>(); for (Element e : elements) { String name = getText(e, uniqueTag, xpath); if (name != null) styles.put(name, e); } return styles.values().toArray(new Element[1]); } /** * Returns the {@link Double} value of an element identified by an XPath expression. * * @param context the context from which to start the XPath search. * @param path the XPath expression. * @param xpath an {@link XPath} object to use for the search. This allows the caller to re-use XPath objects when * performing multiple searches. May be null. * * @return the value of an element matching the XPath expression, or null if no match is found or the match does not * contain a {@link Double}. * * @throws IllegalArgumentException if the context or XPath expression are null. */ public static Double getDouble(Element context, String path, XPath xpath) { if (context == null) { String message = Logging.getMessage("nullValue.ContextIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } if (path == null) { String message = Logging.getMessage("nullValue.PathIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } String s = null; try { s = getText(context, path, xpath); if (s == null || s.length() == 0) return null; return Double.valueOf(s); } catch (NumberFormatException e) { String message = Logging.getMessage("generic.ConversionError", s); Logging.logger().log(java.util.logging.Level.SEVERE, message, e); return null; } } /** * Returns the {@link Integer} value of an element identified by an XPath expression. * * @param context the context from which to start the XPath search. * @param path the XPath expression. * @param xpath an {@link XPath} object to use for the search. This allows the caller to re-use XPath objects when * performing multiple searches. May be null. * * @return the value of an element matching the XPath expression, or null if no match is found or the match does not * contain a {@link Integer}. * * @throws IllegalArgumentException if the context or XPath expression are null. */ public static Integer getInteger(Element context, String path, XPath xpath) { if (context == null) { String message = Logging.getMessage("nullValue.ContextIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } if (path == null) { String message = Logging.getMessage("nullValue.PathIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } String s = null; try { s = getText(context, path, xpath); if (s == null || s.length() == 0) return null; return Integer.valueOf(s); } catch (NumberFormatException e) { String message = Logging.getMessage("generic.ConversionError", s); Logging.logger().log(java.util.logging.Level.SEVERE, message, e); return null; } } /** * Returns the {@link Long} value of an element identified by an XPath expression. * * @param context the context from which to start the XPath search. * @param path the XPath expression. * @param xpath an {@link XPath} object to use for the search. This allows the caller to re-use XPath objects when * performing multiple searches. May be null. * * @return the value of an element matching the XPath expression, or null if no match is found or the match does not * contain a {@link Integer}. * * @throws IllegalArgumentException if the context or XPath expression are null. */ public static Long getLong(Element context, String path, XPath xpath) { if (context == null) { String message = Logging.getMessage("nullValue.ContextIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } if (path == null) { String message = Logging.getMessage("nullValue.PathIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } String s = null; try { s = getText(context, path, xpath); if (s == null || s.length() == 0) return null; return Long.valueOf(s); } catch (NumberFormatException e) { String message = Logging.getMessage("generic.ConversionError", s); Logging.logger().log(java.util.logging.Level.SEVERE, message, e); return null; } } /** * Returns the {@link Boolean} value of an element identified by an XPath expression. * * @param context the context from which to start the XPath search. * @param path the XPath expression. * @param xpath an {@link XPath} object to use for the search. This allows the caller to re-use XPath objects when * performing multiple searches. May be null. * * @return the value of an element matching the XPath expression, or null if no match is found or the match does not * contain a {@link Boolean}. * * @throws IllegalArgumentException if the context or XPath expression are null. */ public static Boolean getBoolean(Element context, String path, XPath xpath) { if (context == null) { String message = Logging.getMessage("nullValue.ContextIsNull");
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?