package com.ctc.wstx.io;
import java.io.Serializable;
import javax.xml.stream.Location;
import org.codehaus.stax2.XMLStreamLocation2;
import com.ctc.wstx.util.StringUtil;
public class WstxInputLocation
implements Serializable, XMLStreamLocation2
{
private static final long serialVersionUID = 1L;
private final static WstxInputLocation sEmptyLocation
= new WstxInputLocation(null, "", "", -1, -1, -1);
final protected WstxInputLocation mContext;
final protected String mPublicId, mSystemId;
final protected long mCharOffset;
final protected int mCol, mRow;
transient protected String mDesc = null;
public WstxInputLocation(WstxInputLocation ctxt,
String pubId, String sysId,
long charOffset, int row, int col)
{
mContext = ctxt;
mPublicId = pubId;
mSystemId = sysId;
mCharOffset = charOffset;
mCol = col;
mRow = row;
}
public WstxInputLocation(WstxInputLocation ctxt,
String pubId, SystemId sysId, long charOffset, int row, int col)
{
mContext = ctxt;
mPublicId = pubId;
mSystemId = (sysId == null) ? "N/A" : sysId.toString();
mCharOffset = charOffset;
mCol = col;
mRow = row;
}
public static WstxInputLocation getEmptyLocation() {
return sEmptyLocation;
}
public long getCharacterOffsetLong() { return mCharOffset; }
@Override
public int getCharacterOffset() { return (int)mCharOffset; }
@Override
public int getColumnNumber() { return mCol; }
@Override
public int getLineNumber() { return mRow; }
@Override
public String getPublicId() { return mPublicId; }
@Override
public String getSystemId() { return mSystemId; }
@Override
public XMLStreamLocation2 getContext() { return mContext; }
@Override
public String toString()
{
if (mDesc == null) {
StringBuilder sb;
if (mContext != null) {
sb = new StringBuilder(200);
} else {
sb = new StringBuilder(80);
}
appendDesc(sb);
mDesc = sb.toString();
}
return mDesc;
}
@Override
public int hashCode() {
return ((int)mCharOffset) ^ (int)(0xffffffff & mCharOffset >> 32) ^ mRow ^ mCol + (mCol << 3);
}
@Override
public boolean equals(Object o) {
if (!(o instanceof WstxInputLocation)) {
return false;
}
WstxInputLocation other = (WstxInputLocation) o;
if (other.getCharacterOffsetLong() != getCharacterOffsetLong()) {
return false;
}
String otherPub = other.getPublicId();
if (otherPub == null) {
otherPub = "";
}
if (!otherPub.equals(mPublicId)) {
return false;
}
String otherSys = other.getSystemId();
if (otherSys == null) {
otherSys = "";
}
return otherSys.equals(mSystemId);
}
private void appendDesc(StringBuilder sb)
{
String srcId;
if (mSystemId != null) {
sb.append("[row,col,system-id]: ");
srcId = mSystemId;
} else if (mPublicId != null) {
sb.append("[row,col,public-id]: ");
srcId = mPublicId;
} else {
sb.append("[row,col {unknown-source}]: ");
srcId = null;
}
sb.append('[');
sb.append(mRow);
sb.append(',');
sb.append(mCol);
if (srcId != null) {
sb.append(',');
sb.append('"');
sb.append(srcId);
sb.append('"');
}
sb.append(']');
if (mContext != null) {
StringUtil.appendLF(sb);
sb.append(" from ");
mContext.appendDesc(sb);
}
}
}