Question

I'm currently using POI to attempt to extract text out of a batch of Word documents and I need to be able to determine what entries a document contains. I've been able to get as far as pulling the document root and pulling the first entry but I want to be able to view all entries. The getEntries() method seems to provide this functionality but I'm at a loss as to how to use getViewableIterator() to pull them out.

Below is what I have code-wise:

<cfset myFile = createObject("java", "java.io.FileInputStream").init(fileInputPath)>
<cfset fileSystem = CreateObject( "java", "org.apache.poi.poifs.filesystem.POIFSFileSystem" ).Init(myFile)>

<cfloop from="1" to="#fileSystem.getRoot().getEntryCount()#" index="i">
     <cfset viewableIterator = fileSystem.getRoot().getEntries().next().getViewableIterator()>
     <cfset nextEntry = fileSystem.getRoot().getEntries().next()>
     <cfif viewableIterator.hasNext()>
         <cfdump var="#nextEntry.getShortDescription()#">
         <cfset viewableIterator.remove()>
     </cfif>
</cfloop>

On the first loop, I'm able to get the first entry just fine. However, I get an java.lang.IllegalStateException error as soon as remove() is executed. Obviously I'm not using the remove() method correctly but I haven't been able to find any examples of how this should be properly used. Any help would be greatly appreciated.

Was it helpful?

Solution

I don't really understand your XML tags (usually I use Java in its normal form, with curly braces and stuff), but generally a Java iterator works like the following:

while(iterator.hasNext()) {
  x = iterator.next(); // get element
  // do with x what you want
  if (/*you want to remove x from the underlying list*/)
      iterator.remove();
}

In practice, remove is only used very rarely, in cases you want to go through a collection and remove everything you do not need any longer in it. remove can fail if the collecion is readonly or if you are trying to iterate over it twice with two different iterators at the same time. Just stick with hasNext and next.

OTHER TIPS

Ben Nadel of Kinky Solutions fame wrote a component that might handle your situation. Give a look see and report back if his project helped you.

POI Utility ColdFusion Component

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top