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
024 package microsoft.exchange.webservices.data.property.definition;
025
026 import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
027 import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
028 import microsoft.exchange.webservices.data.core.XmlAttributeNames;
029 import microsoft.exchange.webservices.data.core.XmlElementNames;
030 import microsoft.exchange.webservices.data.core.service.schema.ServiceObjectSchema;
031 import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
032 import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
033 import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
034 import microsoft.exchange.webservices.data.misc.OutParam;
035
036 import javax.xml.stream.XMLStreamException;
037
038 /**
039 * Represents the base class for all property definitions.
040 */
041 public abstract class PropertyDefinitionBase {
042
043 /**
044 * Initializes a new instance.
045 */
046 protected PropertyDefinitionBase() {
047 super();
048 }
049
050 /**
051 * Tries to load from XML.
052 *
053 * @param reader The reader.
054 * @param propertyDefinition The property definition.
055 * @return True if property was loaded.
056 * @throws Exception the exception
057 */
058 public static boolean tryLoadFromXml(EwsServiceXmlReader reader,
059 OutParam<PropertyDefinitionBase> propertyDefinition)
060 throws Exception {
061 String strLocalName = reader.getLocalName();
062 if (strLocalName.equals(XmlElementNames.FieldURI)) {
063 PropertyDefinitionBase p = ServiceObjectSchema
064 .findPropertyDefinition(reader.readAttributeValue(XmlAttributeNames.FieldURI));
065 propertyDefinition.setParam(p);
066 return true;
067 } else if (strLocalName.equals(XmlElementNames.IndexedFieldURI)) {
068 reader.skipCurrentElement();
069 return true;
070 } else if (strLocalName.equals(XmlElementNames.ExtendedFieldURI)) {
071 ExtendedPropertyDefinition p = new ExtendedPropertyDefinition();
072 p.loadFromXml(reader);
073 propertyDefinition.setParam(p);
074 return true;
075 } else {
076 return false;
077 }
078
079 }
080
081 /**
082 * Gets the name of the XML element.
083 *
084 * @return XML element name.
085 */
086 protected abstract String getXmlElementName();
087
088 /**
089 * Writes the attribute to XML.
090 *
091 * @param writer The writer.
092 * @throws ServiceXmlSerializationException the service xml serialization exception
093 */
094 protected abstract void writeAttributesToXml(EwsServiceXmlWriter writer)
095 throws ServiceXmlSerializationException;
096
097 /**
098 * Gets the minimum Exchange version that supports this property.
099 *
100 * @return The version.
101 */
102 public abstract ExchangeVersion getVersion();
103
104 /**
105 * Gets the property definition's printable name.
106 *
107 * @return The property definition's printable name.
108 */
109 public abstract String getPrintableName();
110
111 /**
112 * Gets the type of the property.
113 */
114 public abstract Class<?> getType();
115
116 /**
117 * Writes to XML.
118 *
119 * @param writer The writer.
120 * @throws XMLStreamException the XML stream exception
121 * @throws ServiceXmlSerializationException the service xml serialization exception
122 */
123 public void writeToXml(EwsServiceXmlWriter writer)
124 throws XMLStreamException, ServiceXmlSerializationException {
125 writer.writeStartElement(XmlNamespace.Types, this.getXmlElementName());
126 this.writeAttributesToXml(writer);
127 writer.writeEndElement();
128 }
129
130 /*
131 * (non-Javadoc)
132 *
133 * @see java.lang.Object#toString()
134 */
135 @Override
136 /**
137 * Returns a string that represents the current object.
138 * @return A string that represents the current object.
139 */
140 public String toString() {
141 return this.getPrintableName();
142 }
143 }