Return-Path: X-Original-To: apmail-poi-commits-archive@minotaur.apache.org Delivered-To: apmail-poi-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 92FFF7AAA for ; Wed, 20 Jul 2011 15:04:16 +0000 (UTC) Received: (qmail 41208 invoked by uid 500); 20 Jul 2011 15:04:16 -0000 Delivered-To: apmail-poi-commits-archive@poi.apache.org Received: (qmail 41168 invoked by uid 500); 20 Jul 2011 15:04:15 -0000 Mailing-List: contact commits-help@poi.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@poi.apache.org Delivered-To: mailing list commits@poi.apache.org Received: (qmail 41161 invoked by uid 99); 20 Jul 2011 15:04:15 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 20 Jul 2011 15:04:15 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 20 Jul 2011 15:04:10 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 7B079238885D for ; Wed, 20 Jul 2011 15:03:48 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1148810 - in /poi/trunk/src: documentation/content/xdocs/ scratchpad/src/org/apache/poi/hwpf/ scratchpad/src/org/apache/poi/hwpf/model/ scratchpad/src/org/apache/poi/hwpf/usermodel/ scratchpad/testcases/org/apache/poi/hwpf/model/ Date: Wed, 20 Jul 2011 15:03:47 -0000 To: commits@poi.apache.org From: sergey@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110720150348.7B079238885D@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: sergey Date: Wed Jul 20 15:03:43 2011 New Revision: 1148810 URL: http://svn.apache.org/viewvc?rev=1148810&view=rev Log: split BookmarksTables to internal and user-friendly API Added: poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Bookmarks.java poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/usermodel/BookmarksImpl.java Modified: poi/trunk/src/documentation/content/xdocs/status.xml poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/BookmarksTables.java poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/PropertyNode.java poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestBookmarksTables.java Modified: poi/trunk/src/documentation/content/xdocs/status.xml URL: http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/status.xml?rev=1148810&r1=1148809&r2=1148810&view=diff ============================================================================== --- poi/trunk/src/documentation/content/xdocs/status.xml (original) +++ poi/trunk/src/documentation/content/xdocs/status.xml Wed Jul 20 15:03:43 2011 @@ -38,7 +38,7 @@ 51481 - Fixed autofilters in HSSF to avoid warnings in Excel 2007 51533 - Avoid exception when changing name of a sheet containing shared formulas Support for appending images to existing drawings in HSSF - Added initial support for bookmarks in HWFP + Added initial support for bookmarks in HWPF 46250 - Fixed cloning worksheets with images 51524 - PapBinTable constructor is slow (regression) 51514 - allow HSSFObjectData to work with both POIFS and NPOIFS Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java?rev=1148810&r1=1148809&r2=1148810&view=diff ============================================================================== --- poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java (original) +++ poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java Wed Jul 20 15:03:43 2011 @@ -48,6 +48,8 @@ import org.apache.poi.hwpf.model.TextPie import org.apache.poi.hwpf.model.TextPieceTable; import org.apache.poi.hwpf.model.io.HWPFFileSystem; import org.apache.poi.hwpf.model.io.HWPFOutputStream; +import org.apache.poi.hwpf.usermodel.Bookmarks; +import org.apache.poi.hwpf.usermodel.BookmarksImpl; import org.apache.poi.hwpf.usermodel.HWPFList; import org.apache.poi.hwpf.usermodel.Range; import org.apache.poi.poifs.common.POIFSConstants; @@ -102,9 +104,12 @@ public final class HWPFDocument extends /** Holds Office Art objects */ protected ShapesTable _officeArts; - /** Holds the bookmarks */ + /** Holds the bookmarks tables */ protected BookmarksTables _bookmarksTables; - + + /** Holds the bookmarks */ + protected Bookmarks _bookmarks; + /** Holds the fields PLCFs */ protected FieldsTables _fieldsTables; @@ -267,6 +272,7 @@ public final class HWPFDocument extends } _bookmarksTables = new BookmarksTables( _tableStream, _fib ); + _bookmarks = new BookmarksImpl( _bookmarksTables ); _fieldsTables = new FieldsTables(_tableStream, _fib); } @@ -444,12 +450,11 @@ public final class HWPFDocument extends } /** - * @return BookmarksTables object, that is able to extract bookmarks - * descriptors from this document + * @return user-friendly interface to access document bookmarks */ - public BookmarksTables getBookmarksTables() + public Bookmarks getBookmarks() { - return _bookmarksTables; + return _bookmarks; } /** Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/BookmarksTables.java URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/BookmarksTables.java?rev=1148810&r1=1148809&r2=1148810&view=diff ============================================================================== --- poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/BookmarksTables.java (original) +++ poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/BookmarksTables.java Wed Jul 20 15:03:43 2011 @@ -1,10 +1,25 @@ +/* ==================================================================== + 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.io.IOException; import java.util.Arrays; import org.apache.poi.hwpf.model.io.HWPFOutputStream; -import org.apache.poi.hwpf.usermodel.Bookmark; public class BookmarksTables { @@ -14,67 +29,55 @@ public class BookmarksTables private String[] names = new String[0]; - public BookmarksTables() + public BookmarksTables( byte[] tableStream, FileInformationBlock fib ) { + read( tableStream, fib ); } - public BookmarksTables( byte[] tableStream, FileInformationBlock fib ) + public int getBookmarksCount() { - read( tableStream, fib ); + return descriptorsFirst.length(); } - public Bookmark getBookmark( int index ) + public GenericPropertyNode getDescriptorFirst( int index ) + throws IndexOutOfBoundsException { - final GenericPropertyNode first = descriptorsFirst.getProperty( index ); - return new Bookmark() - { - public int getEnd() - { - int currentIndex = Arrays.asList( - descriptorsFirst.toPropertiesArray() ).indexOf( first ); - if ( currentIndex >= descriptorsLim.length() ) - return first.getEnd(); - - GenericPropertyNode lim = descriptorsLim - .getProperty( currentIndex ); - return lim.getStart(); - } - - public String getName() - { - int currentIndex = Arrays.asList( - descriptorsFirst.toPropertiesArray() ).indexOf( first ); - if ( currentIndex >= names.length ) - return ""; - - return names[currentIndex]; - } - - public int getStart() - { - return first.getStart(); - } - - public void setName( String name ) - { - int currentIndex = Arrays.asList( - descriptorsFirst.toPropertiesArray() ).indexOf( first ); - if ( currentIndex < names.length ) - { - String[] newNames = new String[currentIndex + 1]; - System.arraycopy( names, 0, newNames, 0, names.length ); - names = newNames; - } - names[currentIndex] = name; - } - }; + return descriptorsFirst.getProperty( index ); } - public int getBookmarksCount() + public int getDescriptorFirstIndex( GenericPropertyNode descriptorFirst ) + { + // TODO: very non-optimal + return Arrays.asList( descriptorsFirst.toPropertiesArray() ).indexOf( + descriptorFirst ); + } + + public GenericPropertyNode getDescriptorLim( int index ) + throws IndexOutOfBoundsException + { + return descriptorsLim.getProperty( index ); + } + + public int getDescriptorsFirstCount() { return descriptorsFirst.length(); } + public int getDescriptorsLimCount() + { + return descriptorsLim.length(); + } + + public String getName( int index ) throws ArrayIndexOutOfBoundsException + { + return names[index]; + } + + public int getNamesCount() + { + return names.length; + } + private void read( byte[] tableStream, FileInformationBlock fib ) { int namesStart = fib.getFcSttbfbkmk(); @@ -97,6 +100,17 @@ public class BookmarksTables limDescriptorsLength, 0 ); } + public void setName( int index, String name ) + { + if ( index < names.length ) + { + String[] newNames = new String[index + 1]; + System.arraycopy( names, 0, newNames, 0, names.length ); + names = newNames; + } + names[index] = name; + } + public void writePlcfBkmkf( FileInformationBlock fib, HWPFOutputStream tableStream ) throws IOException { Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/PropertyNode.java URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/PropertyNode.java?rev=1148810&r1=1148809&r2=1148810&view=diff ============================================================================== --- poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/PropertyNode.java (original) +++ poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/PropertyNode.java Wed Jul 20 15:03:43 2011 @@ -35,9 +35,10 @@ import org.apache.poi.util.POILogger; public abstract class PropertyNode> implements Comparable, Cloneable { - static final class EndComparator implements Comparator> + public static final class EndComparator implements + Comparator> { - static EndComparator instance = new EndComparator(); + public static EndComparator instance = new EndComparator(); public int compare( PropertyNode o1, PropertyNode o2 ) { @@ -48,9 +49,10 @@ public abstract class PropertyNode> + public static final class StartComparator implements + Comparator> { - static StartComparator instance = new StartComparator(); + public static StartComparator instance = new StartComparator(); public int compare( PropertyNode o1, PropertyNode o2 ) { Added: poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Bookmarks.java URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Bookmarks.java?rev=1148810&view=auto ============================================================================== --- poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Bookmarks.java (added) +++ poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Bookmarks.java Wed Jul 20 15:03:43 2011 @@ -0,0 +1,50 @@ +/* ==================================================================== + 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.usermodel; + +import java.util.List; +import java.util.Map; + +/** + * User-friendly interface to access document bookmarks + * + * @author Sergey Vladimirov (vlsergey {at} gmail {dot} com) + */ +public interface Bookmarks +{ + /** + * @param index + * bookmark document index + * @return {@link Bookmark} with specified index + * @throws IndexOutOfBoundsException + * if bookmark with specified index not present in document + */ + Bookmark getBookmark( int index ) throws IndexOutOfBoundsException; + + /** + * @return count of {@link Bookmark}s in document + */ + int getBookmarksCount(); + + /** + * @return {@link Map} of bookmarks started in specified range, where key is + * start position and value is sorted {@link List} of + * {@link Bookmark} + */ + Map> getBookmarksStartedBetween( + int startInclusive, int endExclusive ); +} Added: poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/usermodel/BookmarksImpl.java URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/usermodel/BookmarksImpl.java?rev=1148810&view=auto ============================================================================== --- poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/usermodel/BookmarksImpl.java (added) +++ poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/usermodel/BookmarksImpl.java Wed Jul 20 15:03:43 2011 @@ -0,0 +1,189 @@ +/* ==================================================================== + 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.usermodel; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import org.apache.poi.hwpf.model.BookmarksTables; +import org.apache.poi.hwpf.model.GenericPropertyNode; +import org.apache.poi.hwpf.model.PropertyNode; + +/** + * Implementation of user-friendly interface for document bookmarks + * + * @author Sergey Vladimirov (vlsergey {at} gmail {doc} com) + */ +public class BookmarksImpl implements Bookmarks +{ + + private final BookmarksTables bookmarksTables; + + private Map> sortedDescriptors = null; + + private int[] sortedStartPositions = null; + + public BookmarksImpl( BookmarksTables bookmarksTables ) + { + this.bookmarksTables = bookmarksTables; + } + + private Bookmark getBookmark( final GenericPropertyNode first ) + { + return new Bookmark() + { + public int getEnd() + { + int currentIndex = bookmarksTables + .getDescriptorFirstIndex( first ); + try + { + GenericPropertyNode descriptorLim = bookmarksTables + .getDescriptorLim( currentIndex ); + return descriptorLim.getStart(); + } + catch ( IndexOutOfBoundsException exc ) + { + return first.getEnd(); + } + } + + public String getName() + { + int currentIndex = bookmarksTables + .getDescriptorFirstIndex( first ); + try + { + return bookmarksTables.getName( currentIndex ); + } + catch ( ArrayIndexOutOfBoundsException exc ) + { + return ""; + } + } + + public int getStart() + { + return first.getStart(); + } + + public void setName( String name ) + { + int currentIndex = bookmarksTables + .getDescriptorFirstIndex( first ); + bookmarksTables.setName( currentIndex, name ); + } + }; + } + + public Bookmark getBookmark( int index ) + { + final GenericPropertyNode first = bookmarksTables + .getDescriptorFirst( index ); + return getBookmark( first ); + } + + public List getBookmarksAt( int startCp ) + { + updateSortedDescriptors(); + + List nodes = sortedDescriptors.get( Integer + .valueOf( startCp ) ); + if ( nodes == null || nodes.isEmpty() ) + return Collections.emptyList(); + + List result = new ArrayList( nodes.size() ); + for ( GenericPropertyNode node : nodes ) + { + result.add( getBookmark( node ) ); + } + return Collections.unmodifiableList( result ); + } + + public int getBookmarksCount() + { + return bookmarksTables.getDescriptorsFirstCount(); + } + + public Map> getBookmarksStartedBetween( + int startInclusive, int endExclusive ) + { + updateSortedDescriptors(); + + int startLookupIndex = Arrays.binarySearch( this.sortedStartPositions, + startInclusive ); + if ( startLookupIndex < 0 ) + startLookupIndex = -( startLookupIndex + 1 ); + int endLookupIndex = Arrays.binarySearch( this.sortedStartPositions, + endExclusive ); + if ( endLookupIndex < 0 ) + endLookupIndex = -( endLookupIndex + 1 ); + + Map> result = new LinkedHashMap>(); + for ( int lookupIndex = startLookupIndex; lookupIndex < endLookupIndex; lookupIndex++ ) + { + int s = sortedStartPositions[lookupIndex]; + List startedAt = getBookmarksAt( s ); + if ( startedAt != null ) + result.put( Integer.valueOf( s ), startedAt ); + } + + return Collections.unmodifiableMap( result ); + } + + private void updateSortedDescriptors() + { + if ( sortedDescriptors != null ) + return; + + Map> result = new HashMap>(); + for ( int b = 0; b < bookmarksTables.getDescriptorsFirstCount(); b++ ) + { + GenericPropertyNode property = bookmarksTables + .getDescriptorFirst( b ); + Integer positionKey = Integer.valueOf( property.getStart() ); + List atPositionList = result.get( positionKey ); + if ( atPositionList == null ) + { + atPositionList = new LinkedList(); + result.put( positionKey, atPositionList ); + } + atPositionList.add( property ); + } + + int counter = 0; + int[] indices = new int[result.size()]; + for ( Map.Entry> entry : result + .entrySet() ) + { + indices[counter++] = entry.getKey().intValue(); + List updated = new ArrayList( + entry.getValue() ); + Collections.sort( updated, PropertyNode.EndComparator.instance ); + entry.setValue( updated ); + } + + this.sortedDescriptors = result; + this.sortedStartPositions = indices; + } +} Modified: poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestBookmarksTables.java URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestBookmarksTables.java?rev=1148810&r1=1148809&r2=1148810&view=diff ============================================================================== --- poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestBookmarksTables.java (original) +++ poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestBookmarksTables.java Wed Jul 20 15:03:43 2011 @@ -1,3 +1,19 @@ +/* ==================================================================== + 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 junit.framework.TestCase; @@ -5,17 +21,24 @@ import junit.framework.TestCase; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.HWPFTestDataSamples; import org.apache.poi.hwpf.usermodel.Bookmark; +import org.apache.poi.hwpf.usermodel.Bookmarks; +/** + * Test cases for {@link BookmarksTables} and default implementation of + * {@link Bookmarks} + * + * @author Sergey Vladimirov (vlsergey {at} gmail {dot} com) + */ public class TestBookmarksTables extends TestCase { public void test() { HWPFDocument doc = HWPFTestDataSamples.openSampleFile( "pageref.doc" ); - BookmarksTables bookmarksTables = doc.getBookmarksTables(); + Bookmarks bookmarks = doc.getBookmarks(); - assertEquals( 1, bookmarksTables.getBookmarksCount() ); + assertEquals( 1, bookmarks.getBookmarksCount() ); - Bookmark bookmark = bookmarksTables.getBookmark( 0 ); + Bookmark bookmark = bookmarks.getBookmark( 0 ); assertEquals( "userref", bookmark.getName() ); assertEquals( 27, bookmark.getStart() ); assertEquals( 38, bookmark.getEnd() ); --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org For additional commands, e-mail: commits-help@poi.apache.org