use of org.apache.poi.hslf.exceptions.HSLFException in project poi by apache.
the class Record method createRecordForType.
/**
* For a given type (little endian bytes 3 and 4 in record header),
* byte array, start position and length:
* will return a Record object that will handle that record
*
* Remember that while PPT stores the record lengths as 8 bytes short
* (not including the size of the header), this code assumes you're
* passing in corrected lengths
*/
public static Record createRecordForType(long type, byte[] b, int start, int len) {
Record toReturn = null;
// would take us passed the end of the file
if (start + len > b.length) {
logger.log(POILogger.WARN, "Warning: Skipping record of type " + type + " at position " + start + " which claims to be longer than the file! (" + len + " vs " + (b.length - start) + ")");
return null;
}
// We use the RecordTypes class to provide us with the right
// class to use for a given type
// A spot of reflection gets us the (byte[],int,int) constructor
// From there, we instanciate the class
// Any special record handling occurs once we have the class
Class<? extends Record> c = null;
try {
c = RecordTypes.forTypeID((short) type).handlingClass;
if (c == null) {
// How odd. RecordTypes normally substitutes in
// a default handler class if it has heard of the record
// type but there's no support for it. Explicitly request
// that now
c = RecordTypes.UnknownRecordPlaceholder.handlingClass;
}
// Grab the right constructor
java.lang.reflect.Constructor<? extends Record> con = c.getDeclaredConstructor(new Class[] { byte[].class, Integer.TYPE, Integer.TYPE });
// Instantiate
toReturn = con.newInstance(new Object[] { b, start, len });
} catch (InstantiationException ie) {
throw new HSLFException("Couldn't instantiate the class for type with id " + type + " on class " + c + " : " + ie, ie);
} catch (java.lang.reflect.InvocationTargetException ite) {
throw new HSLFException("Couldn't instantiate the class for type with id " + type + " on class " + c + " : " + ite + "\nCause was : " + ite.getCause(), ite);
} catch (IllegalAccessException iae) {
throw new HSLFException("Couldn't access the constructor for type with id " + type + " on class " + c + " : " + iae, iae);
} catch (NoSuchMethodException nsme) {
throw new HSLFException("Couldn't access the constructor for type with id " + type + " on class " + c + " : " + nsme, nsme);
}
// If it's a position aware record, tell it where it is
if (toReturn instanceof PositionDependentRecord) {
PositionDependentRecord pdr = (PositionDependentRecord) toReturn;
pdr.setLastOnDiskOffset(start);
}
// Return the created record
return toReturn;
}
use of org.apache.poi.hslf.exceptions.HSLFException in project poi by apache.
the class TextSpecInfoAtom method reset.
/**
* Reset the content to one info run with the default values
* @param size the site of parent text
*/
public void reset(int size) {
TextSpecInfoRun sir = new TextSpecInfoRun(size);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
sir.writeOut(bos);
} catch (IOException e) {
throw new HSLFException(e);
}
_data = bos.toByteArray();
// Update the size (header bytes 5-8)
LittleEndian.putInt(_header, 4, _data.length);
}
use of org.apache.poi.hslf.exceptions.HSLFException in project poi by apache.
the class TextSpecInfoAtom method setParentSize.
/**
* Adapts the size by enlarging the last {@link TextSpecInfoRun}
* or chopping the runs to the given length
*
* @param size
*/
public void setParentSize(int size) {
assert (size > 0);
int covered = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
TextSpecInfoRun[] runs = getTextSpecInfoRuns();
assert (runs.length > 0);
for (int i = 0; i < runs.length && covered < size; i++) {
TextSpecInfoRun run = runs[i];
if (covered + run.getLength() > size || i == runs.length - 1) {
run.setLength(size - covered);
}
covered += run.getLength();
try {
run.writeOut(bos);
} catch (IOException e) {
throw new HSLFException(e);
}
}
_data = bos.toByteArray();
// Update the size (header bytes 5-8)
LittleEndian.putInt(_header, 4, _data.length);
}
use of org.apache.poi.hslf.exceptions.HSLFException in project poi by apache.
the class ActiveXShape method setActiveXIndex.
/**
* Assign a control to this shape
*
* @see org.apache.poi.hslf.usermodel.HSLFSlideShow#addMovie(String, int)
* @param idx the index of the movie
*/
public void setActiveXIndex(int idx) {
ExObjRefAtom oe = getClientDataRecord(RecordTypes.ExObjRefAtom.typeID);
if (oe == null) {
throw new HSLFException("OEShapeAtom for ActiveX doesn't exist");
}
oe.setExObjIdRef(idx);
}
use of org.apache.poi.hslf.exceptions.HSLFException in project poi by apache.
the class ColorSchemeAtom method splitRGB.
/**
* Convert from an integer RGB value to individual R, G, B 0-255 values
*/
public static byte[] splitRGB(int rgb) {
byte[] ret = new byte[3];
// Serialise to bytes, then grab the right ones out
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
writeLittleEndian(rgb, baos);
} catch (IOException ie) {
// Should never happen
throw new HSLFException(ie);
}
byte[] b = baos.toByteArray();
System.arraycopy(b, 0, ret, 0, 3);
return ret;
}
Aggregations