📄 element.java
字号:
content.remove(index); content.addAll(index, newContent); return this; } /** * This adds text content to this element. It does not replace the * existing content as does <code>setText()</code>. * * @param str <code>String</code> to add * @return this element modified * @throws IllegalDataException if <code>str</code> contains an * illegal character such as a vertical tab (as determined * by {@link org.jdom.Verifier#checkCharacterData}) */ public Element addContent(final String str) { return addContent(new Text(str)); } /** * Appends the child to the end of the element's content list. * * @param child child to append to end of content list * @return the element on which the method was called * @throws IllegalAddException if the given child already has a parent. */ public Element addContent(final Content child) { content.add(child); return this; } /** * Appends all children in the given collection to the end of * the content list. In event of an exception during add the * original content will be unchanged and the objects in the supplied * collection will be unaltered. * * @param newContent <code>Collection</code> of content to append * @return the element on which the method was called * @throws IllegalAddException if any item in the collection * already has a parent or is of an inappropriate type. */ public Element addContent(final Collection newContent) { content.addAll(newContent); return this; } /** * Inserts the child into the content list at the given index. * * @param index location for adding the collection * @param child child to insert * @return the parent on which the method was called * @throws IndexOutOfBoundsException if index is negative or beyond * the current number of children * @throws IllegalAddException if the given child already has a parent. */ public Element addContent(final int index, final Content child) { content.add(index, child); return this; } /** * Inserts the content in a collection into the content list * at the given index. In event of an exception the original content * will be unchanged and the objects in the supplied collection will be * unaltered. * * @param index location for adding the collection * @param newContent <code>Collection</code> of content to insert * @return the parent on which the method was called * @throws IndexOutOfBoundsException if index is negative or beyond * the current number of children * @throws IllegalAddException if any item in the collection * already has a parent or is of an inappropriate type. */ public Element addContent(final int index, final Collection newContent) { content.addAll(index, newContent); return this; } public List cloneContent() { final int size = getContentSize(); final List list = new ArrayList(size); for (int i = 0; i < size; i++) { final Content child = getContent(i); list.add(child.clone()); } return list; } public Content getContent(final int index) { return (Content) content.get(index); }// public Content getChild(Filter filter) {// int i = indexOf(0, filter);// return (i < 0) ? null : getContent(i);// } public boolean removeContent(final Content child) { return content.remove(child); } public Content removeContent(final int index) { return (Content) content.remove(index); } /** * Set this element's content to be the supplied child. * <p> * If the supplied child is legal content for this parent and before * it is added, all content in the current content list will * be cleared and all current children will have their parentage set to * null. * <p> * This has the effect that any active list (previously obtained with * a call to one of the {@link #getContent} methods will also change * to reflect the new content. In addition, all content in the supplied * collection will have their parentage set to this parent. If the user * wants to continue working with a <b>"live"</b> list of this parent's * child, then a call to setContent should be followed by a call to one * of the {@link #getContent} methods to obtain a <b>"live"</b> * version of the children. * <p> * Passing a null child clears the existing content. * <p> * In event of an exception the original content will be unchanged and * the supplied child will be unaltered. * * @param child new content to replace existing content * @return the parent on which the method was called * @throws IllegalAddException if the supplied child is already attached * or not legal content for an Element */ public Element setContent(final Content child) { content.clear(); content.add(child); return this; } /** * Determines if this element is the ancestor of another element. * * @param element <code>Element</code> to check against * @return <code>true</code> if this element is the ancestor of the * supplied element */ public boolean isAncestor(final Element element) { Parent p = element.getParent(); while (p instanceof Element) { if (p == this) { return true; } p = p.getParent(); } return false; } /** * <p> * This returns the complete set of attributes for this element, as a * <code>List</code> of <code>Attribute</code> objects in no particular * order, or an empty list if there are none. * The returned list is "live" and changes to it affect the * element's actual attributes. * </p> * * @return attributes for the element */ public List getAttributes() { return attributes; } /** * <p> * This returns the attribute for this element with the given name * and within no namespace, or null if no such attribute exists. * </p> * * @param name name of the attribute to return * @return attribute for the element */ public Attribute getAttribute(final String name) { return getAttribute(name, Namespace.NO_NAMESPACE); } /** * <p> * This returns the attribute for this element with the given name * and within the given Namespace, or null if no such attribute exists. * </p> * * @param name name of the attribute to return * @param ns <code>Namespace</code> to search within * @return attribute for the element */ public Attribute getAttribute(final String name, final Namespace ns) { return (Attribute) attributes.get(name, ns); } /** * <p> * This returns the attribute value for the attribute with the given name * and within no namespace, null if there is no such attribute, and the * empty string if the attribute value is empty. * </p> * * @param name name of the attribute whose value to be returned * @return the named attribute's value, or null if no such attribute */ public String getAttributeValue(final String name) { return getAttributeValue(name, Namespace.NO_NAMESPACE); } /** * <p> * This returns the attribute value for the attribute with the given name * and within no namespace, or the passed-in default if there is no * such attribute. * </p> * * @param name name of the attribute whose value to be returned * @param def a default value to return if the attribute does not exist * @return the named attribute's value, or the default if no such attribute */ public String getAttributeValue(final String name, final String def) { return getAttributeValue(name, Namespace.NO_NAMESPACE, def); } /** * <p> * This returns the attribute value for the attribute with the given name * and within the given Namespace, null if there is no such attribute, and * the empty string if the attribute value is empty. * </p> * * @param name name of the attribute whose valud is to be returned * @param ns <code>Namespace</code> to search within * @return the named attribute's value, or null if no such attribute */ public String getAttributeValue(final String name, final Namespace ns) { return getAttributeValue(name, ns, null); } /** * <p> * This returns the attribute value for the attribute with the given name * and within the given Namespace, or the passed-in default if there is no * such attribute. * </p> * * @param name name of the attribute whose valud is to be returned * @param ns <code>Namespace</code> to search within * @param def a default value to return if the attribute does not exist * @return the named attribute's value, or the default if no such attribute */ public String getAttributeValue(final String name, final Namespace ns, final String def) { final Attribute attribute = (Attribute) attributes.get(name, ns); if (attribute == null) { return def; } return attribute.getValue(); } /** * <p> * This sets the attributes of the element. The supplied Collection should * contain only objects of type <code>Attribute</code>. * </p> * * <p> * When all objects in the supplied List are legal and before the new * attributes are added, all old attributes will have their * parentage set to null (no parent) and the old attribute list will be * cleared. This has the effect that any active attribute list (previously * obtained with a call to {@link #getAttributes}) will also change to * reflect the new attributes. In addition, all attributes 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 attributes. If the user wants to continue * working with a "live" attribute list, then a call to setAttributes * should be followed by a call to {@link #getAttributes} to obtain a * "live" version of the attributes. * </p> * * <p> * Passing a null or empty List clears the existing attributes. * </p> * * <p> * In cases where the List contains duplicate attributes, only the last * one will be retained. This has the same effect as calling * {@link #setAttribute(Attribute)} sequentially. * </p> * * <p> * In event of an exception the original attributes will be unchanged and * the attributes in the supplied attributes will be unaltered. * </p> * * @param newAttributes <code>Collection</code> of attributes to set * @return this element modified * @throws IllegalAddException if the List contains objects * that are not instances of <code>Attribute</code>, * or if any of the <code>Attribute</code> objects have * conflicting namespace prefixes. */ public Element setAttributes(final Collection newAttributes) { attributes.clearAndSet(newAttributes); return this; } /** * <p> * This sets the attributes of the element. It's an alternate form of * the method, accepting a <code>List</code> instead of a * <code>Collection</code>, for backward compatibility. * </p> */ public Element setAttributes(final List newAttributes) { return setAttributes((Collection)newAttributes); } /** * <p> * This sets an attribute value for this element. Any existing attribute * with the same name and namespace URI is removed. * </p> * * @param name name of the attribute to set * @param value value of the attribute to set * @return this element modified * @throws IllegalNameException if the given name is illegal as an * attribute name. * @throws IllegalDataException if the given attribute value is * illegal character data (as determined by * {@link org.jdom.Verifier#checkCharacterData}). */ public Element setAttribute(final String name, final String value) { final Attribute attribute = getAttribute(name); if (attribute == null) { final Attribute newAttribute = new Attribute(name, value); setAttribute(newAttribute); } else { attribute.setValue(value); } return this; } /** * <p> * This sets an attribute value for this element. Any existing attribute * with the same name and namespace URI is removed. * </p> * * @param name name of the attribute to set * @param value value of the attribute to set * @param ns namespace of the attribute to set * @return this element modified * @throws IllegalNameException if the given name is illegal as an * attribute name, or if the namespace is an unprefixed default * namespace * @throws IllegalDataException if the given attribute value is * illegal character data (as determined by * {@link org.jdom.Verifier#checkCharacterData}). * @throws IllegalAddException if the attribute namespace prefix * collides with another namespace prefix on the element. */ public Element setAttribute(final String name, final String value, final Namespace ns) { final Attribute attribute = getAttribute(name, ns); if (attribute == null) { final Attribute newAttribute = new Attribute(name, value, ns); setAttribute(newAttribute); } else { attribute.setValue(value); } return this; } /** * <p> * This sets an attribute value for this element. Any existing attribute * with the same name and namespace URI is removed. * </p> * * @param attribute <code>Attribute</code> to set * @return this element modified * @throws IllegalAddException if the attribute being added already has a
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -