+ "'s PRM references to unknown grpprl" );
continue;
}
boolean hasPap = false;
SprmBuffer sprmBuffer = sprmBuffers[igrpprl];
for ( SprmIterator iterator = sprmBuffer.iterator(); iterator
.hasNext(); )
{
SprmOperation sprmOperation = iterator.next();
if ( sprmOperation.getType() == SprmOperation.TYPE_PAP )
{
hasPap = true;
break;
}
}
if ( hasPap )
{
SprmBuffer newSprmBuffer = new SprmBuffer( 2 );
newSprmBuffer.append( sprmBuffer.toByteArray() );
PAPX papx = new PAPX( textPiece.getStart(),
textPiece.getEnd(), newSprmBuffer );
paragraphs.add( papx );
}
}
logger.log( POILogger.DEBUG,
"Merged (?) with PAPX from complex file table in ",
Long.valueOf( System.currentTimeMillis() - start ),
" ms (", Integer.valueOf( paragraphs.size() ),
" elements in total)" );
start = System.currentTimeMillis();
}
List<PAPX> oldPapxSortedByEndPos = new ArrayList<PAPX>( paragraphs );
Collections.sort( oldPapxSortedByEndPos,
PropertyNode.EndComparator.instance );
logger.log( POILogger.DEBUG, "PAPX sorted by end position in ",
Long.valueOf( System.currentTimeMillis() - start ), " ms" );
start = System.currentTimeMillis();
final Map<PAPX, Integer> papxToFileOrder = new IdentityHashMap<PAPX, Integer>();
{
int counter = 0;
for ( PAPX papx : paragraphs )
{
papxToFileOrder.put( papx, Integer.valueOf( counter++ ) );
}
}
final Comparator<PAPX> papxFileOrderComparator = new Comparator<PAPX>()
{
public int compare( PAPX o1, PAPX o2 )
{
Integer i1 = papxToFileOrder.get( o1 );
Integer i2 = papxToFileOrder.get( o2 );
return i1.compareTo( i2 );
}
};
logger.log( POILogger.DEBUG, "PAPX's order map created in ",
Long.valueOf( System.currentTimeMillis() - start ), " ms" );
start = System.currentTimeMillis();
List<PAPX> newPapxs = new LinkedList<PAPX>();
int lastParStart = 0;
int lastPapxIndex = 0;
for ( int charIndex = 0; charIndex < docText.length(); charIndex++ )
{
final char c = docText.charAt( charIndex );
if ( c != 13 && c != 7 && c != 12 )
continue;
final int startInclusive = lastParStart;
final int endExclusive = charIndex + 1;
boolean broken = false;
List<PAPX> papxs = new LinkedList<PAPX>();
for ( int papxIndex = lastPapxIndex; papxIndex < oldPapxSortedByEndPos
.size(); papxIndex++ )
{
broken = false;
PAPX papx = oldPapxSortedByEndPos.get( papxIndex );
assert startInclusive == 0
|| papxIndex + 1 == oldPapxSortedByEndPos.size()
|| papx.getEnd() > startInclusive;
if ( papx.getEnd() - 1 > charIndex )
{
lastPapxIndex = papxIndex;
broken = true;
break;
}
papxs.add( papx );
}
if ( !broken )
{
lastPapxIndex = oldPapxSortedByEndPos.size() - 1;
}
if ( papxs.size() == 0 )
{
logger.log( POILogger.WARN, "Paragraph [",
Integer.valueOf( startInclusive ), "; ",
Integer.valueOf( endExclusive ),
") has no PAPX. Creating new one." );
// create it manually
PAPX papx = new PAPX( startInclusive, endExclusive,
new SprmBuffer( 2 ) );
newPapxs.add( papx );
lastParStart = endExclusive;
continue;
}
if ( papxs.size() == 1 )
{
// can we reuse existing?
PAPX existing = papxs.get( 0 );
if ( existing.getStart() == startInclusive
&& existing.getEnd() == endExclusive )
{
newPapxs.add( existing );
lastParStart = endExclusive;
continue;
}
}
// restore file order of PAPX
Collections.sort( papxs, papxFileOrderComparator );
SprmBuffer sprmBuffer = null;
for ( PAPX papx : papxs )
{
if ( papx.getGrpprl() == null || papx.getGrpprl().length == 0 )
continue;
if ( sprmBuffer == null )
try
{
sprmBuffer = (SprmBuffer) papx.getSprmBuf().clone();
}
catch ( CloneNotSupportedException e )
{
// can't happen
throw new Error( e );
}
else
{
sprmBuffer.append( papx.getGrpprl(), 2 );
}
}
PAPX newPapx = new PAPX( startInclusive, endExclusive, sprmBuffer );
newPapxs.add( newPapx );