001/*
002 * The MIT License
003 * Copyright (c) 2012 Microsoft Corporation
004 *
005 * Permission is hereby granted, free of charge, to any person obtaining a copy
006 * of this software and associated documentation files (the "Software"), to deal
007 * in the Software without restriction, including without limitation the rights
008 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
009 * copies of the Software, and to permit persons to whom the Software is
010 * furnished to do so, subject to the following conditions:
011 *
012 * The above copyright notice and this permission notice shall be included in
013 * all copies or substantial portions of the Software.
014 *
015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
016 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
017 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
019 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
020 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
021 * THE SOFTWARE.
022 */
023
024package microsoft.exchange.webservices.data.autodiscover;
025
026import microsoft.exchange.webservices.data.autodiscover.response.AutodiscoverResponse;
027import microsoft.exchange.webservices.data.core.EwsXmlReader;
028import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
029import microsoft.exchange.webservices.data.security.XmlNodeType;
030
031import java.util.ArrayList;
032import java.util.Iterator;
033import java.util.List;
034
035/**
036 * Represents a collection of response to a call to the Autodiscover service.
037 *
038 * @param <TResponse> The type of the response in the collection.
039 */
040public abstract class AutodiscoverResponseCollection
041    <TResponse extends AutodiscoverResponse>
042    extends AutodiscoverResponse implements Iterable<TResponse> {
043
044  /**
045   * The response.
046   */
047  private List<TResponse> responses;
048
049  /**
050   * Initializes a new instance of the AutodiscoverResponseCollection class.
051   */
052  public AutodiscoverResponseCollection() {
053    super();
054    this.responses = new ArrayList<TResponse>();
055  }
056
057  /**
058   * Gets the number of response in the collection.
059   *
060   * @return the count
061   */
062  public int getCount() {
063    return this.responses.size();
064  }
065
066  /**
067   * Gets the response at the specified index.
068   *
069   * @param index the index
070   * @return the t response at index
071   */
072  public TResponse getTResponseAtIndex(int index) {
073    return this.responses.get(index);
074  }
075
076  /**
077   * Gets the response.
078   *
079   * @return the response
080   */
081  public List<TResponse> getResponses() {
082    return responses;
083  }
084
085  /**
086   * Loads response from XML.
087   *
088   * @param reader         the reader
089   * @param endElementName End element name.
090   * @throws Exception the exception
091   */
092  public void loadFromXml(EwsXmlReader reader, String endElementName)
093      throws Exception {
094    do {
095      reader.read();
096
097      if (reader.getNodeType().getNodeType() == XmlNodeType.START_ELEMENT) {
098        if (reader.getLocalName().equals(
099            this.getResponseCollectionXmlElementName())) {
100          this.loadResponseCollectionFromXml(reader);
101        } else {
102          super.loadFromXml(reader, endElementName);
103        }
104      }
105    } while (!reader
106        .isEndElement(XmlNamespace.Autodiscover, endElementName));
107  }
108
109  /**
110   * Loads response from XML.
111   *
112   * @param reader the reader
113   * @throws Exception the exception
114   */
115  private void loadResponseCollectionFromXml(EwsXmlReader reader)
116      throws Exception {
117    if (!reader.isEmptyElement()) {
118      do {
119        reader.read();
120        if ((reader.getNodeType().getNodeType() == XmlNodeType.START_ELEMENT) &&
121            (reader.getLocalName().equals(this
122                .getResponseInstanceXmlElementName()))) {
123          TResponse response = this.createResponseInstance();
124          response.loadFromXml(reader, this
125              .getResponseInstanceXmlElementName());
126          this.responses.add(response);
127        }
128      } while (!reader.isEndElement(XmlNamespace.Autodiscover, this
129          .getResponseCollectionXmlElementName()));
130    } else {
131      reader.read();
132    }
133  }
134
135  /**
136   * Gets the name of the response collection XML element.
137   *
138   * @return Response collection XMl element name.
139   */
140  protected abstract String getResponseCollectionXmlElementName();
141
142  /**
143   * Gets the name of the response instance XML element.
144   *
145   * @return Response collection XMl element name.
146   */
147  protected abstract String getResponseInstanceXmlElementName();
148
149  /**
150   * Create a response instance.
151   *
152   * @return TResponse.
153   */
154  protected abstract TResponse createResponseInstance();
155
156  /**
157   * Gets an Iterator that iterates through the elements of the collection.
158   *
159   * @return An Iterator for the collection.
160   */
161  public Iterator<TResponse> iterator() {
162    return this.responses.iterator();
163  }
164}