001: /* Copyright (c) 2006-2007, Vladimir Nikic
002: All rights reserved.
003:
004: Redistribution and use of this software in source and binary forms,
005: with or without modification, are permitted provided that the following
006: conditions are met:
007:
008: * Redistributions of source code must retain the above
009: copyright notice, this list of conditions and the
010: following disclaimer.
011:
012: * Redistributions in binary form must reproduce the above
013: copyright notice, this list of conditions and the
014: following disclaimer in the documentation and/or other
015: materials provided with the distribution.
016:
017: * The name of HtmlCleaner may not be used to endorse or promote
018: products derived from this software without specific prior
019: written permission.
020:
021: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
022: AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
023: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
024: ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
025: LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
026: CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
027: SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
028: INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
029: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
030: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
031: POSSIBILITY OF SUCH DAMAGE.
032:
033: You can contact Vladimir Nikic by sending e-mail to
034: nikic_vladimir@yahoo.com. Please include the word "HtmlCleaner" in the
035: subject line.
036: */
037:
038: package org.htmlcleaner;
039:
040: import java.io.IOException;
041: import java.util.*;
042:
043: /**
044: * <p>XML node node tag - it is produced during cleaning process when all start and end
045: * tokens are removed and replaced by instances of TagNode.</p>
046: *
047: * Created by: Vladimir Nikic<br/>
048: * Date: November, 2006.
049: */
050: public class TagNode extends TagToken {
051:
052: private TagNode parent = null;
053: private Map attributes = new TreeMap();
054: private List children = new ArrayList();
055: private List itemsToMove = null;
056:
057: private transient boolean isFormed = false;
058:
059: public TagNode() {
060: }
061:
062: public TagNode(String name) {
063: super (name.toLowerCase());
064: }
065:
066: public Map getAttributes() {
067: return attributes;
068: }
069:
070: public List getChildren() {
071: return children;
072: }
073:
074: public TagNode getParent() {
075: return parent;
076: }
077:
078: public void addAttribute(String attName, String attValue) {
079: if (attName != null && !"".equals(attName.trim())) {
080: attributes.put(attName.toLowerCase(), attValue == null ? ""
081: : attValue);
082: }
083: }
084:
085: public void addChild(Object child) {
086: children.add(child);
087: if (child instanceof TagNode) {
088: TagNode childTagNode = (TagNode) child;
089: childTagNode.parent = this ;
090: }
091: }
092:
093: public void addChildren(List children) {
094: if (children != null) {
095: Iterator it = children.iterator();
096: while (it.hasNext()) {
097: Object child = it.next();
098: addChild(child);
099: }
100: }
101: }
102:
103: public void addItemForMoving(Object item) {
104: if (itemsToMove == null) {
105: itemsToMove = new ArrayList();
106: }
107:
108: itemsToMove.add(item);
109: }
110:
111: public List getItemsToMove() {
112: return itemsToMove;
113: }
114:
115: public void setItemsToMove(List itemsToMove) {
116: this .itemsToMove = itemsToMove;
117: }
118:
119: public boolean isFormed() {
120: return isFormed;
121: }
122:
123: public void setFormed() {
124: this .isFormed = true;
125: }
126:
127: public void serialize(XmlSerializer xmlSerializer)
128: throws IOException {
129: xmlSerializer.serialize(this );
130: }
131:
132: public TagNode makeCopy() {
133: TagNode copy = new TagNode(this.name);
134: copy.attributes = this.attributes;
135:
136: return copy;
137: }
138:
139: }
|