com.topologi.diffx.event.impl.OpenElementEventImpl Maven / Gradle / Ivy
Show all versions of docx4j Show documentation
/*
* This file is part of the DiffX library.
*
* For licensing information please see the file license.txt included in the release.
* A copy of this licence can also be found at
* http://www.opensource.org/licenses/artistic-license-2.0.php
*/
package com.topologi.diffx.event.impl;
import java.io.IOException;
import javax.xml.XMLConstants;
import com.topologi.diffx.event.DiffXEvent;
import com.topologi.diffx.event.OpenElementEvent;
import com.topologi.diffx.xml.XMLWriter;
/**
* A basic implementation of the close element event.
*
* It corresponds to the startElement
SAX event.
*
*
This implementation is not namespace aware.
*
* @author Christophe Lauret
* @version 27 March 2010
*/
public final class OpenElementEventImpl extends DiffXEventBase implements OpenElementEvent {
/**
* The local name of the element.
*/
private final String name;
/**
* Hashcode value for this event.
*/
private final int hashCode;
/**
* Creates a new open element event.
*
* @param name The local name of the element
*
* @throws NullPointerException if the name is null
.
*/
public OpenElementEventImpl(String name) throws NullPointerException {
if (name == null)
throw new NullPointerException("Element must have a name.");
this.name = name;
this.hashCode = toHashCode(name);
}
/**
* @return Returns the name.
*/
@Override
public String getName() {
return this.name;
}
/**
* @return Returns the Namespace URI.
*/
@Override
public String getURI() {
return XMLConstants.NULL_NS_URI;
}
@Override
public int hashCode() {
return this.hashCode;
}
/**
* Returns true
if the event is an open element event.
*
* @param e The event to compare with this event.
*
* @return true
if this event is equal to the specified event;
* false
otherwise.
*/
@Override
public boolean equals(DiffXEvent e) {
if (e.getClass() != this.getClass()) return false;
OpenElementEventImpl oee = (OpenElementEventImpl)e;
return oee.name.equals(this.name);
}
@Override
public String toString() {
return "openElement: "+this.name;
}
@Override
public void toXML(XMLWriter xml) throws IOException {
xml.openElement(this.name, false);
}
/**
* Converts this event to an XML open tag.
*
*
Note that this method does not allow attributes to be put after this element.
*
* {@inheritDoc}
*/
@Override
public StringBuffer toXML(StringBuffer xml) {
return xml.append('<').append(this.name).append('>');
}
/**
* Calculates the hashcode for this event.
*
* @param s String from which the hashcode is calculated.
* @return a number suitable as a hashcode.
*/
private static int toHashCode(String s) {
return 11 * 41 + (s != null? s.hashCode() : 0);
}
}