CExoLocStringData.java
// <editor-fold defaultstate="collapsed" desc="license">
/*
* Copyright (c) 2014, Karl H. Beckers
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of the <ORGANIZATION> nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
**/
// </editor-fold>
package net.jarre_de_the.griffin.types.data;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Implementation of the CEXOLOCSTRING used to provide localized text.
* Physically, the CEXOLOCSTRING first has a total length DWORD, then a DWORD
* StrRef, then a DWORD count of embedded strings, then the strings as
* {@code CExoLocSubStringData}.
* <p>
* @author charly4711
*/
public class CExoLocStringData extends AbstractData
implements Cloneable {
private static final Logger LOGGER = LoggerFactory.getLogger(CExoLocStringData.class);
private DWordData stringRef;
private List<CExoLocSubStringData> subStrings
= new ArrayList<CExoLocSubStringData>();
/*
*
* constructors
*
*/
protected CExoLocStringData() {
}
/**
* Creates a new instance of {@code CExoLocStringData} and sets defined
* values.
* <p>
* @param stringRef The StrRef value to set, -1 means: Don't use dialog.tlk.
* You must, then, pass subStrings. This is not enforced, though. The
* Bioware docs are a bit strange here: It's a DWORD, a DWORD is unsigned,
* how can it be -1? Guess, they don't mean stringRef is an INT but just
* that -1 is meant to read 0xFFFFFFFF.
* @param subStrings The substrings to set.
* @see #setStringRef(DWordData sr)
* @see #setSubStrings(List)
*/
public CExoLocStringData(DWordData stringRef,
List<CExoLocSubStringData> subStrings) {
setStringRef(stringRef);
setSubStrings(subStrings);
}
/*
*
* read from file
*
*/
/**
* Creates a new instance of {@code CExoLocStringData} based on the data read from
* the file passed. Note that the contents are read from whatever position
* the file pointer is at.
* <p>
* @param in An open {@code RandomAccessFile}.
* @return
* @throws java.io.IOException If there are any I/O related problems with
* file access to in.
*/
public static CExoLocStringData read(RandomAccessFile in)
throws IOException {
LOGGER.debug("Reading CExoLocStringData from file.");
CExoLocStringData ret = new CExoLocStringData();
List<CExoLocSubStringData> subStrings
= new ArrayList<CExoLocSubStringData>();
// first read the total length
DWordData len = DWordData.read(in);
// then read the StringRef
ret.setStringRef(DWordData.read(in));
LOGGER.trace("StringRef is: "
+ Integer.toHexString(ret.getStringRef().getValue()));
// if the total length is greater than the length of the StrRef,
// we have embedded strings we need to read
if (len.getValueAsNumber() > DWordData.LENGTH) {
len = DWordData.read(in);
LOGGER.trace("Need to read " + len.getValueAsNumber() + " elements");
for (int i = 0; i < len.getValueAsNumber().intValue(); i++) {
CExoLocSubStringData css = CExoLocSubStringData.read(in);
LOGGER.trace("Adding string " + i + ": "
+ new String(css.getValueAsByteArray(), css.getLangId().charset()));
subStrings.add(css);
}
}
ret.setSubStrings(subStrings);
LOGGER.debug("Read " + subStrings.size() + " substrings");
return ret;
}
/*
*
* setter
*
*/
private void setSubStrings(List<CExoLocSubStringData> subStrings) {
LOGGER.debug("Setting substrings to passed list of "
+ (subStrings == null ? null : subStrings.size()) + " strings");
if (subStrings != null) {
List<CExoLocSubStringData> in = new ArrayList<CExoLocSubStringData>();
for (CExoLocSubStringData sub : subStrings) {
try {
if (null != sub) {
in.add(sub.clone());
}
} catch (CloneNotSupportedException ex) {
LOGGER.error(null, ex);
}
}
this.subStrings = in;
}
}
private void setStringRef(DWordData sr) {
LOGGER.debug("Setting StringRef to DWord of value: "
+ (null != sr ? Integer.toHexString(sr.getValue()) : null));
if (null != sr) {
try {
stringRef = sr.clone();
} catch (CloneNotSupportedException ex) {
LOGGER.error(null, ex);
}
} else {
stringRef = null;
}
}
/*
*
* getter
*
*/
/**
* Returns the StrRef DWORD.
* <p>
* @return This instance's StrRef.
*/
public DWordData getStringRef() {
DWordData result = null;
if (null != stringRef) {
try {
result = stringRef.clone();
} catch (CloneNotSupportedException ex) {
LOGGER.error(null, ex);
}
}
return result;
}
/**
* Returns the substrings contained in this instance.
* <p>
* @return A list of {@code CExoLocSubStringData} objects.
*/
public List<CExoLocSubStringData> getSubStrings() {
List<CExoLocSubStringData> ret = new ArrayList<CExoLocSubStringData>();
for (CExoLocSubStringData sub : subStrings) {
try {
ret.add(sub.clone());
} catch (CloneNotSupportedException ex) {
LOGGER.error(null, ex);
}
}
return ret;
}
/**
* Returns the a certain numbered substring contained in this instance.
* This is more efficient than #getSubStrings().get(n) because #getSubStrings()
* clones the whole list before returning, while this one only clones
* the element returned.
* <p>
* @param i The index of the substring list item to return.
* @return A {@code CExoLocSubStringData} object.
*/
public CExoLocSubStringData getSubString(int i) {
CExoLocSubStringData result = null;
if (getSize() > i) {
try {
result = subStrings.get(i).clone();
} catch (CloneNotSupportedException ex) {
LOGGER.error(null, ex);
}
}
return result;
}
/**
* Retrieve the number of substrings contained. This is more efficient than
* #getSubStrings().size() because #getSubStrings() clones the whole list
* before returning, while this returns the size of the original list.
* <p>
* @return The number of substrings contained.
*/
public int getSize() {
return subStrings.size();
}
/*
*
* utility
*
*/
/**
* Overrides the {@code java.lang.Object} method to ensure we always get
* back an instance of {@code CExoLocStringData} rather than just
* {@code Object}.
* <p>
* @return A deep copy of this object.
* @throws java.lang.CloneNotSupportedException
*/
@Override
public CExoLocStringData clone() throws CloneNotSupportedException {
LOGGER.debug("Cloning object");
CExoLocStringData clone = (CExoLocStringData) super.clone();
List<CExoLocSubStringData> subStringCopy
= new ArrayList<CExoLocSubStringData>(subStrings.size());
for (CExoLocSubStringData sS : subStrings) {
subStringCopy.add(sS.clone());
}
if (null != stringRef) {
clone.setStringRef(stringRef.clone());
} else {
clone.setStringRef(null);
}
clone.setSubStrings(subStringCopy);
return clone;
}
/**
* Overrides the {@code java.lang.Object} method to make test for equality
* possible for this class.
* <p>
* @param compare compare Object to compare this object with.
* @return True if the objects have the same value, false if not.
*/
@Override
public boolean equals(Object compare) {
if (compare == this) {
return true;
}
if (!(compare instanceof CExoLocStringData)) {
return false;
}
CExoLocStringData dw = (CExoLocStringData) compare;
if (dw.getSize() != this.getSize()) {
return false;
}
boolean subStringsEqual = true;
for (int i = 0; i < dw.getSize(); i++) {
if (!getSubString(i).equals(dw.getSubString(i))) {
subStringsEqual = false;
}
}
boolean strRefEqual = false;
if (null == getStringRef() && null == dw.getStringRef()) {
strRefEqual = true;
} else if (null == getStringRef() && new DWordData(-1).equals(dw.getStringRef())) {
strRefEqual = true;
} else if (null == dw.getStringRef() && new DWordData(-1).equals(getStringRef())) {
strRefEqual = true;
} else if ((null != getStringRef()) && getStringRef().equals(dw.getStringRef())) {
strRefEqual = true;
}
return subStringsEqual && strRefEqual;
}
/**
* Overrides the {@code java.lang.Object} method to ensure we have a hash
* code consistent with the test for equality.
* <p>
* @return The hash returned is based on the value, but VERY poorly
* distributed.
*/
@Override
public int hashCode() {
int i = 0;
if (null != getStringRef()) {
i = getStringRef().getValue();
}
return i | subStrings.size();
}
@Override
public String toString() {
if (stringRef != null && !stringRef.equals(new DWordData(-1))) {
return "StringRef_" + Integer.toHexString(stringRef.getValue());
} else {
if (subStrings.size() > 0) {
CExoLocSubStringData first = subStrings.get(0);
// we are not adding null elements ... therefore we can't have them, here
// if (first != null) {
return first.toString();
// }
}
return EMPTY_VALUE;
}
}
}