@Override protected Object internalConvert(Object value, Type targetType) { return (value == null ? null : xmlUtil.buildDocument((String)value)); } }
/** * Returns the text value of the element (or attribute or text) that matches the supplied * XPath expression. * * @param key an XPath expression. * @return the text value of the matched element or null if the element does not exist or have * no value. * @throws RuntimeException if the XPath expression isn't correct. */ public String get(String key) { if (root == null) return null; try { return getXMLUtil().getNormalizedValue(getXMLUtil().selectSingleNode(root, key)); } catch (Exception e) { throw new RuntimeException(e); } }
/** * Constructs a new XMap instance from an XML input stream. * * @param input an XML input stream. */ public XMap(InputStream input, EntityResolver resolver) throws IOException, SAXException { this.root = getXMLUtil().loadDocument(input, resolver, null).getDocumentElement(); }
root = getXMLUtil().newDocument(DEFAULT_ROOT_NAME).getDocumentElement(); Node selectResult = getXMLUtil().selectSingleNode(root, key); if (selectResult != null) return getXMLUtil().setValue(selectResult, value); } catch(RuntimeException e) { throw e; Node selectResult = null; try { selectResult = getXMLUtil().selectSingleNode(root, key.substring(0, iLastSlash)); } catch (Exception e) { throw new RuntimeException(e); parent.setAttribute(name.substring(1), value); else getXMLUtil().newElement(parent, name, value);
/** * Returns true if the supplied key XPath expression matches at least one element, attribute * or text in the root element of this XMap. * * @param key an XPath expression. * @return true if the supplied key XPath expression matches at least one element, attribute * or text in the root element of this XMap, false otherwise. * @throws RuntimeException if the XPath expression isn't correct. */ public boolean containsKey(String key) { if (root == null) return false; try { Node result = getXMLUtil().selectSingleNode(root, key); return ( result != null && ( result.getNodeType() == Node.ELEMENT_NODE || result.getNodeType() == Node.TEXT_NODE || result.getNodeType() == Node.ATTRIBUTE_NODE ) ); } catch (Exception e) { throw new RuntimeException(e); } }
/** * Constructs a new XMap instance. * * @param root the name of the root element (may be null). */ public XMap(String root) { if (root != null) { this.root = getXMLUtil().newDocument(root).getDocumentElement(); } }
/** * Returns a "pretty" XML representation of the root element of this XMap (may be null). * * @return a "pretty" XML representation of the root element of this XMap (may be null). */ @Override public String toString() { return getXMLUtil().toNodeString(root); }
/** * Write java.io.Serializable method. * * @param out the ObjectOutputStream where to write this XMap. * @throws IOException if writing fails. */ private void writeObject(ObjectOutputStream out) throws IOException { if (root == null) out.writeInt(0); else { ByteArrayOutputStream output = new ByteArrayOutputStream(); try { getXMLUtil().saveDocument(root.getOwnerDocument(), output); } catch (Exception e) { IOException ioe = new IOException("Could not serialize this XMap"); ioe.initCause(e); throw ioe; } out.writeInt(output.size()); out.write(output.toByteArray()); } }
/** * Returns a list of XMap instances with all elements that match the * supplied XPath expression. Note that XPath result nodes that are not instance of * Element are ignored. Note also that returned XMaps contain original child elements of * the root element of this XMap so modifications made to child elements affect this XMap * instance as well. * * @param key an XPath expression. * @return an unmodifiable list of XMap instances. * @throws RuntimeException if the XPath expression isn't correct. */ public List<XMap> getAll(String key) { if (root == null) return new ArrayList<XMap>(0); try { List<Node> result = getXMLUtil().selectNodeSet(root, key); List<XMap> xMaps = new ArrayList<XMap>(result.size()); for (Node node : result) { if (node.getNodeType() == Node.ELEMENT_NODE) xMaps.add(new XMap(this.xmlUtil, (Element)node, false)); } return xMaps; } catch (Exception e) { throw new RuntimeException(e); } }
root = getXMLUtil().newDocument(DEFAULT_ROOT_NAME).getDocumentElement(); Node selectResult = getXMLUtil().selectSingleNode(root, key); if (selectResult != null) return getXMLUtil().setValue(selectResult, value); } catch(RuntimeException e) { throw e; Node selectResult = null; try { selectResult = getXMLUtil().selectSingleNode(root, key.substring(0, iLastSlash)); } catch (Exception e) { throw new RuntimeException(e); parent.setAttribute(name.substring(1), value); else getXMLUtil().newElement(parent, name, value);
/** * Returns true if the supplied key XPath expression matches at least one element, attribute * or text in the root element of this XMap. * * @param key an XPath expression. * @return true if the supplied key XPath expression matches at least one element, attribute * or text in the root element of this XMap, false otherwise. * @throws RuntimeException if the XPath expression isn't correct. */ public boolean containsKey(String key) { if (root == null) return false; try { Node result = getXMLUtil().selectSingleNode(root, key); return ( result != null && ( result.getNodeType() == Node.ELEMENT_NODE || result.getNodeType() == Node.TEXT_NODE || result.getNodeType() == Node.ATTRIBUTE_NODE ) ); } catch (Exception e) { throw new RuntimeException(e); } }
/** * Constructs a new XMap instance. * * @param root the name of the root element (may be null). */ public XMap(String root) { if (root != null) { this.root = getXMLUtil().newDocument(root).getDocumentElement(); } }
/** * Returns a "pretty" XML representation of the root element of this XMap (may be null). * * @return a "pretty" XML representation of the root element of this XMap (may be null). */ @Override public String toString() { return getXMLUtil().toNodeString(root); }
/** * Write java.io.Serializable method. * * @param out the ObjectOutputStream where to write this XMap. * @throws IOException if writing fails. */ private void writeObject(ObjectOutputStream out) throws IOException { if (root == null) out.writeInt(0); else { ByteArrayOutputStream output = new ByteArrayOutputStream(); try { getXMLUtil().saveDocument(root.getOwnerDocument(), output); } catch (Exception e) { IOException ioe = new IOException("Could not serialize this XMap"); ioe.initCause(e); throw ioe; } out.writeInt(output.size()); out.write(output.toByteArray()); } }
/** * Returns a list of XMap instances with all elements that match the * supplied XPath expression. Note that XPath result nodes that are not instance of * Element are ignored. Note also that returned XMaps contain original child elements of * the root element of this XMap so modifications made to child elements affect this XMap * instance as well. * * @param key an XPath expression. * @return an unmodifiable list of XMap instances. * @throws RuntimeException if the XPath expression isn't correct. */ public List<XMap> getAll(String key) { if (root == null) return new ArrayList<XMap>(0); try { List<Node> result = getXMLUtil().selectNodeSet(root, key); List<XMap> xMaps = new ArrayList<XMap>(result.size()); for (Node node : result) { if (node.getNodeType() == Node.ELEMENT_NODE) xMaps.add(new XMap(this.xmlUtil, (Element)node, false)); } return xMaps; } catch (Exception e) { throw new RuntimeException(e); } }
root = getXMLUtil().newDocument(DEFAULT_ROOT_NAME).getDocumentElement(); Node selectResult = getXMLUtil().selectSingleNode(root, key); if (selectResult != null) return getXMLUtil().setValue(selectResult, value); } catch(RuntimeException e) { throw e; Node selectResult = null; try { selectResult = getXMLUtil().selectSingleNode(root, key.substring(0, iLastSlash)); } catch (Exception e) { throw new RuntimeException(e); parent.setAttribute(name.substring(1), value); else getXMLUtil().newElement(parent, name, value);
/** * Returns the text value of the element (or attribute or text) that matches the supplied * XPath expression. * * @param key an XPath expression. * @return the text value of the matched element or null if the element does not exist or have * no value. * @throws RuntimeException if the XPath expression isn't correct. */ public String get(String key) { if (root == null) return null; try { return getXMLUtil().getNormalizedValue(getXMLUtil().selectSingleNode(root, key)); } catch (Exception e) { throw new RuntimeException(e); } }
/** * Constructs a new XMap instance from an XML input stream. * * @param input an XML input stream. */ public XMap(InputStream input, EntityResolver resolver) throws IOException, SAXException { this.root = getXMLUtil().loadDocument(input, resolver, null).getDocumentElement(); }
@Override protected Object internalConvert(Object value, Type targetType) { return (value == null ? null : xmlUtil.buildDocument((String)value)); } }
/** * Returns true if the supplied key XPath expression matches at least one element, attribute * or text in the root element of this XMap. * * @param key an XPath expression. * @return true if the supplied key XPath expression matches at least one element, attribute * or text in the root element of this XMap, false otherwise. * @throws RuntimeException if the XPath expression isn't correct. */ public boolean containsKey(String key) { if (root == null) return false; try { Node result = getXMLUtil().selectSingleNode(root, key); return ( result != null && ( result.getNodeType() == Node.ELEMENT_NODE || result.getNodeType() == Node.TEXT_NODE || result.getNodeType() == Node.ATTRIBUTE_NODE ) ); } catch (Exception e) { throw new RuntimeException(e); } }