FileDocCategorySizeDatePackage
ListTables.javaAPI DocApache Poi 3.0.16844Mon Jan 01 18:55:32 GMT 2007org.apache.poi.hwpf.model

ListTables

public class ListTables extends Object
author
Ryan Ackley

Fields Summary
private static final int
LIST_DATA_SIZE
private static final int
LIST_FORMAT_OVERRIDE_SIZE
HashMap
_listMap
ArrayList
_overrideList
Constructors Summary
public ListTables()


   
  

  
public ListTables(byte[] tableStream, int lstOffset, int lfoOffset)

    // get the list data
    int length = LittleEndian.getShort(tableStream, lstOffset);
    lstOffset += LittleEndian.SHORT_SIZE;
    int levelOffset = lstOffset + (length * LIST_DATA_SIZE);

    for (int x = 0; x < length; x++)
    {
      ListData lst = new ListData(tableStream, lstOffset);
      _listMap.put(new Integer(lst.getLsid()), lst);
      lstOffset += LIST_DATA_SIZE;

      int num = lst.numLevels();
      for (int y = 0; y < num; y++)
      {
        ListLevel lvl = new ListLevel(tableStream, levelOffset);
        lst.setLevel(y, lvl);
        levelOffset += lvl.getSizeInBytes();
      }
    }

    // now get the list format overrides. The size is an int unlike the LST size
    length = LittleEndian.getInt(tableStream, lfoOffset);
    lfoOffset += LittleEndian.INT_SIZE;
    int lfolvlOffset = lfoOffset + (LIST_FORMAT_OVERRIDE_SIZE * length);
    for (int x = 0; x < length; x++)
    {
      ListFormatOverride lfo = new ListFormatOverride(tableStream, lfoOffset);
      lfoOffset += LIST_FORMAT_OVERRIDE_SIZE;
      int num = lfo.numOverrides();
      for (int y = 0; y < num; y++)
      {
        while(tableStream[lfolvlOffset] == -1)
        {
          lfolvlOffset++;
        }
        ListFormatOverrideLevel lfolvl = new ListFormatOverrideLevel(tableStream, lfolvlOffset);
        lfo.setOverride(y, lfolvl);
        lfolvlOffset += lfolvl.getSizeInBytes();
      }
      _overrideList.add(lfo);
    }
  
Methods Summary
public intaddList(org.apache.poi.hwpf.model.ListData lst, org.apache.poi.hwpf.model.ListFormatOverride override)

    int lsid = lst.getLsid();
    while (_listMap.get(new Integer(lsid)) != null)
    {
      lsid = lst.resetListID();
      override.setLsid(lsid);
    }
    _listMap.put(new Integer(lsid), lst);
    _overrideList.add(override);
    return lsid;
  
public booleanequals(java.lang.Object obj)

    if (obj == null)
    {
      return false;
    }

    ListTables tables = (ListTables)obj;

    if (_listMap.size() == tables._listMap.size())
    {
      Iterator it = _listMap.keySet().iterator();
      while (it.hasNext())
      {
        Object key = it.next();
        ListData lst1 = (ListData)_listMap.get(key);
        ListData lst2 = (ListData)tables._listMap.get(key);
        if (!lst1.equals(lst2))
        {
          return false;
        }
      }
      int size = _overrideList.size();
      if (size == tables._overrideList.size())
      {
        for (int x = 0; x < size; x++)
        {
          if (!_overrideList.get(x).equals(tables._overrideList.get(x)))
          {
            return false;
          }
        }
        return true;
      }
    }
    return false;
  
public org.apache.poi.hwpf.model.ListLevelgetLevel(int listID, int level)

    ListData lst = (ListData)_listMap.get(new Integer(listID));
    ListLevel lvl = lst.getLevels()[level];
    return lvl;
  
public org.apache.poi.hwpf.model.ListDatagetListData(int listID)

    return (ListData) _listMap.get(new Integer(listID));
  
public org.apache.poi.hwpf.model.ListFormatOverridegetOverride(int lfoIndex)

    return (ListFormatOverride)_overrideList.get(lfoIndex - 1);
  
public intgetOverrideIndexFromListID(int lstid)

    int returnVal = -1;
    int size = _overrideList.size();
    for (int x = 0; x < size; x++)
    {
      ListFormatOverride next = (ListFormatOverride)_overrideList.get(x);
      if (next.getLsid() == lstid)
      {
        // 1-based index I think
        returnVal = x+1;
        break;
      }
    }
    if (returnVal == -1)
    {
      throw new NoSuchElementException("No list found with the specified ID");
    }
    return returnVal;
  
public voidwriteListDataTo(org.apache.poi.hwpf.model.io.HWPFOutputStream tableStream)


    Integer[] intList = (Integer[])_listMap.keySet().toArray(new Integer[0]);

    // use this stream as a buffer for the levels since their size varies.
    ByteArrayOutputStream levelBuf = new ByteArrayOutputStream();

    // use a byte array for the lists because we know their size.
    byte[] listBuf = new byte[intList.length * LIST_DATA_SIZE];

    byte[] shortHolder = new byte[2];
    LittleEndian.putShort(shortHolder, (short)intList.length);
    tableStream.write(shortHolder);

    for (int x = 0; x < intList.length; x++)
    {
      ListData lst = (ListData)_listMap.get(intList[x]);
      tableStream.write(lst.toByteArray());
      ListLevel[] lvls = lst.getLevels();
      for (int y = 0; y < lvls.length; y++)
      {
        levelBuf.write(lvls[y].toByteArray());
      }
    }
    tableStream.write(levelBuf.toByteArray());
  
public voidwriteListOverridesTo(org.apache.poi.hwpf.model.io.HWPFOutputStream tableStream)


    // use this stream as a buffer for the levels since their size varies.
    ByteArrayOutputStream levelBuf = new ByteArrayOutputStream();

    int size = _overrideList.size();

    byte[] intHolder = new byte[4];
    LittleEndian.putInt(intHolder, size);
    tableStream.write(intHolder);

    for (int x = 0; x < size; x++)
    {
      ListFormatOverride lfo = (ListFormatOverride)_overrideList.get(x);
      tableStream.write(lfo.toByteArray());
      ListFormatOverrideLevel[] lfolvls = lfo.getLevelOverrides();
      for (int y = 0; y < lfolvls.length; y++)
      {
        levelBuf.write(lfolvls[y].toByteArray());
      }
    }
    tableStream.write(levelBuf.toByteArray());