fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * The contents of this file are subject to the terms
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * of the Common Development and Distribution License
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * (the License). You may not use this file except in
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * compliance with the License.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * You can obtain a copy of the License at
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * https://opensso.dev.java.net/public/CDDLv1.0.html or
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * See the License for the specific language governing
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * permission and limitations under the License.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * When distributing Covered Code, include this CDDL
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Header Notice in each file and include the License file
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * If applicable, add the following below the CDDL Header,
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * with the fields enclosed by brackets [] replaced by
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * your own identifying information:
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * "Portions Copyrighted [year] [name of copyright owner]"
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * $Id: XMLElement.java,v 1.2 2008/06/25 05:51:31 qcheng Exp $
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Represents a simple XML element that may have given attributes, child
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * elements or a string value. Note that if the delete method is called on an
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * element, the element will disallow any further method invocations by
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * throwing IllegalStateExceptions.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Majorpublic class XMLElement implements IXMLUtilsConstants {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Deletes this element from the master document. Once an element has been
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * deleted, any further method invocations on this element will result in
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * IllegalStateException being generated.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @throws Exception
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * if the delete operation fails
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major throw new Exception("Deletion of root element not permitted");
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major int startTokenIndex = getStartToken().getTokenIndex();
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major getDocument().deleteTokens(startTokenIndex, endTokenIndex);
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Returns the value of the attribute with the given name as present in the
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * current element. Note that this value may be null if the given attribute
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * is not already present in this document. Also note that even though the
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * attributes are defined with leading and trailing double quotes, the
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * value returned by this method will be devoid of these quotes.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param name
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * the name of the attribute whoes value must be returned.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @return the value of given named attribute, or null if the attribute is
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * not present.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Adds the given child element at the given index.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param child
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param index
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @throws Exception
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major public void addChildElementAt(XMLElement child, int index) throws Exception
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Adds the given child element at the specified index. If the index is out
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * of bounds, this method will throw an exception.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param child
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * the element to be added as a child of this element
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param index
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * the index at which the child element has to be added
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @throws Exception
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * if the addition operation fails
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major public void addChildElementAt(XMLElement child, int index,
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major XMLElement lastChildElement = (XMLElement) childElements
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major addAfterToken = lastChildElement.getStartToken();
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major addAfterToken.getTokenIndex(), child, addAfterNewLine,
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major throw new Exception("Failed to add child element: no such index: "
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major private void insertEndToken() throws Exception {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major String startTokenString = getStartToken().getTokenString();
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major String newStartTokenString = startTokenString.substring(0,
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major getStartToken().setTokenString(newStartTokenString);
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major setEndToken(new BoundedToken("</" + getName() + ">"));
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major throw new Exception("Failed to open collapsed element for update: "
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Appends the given child element to the end of the list of existing child
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param child
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param addAfterNewLine
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @throws Exception
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major public void addChildElement(XMLElement child, boolean addAfterNewLine)
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Appends the given child element to the end of the list of existing child
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param child
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * the child element to append
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @throws Exception
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * if the addition operation fails.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major public void addChildElement(XMLElement child) throws Exception {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Returns a list of named chiled elements of this element.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param name
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * the name of the child elements which must be returned
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @return the child elements with the given names.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major public ArrayList getNamedChildElements(String name) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major if (childElements != null && childElements.size() > 0) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major for (int i = 0; i < childElements.size(); i++) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major XMLElement nextChild = (XMLElement) childElements.get(i);
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Updates the value of this element. This method will fail with an
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * exception if the current element does not store any value.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param value
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * to be updated
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @throws Exception
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * if the update operation fails
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major public void updateValue(String value) throws Exception {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major if (childElements != null && childElements.size() > 0) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major throw new Exception("Invalid operation: update value on: " + this);
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major getDocument().addValueTokenForElement(this, "");
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Updates the value of the an attribute identified by the given name. If
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * this attribute is not present, the value is added to the attribute list
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * at the end of the already present attributes. Note that it is not
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * possible to add an attribute in the middle of the attribute list if
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * already certain other attributes are present. One way to work around
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * this limitation is to remove all attributes from this element and then
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * add them in the required order as necessary.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param name
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * the name of the attribute to be updated or added
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param value
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * the value of the attirbute
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @throws Exception
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * if the update was not successful
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major public void updateAttribute(String name, String value) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Removes any attribute with the given name from this element. The return
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * value indicates the number of attributes that were removed. If the give
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * attribute is not present, the return value is <code>0</code>.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param name
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * the name of the attribute that must be removed.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @return the number of attributes that were removed
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major if (attributes != null && attributes.size() > 0) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major XMLElementAttribute attr = (XMLElementAttribute) it.next();
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Returns an indented string representation of this element and any
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * contained child elements.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @return a string representation of this element along with its sub-tree
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Returns an xml fragment that represents this element and any contained
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * child elements.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @return an xml fragment representing this element
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major int beginTokenIndex = getStartToken().getTokenIndex();
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major return getDocument().toXMLFragment(beginTokenIndex, endTokenIndex);
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Returns the value stored in this element.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @return the value stored in this element
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Returns the name of this element
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @return the name of this element
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Returns an ordered collection of child elements of this element
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @return the collection of child elements in the order they are defined
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Returns a string representing the path of this XMLElement in the current
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @return the path string
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major buff.append(((XMLElement) it.next()).getName());
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Constructor
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param document
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param name
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Constructor
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param document
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param name
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param value
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major XMLElement(XMLDocument document, String name, String value) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Constructor
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param document
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param name
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param childElements
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major XMLElement(XMLDocument document, String name, ArrayList childElements) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Returns a list of tokens in a collapsed manner that can later be
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * inserted into the main document.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param outerIndentString
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param indentIncrementString
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param indentValueToken
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param outerIndent
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major ArrayList getCollapsedTokens(String outerIndentString,
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major String indentIncrementString, boolean indentValueToken,
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major valueIndentString = NEW_LINE + outerIndentString
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major result.add(new WhiteSpaceToken(topIndentString));
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major result.add(new WhiteSpaceToken(outerIndentString));
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major result.add(new WhiteSpaceToken(valueIndentString));
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major result.add(new WhiteSpaceToken(topIndentString));
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major if (childElements != null && childElements.size() > 0) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major for (int i = 0; i < childElements.size(); i++) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major XMLElement nextChild = (XMLElement) childElements.get(i);
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major indentIncrementString, indentValueToken, true));
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major result.add(new WhiteSpaceToken(topIndentString));
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Returns an integer indicating the indent level of this element given its
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * parent's indent level.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param tokenIndex
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param parentIndentLevel
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major int getIndentLevelForToken(int tokenIndex, int parentIndentLevel) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major if (getStartToken().getTokenIndex() == tokenIndex) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major && getEndToken().getTokenIndex() == tokenIndex) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major if (childElements != null && childElements.size() > 0) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major for (int i = 0; i < childElements.size(); i++) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major XMLElement child = (XMLElement) childElements.get(i);
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major int level = child.getIndentLevelForToken(tokenIndex,
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Returns an indented representation of the tree starting at this element.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * The given index is to be used as the indent level for this element.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param index
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major .valueOf(getChildElements().size())) + ":" + "attr="
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major + getAttributes() + ":" + getStartToken().getTokenIndex()
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major + "]\n" + getChildElementsIndentedString(index + 1);
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Sets the given attributes for this element.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param attributes
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Sets the starting token for this element.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param startToken
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Sets the ending token for this element. The ending token may not always
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * be set if the element is closed in the starting token itself.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param endToken
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Sets the value token for this element. The value token may not always be
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * set such as when this element has no value or contains child elements.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param valueToken
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Adds the given child elements to this element.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param childElements
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major void setChildElements(ArrayList childElements) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major if (childElements != null && childElements.size() > 0) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major for (int i = 0; i < childElements.size(); i++) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major XMLElement nextChild = (XMLElement) childElements.get(i);
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Returns the ending token of this element. May be null.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Returns the starting token for this element. May be null if this element
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * has not completely been initialized.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Returns the value token for this element. May be null.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Returns the attributes for this particular element.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Returns the value of the given named attribute. The boolean flag when
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * set to true will result in the value being devoid of the leading and
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * trailing quotes.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param name
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param stripQuotes
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major private String getAttributeValue(String name, boolean stripQuotes) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major if (attributes != null && attributes.size() > 0) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major XMLElementAttribute attr = (XMLElementAttribute) attributes
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Updates the value of the given attribute. If the attribute is not
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * present, it is added to the list of attributes for this element. The
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * boolean flag when set to true will result in the addition of leading
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * and trailing double quotes for the value of the attribute.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param name
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param value
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param addQuotes
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major private void updateAttribute(String name, String value, boolean addQuotes)
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major boolean found = false;
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major XMLElementAttribute attr = (XMLElementAttribute) attributes.get(i);
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major if (value.startsWith("\"") && value.endsWith("\"")) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major attributes.add(new XMLElementAttribute(name, attrValue));
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Returns an indented string representation of the child elements of this
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param index
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major private String getChildElementsIndentedString(int index) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major for (int i = 0; i < childElements.size(); i++) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Constructor
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param document
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param name
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param childElements
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param value
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major private XMLElement(XMLDocument document, String name,
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Updates the attribute string of the element's start and end tokens.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major if (attributes != null && attributes.size() > 0) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major XMLElementAttribute attr = (XMLElementAttribute) attributes
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major buff.append(attr.getName()).append("=").append(attr.getValue())
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major getStartToken().updateAttributeString(buff.toString());
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Sets the name of this element.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param name
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Sets the value of this element.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param value
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Returns the reference of the containing document.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Sets the reference of the containing document.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param document
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major private void setDocument(XMLDocument document) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Marks the given element deleted.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major if (childElements != null && childElements.size() > 0) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major for (int i = 0; i < childElements.size(); i++) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major XMLElement nextChild = (XMLElement) childElements.get(i);
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Returns true if the element is marked deleted, false otherwise.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Fails if this element has been deleted previously by throwing an
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * IllegalStateException.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major "Operation failed: element is deleted");
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Removes the given child element from this element.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param child
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @throws Exception
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major private void removeChildElement(XMLElement child) throws Exception {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major boolean removed = false;
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major if (childElements != null && childElements.size() > 0) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major int childStartTokenIndex = child.getStartToken().getTokenIndex();
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major if (nextChild.getStartToken().getTokenIndex() ==
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major throw new Exception("Failed to remove child element: " + child
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major + " from: " + this);
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Adds the parent reference to this element.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param parent
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Returns the reference of the parent of this document.