Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * $Id: Tag.java 371 2012-10-01 14:43:58Z achingarev $
   *
   * This file is part of the iText (R) project.
   * Copyright (c) 1998-2012 1T3XT BVBA
   * Authors: Balder Van Camp, Emiel Ackermann, et al.
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU Affero General Public License version 3
  * as published by the Free Software Foundation with the addition of the
  * following permission added to Section 15 as permitted in Section 7(a):
  * FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY 1T3XT,
  * 1T3XT DISCLAIMS THE WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  *
  * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  * or FITNESS FOR A PARTICULAR PURPOSE.
  * See the GNU Affero General Public License for more details.
  * You should have received a copy of the GNU Affero General Public License
  * along with this program; if not, see http://www.gnu.org/licenses or write to
  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  * Boston, MA, 02110-1301 USA, or download the license from the following URL:
  * http://itextpdf.com/terms-of-use/
  *
  * The interactive user interfaces in modified source and object code versions
  * of this program must display Appropriate Legal Notices, as required under
  * Section 5 of the GNU Affero General Public License.
  *
  * In accordance with Section 7(b) of the GNU Affero General Public License,
  * a covered work must retain the producer line in every PDF that is created
  * or manipulated using iText.
  *
  * You can be released from the requirements of the license by purchasing
  * a commercial license. Buying such a license is mandatory as soon as you
  * develop commercial activities involving the iText software without
  * disclosing the source code of your own applications.
  * These activities include: offering paid services to customers as an ASP,
  * serving PDFs on the fly in a web application, shipping iText with a closed
  * source product.
  *
  * For more information, please contact iText Software Corp. at this
  * address: sales@itextpdf.com
  */
 package com.itextpdf.tool.xml;
 
 import java.util.*;

Represents an encountered tag.

Author(s):
redlab_b
 
 public class Tag implements Iterable<Tag> {
 
 	private Tag parent;
 	private final String tag;
 	private final Map<StringStringattributes;
 	private Map<StringStringcss;
 	private final List<Tagchildren;
 	private final String ns;
     private Object lastMarginBottom = null;


Construct a tag.

Parameters:
tag the tag name
attr the attributes in the tag
 
 	public Tag(final String tagfinal Map<StringStringattr) {
 		this(tagattrnew HashMap<StringString>(0), "");
 	}

Parameters:
tag the tag name
 
 	public Tag(final String tag) {
 		this(tagnew HashMap<StringString>(0), new HashMap<StringString>(0), "");
 	}

Parameters:
tag the tag name
attr the attributes
css a map with CSS
ns the namespace
 
 	public Tag(final String tagfinal Map<StringStringattrfinal Map<StringStringcssfinal String ns ) {
 		this. = tag;
 		this. = attr;
 		this. = css;
 		this. = new LinkedList<Tag>();
 		if (ns == null) {
 			throw new NullPointerException("NS cannot be null");
 		}
 		this. = ns;
 	}

Create a new tag object.

Parameters:
tag the tag name
attr the attributes
ns the namespace
	public Tag(final String tagfinal Map<StringStringattrfinal String ns) {
		this(tagattr,new HashMap<StringString>(0),ns );
	}

Create a new tag object.

Parameters:
tag the name of the tag
ns the namespace of the tag (do not set null, set an empty String)
	public Tag(final String tagfinal String ns) {
		 this(tagnew HashMap<StringString>(0), new HashMap<StringString>(0), ns);
	}

Set the tags parent tag.

Parameters:
parent the parent tag of this tag
	public void setParent(final Tag parent) {
		this. = parent;
	}

Returns the parent tag for this tag.

Returns:
the parent tag or null if none
	public Tag getParent() {
		return this.;
	}

The tags name.

Deprecated:
marked as deprecated in favor for getName, we won't remove it yet.
Returns:
the tag name
	public String getTag() {
		return this.;
	}

Returns a Map of css property, value.

Returns:
the css, never null but can be an empty map.
	public Map<StringStringgetCSS() {
		return this.;
	}

Set the css map. If null is given the css is cleared.

Parameters:
css set css properties
	public void setCSS(final Map<StringStringcss) {
		if (null != css) {
			this. = css;
else {
			this..clear();
		}
	}

Returns:
the attributes of the tag
	public Map<StringStringgetAttributes() {
		return ;
	}

Add a child tag to this tag. The given tags parent is set to this tag.

Parameters:
t the tag
	public void addChild(final Tag t) {
		t.setParent(this);
		this..add(t);
	}

Returns all children of this tag.

Returns:
the children tags of this tag.
	public List<TaggetChildren() {
		return this.;
	}

Returns all children of this tag with the given name.

Parameters:
name the name of the tags to look for
Returns:
the children tags of this tag with the given name.
	public List<TaggetChildren(final String name) {
		List<Tagnamed = new LinkedList<Tag>();
		for(Tag childthis.) {
			if(child.getName().equals(name)) {
				named.add(child);
			}
		}
		return named;
	}

Returns:
the ns
	public String getNameSpace() {
		return ;
	}

Print the tag
	public String toString() {
		if ("".equalsIgnoreCase()) {
			return String.format("%s"this.);
		}
		return  String.format("%s:%s"this.this.);
	}

Compare this tag with t for namespace and name equality.

Parameters:
t the tag to compare with
Returns:
true if the namespace and tag are the same.
	public boolean compareTag(final Tag t) {
		if (this == t) {
			return true;
		}
		if (t == null) {
			return false;
		}
		Tag other = t;
		if ( == null) {
			if (other.ns != null) {
				return false;
			}
else if (!.equals(other.ns)) {
			return false;
		}
		if ( == null) {
			if (other.tag != null) {
				return false;
			}
else if (!.equals(other.tag)) {
			return false;
		}
		return true;
	}

Returns:
the child iterator.
	public Iterator<Tagiterator() {
		return .iterator();
	}

Finds the first child that matches the given name and namespace.

Parameters:
name the name of the tag
ns the namespace
Returns:
the child
	public Tag getChild(final String namefinal String ns) {
        return getChild(namensfalse);
	}

Finds the first child that matches the given name and ns. Optionally look in the whole tree (in children of children of children ...)

Parameters:
name name of the tag
ns the namespace
recursive true if the tree should be fully inwards inspected.
Returns:
the child if found
	public Tag getChild(final String namefinal String nsfinal boolean recursive) {
        return recursiveGetChild(thisnamensrecursive);
	}

Whether or not this tag has children.

Returns:
true if there are children
	public boolean hasChildren() {
		return getChildren().size() != 0;
	}

Whether or not this tag has a parent.

Returns:
true if parent is not null
	public boolean hasParent() {
		return getParent() != null;
	}

Check if this tag has a child with the given name and namespace.

Parameters:
name the name of the tag to look for
ns the namespace (if no namespace, set an empty String)
Returns:
true if a child with given name and ns is found
	public boolean hasChild(final String namefinal String ns) {
		return hasChild(namensfalse);
	}

Check if this tag has a child with the given name and namespace.

Parameters:
name the name of the tag to look for
ns the namespace (if no namespace, set an empty String)
recursive true if children's children children children ... should be inspected too.
Returns:
true if a child with the given name and ns is found.
	public boolean hasChild(final String namefinal String nsfinal boolean recursive) {
        if (recursive) {
			return recursiveHasChild(thisnamenstrue);
else {
			return recursiveHasChild(thisnamensfalse);
		}
	}

Parameters:
tag
name
ns
recursive
Returns:
true if the child is found in the child tree
	private boolean recursiveHasChild(final Tag tagfinal String namefinal String nsfinal boolean recursive) {
		for (Tag t : tag) {
			if (t.tag.equals(name) && t.ns.equals(ns)) {
				return true;
else if (recursive) {
				if (recursiveHasChild(tnamensrecursive)) {
					return true;
				}
			}
		}
		return false;
	}

Parameters:
tag
name
ns
recursive
Returns:
the child tag
	private Tag recursiveGetChild(final Tag tagfinal String namefinal String nsfinal boolean recursive) {
		for (Tag t : tag) {
			if (t.tag.equals(name) && t.ns.equals(ns)) {
				return t;
else if (recursive) {
				Tag rT = null;
				if (null != (rT = recursiveGetChild(tnamensrecursive))) {
					return rT;
				}
			}
		}
		return null;
	}

Returns the name of the tag.
(Actually the same as getTag method, but after using XMLWorker for a while we caught ourself always trying to call Tag#getName() instead of Tag#getTag())

Returns:
the name of the tag.
	public String getName() {
		return this.;
	}
    public Object getLastMarginBottom() {
        return ;
    }
    public void setLastMarginBottom(Object lastMarginBottom) {
        this. = lastMarginBottom;
    }
New to GrepCode? Check out our FAQ X