⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 elementimpl.java

📁 数据仓库工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	/**
	 * To the <code>name</code> attribute set value to <code>value</code>.
	 *
	 * @param name attribute value.
	 * @param value new attribute value.
	 */
	public void setAttribute(String name, String value) {
		// Note minor dependency on Crimson package
		// Steal the code if Crimson ever goes away

                // Code below does not work with jdk1.3
                /*
		if (!org.apache.crimson.util.XmlNames.isName(name)) {
			throw new NodeDOMException(
				DOMException.INVALID_CHARACTER_ERR,
				"Attribute name is illegal!");
		}
              */
		attributes.put(name, new AttrImpl(this, name, value));
	}


	/**
	 * Equivalent to <code>setAttribute(qualifiedName, value)</code>.
	 *
	 * @see #getAttributeNS
         * @param namespaceURI is name space of the node
         * @param qualifiedName is string
         * @param value is value of the node
	 */
	public void setAttributeNS(String namespaceURI, String qualifiedName, String value) {
		setAttribute(qualifiedName, value);
	}


	/**
	 * Removes attribute with the given name.
	 *
	 * @param name attribute name.
	 */
	public void removeAttribute(String name) {
		if (type != ELEMENT_NODE)
			throw new NodeDOMException(
				DOMException.NOT_SUPPORTED_ERR,
				"Node doesn't have attributes");
		removeAttribute(name, true);
	}


	private void removeAttribute(String name, boolean checkPresent) {
		if (attributes.remove(name) != null)
			return;
		// If we get here, the attribute doesn't exist
		if (checkPresent) {
			throw new NodeDOMException(
				DOMException.NOT_FOUND_ERR,
				"No such attribute!");
		}
	}


	/**
	 * Returns <code>true</code>, if this node has attributes, otherwise
	 * <code>false</code>.
	 *
	 * @return <code>true</code> if node has attributes, otherwise <code>false</code>..
	 */
	public boolean hasAttributes() {
		return attributes.size() > 0;
	}


	/**
	 * Returns <code>true</code>, if this node has attribute with given name,
	 * otherwise <code>false</code>.
	 *
	 * @return <code>true</code> if node has given attribute, otherwise <code>false</code>..
	 * @param name is name of the node
         */
	public boolean hasAttribute(String name) {
		return getAttributeNode(name) != null;
	}


	/**
	 * Equivalent to <code>removeAttribute(localName)</code>.
         * @param namespaceURI is name space of the node
         * @param localName is name of the node
	 */
	public void removeAttributeNS(String namespaceURI, String localName) {
		removeAttribute(localName);
	}


	/**
	 * Returns attribute value with given name of this node.
	 *
	 * @param name name of attribute.
     *
	 * @return value of attribute.
	 */
	public Attr getAttributeNode(String name) {
		return (Attr) attributes.get(name);
	}


	/**
	 * Equivalent to <code>getAttributeNode(localName)</code>.
	 *
	 * @see #setAttributeNodeNS
         * @param namespaceURI is name space of the node
         * @param localName is name of the node
         * @return node
	 */
	public Attr getAttributeNodeNS(String namespaceURI, String localName) {
		return getAttributeNode(localName);
	}


	/**
	 * Add new attribute to this node.
	 *
	 * @param newAttr new attribute.
     *
	 * @return new attribute as <code>AttrImpl</code>
         * @throws DOMException
	 */
	public Attr setAttributeNode(Attr newAttr) throws DOMException {
		AttrImpl attr;
		if (newAttr instanceof AttrImpl) {
			attr = (AttrImpl) newAttr;
		} else {
			attr = new AttrImpl(newAttr);
		}
		attributes.put(attr.getName(), attr);
		return attr;
	}


	/**
	 * Equivalent to <code>setAttributeNode(newAttr)</code>.
	 *
	 * @see #getAttributeNodeNS
         * @param newAttr is attribute of the node
         * @return node
	 */
	public Attr setAttributeNodeNS(Attr newAttr) {
		return setAttributeNode(newAttr);
	}


	/**
	 * Remove attribute from this node.
	 *
	 * @param oldAttr attribute that will be removed.
     *
	 * @return old attribute as <code>AttrImpl</code>.
	 */
	public Attr removeAttributeNode(Attr oldAttr) {
		removeAttribute(oldAttr.getName());
		return oldAttr;
	}


	/**
	 * Equivalent to <code>hasAttribute(localName)</code>.
         * @param namespaceURI is name space of the node
         * @param localName is name of the node
         * @return node
	 */
	public boolean hasAttributeNS(String namespaceURI, String localName) {
		return hasAttribute(localName);
	}


	/**
	 * Returns all <code>Element</code> nodes with given name,
	 * searching by all sub nodes from this node.
	 *
	 * @param name tag name.
     *
	 * @return all <code>Element</code> vith given name as <code>NodeList</code>.
	 */
    public NodeList getElementsByTagName(String name) {
        List list = new ArrayList();
        getElementsByTagName(name, list);
        return new NodeListImpl(list);
    }


    private void getElementsByTagName(String name, List list) {
        if (nodeName.equals(name)) {
            list.add(this);
        }

        Node child = getFirstChild();
        while (child != null) {
            if (child.getNodeType() == Node.ELEMENT_NODE)
                ((ElementImpl)child).getElementsByTagName(name, list);
            child = child.getNextSibling();
        }
    }


	/**
	 * Equivalent to <code>getElementsByTagName(localName)</code>.
         * @param namespaceURI is name space of the node
         * @param localName is name of the node
         * @return node
	 */
	public NodeList getElementsByTagNameNS(String namespaceURI,	String localName) {
		return getElementsByTagName(localName);
	}


	/**
	 * Returns <code>true</code> if this node has children nodes.
	 *
	 * @return <code>true</code> if this node has children.
	 */
	public boolean hasElementChildNodes() {
        Node child = getFirstChild();
        while (child != null) {
            if (child.getNodeType() == Node.ELEMENT_NODE)
                return true;
            child = child.getNextSibling();
        }
		return false;
	}


	/**
	 * Method beginToString for this class writes the xml
	 * begining tag string and all attributes.
	 *
	 * @param sb string buffer to add resulting string.
	 * @param indent used in formating the output.
	 */
	protected void beginToString(StringBuffer sb, Indent indent) {
		sb.append("\n" + indent + "<" + this.nodeName);

		for (Iterator iter = attributes.values().iterator(); iter.hasNext();) {
			Attr attr = (Attr) iter.next();
			sb.append(" " + attr.getNodeName() + "=\"" + attr.getNodeValue() + "\"");
		}
//        if (hasChildNodes()) {
       		sb.append(">");
        	indent.increment();
//        } else
//       		sb.append("/>");
	}

        /**
         * Check that the node is either <code>null</code> or an
         * <code>NodeImpl</code>.
         *
         * @exception DOMException if node is not an instance of <code>NodeImpl</code>.
         */
//        protected void checkNode(Node node) throws DOMException {
//                if (node == null) {
//                        return;
//                }
//                if (!(node instanceof ElementImpl))
//                        throw new NodeDOMException(DOMException.WRONG_DOCUMENT_ERR, "Node is not an instance of ElementImpl!");
//	}

	/**
	 * Method endToString for this class writes the xml
	 * ending tag string.
	 *
	 * @param sb string buffer to add resulting string.
	 * @param indent used in formating the output.
	 */
	protected void endToString(StringBuffer sb, Indent indent) {
//        if (hasChildNodes()) {
        	indent.decrement();
        	if (hasElementChildNodes())
			    sb.append("\n" + indent + "</" + this.nodeName + ">");
			else
			    sb.append("</" + this.nodeName + ">");
//        }
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -