Search in sources :

Example 1 with HSLFException

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;
}
Also used : HSLFException(org.apache.poi.hslf.exceptions.HSLFException)

Example 2 with HSLFException

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);
}
Also used : HSLFException(org.apache.poi.hslf.exceptions.HSLFException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 3 with HSLFException

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);
}
Also used : HSLFException(org.apache.poi.hslf.exceptions.HSLFException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 4 with HSLFException

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);
}
Also used : ExObjRefAtom(org.apache.poi.hslf.record.ExObjRefAtom) HSLFException(org.apache.poi.hslf.exceptions.HSLFException)

Example 5 with HSLFException

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;
}
Also used : HSLFException(org.apache.poi.hslf.exceptions.HSLFException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

HSLFException (org.apache.poi.hslf.exceptions.HSLFException)20 IOException (java.io.IOException)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 DrawPaint (org.apache.poi.sl.draw.DrawPaint)4 SolidPaint (org.apache.poi.sl.usermodel.PaintStyle.SolidPaint)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 HashMap (java.util.HashMap)3 TextPropCollection (org.apache.poi.hslf.model.textproperties.TextPropCollection)3 Record (org.apache.poi.hslf.record.Record)3 InputStream (java.io.InputStream)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 InflaterInputStream (java.util.zip.InflaterInputStream)2 EscherContainerRecord (org.apache.poi.ddf.EscherContainerRecord)2 EscherOptRecord (org.apache.poi.ddf.EscherOptRecord)2 EscherTextboxWrapper (org.apache.poi.hslf.record.EscherTextboxWrapper)2 TextHeaderAtom (org.apache.poi.hslf.record.TextHeaderAtom)2 Rectangle2D (java.awt.geom.Rectangle2D)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1