Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.apache.wicket.markup.parser;
 
 import java.util.Map;
 
A subclass of MarkupElement which represents a tag including namespace and its optional attributes. XmlTags are returned by the XML parser.

Author(s):
Jonathan Locke
 
 public class XmlTag extends MarkupElement
 {
A close tag, like </TAG>.
 
 	public static final Type CLOSE = new Type("CLOSE");

An open tag, like <TAG componentId = "xyz">.
 
 	public static final Type OPEN = new Type("OPEN");

An open/close tag, like <TAG componentId = "xyz"/>.
 
 	public static final Type OPEN_CLOSE = new Type("OPEN_CLOSE");

Attribute map.
 
 	private IValueMap attributes;

Column number.
 
 	int columnNumber;

Length of this tag in characters.
 
 	int length;

Line number.
 
 	int lineNumber;

Name of tag, such as "img" or "input".
 
Namespace of the tag, if available, such as <wicket:link ...>
 
Position of this tag in the input that was parsed.
 
 	int pos;

Full text of tag.
 
The tag type (OPEN, CLOSE or OPEN_CLOSE).
 
 	Type type;

Any component tag that this tag closes.
 
 	private XmlTag closes;

If mutable, the immutable tag that this tag is a mutable copy of.
 
 	private XmlTag copyOf = this;

True if this tag is mutable, false otherwise.
 
 	private boolean isMutable = true;

True if the name of this tag was changed.
 
 	private boolean nameChanged = false;

Enumerated type for different kinds of component tags.
 
 	public static final class Type extends EnumeratedType
 	{
 		private static final long serialVersionUID = 1L;

Construct.

Parameters:
name name of type
		Type(final String name)
		{
			super(name);
		}
	}

Construct.
	public XmlTag()
	{
		super();
	}

Gets whether this tag closes the provided open tag.

Parameters:
open The open tag
Returns:
True if this tag closes the given open tag
	public final boolean closes(final XmlTag open)
	{
		return ( == open) || ( == open.copyOf);
	}

	public final boolean equalTo(final MarkupElement element)
	{
		if (element instanceof XmlTag)
		{
			final XmlTag that = (XmlTag)element;
			if (!Objects.equal(getNamespace(), that.getNamespace()))
			{
				return false;
			}
			if (!getName().equals(that.getName()))
			{
				return false;
			}
			{
				return false;
			}
			return true;
		}
		return false;
	}

Gets a hashmap of this tag's attributes.

Returns:
The tag's attributes
	{
		if ( == null)
		{
			if (( == this) || ( == null) || (. == null))
			{
				 = new ValueMap();
			}
			else
			{
			}
		}
		return ;
	}

Returns:
true if there 1 or more attributes.
	public boolean hasAttributes()
	{
		return  != null && .size() > 0;
	}

Get the column number.

Returns:
Returns the columnNumber.
	public int getColumnNumber()
	{
		return ;
	}

Gets the length of the tag in characters.

Returns:
The tag's length
	public int getLength()
	{
		return ;
	}

Get the line number.

Returns:
Returns the lineNumber.
	public int getLineNumber()
	{
		return ;
	}

Gets the name of the tag, for example the tag <b>'s name would be 'b'.

Returns:
The tag's name
	public String getName()
	{
		return ;
	}

Get whether the name of this component tag was changed.

Returns:
Returns true if the name of this component tag was changed
	public boolean getNameChanged()
	{
		return ;
	}

Namespace of the tag, if available. For example, <wicket:link>.

Returns:
The tag's namespace
	{
		return ;
	}

Assuming this is a close tag, return the corresponding open tag

Returns:
The open tag. Null, if no open tag available
	public final XmlTag getOpenTag()
	{
		return ;
	}

Gets the location of the tag in the input string.

Returns:
Tag location (index in input string)
	public int getPos()
	{
		return ;
	}

Get a string attribute.

Parameters:
key The key
Returns:
The string value
	public CharSequence getString(final String key)
	{
	}

Get the tag type.

Returns:
the tag type (OPEN, CLOSE or OPEN_CLOSE).
	public Type getType()
	{
		return ;
	}

Gets whether this is a close tag.

Returns:
True if this tag is a close tag
	public boolean isClose()
	{
		return  == ;
	}

Returns:
True, if tag is mutable
	public final boolean isMutable()
	{
		return ;
	}

Gets whether this is an open tag.

Returns:
True if this tag is an open tag
	public boolean isOpen()
	{
		return  == ;
	}

Gets whether this tag is an open/ close tag.

Returns:
True if this tag is an open and a close tag
	public boolean isOpenClose()
	{
		return  == ;
	}

Compare tag name including namespace

Parameters:
tag
Returns:
true if name and namespace are equal
	public boolean hasEqualTagName(final XmlTag tag)
	{
		{
			return false;
		}
		if ((getNamespace() == null) && (tag.getNamespace() == null))
		{
			return true;
		}
		if ((getNamespace() != null) && (tag.getNamespace() != null))
		{
		}
		return false;
	}

Makes this tag object immutable by making the attribute map unmodifiable. Immutable tags cannot be made mutable again. They can only be copied into new mutable tag objects.
	public void makeImmutable()
	{
		{
			 = false;
			if ( != null)
			{
				 = null;
			}
		}
	}

Gets this tag if it is already mutable, or a mutable copy of this tag if it is immutable.

Returns:
This tag if it is already mutable, or a mutable copy of this tag if it is immutable.
	public XmlTag mutable()
	{
		{
			return this;
		}
		else
		{
			final XmlTag tag = new XmlTag();
			return tag;
		}
	}

Copies all internal properties from this tag to dest. This is basically cloning without instance creation.

Parameters:
dest tag whose properties will be set
	{
		dest.namespace = ;
		dest.name = ;
		dest.pos = ;
		dest.length = ;
		dest.text = ;
		dest.type = ;
		dest.isMutable = true;
		dest.closes = ;
		dest.copyOf = ;
		if ( != null)
		{
			dest.attributes = new ValueMap();
		}
	}

Puts a boolean attribute.

Parameters:
key The key
value The value
Returns:
previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the map previously associated null with the specified key, if the implementation supports null values.
	public Object put(final String keyfinal boolean value)
	{
		return put(key, Boolean.toString(value));
	}

Puts an int attribute.

Parameters:
key The key
value The value
Returns:
previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the map previously associated null with the specified key, if the implementation supports null values.
	public Object put(final String keyfinal int value)
	{
		return put(key, Integer.toString(value));
	}

Puts a string attribute.

Parameters:
key The key
value The value
Returns:
previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the map previously associated null with the specified key, if the implementation supports null values.
	public Object put(final String keyfinal CharSequence value)
	{
		return getAttributes().put(keyvalue);
	}

Puts a org.apache.wicket.util.string.StringValueattribute.

Parameters:
key The key
value The value
Returns:
previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the map previously associated null with the specified key, if the implementation supports null values.
	public Object put(final String keyfinal StringValue value)
	{
		return getAttributes().put(key, (value != null) ? value.toString() : null);
	}

Puts all attributes in map

Parameters:
map A key/value map
	public void putAll(final Map map)
	{
		for (final Iterator iterator = map.entrySet().iterator(); iterator.hasNext();)
		{
			final Map.Entry entry = (Map.Entryiterator.next();
			Object value = entry.getValue();
			put((Stringentry.getKey(), (value != null) ? value.toString() : null);
		}
	}

Removes an attribute.

Parameters:
key The key to remove
	public void remove(final String key)
	{
	}

Sets the tag name.

Parameters:
name New tag name
	public void setName(final String name)
	{
		{
			this. = name;
			this. = true;
		}
		else
		{
			throw new UnsupportedOperationException("Attempt to set name of immutable tag");
		}
	}

Sets the tag namespace.

Parameters:
namespace New tag name
	public void setNamespace(final String namespace)
	{
		{
			this. = namespace;
			this. = true;
		}
		else
		{
			throw new UnsupportedOperationException("Attempt to set namespace of immutable tag");
		}
	}

Assuming this is a close tag, assign it's corresponding open tag.

Parameters:
tag the open-tag
Throws:
java.lang.RuntimeException if 'this' is not a close tag
	public void setOpenTag(final XmlTag tag)
	{
		this. = tag;
	}

Sets type of this tag if it is not immutable.

Parameters:
type The new type
	public void setType(final Type type)
	{
		{
			this. = type;
		}
		else
		{
			throw new UnsupportedOperationException("Attempt to set type of immutable tag");
		}
	}

Converts this object to a string representation.

Returns:
String version of this object
	{
		return "[Tag name = " +  + ", pos = " +  + ", line = " +  + ", length = " +
				 + ", attributes = [" + getAttributes() + "], type = " +  + "]";
	}

Converts this object to a string representation.

Returns:
String version of this object
	public String toString()
	{
	}

	{
		if (! && ( != null))
		{
			return ;
		}
		return toXmlString(null);
	}

Converts this object to a string representation.

Returns:
String version of this object
	{
		return "'" + toString() + "' (line " +  + ", column " +  + ")";
	}

Assuming some attributes have been changed, toXmlString() rebuilds the String on based on the tags informations.

Parameters:
attributeToBeIgnored
Returns:
A xml string matching the tag
	public CharSequence toXmlString(final String attributeToBeIgnored)
	{
		buffer.append('<');
		if ( == )
		{
			buffer.append('/');
		}
		if ( != null)
		{
			buffer.append(':');
		}
		buffer.append();
		final IValueMap attributes = getAttributes();
		if (attributes.size() > 0)
		{
			final Iterator iterator = attributes.keySet().iterator();
			for (; iterator.hasNext();)
			{
				final String key = (String)iterator.next();
				if ((key != null) &&
						((attributeToBeIgnored == null) || !key
								.equalsIgnoreCase(attributeToBeIgnored)))
				{
					buffer.append(" ");
					buffer.append(key);
					CharSequence value = getString(key);
					// Attributes without values are possible, e.g. 'disabled'
					if (value != null)
					{
						buffer.append("=\"");
						value = Strings.replaceAll(value"\"""\\\"");
						buffer.append(value);
						buffer.append("\"");
					}
				}
			}
		}
		if ( == )
		{
			buffer.append('/');
		}
		buffer.append('>');
		return buffer;
	}
New to GrepCode? Check out our FAQ X