All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.poi.hwpf.model.FSPATable Maven / Gradle / Ivy

There is a newer version: 5.2.5
Show newest version
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */

package org.apache.poi.hwpf.model;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.util.Internal;

/**
 * This class holds all the FSPA (File Shape Address) structures.
 * 
 * @author Squeeself
 */
@Internal
public final class FSPATable
{

    private final Map _byStart = new LinkedHashMap<>();

    public FSPATable( byte[] tableStream, FileInformationBlock fib,
            FSPADocumentPart part )
    {
        int offset = fib.getFSPAPlcfOffset( part );
        int length = fib.getFSPAPlcfLength( part );

        PlexOfCps plex = new PlexOfCps( tableStream, offset, length,
                FSPA.getSize() );
        for ( int i = 0; i < plex.length(); i++ )
        {
            GenericPropertyNode property = plex.getProperty( i );
            _byStart.put( Integer.valueOf( property.getStart() ), property );
        }
    }

    @Deprecated
    public FSPATable( byte[] tableStream, int fcPlcspa, int lcbPlcspa,
            List tpt )
    {
        // Will be 0 if no drawing objects in document
        if ( fcPlcspa == 0 )
            return;

        PlexOfCps plex = new PlexOfCps( tableStream, fcPlcspa, lcbPlcspa,
                FSPA.FSPA_SIZE );
        for ( int i = 0; i < plex.length(); i++ )
        {
            GenericPropertyNode property = plex.getProperty( i );
            _byStart.put( Integer.valueOf( property.getStart() ), property );
        }
    }

    public FSPA getFspaFromCp( int cp )
    {
        GenericPropertyNode propertyNode = _byStart.get( Integer.valueOf( cp ) );
        if ( propertyNode == null )
        {
            return null;
        }
        return new FSPA( propertyNode.getBytes(), 0 );
    }

    public FSPA[] getShapes()
    {
        List result = new ArrayList<>(_byStart.size());
        for ( GenericPropertyNode propertyNode : _byStart.values() )
        {
            result.add( new FSPA( propertyNode.getBytes(), 0 ) );
        }
        return result.toArray(new FSPA[0]);
    }

    public String toString() {
        StringBuilder buf = new StringBuilder();
        buf.append( "[FPSA PLC size=" ).append( _byStart.size() ).append( "]\n" );

        for ( Map.Entry entry : _byStart.entrySet() ) {
            Integer i = entry.getKey();
            buf.append( "  " ).append(i).append( " => \t" );

            try {
                FSPA fspa = getFspaFromCp( i.intValue() );
                buf.append(fspa);
            } catch ( Exception exc ) {
                buf.append( exc.getMessage() );
            }
            buf.append( "\n" );
        }
        buf.append( "[/FSPA PLC]" );
        return buf.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy