📄 element.java
字号:
* and all other markup is stripped out.) * * @return a concatentation of all text node descendants */ public String getValue() { final StringBuffer buffer = new StringBuffer(); final Iterator iter = getContent().iterator(); while (iter.hasNext()) { final Content child = (Content) iter.next(); if (child instanceof Element || child instanceof Text) { buffer.append(child.getValue()); } } return buffer.toString(); } /** * Returns whether this element is a root element. This can be used in * tandem with {@link #getParent} to determine if an element has any * "attachments" to a parent element or document. * * @return whether this is a root element */ public boolean isRootElement() { return parent instanceof Document; } public int getContentSize() { return content.size(); } public int indexOf(final Content child) { return content.indexOf(child); }// private int indexOf(int start, Filter filter) {// int size = getContentSize();// for (int i = start; i < size; i++) {// if (filter.matches(getContent(i))) {// return i;// }// }// return -1;// } /** * Returns the textual content directly held under this element as a string. * This includes all text within this single element, including whitespace * and CDATA sections if they exist. It's essentially the concatenation of * all {@link Text} and {@link CDATA} nodes returned by {@link #getContent}. * The call does not recurse into child elements. If no textual value exists * for the element, an empty string is returned. * * @return text content for this element, or empty * string if none */ public String getText() { if (content.size() == 0) { return ""; } // If we hold only a Text or CDATA, return it directly if (content.size() == 1) { final Object obj = content.get(0); if (obj instanceof Text) { return ((Text) obj).getText(); } else { return ""; } } // Else build String up final StringBuffer textContent = new StringBuffer(); boolean hasText = false; for (int i = 0; i < content.size(); i++) { final Object obj = content.get(i); if (obj instanceof Text) { textContent.append(((Text) obj).getText()); hasText = true; } } if (!hasText) { return ""; } else { return textContent.toString(); } } /** * Returns the textual content of this element with all surrounding * whitespace removed. If no textual value exists for the element, or if * only whitespace exists, the empty string is returned. * * @return trimmed text content for this element, or * empty string if none */ public String getTextTrim() { return getText().trim(); } /** * Returns the textual content of this element with all surrounding * whitespace removed and internal whitespace normalized to a single space. * If no textual value exists for the element, or if only whitespace exists, * the empty string is returned. * * @return normalized text content for this element, or * empty string if none */ public String getTextNormalize() { return Text.normalizeString(getText()); } /** * Returns the textual content of the named child element, or null if * there's no such child. This method is a convenience because calling * <code>getChild().getText()</code> can throw a NullPointerException. * * @param name the name of the child * @return text content for the named child, or null if * no such child */ public String getChildText(final String name) { final Element child = getChild(name); if (child == null) { return null; } return child.getText(); } /** * Returns the trimmed textual content of the named child element, or null * if there's no such child. See <code>{@link #getTextTrim()}</code> for * details of text trimming. * * @param name the name of the child * @return trimmed text content for the named child, or * null if no such child */ public String getChildTextTrim(final String name) { final Element child = getChild(name); if (child == null) { return null; } return child.getTextTrim(); } /** * Returns the normalized textual content of the named child element, or * null if there's no such child. See <code>{@link * #getTextNormalize()}</code> for details of text normalizing. * * @param name the name of the child * @return normalized text content for the named child, * or null if no such child */ public String getChildTextNormalize(final String name) { final Element child = getChild(name); if (child == null) { return null; } return child.getTextNormalize(); } /** * Returns the textual content of the named child element, or null if * there's no such child. * * @param name the name of the child * @param ns the namespace of the child * @return text content for the named child, or null if * no such child */ public String getChildText(final String name, final Namespace ns) { final Element child = getChild(name, ns); if (child == null) { return null; } return child.getText(); } /** * Returns the trimmed textual content of the named child element, or null * if there's no such child. * * @param name the name of the child * @param ns the namespace of the child * @return trimmed text content for the named child, or * null if no such child */ public String getChildTextTrim(final String name, final Namespace ns) { final Element child = getChild(name, ns); if (child == null) { return null; } return child.getTextTrim(); } /** * Returns the normalized textual content of the named child element, or * null if there's no such child. * * @param name the name of the child * @param ns the namespace of the child * @return normalized text content for the named child, * or null if no such child */ public String getChildTextNormalize(final String name, final Namespace ns) { final Element child = getChild(name, ns); if (child == null) { return null; } return child.getTextNormalize(); } /** * Sets the content of the element to be the text given. All existing text * content and non-text context is removed. If this element should have both * textual content and nested elements, use <code>{@link #setContent}</code> * instead. Setting a null text value is equivalent to setting an empty * string value. * * @param text new text content for the element * @return the target element * @throws IllegalDataException if the assigned text contains an illegal * character such as a vertical tab (as * determined by {@link * org.jdom.Verifier#checkCharacterData}) */ public Element setText(final String text) { content.clear(); if (text != null) { addContent(new Text(text)); } return this; } /** * This returns the full content of the element as a List which * may contain objects of type <code>Text</code>, <code>Element</code>, * <code>Comment</code>, <code>ProcessingInstruction</code>, * <code>CDATA</code>, and <code>EntityRef</code>. * The List returned is "live" in document order and modifications * to it affect the element's actual contents. Whitespace content is * returned in its entirety. * * <p> * Sequential traversal through the List is best done with an Iterator * since the underlying implement of List.size() may require walking the * entire list. * </p> * * @return a <code>List</code> containing the mixed content of the * element: may contain <code>Text</code>, * <code>{@link Element}</code>, <code>{@link Comment}</code>, * <code>{@link ProcessingInstruction}</code>, * <code>{@link CDATA}</code>, and * <code>{@link EntityRef}</code> objects. */ public List getContent() { return content; } /** * Return a filter view of this <code>Element</code>'s content. * * <p> * Sequential traversal through the List is best done with a Iterator * since the underlying implement of List.size() may require walking the * entire list. * </p> * * @param filter <code>Filter</code> to apply * @return <code>List</code> - filtered Element content */ public List getContent(final Filter filter) { return content.getView(filter); } /** * Removes all child content from this parent. * * @return list of the old children detached from this parent */ public List removeContent() { final List old = new ArrayList(content); content.clear(); return old; } /** * Remove all child content from this parent matching the supplied filter. * * @param filter filter to select which content to remove * @return list of the old children detached from this parent */ public List removeContent(final Filter filter) { final List old = new ArrayList(); final Iterator iter = content.getView(filter).iterator(); while (iter.hasNext()) { final Content child = (Content) iter.next(); old.add(child); iter.remove(); } return old; } /** * This sets the content of the element. The supplied List should * contain only objects of type <code>Element</code>, <code>Text</code>, * <code>CDATA</code>, <code>Comment</code>, * <code>ProcessingInstruction</code>, and <code>EntityRef</code>. * * <p> * When all objects in the supplied List are legal and before the new * content is added, all objects in the old content will have their * parentage set to null (no parent) and the old content list will be * cleared. This has the effect that any active list (previously obtained * with a call to {@link #getContent} or {@link #getChildren}) will also * change to reflect the new content. In addition, all objects in the * supplied List will have their parentage set to this element, but the * List itself will not be "live" and further removals and additions will * have no effect on this elements content. If the user wants to continue * working with a "live" list, then a call to setContent should be * followed by a call to {@link #getContent} or {@link #getChildren} to * obtain a "live" version of the content. * </p> * * <p> * Passing a null or empty List clears the existing content. * </p> * * <p> * In event of an exception the original content will be unchanged and * the objects in the supplied content will be unaltered. * </p> * * @param newContent <code>Collection</code> of content to set * @return this element modified * @throws IllegalAddException if the List contains objects of * illegal types or with existing parentage. */ public Element setContent(final Collection newContent) { content.clearAndSet(newContent); return this; } /** * Replace the current child the given index with the supplied child. * <p> * In event of an exception the original content will be unchanged and * the supplied child will be unaltered. * </p> * * @param index - index of child to replace. * @param child - child to add. * @return element on which this method was invoked * @throws IllegalAddException if the supplied child is already attached * or not legal content for this parent. * @throws IndexOutOfBoundsException if index is negative or greater * than the current number of children. */ public Element setContent(final int index, final Content child) { content.set(index, child); return this; } /** * Replace the child at the given index whith the supplied * collection. * <p> * In event of an exception the original content will be unchanged and * the content in the supplied collection will be unaltered. * </p> * * @param index - index of child to replace. * @param newContent - <code>Collection</code> of content to replace child. * @return object on which this method was invoked * @throws IllegalAddException if the collection contains objects of * illegal types. * @throws IndexOutOfBoundsException if index is negative or greater * than the current number of children. */ public Parent setContent(final int index, final Collection newContent) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -