+ "'s PRM references to unknown grpprl" );
continue;
}
boolean hasChp = false;
SprmBuffer sprmBuffer = sprmBuffers[igrpprl];
for ( SprmIterator iterator = sprmBuffer.iterator(); iterator
.hasNext(); )
{
SprmOperation sprmOperation = iterator.next();
if ( sprmOperation.getType() == SprmOperation.TYPE_CHP )
{
hasChp = true;
break;
}
}
if ( hasChp )
{
SprmBuffer newSprmBuffer;
try
{
newSprmBuffer = (SprmBuffer) sprmBuffer.clone();
}
catch ( CloneNotSupportedException e )
{
// shall not happen
throw new Error( e );
}
CHPX chpx = new CHPX( textPiece.getStart(),
textPiece.getEnd(), newSprmBuffer );
_textRuns.add( chpx );
}
}
logger.log( POILogger.DEBUG,
"Merged with CHPX from complex file table in ",
Long.valueOf( System.currentTimeMillis() - start ),
" ms (", Integer.valueOf( _textRuns.size() ),
" elements in total)" );
start = System.currentTimeMillis();
}
List<CHPX> oldChpxSortedByStartPos = new ArrayList<CHPX>( _textRuns );
Collections.sort( oldChpxSortedByStartPos,
PropertyNode.StartComparator.instance );
logger.log( POILogger.DEBUG, "CHPX sorted by start position in ",
Long.valueOf( System.currentTimeMillis() - start ), " ms" );
start = System.currentTimeMillis();
final Map<CHPX, Integer> chpxToFileOrder = new IdentityHashMap<CHPX, Integer>();
{
int counter = 0;
for ( CHPX chpx : _textRuns )
{
chpxToFileOrder.put( chpx, Integer.valueOf( counter++ ) );
}
}
final Comparator<CHPX> chpxFileOrderComparator = new Comparator<CHPX>()
{
public int compare( CHPX o1, CHPX o2 )
{
Integer i1 = chpxToFileOrder.get( o1 );
Integer i2 = chpxToFileOrder.get( o2 );
return i1.compareTo( i2 );
}
};
logger.log( POILogger.DEBUG, "CHPX's order map created in ",
Long.valueOf( System.currentTimeMillis() - start ), " ms" );
start = System.currentTimeMillis();
List<Integer> textRunsBoundariesList;
{
Set<Integer> textRunsBoundariesSet = new HashSet<Integer>();
for ( CHPX chpx : _textRuns )
{
textRunsBoundariesSet.add( Integer.valueOf( chpx.getStart() ) );
textRunsBoundariesSet.add( Integer.valueOf( chpx.getEnd() ) );
}
textRunsBoundariesSet.remove( Integer.valueOf( 0 ) );
textRunsBoundariesList = new ArrayList<Integer>(
textRunsBoundariesSet );
Collections.sort( textRunsBoundariesList );
}
logger.log( POILogger.DEBUG, "Texts CHPX boundaries collected in ",
Long.valueOf( System.currentTimeMillis() - start ), " ms" );
start = System.currentTimeMillis();
List<CHPX> newChpxs = new LinkedList<CHPX>();
int lastTextRunStart = 0;
for ( Integer objBoundary : textRunsBoundariesList )
{
final int boundary = objBoundary.intValue();
final int startInclusive = lastTextRunStart;
final int endExclusive = boundary;
lastTextRunStart = endExclusive;
int startPosition = binarySearch( oldChpxSortedByStartPos, boundary );
startPosition = Math.abs( startPosition );
while ( startPosition >= oldChpxSortedByStartPos.size() )
startPosition--;
while ( startPosition > 0
&& oldChpxSortedByStartPos.get( startPosition ).getStart() >= boundary )
startPosition--;
List<CHPX> chpxs = new LinkedList<CHPX>();
for ( int c = startPosition; c < oldChpxSortedByStartPos.size(); c++ )
{
CHPX chpx = oldChpxSortedByStartPos.get( c );
if ( boundary < chpx.getStart() )
break;
int left = Math.max( startInclusive, chpx.getStart() );
int right = Math.min( endExclusive, chpx.getEnd() );
if ( left < right )
{
chpxs.add( chpx );
}
}
if ( chpxs.size() == 0 )
{
logger.log( POILogger.WARN, "Text piece [",
Integer.valueOf( startInclusive ), "; ",
Integer.valueOf( endExclusive ),
") has no CHPX. Creating new one." );
// create it manually
CHPX chpx = new CHPX( startInclusive, endExclusive,
new SprmBuffer( 0 ) );
newChpxs.add( chpx );
continue;
}
if ( chpxs.size() == 1 )
{
// can we reuse existing?
CHPX existing = chpxs.get( 0 );
if ( existing.getStart() == startInclusive
&& existing.getEnd() == endExclusive )
{
newChpxs.add( existing );
continue;
}
}
Collections.sort( chpxs, chpxFileOrderComparator );
SprmBuffer sprmBuffer = new SprmBuffer( 0 );
for ( CHPX chpx : chpxs )
{
sprmBuffer.append( chpx.getGrpprl(), 0 );
}
CHPX newChpx = new CHPX( startInclusive, endExclusive, sprmBuffer );
newChpxs.add( newChpx );
continue;