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 */
020package org.apache.directory.shared.ldap.codec.controls.search.pagedSearch;
021
022
023import org.apache.directory.shared.asn1.ber.AbstractContainer;
024import org.apache.directory.shared.ldap.codec.api.LdapApiService;
025import org.apache.directory.shared.ldap.model.message.controls.PagedResults;
026
027
028/**
029 * A container for the Paged Search Control.
030 * 
031 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
032 */
033public class PagedResultsContainer extends AbstractContainer
034{
035    /** PagedSearchControl */
036    private PagedResultsDecorator control;
037    
038    private LdapApiService codec;
039
040
041    /**
042     * Creates a new PagedSearchControl container object. We will store one grammar,
043     * it's enough ...
044     * @param codec The encoder decoder for this container
045     */
046    public PagedResultsContainer( LdapApiService codec )
047    {
048        super();
049        this.codec = codec;
050        stateStack = new int[1];
051        grammar = PagedResultsGrammar.getInstance();
052        setTransition( PagedResultsStates.START_STATE );
053    }
054
055
056    /**
057     * Creates a new PagedSearchControl container object to contain a PagedResults
058     * Control, which is optionally decorated if is not a decorator already. If it
059     * is a decorator then it is used as the decorator for this container.
060     *
061     * @param codec The encoder decoder for this container
062     * @param control A PagedResults Control to optionally be wrapped.
063     */
064    public PagedResultsContainer( LdapApiService codec, PagedResults control )
065    {
066        this( codec );
067        decorate( control );
068    }
069
070
071    /**
072     * @return Returns the paged search control.
073     */
074    public PagedResultsDecorator getDecorator()
075    {
076
077        return control;
078    }
079
080
081    public void decorate( PagedResults control )
082    {
083        if ( control instanceof PagedResultsDecorator )
084        {
085            this.control = ( PagedResultsDecorator ) control;
086        }
087        else
088        {
089            this.control = new PagedResultsDecorator( codec, control );
090        }
091    }
092
093
094    /**
095     * Set a PagedSearchControl Object into the container. It will be completed by
096     * the ldapDecoder.
097     * 
098     * @param control the PagedSearchControl to set.
099     */
100    public void setPagedSearchControl( PagedResultsDecorator control )
101    {
102        this.control = control;
103    }
104
105    /**
106     * Clean the container
107     */
108    public void clean()
109    {
110        super.clean();
111        control = null;
112    }
113}