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.misc.id;
025
026import microsoft.exchange.webservices.data.ISelfValidate;
027import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
028import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
029import microsoft.exchange.webservices.data.core.XmlAttributeNames;
030import microsoft.exchange.webservices.data.core.enumeration.misc.IdFormat;
031import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
032import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
033
034import javax.xml.stream.XMLStreamException;
035
036/**
037 * Represents the base class for Id expressed in a specific format.
038 */
039public abstract class AlternateIdBase implements ISelfValidate {
040
041  /**
042   * Id format.
043   */
044  private IdFormat format;
045
046  /**
047   * Initializes a new instance of the class.
048   */
049  protected AlternateIdBase() {
050  }
051
052  /**
053   * Initializes a new instance of the class.
054   *
055   * @param format the format
056   */
057  protected AlternateIdBase(IdFormat format) {
058    super();
059    this.format = format;
060  }
061
062  /**
063   * Gets the format in which the Id in expressed.
064   *
065   * @return the format
066   */
067  public IdFormat getFormat() {
068    return this.format;
069  }
070
071  /**
072   * Sets the format.
073   *
074   * @param format the new format
075   */
076  public void setFormat(IdFormat format) {
077    this.format = format;
078  }
079
080  /**
081   * Gets the name of the XML element.
082   *
083   * @return XML element name.
084   */
085  protected abstract String getXmlElementName();
086
087  /**
088   * Writes the attribute to XML.
089   *
090   * @param writer the writer
091   * @throws ServiceXmlSerializationException the service xml serialization exception
092   */
093  protected void writeAttributesToXml(EwsServiceXmlWriter writer)
094      throws ServiceXmlSerializationException {
095    writer.writeAttributeValue(XmlAttributeNames.Format, this.getFormat());
096  }
097
098  /**
099   * Loads the attribute from XML.
100   *
101   * @param reader the reader
102   * @throws Exception the exception
103   */
104  public void loadAttributesFromXml(EwsServiceXmlReader reader)
105      throws Exception {
106    this.setFormat(reader.readAttributeValue(IdFormat.class,
107        XmlAttributeNames.Format));
108  }
109
110  /**
111   * Writes to XML.
112   *
113   * @param writer the writer
114   * @throws ServiceXmlSerializationException the service xml serialization exception
115   * @throws XMLStreamException the XML stream exception
116   */
117  public void writeToXml(EwsServiceXmlWriter writer)
118      throws ServiceXmlSerializationException, XMLStreamException {
119    writer.writeStartElement(XmlNamespace.Types, this.getXmlElementName());
120    this.writeAttributesToXml(writer);
121    writer.writeEndElement(); // this.GetXmlElementName()
122  }
123
124  /**
125   * Validate this instance.
126   *
127   * @throws Exception
128   */
129  protected void internalValidate() throws Exception {
130    // nothing to do.
131  }
132
133  /**
134   * Validates this instance.
135   *
136   * @throws Exception
137   */
138  public void validate() throws Exception {
139    this.internalValidate();
140  }
141
142}