/** * This method is used to add an <code>OutputNode</code> to the * top of the stack. This is used when an element is written to * the XML document, and allows the writer to determine if a * child node can be created from a given output node. * * @param value this is the output node to add to the stack */ public OutputNode push(OutputNode value) { active.add(value); add(value); return value; }
/** * This method is used to determine if the node is the root * node for the XML document. The root node is the first node * in the document and has no sibling nodes. This is false * if the node has a parent node or a sibling node. * * @param node this is the node that is check as the root * * @return true if the node is the root node for the document */ public boolean isRoot(OutputNode node) { return stack.bottom() == node; }
/** * This is used to acquire the <code>OutputNode</code> from the * top of the output stack. This is used when the writer wants to * determine the current element written to the XML document. * * @return this returns the node from the top of the stack */ public OutputNode top() { int size = size(); if(size <= 0) { return null; } return get(size - 1); }
/** * This is used to remove the <code>OutputNode</code> from the * top of the output stack. This is used when an element has been * ended and the output writer wants to block child creation. * * @return this returns the node from the top of the stack */ public OutputNode pop() { int size = size(); if(size <= 0) { return null; } return purge(size - 1); }
/** * This method is used to commit all nodes on the stack up to and * including the specified node. This will effectively create end * tags for any nodes that are currently open up to the specified * element. Once committed the output node can no longer be used * to create child elements, nor can any of its child elements. * * @param parent this is the node that is to be committed */ public void commit(OutputNode parent) throws Exception { if(stack.contains(parent)) { OutputNode top = stack.top(); if(!isCommitted(top)) { writeStart(top); } while(stack.top() != parent) { writeEnd(stack.pop()); } writeEnd(parent); stack.pop(); } }
/** * This is used to remove any uncommitted changes. Removal of an * output node can only be done if it has no siblings and has * not yet been committed. If the node is committed then this * will throw an exception to indicate that it cannot be removed. * * @throws Exception thrown if the node cannot be removed */ public void remove() throws Exception { if(stack.isEmpty()) { throw new NodeException("No root node"); } stack.bottom().remove(); }
/** * This method is used to remove the output node from the output * buffer if that node has not yet been committed. This allows a * node that has been created to be deleted, ensuring that it * will not affect the resulting XML document structure. * * @param node this is the output node that is to be removed */ public void remove(OutputNode node) throws Exception { if(stack.top() != node) { throw new NodeException("Cannot remove node"); } stack.pop(); }
/** * This is used to determine whether this node has been committed. * This will return true if no root element has been created or * if the root element for the document has already been commited. * * @return true if the node is committed or has not been created */ public boolean isCommitted() { return stack.isEmpty(); } }
/** * Removes the match from the cursor position. This also * ensures that the node is removed from the active set so * that it is not longer considered a relevant output node. */ public void remove() { purge(cursor); } }
/** * Returns the <code>OutputNode</code> object at the cursor * position. If the cursor has reached the start of the list * then this returns null instead of the first output node. * * @return this returns the node from the cursor position */ public OutputNode next() { if(hasNext()) { return get(--cursor); } return null; }
/** * The <code>purge</code> method is used to purge a match from * the provided position. This also ensures that the active set * has the node removed so that it is no longer relevant. * * @param index the index of the node that is to be removed * * @return returns the node removed from the specified index */ public OutputNode purge(int index) { OutputNode node = remove(index); if(node != null){ active.remove(node); } return node; }
/** * Constructor for the <code>NodeWriter</code> object. This will * create the object that is used to control an output elements * access to the generated XML document. This keeps a stack of * active and uncommitted elements. * * @param result this is the output for the resulting document * @param format this is used to format the generated document * @param verbose this determines if we expand the namespaces */ private NodeWriter(Writer result, Format format, boolean verbose) { this.writer = new Formatter(result, format); this.active = new HashSet(); this.stack = new OutputStack(active); this.verbose = verbose; } /**
/** * This is used to begin writing on a new XML element. This is * typically done by writing any comments required. This will * create an output node of the specified name before writing * the comment, if any exists. Once the comment has been written * the node is pushed on to the head of the output node stack. * * @param parent this is the parent node to the next output node * @param name this is the name of the node that is to be created * * @return this returns an output node used for writing content */ private OutputNode writeStart(OutputNode parent, String name) throws Exception { OutputNode node = new OutputElement(parent, this, name); if(name == null) { throw new NodeException("Can not have a null name"); } return stack.push(node); }
/** * This method is used to commit all nodes on the stack up to and * including the specified node. This will effectively create end * tags for any nodes that are currently open up to the specified * element. Once committed the output node can no longer be used * to create child elements, nor can any of its child elements. * * @param parent this is the node that is to be committed */ public void commit(OutputNode parent) throws Exception { if(stack.contains(parent)) { OutputNode top = stack.top(); if(!isCommitted(top)) { writeStart(top); } while(stack.top() != parent) { writeEnd(stack.pop()); } writeEnd(parent); stack.pop(); } }
/** * This is used to remove any uncommitted changes. Removal of an * output node can only be done if it has no siblings and has * not yet been committed. If the node is committed then this * will throw an exception to indicate that it cannot be removed. * * @throws Exception thrown if the node cannot be removed */ public void remove() throws Exception { if(stack.isEmpty()) { throw new NodeException("No root node"); } stack.bottom().remove(); }
/** * This is used to remove the <code>OutputNode</code> from the * top of the output stack. This is used when an element has been * ended and the output writer wants to block child creation. * * @return this returns the node from the top of the stack */ public OutputNode pop() { int size = size(); if(size <= 0) { return null; } return purge(size - 1); }
/** * This method is used to remove the output node from the output * buffer if that node has not yet been committed. This allows a * node that has been created to be deleted, ensuring that it * will not affect the resulting XML document structure. * * @param node this is the output node that is to be removed */ public void remove(OutputNode node) throws Exception { if(stack.top() != node) { throw new NodeException("Cannot remove node"); } stack.pop(); }
/** * This is used to determine whether this node has been committed. * This will return true if no root element has been created or * if the root element for the document has already been commited. * * @return true if the node is committed or has not been created */ public boolean isCommitted() { return stack.isEmpty(); } }