001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.server.xdbm.tools;
021    
022    
023    import org.apache.directory.server.xdbm.Index;
024    import org.apache.directory.server.xdbm.IndexCursor;
025    import org.apache.directory.shared.ldap.entry.ServerEntry;
026    
027    import java.io.OutputStream;
028    import java.io.PrintStream;
029    
030    
031    /**
032     * Utility methods for Index objects.
033     *
034     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035     * @version $$Rev$$
036     */
037    public class IndexUtils
038    {
039        public static void printContents( Index<?, ServerEntry, Long> idx ) throws Exception
040        {
041            printContents( idx, System.out );
042        }
043    
044    
045        public static void printContents( Index<?, ServerEntry, Long> idx, OutputStream outputStream ) throws Exception
046        {
047            PrintStream out;
048    
049            if ( outputStream == null )
050            {
051                out = System.out;
052            }
053            else if ( outputStream instanceof PrintStream )
054            {
055                out = ( PrintStream ) outputStream;
056            }
057            else
058            {
059                out = new PrintStream( outputStream );
060            }
061    
062            IndexCursor<?, ServerEntry, Long> cursor = idx.forwardCursor();
063            cursor.first();
064            for ( Object entry : cursor )
065            {
066                out.println( entry );
067            }
068        }
069    }