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.shared.dsmlv2.reponse;
021    
022    
023    import java.util.ArrayList;
024    import java.util.List;
025    
026    import org.apache.directory.shared.dsmlv2.DsmlDecorator;
027    import org.apache.directory.shared.ldap.codec.LdapMessageCodec;
028    import org.dom4j.Element;
029    
030    
031    /**
032     * This class represents the Search Response Dsml Container. 
033     * It is used to store Search Responses (Search Result Entry, 
034     * Search Result Reference and SearchResultDone).
035     *
036     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037     * @version $Rev$, $Date$
038     */
039    public class SearchResponseDsml extends LdapResponseDecorator implements DsmlDecorator
040    {
041        /** The responses */
042        private List<DsmlDecorator> responses = new ArrayList<DsmlDecorator>();
043    
044    
045        /**
046         * Creates a new instance of SearchResponseDsml.
047         */
048        public SearchResponseDsml( LdapMessageCodec response )
049        {
050            super( response );
051        }
052    
053    
054        /**
055         * Adds a response.
056         *
057         * @param response
058         *      the response to add
059         * @return
060         *      true (as per the general contract of the Collection.add method).
061         */
062        public boolean addResponse( DsmlDecorator response )
063        {
064            return responses.add( response );
065        }
066    
067    
068        /**
069         * Removes a response.
070         *
071         * @param response
072         *      the response to remove
073         * @return
074         *      true if this list contained the specified element.
075         */
076        public boolean removeResponse( DsmlDecorator response )
077        {
078            return responses.remove( response );
079        }
080    
081    
082        /* (non-Javadoc)
083         * @see org.apache.directory.shared.dsmlv2.DsmlDecorator#toDsml(org.dom4j.Element)
084         */
085        public Element toDsml( Element root )
086        {
087            Element element = root.addElement( "searchResponse" );
088    
089            // RequestID
090            int requestID = instance.getMessageId();
091            if ( requestID != 0 )
092            {
093                element.addAttribute( "requestID", "" + requestID );
094            }
095    
096            for ( DsmlDecorator response : responses )
097            {
098                response.toDsml( element );
099            }
100    
101            return element;
102        }
103    }