You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by ch...@apache.org on 2015/06/15 10:48:53 UTC
[6/7] olingo-odata4 git commit: [OLINGO-699] Moved serializer code to
client module
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/20351253/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/AtomDeserializer.java
----------------------------------------------------------------------
diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/AtomDeserializer.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/AtomDeserializer.java
new file mode 100644
index 0000000..df88d27
--- /dev/null
+++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/AtomDeserializer.java
@@ -0,0 +1,873 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.olingo.client.core.serialization;
+
+import java.io.InputStream;
+import java.net.URI;
+import java.text.ParseException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLEventReader;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.events.Attribute;
+import javax.xml.stream.events.StartElement;
+import javax.xml.stream.events.XMLEvent;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.olingo.client.api.serialization.ODataDeserializer;
+import org.apache.olingo.client.api.serialization.ODataDeserializerException;
+import org.apache.olingo.commons.api.Constants;
+import org.apache.olingo.commons.api.ODataError;
+import org.apache.olingo.commons.api.ODataPropertyType;
+import org.apache.olingo.commons.api.data.AbstractODataObject;
+import org.apache.olingo.commons.api.data.Annotation;
+import org.apache.olingo.commons.api.data.ComplexValue;
+import org.apache.olingo.commons.api.data.DeletedEntity;
+import org.apache.olingo.commons.api.data.DeletedEntity.Reason;
+import org.apache.olingo.commons.api.data.Delta;
+import org.apache.olingo.commons.api.data.DeltaLink;
+import org.apache.olingo.commons.api.data.Entity;
+import org.apache.olingo.commons.api.data.EntityCollection;
+import org.apache.olingo.commons.api.data.Link;
+import org.apache.olingo.commons.api.data.Operation;
+import org.apache.olingo.commons.api.data.Property;
+import org.apache.olingo.commons.api.data.ResWrap;
+import org.apache.olingo.commons.api.data.Valuable;
+import org.apache.olingo.commons.api.data.ValueType;
+import org.apache.olingo.commons.api.edm.EdmPrimitiveType;
+import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException;
+import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
+import org.apache.olingo.commons.api.edm.geo.Geospatial;
+import org.apache.olingo.commons.api.format.ContentType;
+import org.apache.olingo.commons.core.edm.EdmTypeInfo;
+
+import com.fasterxml.aalto.stax.InputFactoryImpl;
+
+public class AtomDeserializer extends AbstractAtomDealer implements ODataDeserializer {
+
+ protected static final XMLInputFactory FACTORY = new InputFactoryImpl();
+
+ private final AtomGeoValueDeserializer geoDeserializer;
+
+ protected XMLEventReader getReader(final InputStream input) throws XMLStreamException {
+ return FACTORY.createXMLEventReader(input);
+ }
+
+ public AtomDeserializer() {
+ geoDeserializer = new AtomGeoValueDeserializer();
+ }
+
+ private Object fromPrimitive(final XMLEventReader reader, final StartElement start,
+ final EdmTypeInfo typeInfo) throws XMLStreamException, EdmPrimitiveTypeException {
+
+ Object value = null;
+
+ boolean foundEndProperty = false;
+ while (reader.hasNext() && !foundEndProperty) {
+ final XMLEvent event = reader.nextEvent();
+
+ if (event.isStartElement() && typeInfo != null && typeInfo.getPrimitiveTypeKind().isGeospatial()) {
+ final EdmPrimitiveTypeKind geoType =
+ EdmPrimitiveTypeKind.valueOfFQN(typeInfo.getFullQualifiedName().toString());
+ value = geoDeserializer.deserialize(reader, event.asStartElement(), geoType);
+ }
+
+ if (event.isCharacters() && !event.asCharacters().isWhiteSpace()
+ && (typeInfo == null || !typeInfo.getPrimitiveTypeKind().isGeospatial())) {
+
+ final String stringValue = event.asCharacters().getData();
+ value = typeInfo == null ? stringValue : // TODO: add facets
+ ((EdmPrimitiveType) typeInfo.getType()).valueOfString(stringValue, true, null,
+ Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, true,
+ ((EdmPrimitiveType) typeInfo.getType()).getDefaultType());
+ }
+
+ if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) {
+ foundEndProperty = true;
+ }
+ }
+
+ return value;
+ }
+
+ private Object fromComplexOrEnum(final XMLEventReader reader, final StartElement start)
+ throws XMLStreamException, EdmPrimitiveTypeException {
+
+ Object value = null;
+
+ boolean foundEndProperty = false;
+ while (reader.hasNext() && !foundEndProperty) {
+ final XMLEvent event = reader.nextEvent();
+
+ if (event.isStartElement()) {
+ if (value == null) {
+ value = new ComplexValue();
+ }
+
+ if (Constants.QNAME_ATOM_ELEM_LINK.equals(event.asStartElement().getName())) {
+ final Link link = new Link();
+ final Attribute rel = event.asStartElement().getAttributeByName(QName.valueOf(Constants.ATTR_REL));
+ if (rel != null) {
+ link.setRel(rel.getValue());
+ }
+ final Attribute title = event.asStartElement().getAttributeByName(QName.valueOf(Constants.ATTR_TITLE));
+ if (title != null) {
+ link.setTitle(title.getValue());
+ }
+ final Attribute href = event.asStartElement().getAttributeByName(QName.valueOf(Constants.ATTR_HREF));
+ if (href != null) {
+ link.setHref(href.getValue());
+ }
+ final Attribute type = event.asStartElement().getAttributeByName(QName.valueOf(Constants.ATTR_TYPE));
+ if (type != null) {
+ link.setType(type.getValue());
+ }
+
+ if (link.getRel().startsWith(Constants.NS_NAVIGATION_LINK_REL)) {
+
+ ((ComplexValue) value).getNavigationLinks().add(link);
+ inline(reader, event.asStartElement(), link);
+ } else if (link.getRel().startsWith(Constants.NS_ASSOCIATION_LINK_REL)) {
+
+ ((Valuable) value).asComplex().getAssociationLinks().add(link);
+ }
+ } else {
+ ((ComplexValue) value).getValue().add(property(reader, event.asStartElement()));
+ }
+ }
+
+ if (event.isCharacters() && !event.asCharacters().isWhiteSpace()) {
+ value = event.asCharacters().getData();
+ }
+
+ if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) {
+ foundEndProperty = true;
+ }
+ }
+
+ return value;
+ }
+
+ private void fromCollection(final Valuable valuable, final XMLEventReader reader, final StartElement start,
+ final EdmTypeInfo typeInfo) throws XMLStreamException, EdmPrimitiveTypeException {
+
+ List<Object> values = new ArrayList<Object>();
+ ValueType valueType = ValueType.COLLECTION_PRIMITIVE;
+
+ final EdmTypeInfo type = typeInfo == null ? null :
+ new EdmTypeInfo.Builder().setTypeExpression(typeInfo.getFullQualifiedName().toString()).build();
+
+ boolean foundEndProperty = false;
+ while (reader.hasNext() && !foundEndProperty) {
+ final XMLEvent event = reader.nextEvent();
+
+ if (event.isStartElement()) {
+ switch (guessPropertyType(reader, typeInfo)) {
+ case COMPLEX:
+ final Object complexValue = fromComplexOrEnum(reader, event.asStartElement());
+ valueType = ValueType.COLLECTION_COMPLEX;
+ values.add(complexValue);
+ break;
+
+ case ENUM:
+ valueType = ValueType.COLLECTION_ENUM;
+ values.add(fromComplexOrEnum(reader, event.asStartElement()));
+ break;
+
+ case PRIMITIVE:
+ final Object value = fromPrimitive(reader, event.asStartElement(), type);
+ valueType = value instanceof Geospatial ?
+ ValueType.COLLECTION_GEOSPATIAL : ValueType.COLLECTION_PRIMITIVE;
+ values.add(value);
+ break;
+
+ default:
+ // do not add null or empty values
+ }
+ }
+
+ if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) {
+ foundEndProperty = true;
+ }
+ }
+ valuable.setValue(valueType, values);
+ }
+
+ private ODataPropertyType guessPropertyType(final XMLEventReader reader, final EdmTypeInfo typeInfo)
+ throws XMLStreamException {
+
+ XMLEvent child = null;
+ while (reader.hasNext() && child == null) {
+ final XMLEvent event = reader.peek();
+ if (event.isCharacters() && event.asCharacters().isWhiteSpace()) {
+ reader.nextEvent();
+ } else {
+ child = event;
+ }
+ }
+
+ final ODataPropertyType type;
+ if (child == null) {
+ type = typeInfo == null || typeInfo.isPrimitiveType() ? ODataPropertyType.PRIMITIVE : ODataPropertyType.ENUM;
+ } else {
+ if (child.isStartElement()) {
+ if (Constants.NS_GML.equals(child.asStartElement().getName().getNamespaceURI())) {
+ type = ODataPropertyType.PRIMITIVE;
+ } else if (elementQName.equals(child.asStartElement().getName())) {
+ type = ODataPropertyType.COLLECTION;
+ } else {
+ type = ODataPropertyType.COMPLEX;
+ }
+ } else if (child.isCharacters()) {
+ type = typeInfo == null || typeInfo.isPrimitiveType()
+ ? ODataPropertyType.PRIMITIVE
+ : ODataPropertyType.ENUM;
+ } else {
+ type = ODataPropertyType.EMPTY;
+ }
+ }
+
+ return type;
+ }
+
+ private Property property(final XMLEventReader reader, final StartElement start)
+ throws XMLStreamException, EdmPrimitiveTypeException {
+
+ final Property property = new Property();
+
+ if (propertyValueQName.equals(start.getName())) {
+ // retrieve name from context
+ final Attribute context = start.getAttributeByName(contextQName);
+ if (context != null) {
+ property.setName(StringUtils.substringAfterLast(context.getValue(), "/"));
+ }
+ } else {
+ property.setName(start.getName().getLocalPart());
+ }
+
+ valuable(property, reader, start);
+
+ return property;
+ }
+
+ private void valuable(final Valuable valuable, final XMLEventReader reader, final StartElement start)
+ throws XMLStreamException, EdmPrimitiveTypeException {
+
+ final Attribute nullAttr = start.getAttributeByName(nullQName);
+
+ final Attribute typeAttr = start.getAttributeByName(typeQName);
+ final String typeAttrValue = typeAttr == null ? null : typeAttr.getValue();
+
+ final EdmTypeInfo typeInfo = StringUtils.isBlank(typeAttrValue) ? null :
+ new EdmTypeInfo.Builder().setTypeExpression(typeAttrValue).build();
+
+ if (typeInfo != null) {
+ valuable.setType(typeInfo.internal());
+ }
+
+ final ODataPropertyType propType = typeInfo == null ? guessPropertyType(reader, typeInfo) :
+ typeInfo.isCollection() ? ODataPropertyType.COLLECTION :
+ typeInfo.isPrimitiveType() ? ODataPropertyType.PRIMITIVE : ODataPropertyType.COMPLEX;
+
+ if (nullAttr == null) {
+ switch (propType) {
+ case COLLECTION:
+ fromCollection(valuable, reader, start, typeInfo);
+ break;
+
+ case COMPLEX:
+ final Object complexValue = fromComplexOrEnum(reader, start);
+ valuable.setValue(complexValue instanceof ComplexValue ? ValueType.COMPLEX : ValueType.ENUM,
+ complexValue);
+ break;
+
+ case PRIMITIVE:
+ // No type specified? Defaults to Edm.String
+ if (typeInfo == null) {
+ valuable.setType(EdmPrimitiveTypeKind.String.getFullQualifiedName().toString());
+ }
+ final Object value = fromPrimitive(reader, start, typeInfo);
+ valuable.setValue(value instanceof Geospatial ? ValueType.GEOSPATIAL : ValueType.PRIMITIVE, value);
+ break;
+
+ case EMPTY:
+ default:
+ valuable.setValue(ValueType.PRIMITIVE, StringUtils.EMPTY);
+ }
+ } else {
+ valuable.setValue(propType == ODataPropertyType.PRIMITIVE ? ValueType.PRIMITIVE :
+ propType == ODataPropertyType.ENUM ? ValueType.ENUM :
+ propType == ODataPropertyType.COMPLEX ? ValueType.COMPLEX :
+ propType == ODataPropertyType.COLLECTION ? ValueType.COLLECTION_PRIMITIVE : ValueType.PRIMITIVE,
+ null);
+ }
+ }
+
+ @Override
+ public ResWrap<Property> toProperty(final InputStream input) throws ODataDeserializerException {
+ try {
+ final XMLEventReader reader = getReader(input);
+ final StartElement start = skipBeforeFirstStartElement(reader);
+ return getContainer(start, property(reader, start));
+ } catch (XMLStreamException e) {
+ throw new ODataDeserializerException(e);
+ } catch (final EdmPrimitiveTypeException e) {
+ throw new ODataDeserializerException(e);
+ }
+ }
+
+ private StartElement skipBeforeFirstStartElement(final XMLEventReader reader) throws XMLStreamException {
+ StartElement startEvent = null;
+ while (reader.hasNext() && startEvent == null) {
+ final XMLEvent event = reader.nextEvent();
+ if (event.isStartElement()) {
+ startEvent = event.asStartElement();
+ }
+ }
+ if (startEvent == null) {
+ throw new IllegalArgumentException("Cannot find any XML start element");
+ }
+
+ return startEvent;
+ }
+
+ private void common(final XMLEventReader reader, final StartElement start,
+ final AbstractODataObject object, final String key) throws XMLStreamException {
+
+ boolean foundEndElement = false;
+ while (reader.hasNext() && !foundEndElement) {
+ final XMLEvent event = reader.nextEvent();
+
+ if (event.isCharacters() && !event.asCharacters().isWhiteSpace()) {
+ try {
+ object.setCommonProperty(key, event.asCharacters().getData());
+ } catch (ParseException e) {
+ throw new XMLStreamException("While parsing Atom entry or feed common elements", e);
+ }
+ }
+
+ if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) {
+ foundEndElement = true;
+ }
+ }
+ }
+
+ private void inline(final XMLEventReader reader, final StartElement start, final Link link)
+ throws XMLStreamException, EdmPrimitiveTypeException {
+
+ boolean foundEndElement = false;
+ while (reader.hasNext() && !foundEndElement) {
+ final XMLEvent event = reader.nextEvent();
+
+ if (event.isStartElement()) {
+ if (inlineQName.equals(event.asStartElement().getName())) {
+ StartElement inline = getStartElement(reader);
+ if (inline != null) {
+ if (Constants.QNAME_ATOM_ELEM_ENTRY.equals(inline.getName())) {
+ link.setInlineEntity(entity(reader, inline));
+ }
+ if (Constants.QNAME_ATOM_ELEM_FEED.equals(inline.getName())) {
+ link.setInlineEntitySet(entitySet(reader, inline));
+ }
+ }
+ } else if (annotationQName.equals(event.asStartElement().getName())) {
+ link.getAnnotations().add(annotation(reader, event.asStartElement()));
+ }
+ }
+
+ if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) {
+ foundEndElement = true;
+ }
+ }
+ }
+
+ private StartElement getStartElement(final XMLEventReader reader) throws XMLStreamException {
+ while (reader.hasNext()) {
+ final XMLEvent innerEvent = reader.peek();
+ if (innerEvent.isCharacters() && innerEvent.asCharacters().isWhiteSpace()) {
+ reader.nextEvent();
+ } else if (innerEvent.isStartElement()) {
+ return innerEvent.asStartElement();
+ } else if (innerEvent.isEndElement() && inlineQName.equals(innerEvent.asEndElement().getName())) {
+ return null;
+ }
+ }
+ return null;
+ }
+
+ public ResWrap<Delta> delta(final InputStream input)
+ throws XMLStreamException, EdmPrimitiveTypeException {
+ final XMLEventReader reader = getReader(input);
+ final StartElement start = skipBeforeFirstStartElement(reader);
+ return getContainer(start, delta(reader, start));
+ }
+
+ private Delta delta(final XMLEventReader reader, final StartElement start)
+ throws XMLStreamException, EdmPrimitiveTypeException {
+ if (!Constants.QNAME_ATOM_ELEM_FEED.equals(start.getName())) {
+ return null;
+ }
+ final Delta delta = new Delta();
+ final Attribute xmlBase = start.getAttributeByName(Constants.QNAME_ATTR_XML_BASE);
+ if (xmlBase != null) {
+ delta.setBaseURI(URI.create(xmlBase.getValue()));
+ }
+
+ boolean foundEndFeed = false;
+ while (reader.hasNext() && !foundEndFeed) {
+ final XMLEvent event = reader.nextEvent();
+ if (event.isStartElement()) {
+ if (countQName.equals(event.asStartElement().getName())) {
+ count(reader, event.asStartElement(), delta);
+ } else if (Constants.QNAME_ATOM_ELEM_ID.equals(event.asStartElement().getName())) {
+ common(reader, event.asStartElement(), delta, "id");
+ } else if (Constants.QNAME_ATOM_ELEM_TITLE.equals(event.asStartElement().getName())) {
+ common(reader, event.asStartElement(), delta, "title");
+ } else if (Constants.QNAME_ATOM_ELEM_SUMMARY.equals(event.asStartElement().getName())) {
+ common(reader, event.asStartElement(), delta, "summary");
+ } else if (Constants.QNAME_ATOM_ELEM_UPDATED.equals(event.asStartElement().getName())) {
+ common(reader, event.asStartElement(), delta, "updated");
+ } else if (Constants.QNAME_ATOM_ELEM_LINK.equals(event.asStartElement().getName())) {
+ final Attribute rel = event.asStartElement().getAttributeByName(QName.valueOf(Constants.ATTR_REL));
+ if (rel != null) {
+ if (Constants.NEXT_LINK_REL.equals(rel.getValue())) {
+ final Attribute href = event.asStartElement().getAttributeByName(QName.valueOf(Constants.ATTR_HREF));
+ if (href != null) {
+ delta.setNext(URI.create(href.getValue()));
+ }
+ }
+ if (Constants.NS_DELTA_LINK_REL.equals(rel.getValue())) {
+ final Attribute href = event.asStartElement().getAttributeByName(QName.valueOf(Constants.ATTR_HREF));
+ if (href != null) {
+ delta.setDeltaLink(URI.create(href.getValue()));
+ }
+ }
+ }
+ } else if (Constants.QNAME_ATOM_ELEM_ENTRY.equals(event.asStartElement().getName())) {
+ delta.getEntities().add(entity(reader, event.asStartElement()));
+ } else if (deletedEntryQName.equals(event.asStartElement().getName())) {
+ final DeletedEntity deletedEntity = new DeletedEntity();
+
+ final Attribute ref = event.asStartElement().getAttributeByName(QName.valueOf(Constants.ATTR_REF));
+ if (ref != null) {
+ deletedEntity.setId(URI.create(ref.getValue()));
+ }
+ final Attribute reason = event.asStartElement().getAttributeByName(reasonQName);
+ if (reason != null) {
+ deletedEntity.setReason(Reason.valueOf(reason.getValue()));
+ }
+
+ delta.getDeletedEntities().add(deletedEntity);
+ } else if (linkQName.equals(event.asStartElement().getName())
+ || deletedLinkQName.equals(event.asStartElement().getName())) {
+
+ final DeltaLink link = new DeltaLink();
+
+ final Attribute source = event.asStartElement().getAttributeByName(QName.valueOf(Constants.ATTR_SOURCE));
+ if (source != null) {
+ link.setSource(URI.create(source.getValue()));
+ }
+ final Attribute relationship =
+ event.asStartElement().getAttributeByName(QName.valueOf(Constants.ATTR_RELATIONSHIP));
+ if (relationship != null) {
+ link.setRelationship(relationship.getValue());
+ }
+ final Attribute target = event.asStartElement().getAttributeByName(QName.valueOf(Constants.ATTR_TARGET));
+ if (target != null) {
+ link.setTarget(URI.create(target.getValue()));
+ }
+
+ if (linkQName.equals(event.asStartElement().getName())) {
+ delta.getAddedLinks().add(link);
+ } else {
+ delta.getDeletedLinks().add(link);
+ }
+ }
+ }
+
+ if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) {
+ foundEndFeed = true;
+ }
+ }
+
+ return delta;
+ }
+
+ private void properties(final XMLEventReader reader, final StartElement start, final Entity entity)
+ throws XMLStreamException, EdmPrimitiveTypeException {
+
+ final Map<String, List<Annotation>> annotations = new HashMap<String, List<Annotation>>();
+
+ boolean foundEndProperties = false;
+ while (reader.hasNext() && !foundEndProperties) {
+ final XMLEvent event = reader.nextEvent();
+
+ if (event.isStartElement()) {
+ if (annotationQName.equals(event.asStartElement().getName())) {
+ final String target = event.asStartElement().
+ getAttributeByName(QName.valueOf(Constants.ATTR_TARGET)).getValue();
+ if (!annotations.containsKey(target)) {
+ annotations.put(target, new ArrayList<Annotation>());
+ }
+ annotations.get(target).add(annotation(reader, event.asStartElement()));
+ } else {
+ entity.getProperties().add(property(reader, event.asStartElement()));
+ }
+ }
+
+ if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) {
+ foundEndProperties = true;
+ }
+ }
+
+ for (Property property : entity.getProperties()) {
+ if (annotations.containsKey(property.getName())) {
+ property.getAnnotations().addAll(annotations.get(property.getName()));
+ }
+ }
+ }
+
+ private Annotation annotation(final XMLEventReader reader, final StartElement start)
+ throws XMLStreamException, EdmPrimitiveTypeException {
+
+ final Annotation annotation = new Annotation();
+
+ annotation.setTerm(start.getAttributeByName(QName.valueOf(Constants.ATOM_ATTR_TERM)).getValue());
+ valuable(annotation, reader, start);
+
+ return annotation;
+ }
+
+ private Entity entityRef(final StartElement start) throws XMLStreamException {
+ final Entity entity = new Entity();
+
+ final Attribute entityRefId = start.getAttributeByName(Constants.QNAME_ATOM_ATTR_ID);
+ if (entityRefId != null) {
+ entity.setId(URI.create(entityRefId.getValue()));
+ }
+
+ return entity;
+ }
+
+ private Entity entity(final XMLEventReader reader, final StartElement start)
+ throws XMLStreamException, EdmPrimitiveTypeException {
+ final Entity entity;
+ if (entryRefQName.equals(start.getName())) {
+ entity = entityRef(start);
+ } else if (Constants.QNAME_ATOM_ELEM_ENTRY.equals(start.getName())) {
+ entity = new Entity();
+ final Attribute xmlBase = start.getAttributeByName(Constants.QNAME_ATTR_XML_BASE);
+ if (xmlBase != null) {
+ entity.setBaseURI(URI.create(xmlBase.getValue()));
+ }
+
+ final Attribute etag = start.getAttributeByName(etagQName);
+ if (etag != null) {
+ entity.setETag(etag.getValue());
+ }
+
+ boolean foundEndEntry = false;
+ while (reader.hasNext() && !foundEndEntry) {
+ final XMLEvent event = reader.nextEvent();
+
+ if (event.isStartElement()) {
+ if (Constants.QNAME_ATOM_ELEM_ID.equals(event.asStartElement().getName())) {
+ common(reader, event.asStartElement(), entity, "id");
+ } else if (Constants.QNAME_ATOM_ELEM_TITLE.equals(event.asStartElement().getName())) {
+ common(reader, event.asStartElement(), entity, "title");
+ } else if (Constants.QNAME_ATOM_ELEM_SUMMARY.equals(event.asStartElement().getName())) {
+ common(reader, event.asStartElement(), entity, "summary");
+ } else if (Constants.QNAME_ATOM_ELEM_UPDATED.equals(event.asStartElement().getName())) {
+ common(reader, event.asStartElement(), entity, "updated");
+ } else if (Constants.QNAME_ATOM_ELEM_CATEGORY.equals(event.asStartElement().getName())) {
+ final Attribute term = event.asStartElement().getAttributeByName(QName.valueOf(Constants.ATOM_ATTR_TERM));
+ if (term != null) {
+ entity.setType(new EdmTypeInfo.Builder().setTypeExpression(term.getValue()).build().internal());
+ }
+ } else if (Constants.QNAME_ATOM_ELEM_LINK.equals(event.asStartElement().getName())) {
+ final Link link = new Link();
+ final Attribute rel = event.asStartElement().getAttributeByName(QName.valueOf(Constants.ATTR_REL));
+ if (rel != null) {
+ link.setRel(rel.getValue());
+ }
+ final Attribute title = event.asStartElement().getAttributeByName(QName.valueOf(Constants.ATTR_TITLE));
+ if (title != null) {
+ link.setTitle(title.getValue());
+ }
+ final Attribute href = event.asStartElement().getAttributeByName(QName.valueOf(Constants.ATTR_HREF));
+ if (href != null) {
+ link.setHref(href.getValue());
+ }
+ final Attribute type = event.asStartElement().getAttributeByName(QName.valueOf(Constants.ATTR_TYPE));
+ if (type != null) {
+ link.setType(type.getValue());
+ }
+
+ if (Constants.SELF_LINK_REL.equals(link.getRel())) {
+ entity.setSelfLink(link);
+ } else if (Constants.EDIT_LINK_REL.equals(link.getRel())) {
+ entity.setEditLink(link);
+ } else if (Constants.EDITMEDIA_LINK_REL.equals(link.getRel())) {
+ final Attribute mediaETag = event.asStartElement().getAttributeByName(etagQName);
+ if (mediaETag != null) {
+ entity.setMediaETag(mediaETag.getValue());
+ }
+ } else if (link.getRel().startsWith(Constants.NS_NAVIGATION_LINK_REL)) {
+
+ entity.getNavigationLinks().add(link);
+ inline(reader, event.asStartElement(), link);
+ } else if (link.getRel().startsWith(Constants.NS_ASSOCIATION_LINK_REL)) {
+
+ entity.getAssociationLinks().add(link);
+ } else if (link.getRel().startsWith(Constants.NS_MEDIA_EDIT_LINK_REL)) {
+
+ final Attribute metag = event.asStartElement().getAttributeByName(etagQName);
+ if (metag != null) {
+ link.setMediaETag(metag.getValue());
+ }
+ entity.getMediaEditLinks().add(link);
+ }
+ } else if (actionQName.equals(event.asStartElement().getName())) {
+ final Operation operation = new Operation();
+ final Attribute metadata =
+ event.asStartElement().getAttributeByName(QName.valueOf(Constants.ATTR_METADATA));
+ if (metadata != null) {
+ operation.setMetadataAnchor(metadata.getValue());
+ }
+ final Attribute title = event.asStartElement().getAttributeByName(QName.valueOf(Constants.ATTR_TITLE));
+ if (title != null) {
+ operation.setTitle(title.getValue());
+ }
+ final Attribute target = event.asStartElement().getAttributeByName(QName.valueOf(Constants.ATTR_TARGET));
+ if (target != null) {
+ operation.setTarget(URI.create(target.getValue()));
+ }
+
+ entity.getOperations().add(operation);
+ } else if (Constants.QNAME_ATOM_ELEM_CONTENT.equals(event.asStartElement().getName())) {
+ final Attribute type = event.asStartElement().getAttributeByName(QName.valueOf(Constants.ATTR_TYPE));
+ if (type == null || ContentType.APPLICATION_XML.toContentTypeString().equals(type.getValue())) {
+ properties(reader, skipBeforeFirstStartElement(reader), entity);
+ } else {
+ entity.setMediaContentType(type.getValue());
+ final Attribute src = event.asStartElement().getAttributeByName(QName.valueOf(Constants.ATOM_ATTR_SRC));
+ if (src != null) {
+ entity.setMediaContentSource(URI.create(src.getValue()));
+ }
+ }
+ } else if (propertiesQName.equals(event.asStartElement().getName())) {
+ properties(reader, event.asStartElement(), entity);
+ } else if (annotationQName.equals(event.asStartElement().getName())) {
+ entity.getAnnotations().add(annotation(reader, event.asStartElement()));
+ }
+ }
+
+ if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) {
+ foundEndEntry = true;
+ }
+ }
+ } else {
+ entity = null;
+ }
+
+ return entity;
+ }
+
+ @Override
+ public ResWrap<Entity> toEntity(final InputStream input) throws ODataDeserializerException {
+ try {
+ final XMLEventReader reader = getReader(input);
+ final StartElement start = skipBeforeFirstStartElement(reader);
+ final Entity entity = entity(reader, start);
+ if (entity == null) {
+ throw new ODataDeserializerException("No entity found!");
+ } else {
+ return getContainer(start, entity);
+ }
+ } catch (XMLStreamException e) {
+ throw new ODataDeserializerException(e);
+ } catch (final EdmPrimitiveTypeException e) {
+ throw new ODataDeserializerException(e);
+ }
+ }
+
+ private void count(final XMLEventReader reader, final StartElement start, final EntityCollection entitySet)
+ throws XMLStreamException {
+
+ boolean foundEndElement = false;
+ while (reader.hasNext() && !foundEndElement) {
+ final XMLEvent event = reader.nextEvent();
+
+ if (event.isCharacters() && !event.asCharacters().isWhiteSpace()) {
+ entitySet.setCount(Integer.valueOf(event.asCharacters().getData()));
+ }
+
+ if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) {
+ foundEndElement = true;
+ }
+ }
+ }
+
+ private EntityCollection entitySet(final XMLEventReader reader, final StartElement start)
+ throws XMLStreamException, EdmPrimitiveTypeException {
+ if (!Constants.QNAME_ATOM_ELEM_FEED.equals(start.getName())) {
+ return null;
+ }
+ final EntityCollection entitySet = new EntityCollection();
+ final Attribute xmlBase = start.getAttributeByName(Constants.QNAME_ATTR_XML_BASE);
+ if (xmlBase != null) {
+ entitySet.setBaseURI(URI.create(xmlBase.getValue()));
+ }
+
+ boolean foundEndFeed = false;
+ while (reader.hasNext() && !foundEndFeed) {
+ final XMLEvent event = reader.nextEvent();
+ if (event.isStartElement()) {
+ if (countQName.equals(event.asStartElement().getName())) {
+ count(reader, event.asStartElement(), entitySet);
+ } else if (Constants.QNAME_ATOM_ELEM_ID.equals(event.asStartElement().getName())) {
+ common(reader, event.asStartElement(), entitySet, "id");
+ } else if (Constants.QNAME_ATOM_ELEM_TITLE.equals(event.asStartElement().getName())) {
+ common(reader, event.asStartElement(), entitySet, "title");
+ } else if (Constants.QNAME_ATOM_ELEM_SUMMARY.equals(event.asStartElement().getName())) {
+ common(reader, event.asStartElement(), entitySet, "summary");
+ } else if (Constants.QNAME_ATOM_ELEM_UPDATED.equals(event.asStartElement().getName())) {
+ common(reader, event.asStartElement(), entitySet, "updated");
+ } else if (Constants.QNAME_ATOM_ELEM_LINK.equals(event.asStartElement().getName())) {
+ final Attribute rel = event.asStartElement().getAttributeByName(QName.valueOf(Constants.ATTR_REL));
+ if (rel != null) {
+ if (Constants.NEXT_LINK_REL.equals(rel.getValue())) {
+ final Attribute href = event.asStartElement().getAttributeByName(QName.valueOf(Constants.ATTR_HREF));
+ if (href != null) {
+ entitySet.setNext(URI.create(href.getValue()));
+ }
+ }
+ if (Constants.NS_DELTA_LINK_REL.equals(rel.getValue())) {
+ final Attribute href = event.asStartElement().getAttributeByName(QName.valueOf(Constants.ATTR_HREF));
+ if (href != null) {
+ entitySet.setDeltaLink(URI.create(href.getValue()));
+ }
+ }
+ }
+ } else if (Constants.QNAME_ATOM_ELEM_ENTRY.equals(event.asStartElement().getName())) {
+ entitySet.getEntities().add(entity(reader, event.asStartElement()));
+ } else if (entryRefQName.equals(event.asStartElement().getName())) {
+ entitySet.getEntities().add(entityRef(event.asStartElement()));
+ } else if (annotationQName.equals(event.asStartElement().getName())) {
+ entitySet.getAnnotations().add(annotation(reader, event.asStartElement()));
+ }
+ }
+
+ if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) {
+ foundEndFeed = true;
+ }
+ }
+
+ return entitySet;
+ }
+
+ @Override
+ public ResWrap<EntityCollection> toEntitySet(final InputStream input) throws ODataDeserializerException {
+ try {
+ final XMLEventReader reader = getReader(input);
+ final StartElement start = skipBeforeFirstStartElement(reader);
+ return getContainer(start, entitySet(reader, start));
+ } catch (XMLStreamException e) {
+ throw new ODataDeserializerException(e);
+ } catch (final EdmPrimitiveTypeException e) {
+ throw new ODataDeserializerException(e);
+ }
+ }
+
+ private ODataError error(final XMLEventReader reader, final StartElement start) throws XMLStreamException {
+ final ODataError error = new ODataError();
+
+ boolean setCode = false;
+ boolean codeSet = false;
+ boolean setMessage = false;
+ boolean messageSet = false;
+ boolean setTarget = false;
+ boolean targetSet = false;
+
+ boolean foundEndElement = false;
+ while (reader.hasNext() && !foundEndElement) {
+ final XMLEvent event = reader.nextEvent();
+
+ if (event.isStartElement()) {
+ if (errorCodeQName.equals(event.asStartElement().getName())) {
+ setCode = true;
+ } else if (errorMessageQName.equals(event.asStartElement().getName())) {
+ setMessage = true;
+ } else if (errorTargetQName.equals(event.asStartElement().getName())) {
+ setTarget = true;
+ }
+ }
+
+ if (event.isCharacters() && !event.asCharacters().isWhiteSpace()) {
+ if (setCode && !codeSet) {
+ error.setCode(event.asCharacters().getData());
+ setCode = false;
+ codeSet = true;
+ }
+ if (setMessage && !messageSet) {
+ error.setMessage(event.asCharacters().getData());
+ setMessage = false;
+ messageSet = true;
+ }
+ if (setTarget && !targetSet) {
+ error.setTarget(event.asCharacters().getData());
+ setTarget = false;
+ targetSet = true;
+ }
+ }
+
+ if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) {
+ foundEndElement = true;
+ }
+ }
+
+ return error;
+ }
+
+ @Override
+ public ODataError toError(final InputStream input) throws ODataDeserializerException {
+ try {
+ final XMLEventReader reader = getReader(input);
+ final StartElement start = skipBeforeFirstStartElement(reader);
+ return error(reader, start);
+ } catch (XMLStreamException e) {
+ throw new ODataDeserializerException(e);
+ }
+ }
+
+ private <T> ResWrap<T> getContainer(final StartElement start, final T object) {
+ final Attribute context = start.getAttributeByName(contextQName);
+ final Attribute metadataETag = start.getAttributeByName(metadataEtagQName);
+
+ return new ResWrap<T>(
+ context == null ? null : URI.create(context.getValue()),
+ metadataETag == null ? null : metadataETag.getValue(),
+ object);
+ }
+}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/20351253/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/AtomGeoValueDeserializer.java
----------------------------------------------------------------------
diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/AtomGeoValueDeserializer.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/AtomGeoValueDeserializer.java
new file mode 100644
index 0000000..38add8d
--- /dev/null
+++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/AtomGeoValueDeserializer.java
@@ -0,0 +1,269 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.olingo.client.core.serialization;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import javax.xml.stream.XMLEventReader;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.events.Attribute;
+import javax.xml.stream.events.StartElement;
+import javax.xml.stream.events.XMLEvent;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.olingo.commons.api.Constants;
+import org.apache.olingo.commons.api.data.GeoUtils;
+import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException;
+import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
+import org.apache.olingo.commons.api.edm.geo.Geospatial;
+import org.apache.olingo.commons.api.edm.geo.GeospatialCollection;
+import org.apache.olingo.commons.api.edm.geo.LineString;
+import org.apache.olingo.commons.api.edm.geo.MultiLineString;
+import org.apache.olingo.commons.api.edm.geo.MultiPoint;
+import org.apache.olingo.commons.api.edm.geo.MultiPolygon;
+import org.apache.olingo.commons.api.edm.geo.Point;
+import org.apache.olingo.commons.api.edm.geo.Polygon;
+import org.apache.olingo.commons.api.edm.geo.SRID;
+import org.apache.olingo.commons.core.edm.primitivetype.EdmDouble;
+
+class AtomGeoValueDeserializer {
+
+ private List<Point> points(final XMLEventReader reader, final StartElement start,
+ final EdmPrimitiveTypeKind type, final SRID srid) throws XMLStreamException {
+
+ final List<Point> result = new ArrayList<Point>();
+
+ boolean foundEndProperty = false;
+ while (reader.hasNext() && !foundEndProperty) {
+ final XMLEvent event = reader.nextEvent();
+
+ if (event.isCharacters() && !event.asCharacters().isWhiteSpace()) {
+ final String[] pointInfo = event.asCharacters().getData().split(" ");
+
+ final Point point = new Point(GeoUtils.getDimension(type), srid);
+ try {
+ point.setX(EdmDouble.getInstance().valueOfString(pointInfo[0], null, null,
+ Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null, Double.class));
+ point.setY(EdmDouble.getInstance().valueOfString(pointInfo[1], null, null,
+ Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null, Double.class));
+ } catch (EdmPrimitiveTypeException e) {
+ throw new XMLStreamException("While deserializing point coordinates as double", e);
+ }
+ result.add(point);
+ }
+
+ if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) {
+ foundEndProperty = true;
+ }
+ }
+
+ // handles bad input, e.g. things like <gml:pos/>
+ if (result.isEmpty()) {
+ result.add(new Point(GeoUtils.getDimension(type), srid));
+ }
+
+ return result;
+ }
+
+ private MultiPoint multipoint(final XMLEventReader reader, final StartElement start,
+ final EdmPrimitiveTypeKind type, final SRID srid) throws XMLStreamException {
+
+ List<Point> points = Collections.<Point> emptyList();
+
+ boolean foundEndProperty = false;
+ while (reader.hasNext() && !foundEndProperty) {
+ final XMLEvent event = reader.nextEvent();
+
+ if (event.isStartElement() && event.asStartElement().getName().equals(Constants.QNAME_POINTMEMBERS)) {
+ points = points(reader, event.asStartElement(), type, null);
+ }
+
+ if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) {
+ foundEndProperty = true;
+ }
+ }
+
+ return new MultiPoint(GeoUtils.getDimension(type), srid, points);
+ }
+
+ private LineString lineString(final XMLEventReader reader, final StartElement start,
+ final EdmPrimitiveTypeKind type, final SRID srid) throws XMLStreamException {
+
+ return new LineString(GeoUtils.getDimension(type), srid, points(reader, start, type, null));
+ }
+
+ private Polygon polygon(final XMLEventReader reader, final StartElement start,
+ final EdmPrimitiveTypeKind type, final SRID srid) throws XMLStreamException {
+
+ List<Point> extPoints = null;
+ List<Point> intPoints = null;
+
+ boolean foundEndProperty = false;
+ while (reader.hasNext() && !foundEndProperty) {
+ final XMLEvent event = reader.nextEvent();
+
+ if (event.isStartElement()) {
+ if (event.asStartElement().getName().equals(Constants.QNAME_POLYGON_EXTERIOR)) {
+ extPoints = points(reader, event.asStartElement(), type, null);
+ }
+ if (event.asStartElement().getName().equals(Constants.QNAME_POLYGON_INTERIOR)) {
+ intPoints = points(reader, event.asStartElement(), type, null);
+ }
+ }
+
+ if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) {
+ foundEndProperty = true;
+ }
+ }
+
+ return new Polygon(GeoUtils.getDimension(type), srid, intPoints, extPoints);
+ }
+
+ private MultiLineString multiLineString(final XMLEventReader reader, final StartElement start,
+ final EdmPrimitiveTypeKind type, final SRID srid) throws XMLStreamException {
+
+ final List<LineString> lineStrings = new ArrayList<LineString>();
+
+ boolean foundEndProperty = false;
+ while (reader.hasNext() && !foundEndProperty) {
+ final XMLEvent event = reader.nextEvent();
+
+ if (event.isStartElement() && event.asStartElement().getName().equals(Constants.QNAME_LINESTRING)) {
+ lineStrings.add(lineString(reader, event.asStartElement(), type, null));
+ }
+
+ if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) {
+ foundEndProperty = true;
+ }
+ }
+
+ return new MultiLineString(GeoUtils.getDimension(type), srid, lineStrings);
+ }
+
+ private MultiPolygon multiPolygon(final XMLEventReader reader, final StartElement start,
+ final EdmPrimitiveTypeKind type, final SRID srid) throws XMLStreamException {
+
+ final List<Polygon> polygons = new ArrayList<Polygon>();
+
+ boolean foundEndProperty = false;
+ while (reader.hasNext() && !foundEndProperty) {
+ final XMLEvent event = reader.nextEvent();
+
+ if (event.isStartElement() && event.asStartElement().getName().equals(Constants.QNAME_POLYGON)) {
+ polygons.add(polygon(reader, event.asStartElement(), type, null));
+ }
+
+ if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) {
+ foundEndProperty = true;
+ }
+ }
+
+ return new MultiPolygon(GeoUtils.getDimension(type), srid, polygons);
+ }
+
+ private GeospatialCollection collection(final XMLEventReader reader, final StartElement start,
+ final EdmPrimitiveTypeKind type, final SRID srid) throws XMLStreamException {
+
+ final List<Geospatial> geospatials = new ArrayList<Geospatial>();
+
+ boolean foundEndCollection = false;
+ while (reader.hasNext() && !foundEndCollection) {
+ final XMLEvent event = reader.nextEvent();
+
+ if (event.isStartElement() && event.asStartElement().getName().equals(Constants.QNAME_GEOMEMBERS)) {
+ boolean foundEndMembers = false;
+ while (reader.hasNext() && !foundEndMembers) {
+ final XMLEvent subevent = reader.nextEvent();
+
+ if (subevent.isStartElement()) {
+ geospatials.add(deserialize(reader, subevent.asStartElement(),
+ GeoUtils.getType(GeoUtils.getDimension(type), subevent.asStartElement().getName().getLocalPart())));
+ }
+
+ if (subevent.isEndElement() && Constants.QNAME_GEOMEMBERS.equals(subevent.asEndElement().getName())) {
+ foundEndMembers = true;
+ }
+ }
+ }
+
+ if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) {
+ foundEndCollection = true;
+ }
+ }
+
+ return new GeospatialCollection(GeoUtils.getDimension(type), srid, geospatials);
+ }
+
+ public Geospatial deserialize(final XMLEventReader reader, final StartElement start,
+ final EdmPrimitiveTypeKind type) throws XMLStreamException {
+
+ SRID srid = null;
+ final Attribute srsName = start.getAttributeByName(Constants.QNAME_ATTR_SRSNAME);
+ if (srsName != null) {
+ srid = SRID.valueOf(StringUtils.substringAfterLast(srsName.getValue(), "/"));
+ }
+
+ Geospatial value;
+
+ switch (type) {
+ case GeographyPoint:
+ case GeometryPoint:
+ value = points(reader, start, type, srid).get(0);
+ break;
+
+ case GeographyMultiPoint:
+ case GeometryMultiPoint:
+ value = multipoint(reader, start, type, srid);
+ break;
+
+ case GeographyLineString:
+ case GeometryLineString:
+ value = lineString(reader, start, type, srid);
+ break;
+
+ case GeographyMultiLineString:
+ case GeometryMultiLineString:
+ value = multiLineString(reader, start, type, srid);
+ break;
+
+ case GeographyPolygon:
+ case GeometryPolygon:
+ value = polygon(reader, start, type, srid);
+ break;
+
+ case GeographyMultiPolygon:
+ case GeometryMultiPolygon:
+ value = multiPolygon(reader, start, type, srid);
+ break;
+
+ case GeographyCollection:
+ case GeometryCollection:
+ value = collection(reader, start, type, srid);
+ break;
+
+ default:
+ value = null;
+ }
+
+ return value;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/20351253/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/AtomGeoValueSerializer.java
----------------------------------------------------------------------
diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/AtomGeoValueSerializer.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/AtomGeoValueSerializer.java
new file mode 100644
index 0000000..1030597
--- /dev/null
+++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/AtomGeoValueSerializer.java
@@ -0,0 +1,223 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.olingo.client.core.serialization;
+
+import java.util.Collections;
+import java.util.Iterator;
+
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.apache.olingo.commons.api.Constants;
+import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException;
+import org.apache.olingo.commons.api.edm.geo.Geospatial;
+import org.apache.olingo.commons.api.edm.geo.GeospatialCollection;
+import org.apache.olingo.commons.api.edm.geo.LineString;
+import org.apache.olingo.commons.api.edm.geo.MultiLineString;
+import org.apache.olingo.commons.api.edm.geo.MultiPoint;
+import org.apache.olingo.commons.api.edm.geo.MultiPolygon;
+import org.apache.olingo.commons.api.edm.geo.Point;
+import org.apache.olingo.commons.api.edm.geo.Polygon;
+import org.apache.olingo.commons.core.edm.primitivetype.EdmDouble;
+
+class AtomGeoValueSerializer {
+
+ private void points(final XMLStreamWriter writer, final Iterator<Point> itor, final boolean wrap)
+ throws XMLStreamException {
+
+ while (itor.hasNext()) {
+ final Point point = itor.next();
+
+ if (wrap) {
+ writer.writeStartElement(Constants.PREFIX_GML, Constants.ELEM_POINT, Constants.NS_GML);
+ }
+
+ writer.writeStartElement(Constants.PREFIX_GML, Constants.ELEM_POS, Constants.NS_GML);
+ try {
+ writer.writeCharacters(EdmDouble.getInstance().valueToString(point.getX(), null, null,
+ Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null)
+ + " "
+ + EdmDouble.getInstance().valueToString(point.getY(), null, null,
+ Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null));
+ } catch (EdmPrimitiveTypeException e) {
+ throw new XMLStreamException("While serializing point coordinates as double", e);
+ }
+ writer.writeEndElement();
+
+ if (wrap) {
+ writer.writeEndElement();
+ }
+ }
+ }
+
+ private void lineStrings(final XMLStreamWriter writer, final Iterator<LineString> itor, final boolean wrap)
+ throws XMLStreamException {
+
+ while (itor.hasNext()) {
+ final LineString lineString = itor.next();
+
+ if (wrap) {
+ writer.writeStartElement(Constants.PREFIX_GML, Constants.ELEM_LINESTRING, Constants.NS_GML);
+ }
+
+ points(writer, lineString.iterator(), false);
+
+ if (wrap) {
+ writer.writeEndElement();
+ }
+ }
+ }
+
+ private void polygons(final XMLStreamWriter writer, final Iterator<Polygon> itor, final boolean wrap)
+ throws XMLStreamException {
+
+ while (itor.hasNext()) {
+ final Polygon polygon = itor.next();
+
+ if (wrap) {
+ writer.writeStartElement(Constants.PREFIX_GML, Constants.ELEM_POLYGON, Constants.NS_GML);
+ }
+
+ if (!polygon.getExterior().isEmpty()) {
+ writer.writeStartElement(Constants.PREFIX_GML, Constants.ELEM_POLYGON_EXTERIOR, Constants.NS_GML);
+ writer.writeStartElement(Constants.PREFIX_GML, Constants.ELEM_POLYGON_LINEARRING, Constants.NS_GML);
+
+ points(writer, polygon.getExterior().iterator(), false);
+
+ writer.writeEndElement();
+ writer.writeEndElement();
+ }
+ if (!polygon.getInterior().isEmpty()) {
+ writer.writeStartElement(Constants.PREFIX_GML, Constants.ELEM_POLYGON_INTERIOR, Constants.NS_GML);
+ writer.writeStartElement(Constants.PREFIX_GML, Constants.ELEM_POLYGON_LINEARRING, Constants.NS_GML);
+
+ points(writer, polygon.getInterior().iterator(), false);
+
+ writer.writeEndElement();
+ writer.writeEndElement();
+ }
+
+ if (wrap) {
+ writer.writeEndElement();
+ }
+ }
+ }
+
+ private void writeSrsName(final XMLStreamWriter writer, final Geospatial value) throws XMLStreamException {
+ if (value.getSrid() != null && value.getSrid().isNotDefault()) {
+ writer.writeAttribute(Constants.PREFIX_GML, Constants.NS_GML, Constants.ATTR_SRSNAME,
+ Constants.SRS_URLPREFIX + value.getSrid().toString());
+ }
+ }
+
+ public void serialize(final XMLStreamWriter writer, final Geospatial value) throws XMLStreamException {
+ switch (value.getEdmPrimitiveTypeKind()) {
+ case GeographyPoint:
+ case GeometryPoint:
+ writer.writeStartElement(Constants.PREFIX_GML, Constants.ELEM_POINT, Constants.NS_GML);
+ writeSrsName(writer, value);
+
+ points(writer, Collections.singleton((Point) value).iterator(), false);
+
+ writer.writeEndElement();
+ break;
+
+ case GeometryMultiPoint:
+ case GeographyMultiPoint:
+ writer.writeStartElement(Constants.PREFIX_GML, Constants.ELEM_MULTIPOINT, Constants.NS_GML);
+ writeSrsName(writer, value);
+
+ if (!((MultiPoint) value).isEmpty()) {
+ writer.writeStartElement(Constants.PREFIX_GML, Constants.ELEM_POINTMEMBERS, Constants.NS_GML);
+ points(writer, ((MultiPoint) value).iterator(), true);
+ writer.writeEndElement();
+ }
+
+ writer.writeEndElement();
+ break;
+
+ case GeometryLineString:
+ case GeographyLineString:
+ writer.writeStartElement(Constants.PREFIX_GML, Constants.ELEM_LINESTRING, Constants.NS_GML);
+ writeSrsName(writer, value);
+
+ lineStrings(writer, Collections.singleton((LineString) value).iterator(), false);
+
+ writer.writeEndElement();
+ break;
+
+ case GeometryMultiLineString:
+ case GeographyMultiLineString:
+ writer.writeStartElement(Constants.PREFIX_GML, Constants.ELEM_MULTILINESTRING, Constants.NS_GML);
+ writeSrsName(writer, value);
+
+ if (!((MultiLineString) value).isEmpty()) {
+ writer.writeStartElement(Constants.PREFIX_GML, Constants.ELEM_LINESTRINGMEMBERS, Constants.NS_GML);
+ lineStrings(writer, ((MultiLineString) value).iterator(), true);
+ writer.writeEndElement();
+ }
+
+ writer.writeEndElement();
+ break;
+
+ case GeographyPolygon:
+ case GeometryPolygon:
+ writer.writeStartElement(Constants.PREFIX_GML, Constants.ELEM_POLYGON, Constants.NS_GML);
+ writeSrsName(writer, value);
+
+ polygons(writer, Collections.singleton(((Polygon) value)).iterator(), false);
+
+ writer.writeEndElement();
+ break;
+
+ case GeographyMultiPolygon:
+ case GeometryMultiPolygon:
+ writer.writeStartElement(Constants.PREFIX_GML, Constants.ELEM_MULTIPOLYGON, Constants.NS_GML);
+ writeSrsName(writer, value);
+
+ if (!((MultiPolygon) value).isEmpty()) {
+ writer.writeStartElement(Constants.PREFIX_GML, Constants.ELEM_SURFACEMEMBERS, Constants.NS_GML);
+ polygons(writer, ((MultiPolygon) value).iterator(), true);
+ writer.writeEndElement();
+ }
+
+ writer.writeEndElement();
+ break;
+
+ case GeographyCollection:
+ case GeometryCollection:
+ writer.writeStartElement(Constants.PREFIX_GML, Constants.ELEM_GEOCOLLECTION, Constants.NS_GML);
+ writeSrsName(writer, value);
+
+ if (!((GeospatialCollection) value).isEmpty()) {
+ writer.writeStartElement(Constants.PREFIX_GML, Constants.ELEM_GEOMEMBERS, Constants.NS_GML);
+ for (Geospatial geospatial : ((GeospatialCollection) value)) {
+ serialize(writer, geospatial);
+ }
+ writer.writeEndElement();
+ }
+
+ writer.writeEndElement();
+ break;
+
+ default:
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/20351253/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/AtomSerializer.java
----------------------------------------------------------------------
diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/AtomSerializer.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/AtomSerializer.java
new file mode 100644
index 0000000..bc56e48
--- /dev/null
+++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/AtomSerializer.java
@@ -0,0 +1,570 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.olingo.client.core.serialization;
+
+import java.io.Writer;
+import java.net.URI;
+import java.util.Collections;
+import java.util.List;
+
+import javax.xml.XMLConstants;
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.olingo.client.api.serialization.ODataSerializer;
+import org.apache.olingo.client.api.serialization.ODataSerializerException;
+import org.apache.olingo.commons.api.Constants;
+import org.apache.olingo.commons.api.ODataRuntimeException;
+import org.apache.olingo.commons.api.data.AbstractODataObject;
+import org.apache.olingo.commons.api.data.Annotation;
+import org.apache.olingo.commons.api.data.ComplexValue;
+import org.apache.olingo.commons.api.data.ContextURL;
+import org.apache.olingo.commons.api.data.Entity;
+import org.apache.olingo.commons.api.data.EntityCollection;
+import org.apache.olingo.commons.api.data.Link;
+import org.apache.olingo.commons.api.data.Operation;
+import org.apache.olingo.commons.api.data.Property;
+import org.apache.olingo.commons.api.data.ResWrap;
+import org.apache.olingo.commons.api.data.ValueType;
+import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException;
+import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
+import org.apache.olingo.commons.api.edm.geo.Geospatial;
+import org.apache.olingo.commons.api.format.ContentType;
+import org.apache.olingo.commons.core.edm.EdmTypeInfo;
+import org.apache.olingo.commons.core.edm.primitivetype.EdmPrimitiveTypeFactory;
+
+import com.fasterxml.aalto.stax.OutputFactoryImpl;
+
+public class AtomSerializer extends AbstractAtomDealer implements ODataSerializer {
+
+ private static final XMLOutputFactory FACTORY = new OutputFactoryImpl();
+
+ private final AtomGeoValueSerializer geoSerializer;
+
+ private final boolean serverMode;
+
+ public AtomSerializer() {
+ this(false);
+ }
+
+ public AtomSerializer(final boolean serverMode) {
+ geoSerializer = new AtomGeoValueSerializer();
+ this.serverMode = serverMode;
+ }
+
+ private void collection(final XMLStreamWriter writer,
+ final ValueType valueType, final EdmPrimitiveTypeKind kind, final List<?> value)
+ throws XMLStreamException, EdmPrimitiveTypeException {
+ for (Object item : value) {
+ writer.writeStartElement(Constants.PREFIX_METADATA, Constants.ELEM_ELEMENT, namespaceMetadata);
+ value(writer, valueType, kind, item);
+ writer.writeEndElement();
+ }
+ }
+
+ private void value(final XMLStreamWriter writer,
+ final ValueType valueType, final EdmPrimitiveTypeKind kind, final Object value)
+ throws XMLStreamException, EdmPrimitiveTypeException {
+ if (value == null) {
+ writer.writeAttribute(Constants.PREFIX_METADATA, namespaceMetadata,
+ Constants.ATTR_NULL, Boolean.TRUE.toString());
+ return;
+ }
+ switch (valueType) {
+ case PRIMITIVE:
+ writer.writeCharacters(kind == null ? value.toString() :
+ EdmPrimitiveTypeFactory.getInstance(kind) // TODO: add facets
+ .valueToString(value, null, null, Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null));
+ break;
+ case ENUM:
+ writer.writeCharacters(value.toString());
+ break;
+ case GEOSPATIAL:
+ geoSerializer.serialize(writer, (Geospatial) value);
+ break;
+ case COLLECTION_PRIMITIVE:
+ case COLLECTION_GEOSPATIAL:
+ case COLLECTION_ENUM:
+ case COLLECTION_COMPLEX:
+ collection(writer, valueType.getBaseType(), kind, (List<?>) value);
+ break;
+ case COMPLEX:
+ for (Property property : ((ComplexValue) value).getValue()) {
+ property(writer, property, false);
+ }
+ break;
+ case ENTITY:
+ case COLLECTION_ENTITY:
+ throw new ODataRuntimeException("Entities cannot appear in this payload");
+ }
+ }
+
+ public void property(final XMLStreamWriter writer, final Property property, final boolean standalone)
+ throws XMLStreamException, EdmPrimitiveTypeException {
+
+ if (standalone) {
+ writer.writeStartElement(Constants.PREFIX_METADATA, Constants.VALUE, namespaceData);
+ namespaces(writer);
+ } else {
+ writer.writeStartElement(Constants.PREFIX_DATASERVICES, property.getName(), namespaceData);
+ }
+
+ EdmTypeInfo typeInfo = null;
+ if (StringUtils.isNotBlank(property.getType())) {
+ typeInfo = new EdmTypeInfo.Builder().setTypeExpression(property.getType()).build();
+ if (!EdmPrimitiveTypeKind.String.getFullQualifiedName().toString().equals(typeInfo.internal())) {
+ writer.writeAttribute(Constants.PREFIX_METADATA, namespaceMetadata,
+ Constants.ATTR_TYPE, typeInfo.external());
+ }
+ }
+
+ value(writer, property.getValueType(), typeInfo == null ? null : typeInfo.getPrimitiveTypeKind(),
+ property.getValue());
+ if (!property.isNull() && property.isComplex() && !property.isCollection()) {
+ links(writer, property.asComplex().getAssociationLinks());
+ links(writer, property.asComplex().getNavigationLinks());
+ }
+
+ writer.writeEndElement();
+
+ for (Annotation annotation : property.getAnnotations()) {
+ annotation(writer, annotation, property.getName());
+ }
+ }
+
+ private void property(final XMLStreamWriter writer, final Property property)
+ throws XMLStreamException, EdmPrimitiveTypeException {
+ property(writer, property, true);
+ }
+
+ private void startDocument(final XMLStreamWriter writer, final String rootElement) throws XMLStreamException {
+ writer.writeStartDocument();
+ writer.setDefaultNamespace(Constants.NS_ATOM);
+
+ writer.writeStartElement(rootElement);
+
+ namespaces(writer);
+ }
+
+ private void property(final Writer outWriter, final Property property)
+ throws XMLStreamException, EdmPrimitiveTypeException {
+ final XMLStreamWriter writer = FACTORY.createXMLStreamWriter(outWriter);
+
+ writer.writeStartDocument();
+
+ property(writer, property);
+
+ writer.writeEndDocument();
+ writer.flush();
+ }
+
+ private void links(final XMLStreamWriter writer, final List<Link> links)
+ throws XMLStreamException, EdmPrimitiveTypeException {
+ for (Link link : links) {
+ writer.writeStartElement(Constants.ATOM_ELEM_LINK);
+
+ if (StringUtils.isNotBlank(link.getRel())) {
+ writer.writeAttribute(Constants.ATTR_REL, link.getRel());
+ }
+ if (StringUtils.isNotBlank(link.getTitle())) {
+ writer.writeAttribute(Constants.ATTR_TITLE, link.getTitle());
+ }
+ if (StringUtils.isNotBlank(link.getHref())) {
+ writer.writeAttribute(Constants.ATTR_HREF, link.getHref());
+ }
+ if (StringUtils.isNotBlank(link.getType())) {
+ writer.writeAttribute(Constants.ATTR_TYPE, link.getType());
+ }
+
+ if (link.getInlineEntity() != null || link.getInlineEntitySet() != null) {
+ writer.writeStartElement(Constants.PREFIX_METADATA, Constants.ATOM_ELEM_INLINE, namespaceMetadata);
+
+ if (link.getInlineEntity() != null) {
+ writer.writeStartElement(Constants.ATOM_ELEM_ENTRY);
+ entity(writer, link.getInlineEntity());
+ writer.writeEndElement();
+ }
+ if (link.getInlineEntitySet() != null) {
+ writer.writeStartElement(Constants.ATOM_ELEM_FEED);
+ entitySet(writer, link.getInlineEntitySet());
+ writer.writeEndElement();
+ }
+
+ writer.writeEndElement();
+ }
+
+ for (Annotation annotation : link.getAnnotations()) {
+ annotation(writer, annotation, null);
+ }
+
+ writer.writeEndElement();
+ }
+ }
+
+ private void common(final XMLStreamWriter writer, final AbstractODataObject object) throws XMLStreamException {
+ if (StringUtils.isNotBlank(object.getTitle())) {
+ writer.writeStartElement(Constants.ATOM_ELEM_TITLE);
+ writer.writeAttribute(Constants.ATTR_TYPE, TYPE_TEXT);
+ writer.writeCharacters(object.getTitle());
+ writer.writeEndElement();
+ }
+ }
+
+ private void properties(final XMLStreamWriter writer, final List<Property> properties)
+ throws XMLStreamException, EdmPrimitiveTypeException {
+ for (Property property : properties) {
+ property(writer, property, false);
+ }
+ }
+
+ private void annotation(final XMLStreamWriter writer, final Annotation annotation, final String target)
+ throws XMLStreamException, EdmPrimitiveTypeException {
+
+ writer.writeStartElement(Constants.PREFIX_METADATA, Constants.ANNOTATION, namespaceMetadata);
+
+ writer.writeAttribute(Constants.ATOM_ATTR_TERM, annotation.getTerm());
+
+ if (target != null) {
+ writer.writeAttribute(Constants.ATTR_TARGET, target);
+ }
+
+ EdmTypeInfo typeInfo = null;
+ if (StringUtils.isNotBlank(annotation.getType())) {
+ typeInfo = new EdmTypeInfo.Builder().setTypeExpression(annotation.getType()).build();
+ if (!EdmPrimitiveTypeKind.String.getFullQualifiedName().toString().equals(typeInfo.internal())) {
+ writer.writeAttribute(Constants.PREFIX_METADATA, namespaceMetadata,
+ Constants.ATTR_TYPE, typeInfo.external());
+ }
+ }
+
+ value(writer, annotation.getValueType(), typeInfo == null ? null : typeInfo.getPrimitiveTypeKind(),
+ annotation.getValue());
+
+ writer.writeEndElement();
+ }
+
+ private void entity(final XMLStreamWriter writer, final Entity entity)
+ throws XMLStreamException, EdmPrimitiveTypeException {
+ if (entity.getBaseURI() != null) {
+ writer.writeAttribute(XMLConstants.XML_NS_URI, Constants.ATTR_XML_BASE, entity.getBaseURI().toASCIIString());
+ }
+
+ if (serverMode && StringUtils.isNotBlank(entity.getETag())) {
+ writer.writeAttribute(namespaceMetadata, Constants.ATOM_ATTR_ETAG, entity.getETag());
+ }
+
+ if (entity.getId() != null) {
+ writer.writeStartElement(Constants.ATOM_ELEM_ID);
+ writer.writeCharacters(entity.getId().toASCIIString());
+ writer.writeEndElement();
+ }
+
+ writer.writeStartElement(Constants.ATOM_ELEM_CATEGORY);
+ writer.writeAttribute(Constants.ATOM_ATTR_SCHEME, Constants.NS_SCHEME);
+ if (StringUtils.isNotBlank(entity.getType())) {
+ writer.writeAttribute(Constants.ATOM_ATTR_TERM,
+ new EdmTypeInfo.Builder().setTypeExpression(entity.getType()).build().external());
+ }
+ writer.writeEndElement();
+
+ common(writer, entity);
+
+ if (serverMode) {
+ if (entity.getEditLink() != null) {
+ links(writer, Collections.singletonList(entity.getEditLink()));
+ }
+
+ if (entity.getSelfLink() != null) {
+ links(writer, Collections.singletonList(entity.getSelfLink()));
+ }
+ }
+
+ links(writer, entity.getAssociationLinks());
+ links(writer, entity.getNavigationLinks());
+ links(writer, entity.getMediaEditLinks());
+
+ if (serverMode) {
+ for (Operation operation : entity.getOperations()) {
+ writer.writeStartElement(namespaceMetadata, Constants.ATOM_ELEM_ACTION);
+ writer.writeAttribute(Constants.ATTR_METADATA, operation.getMetadataAnchor());
+ writer.writeAttribute(Constants.ATTR_TITLE, operation.getTitle());
+ writer.writeAttribute(Constants.ATTR_TARGET, operation.getTarget().toASCIIString());
+ writer.writeEndElement();
+ }
+ }
+
+ writer.writeStartElement(Constants.ATOM_ELEM_CONTENT);
+ if (entity.isMediaEntity()) {
+ if (StringUtils.isNotBlank(entity.getMediaContentType())) {
+ writer.writeAttribute(Constants.ATTR_TYPE, entity.getMediaContentType());
+ }
+ if (entity.getMediaContentSource() != null) {
+ writer.writeAttribute(Constants.ATOM_ATTR_SRC, entity.getMediaContentSource().toASCIIString());
+ }
+ writer.writeEndElement();
+
+ writer.writeStartElement(namespaceMetadata, Constants.PROPERTIES);
+ properties(writer, entity.getProperties());
+ } else {
+ writer.writeAttribute(Constants.ATTR_TYPE, ContentType.APPLICATION_XML.toContentTypeString());
+ writer.writeStartElement(namespaceMetadata, Constants.PROPERTIES);
+ properties(writer, entity.getProperties());
+ writer.writeEndElement();
+ }
+ writer.writeEndElement();
+
+ for (Annotation annotation : entity.getAnnotations()) {
+ annotation(writer, annotation, null);
+ }
+ }
+
+ private void entityRef(final XMLStreamWriter writer, final Entity entity) throws XMLStreamException {
+ writer.writeStartElement(Constants.ATOM_ELEM_ENTRY_REF);
+ writer.writeNamespace(StringUtils.EMPTY, namespaceMetadata);
+ writer.writeAttribute(Constants.ATOM_ATTR_ID, entity.getId().toASCIIString());
+ }
+
+ private void entityRef(final XMLStreamWriter writer, final ResWrap<Entity> container) throws XMLStreamException {
+ writer.writeStartElement(Constants.ATOM_ELEM_ENTRY_REF);
+ writer.writeNamespace(StringUtils.EMPTY, namespaceMetadata);
+ addContextInfo(writer, container);
+ writer.writeAttribute(Constants.ATOM_ATTR_ID, container.getPayload().getId().toASCIIString());
+ }
+
+ private void entity(final Writer outWriter, final Entity entity)
+ throws XMLStreamException, EdmPrimitiveTypeException {
+ final XMLStreamWriter writer = FACTORY.createXMLStreamWriter(outWriter);
+
+ if (entity.getType() == null && entity.getProperties().isEmpty()) {
+ writer.writeStartDocument();
+ writer.setDefaultNamespace(namespaceMetadata);
+
+ entityRef(writer, entity);
+ } else {
+ startDocument(writer, Constants.ATOM_ELEM_ENTRY);
+
+ entity(writer, entity);
+ }
+
+ writer.writeEndElement();
+ writer.writeEndDocument();
+ writer.flush();
+ }
+
+ private void entity(final Writer outWriter, final ResWrap<Entity> container)
+ throws XMLStreamException, EdmPrimitiveTypeException {
+ final Entity entity = container.getPayload();
+
+ final XMLStreamWriter writer = FACTORY.createXMLStreamWriter(outWriter);
+
+ if (entity.getType() == null && entity.getProperties().isEmpty()) {
+ writer.writeStartDocument();
+ writer.setDefaultNamespace(namespaceMetadata);
+
+ entityRef(writer, container);
+ } else {
+ startDocument(writer, Constants.ATOM_ELEM_ENTRY);
+
+ addContextInfo(writer, container);
+
+ entity(writer, entity);
+ }
+
+ writer.writeEndElement();
+ writer.writeEndDocument();
+ writer.flush();
+ }
+
+ private void entitySet(final XMLStreamWriter writer, final EntityCollection entitySet)
+ throws XMLStreamException, EdmPrimitiveTypeException {
+ if (entitySet.getBaseURI() != null) {
+ writer.writeAttribute(XMLConstants.XML_NS_URI, Constants.ATTR_XML_BASE, entitySet.getBaseURI().toASCIIString());
+ }
+
+ if (entitySet.getCount() != null) {
+ writer.writeStartElement(namespaceMetadata, Constants.ATOM_ELEM_COUNT);
+ writer.writeCharacters(Integer.toString(entitySet.getCount()));
+ writer.writeEndElement();
+ }
+
+ if (entitySet.getId() != null) {
+ writer.writeStartElement(Constants.ATOM_ELEM_ID);
+ writer.writeCharacters(entitySet.getId().toASCIIString());
+ writer.writeEndElement();
+ }
+
+ common(writer, entitySet);
+
+ for (Entity entity : entitySet.getEntities()) {
+ if (entity.getType() == null && entity.getProperties().isEmpty()) {
+ entityRef(writer, entity);
+ writer.writeEndElement();
+ } else {
+ writer.writeStartElement(Constants.ATOM_ELEM_ENTRY);
+ entity(writer, entity);
+ writer.writeEndElement();
+ }
+ }
+
+ if (serverMode) {
+ if (entitySet.getNext() != null) {
+ final Link next = new Link();
+ next.setRel(Constants.NEXT_LINK_REL);
+ next.setHref(entitySet.getNext().toASCIIString());
+
+ links(writer, Collections.<Link> singletonList(next));
+ }
+ if (entitySet.getDeltaLink() != null) {
+ final Link next = new Link();
+ next.setRel(Constants.NS_DELTA_LINK_REL);
+ next.setHref(entitySet.getDeltaLink().toASCIIString());
+
+ links(writer, Collections.<Link> singletonList(next));
+ }
+ }
+ }
+
+ private void entitySet(final Writer outWriter, final EntityCollection entitySet)
+ throws XMLStreamException, EdmPrimitiveTypeException {
+ final XMLStreamWriter writer = FACTORY.createXMLStreamWriter(outWriter);
+
+ startDocument(writer, Constants.ATOM_ELEM_FEED);
+
+ entitySet(writer, entitySet);
+
+ writer.writeEndElement();
+ writer.writeEndDocument();
+ writer.flush();
+ }
+
+ private void entitySet(final Writer outWriter, final ResWrap<EntityCollection> entitySet)
+ throws XMLStreamException, EdmPrimitiveTypeException {
+ final XMLStreamWriter writer = FACTORY.createXMLStreamWriter(outWriter);
+
+ startDocument(writer, Constants.ATOM_ELEM_FEED);
+
+ addContextInfo(writer, entitySet);
+
+ entitySet(writer, entitySet.getPayload());
+
+ writer.writeEndElement();
+ writer.writeEndDocument();
+ writer.flush();
+ }
+
+ private void link(final Writer outWriter, final Link link) throws XMLStreamException {
+ final XMLStreamWriter writer = FACTORY.createXMLStreamWriter(outWriter);
+
+ writer.writeStartDocument();
+
+ writer.writeStartElement(Constants.ELEM_LINKS);
+ writer.writeDefaultNamespace(namespaceData);
+
+ writer.writeStartElement(Constants.ELEM_URI);
+ writer.writeCharacters(link.getHref());
+ writer.writeEndElement();
+
+ writer.writeEndElement();
+
+ writer.writeEndDocument();
+ writer.flush();
+ }
+
+ @Override
+ public <T> void write(final Writer writer, final T obj) throws ODataSerializerException {
+ try {
+ if (obj instanceof EntityCollection) {
+ entitySet(writer, (EntityCollection) obj);
+ } else if (obj instanceof Entity) {
+ entity(writer, (Entity) obj);
+ } else if (obj instanceof Property) {
+ property(writer, (Property) obj);
+ } else if (obj instanceof Link) {
+ link(writer, (Link) obj);
+ }
+ } catch (final XMLStreamException e) {
+ throw new ODataSerializerException(e);
+ } catch (final EdmPrimitiveTypeException e) {
+ throw new ODataSerializerException(e);
+ }
+ }
+
+ private void reference(final Writer outWriter, final ResWrap<URI> container) throws XMLStreamException {
+ final XMLStreamWriter writer = FACTORY.createXMLStreamWriter(outWriter);
+
+ writer.writeStartDocument();
+
+ writer.writeStartElement(Constants.ATTR_METADATA, Constants.ATTR_REF);
+ writer.writeNamespace(Constants.ATTR_METADATA, Constants.NS_METADATA);
+ writer.writeAttribute(Constants.ATTR_METADATA, Constants.CONTEXT, container.getContextURL().toASCIIString());
+ writer.writeAttribute(Constants.ATOM_ATTR_ID, container.getPayload().toASCIIString());
+ writer.writeEndElement();
+
+ writer.writeEndDocument();
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public <T> void write(final Writer writer, final ResWrap<T> container) throws ODataSerializerException {
+ final T obj = container == null ? null : container.getPayload();
+
+ try {
+ if (obj instanceof EntityCollection) {
+ this.entitySet(writer, (ResWrap<EntityCollection>) container);
+ } else if (obj instanceof Entity) {
+ entity(writer, (ResWrap<Entity>) container);
+ } else if (obj instanceof Property) {
+ property(writer, (Property) obj);
+ } else if (obj instanceof Link) {
+ link(writer, (Link) obj);
+ } else if (obj instanceof URI) {
+ reference(writer, (ResWrap<URI>) container);
+ }
+ } catch (final XMLStreamException e) {
+ throw new ODataSerializerException(e);
+ } catch (final EdmPrimitiveTypeException e) {
+ throw new ODataSerializerException(e);
+ }
+ }
+
+ private <T> void addContextInfo(
+ final XMLStreamWriter writer, final ResWrap<T> container) throws XMLStreamException {
+
+ if (container.getContextURL() != null) {
+ final ContextURL contextURL = ContextURLParser.parse(container.getContextURL());
+ final URI base = contextURL.getServiceRoot();
+ if (container.getPayload() instanceof EntityCollection) {
+ ((EntityCollection) container.getPayload()).setBaseURI(base);
+ }
+ if (container.getPayload() instanceof Entity) {
+ ((Entity) container.getPayload()).setBaseURI(base);
+ }
+
+ writer.writeAttribute(namespaceMetadata, Constants.CONTEXT,
+ container.getContextURL().toASCIIString());
+ }
+
+ if (StringUtils.isNotBlank(container.getMetadataETag())) {
+ writer.writeAttribute(namespaceMetadata, Constants.ATOM_ATTR_METADATAETAG,
+ container.getMetadataETag());
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/20351253/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/ClientODataDeserializerImpl.java
----------------------------------------------------------------------
diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/ClientODataDeserializerImpl.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/ClientODataDeserializerImpl.java
index 92bd57b..89dea37 100644
--- a/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/ClientODataDeserializerImpl.java
+++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/ClientODataDeserializerImpl.java
@@ -26,6 +26,8 @@ import javax.xml.stream.XMLStreamException;
import org.apache.olingo.client.api.data.ServiceDocument;
import org.apache.olingo.client.api.edm.xml.XMLMetadata;
import org.apache.olingo.client.api.serialization.ClientODataDeserializer;
+import org.apache.olingo.client.api.serialization.ODataDeserializer;
+import org.apache.olingo.client.api.serialization.ODataDeserializerException;
import org.apache.olingo.client.core.data.JSONServiceDocumentDeserializer;
import org.apache.olingo.client.core.data.XMLServiceDocumentDeserializer;
import org.apache.olingo.client.core.edm.xml.ClientCsdlEdmx;
@@ -38,11 +40,6 @@ import org.apache.olingo.commons.api.data.ResWrap;
import org.apache.olingo.commons.api.ODataError;
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException;
import org.apache.olingo.commons.api.format.ODataFormat;
-import org.apache.olingo.commons.api.serialization.ODataDeserializer;
-import org.apache.olingo.commons.api.serialization.ODataDeserializerException;
-import org.apache.olingo.commons.core.serialization.AtomDeserializer;
-import org.apache.olingo.commons.core.serialization.JsonDeltaDeserializer;
-import org.apache.olingo.commons.core.serialization.JsonDeserializer;
import com.fasterxml.aalto.stax.InputFactoryImpl;
import com.fasterxml.aalto.stax.OutputFactoryImpl;
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/20351253/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/ContextURLParser.java
----------------------------------------------------------------------
diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/ContextURLParser.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/ContextURLParser.java
new file mode 100644
index 0000000..873ca4c
--- /dev/null
+++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/ContextURLParser.java
@@ -0,0 +1,118 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.olingo.client.core.serialization;
+
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.olingo.commons.api.Constants;
+import org.apache.olingo.commons.api.data.ContextURL;
+import org.apache.olingo.commons.api.data.ContextURL.Suffix;
+
+public class ContextURLParser {
+
+ public static ContextURL parse(final URI contextURL) {
+ if (contextURL == null) {
+ return null;
+ }
+
+ final ContextURL.Builder contextUrl = ContextURL.with();
+
+ String contextURLasString = contextURL.toASCIIString();
+
+ boolean isEntity = false;
+ if (contextURLasString.endsWith("/$entity") || contextURLasString.endsWith("/@Element")) {
+ isEntity = true;
+ contextUrl.suffix(Suffix.ENTITY);
+ contextURLasString = contextURLasString.replace("/$entity", StringUtils.EMPTY).
+ replace("/@Element", StringUtils.EMPTY);
+ } else if (contextURLasString.endsWith("/$ref")) {
+ contextUrl.suffix(Suffix.REFERENCE);
+ contextURLasString = contextURLasString.replace("/$ref", StringUtils.EMPTY);
+ } else if (contextURLasString.endsWith("/$delta")) {
+ contextUrl.suffix(Suffix.DELTA);
+ contextURLasString = contextURLasString.replace("/$delta", StringUtils.EMPTY);
+ } else if (contextURLasString.endsWith("/$deletedEntity")) {
+ contextUrl.suffix(Suffix.DELTA_DELETED_ENTITY);
+ contextURLasString = contextURLasString.replace("/$deletedEntity", StringUtils.EMPTY);
+ } else if (contextURLasString.endsWith("/$link")) {
+ contextUrl.suffix(Suffix.DELTA_LINK);
+ contextURLasString = contextURLasString.replace("/$link", StringUtils.EMPTY);
+ } else if (contextURLasString.endsWith("/$deletedLink")) {
+ contextUrl.suffix(Suffix.DELTA_DELETED_LINK);
+ contextURLasString = contextURLasString.replace("/$deletedLink", StringUtils.EMPTY);
+ }
+
+ contextUrl.serviceRoot(URI.create(StringUtils.substringBefore(contextURLasString, Constants.METADATA)));
+
+ final String rest = StringUtils.substringAfter(contextURLasString, Constants.METADATA + "#");
+
+ String firstToken;
+ String entitySetOrSingletonOrType;
+ if (rest.startsWith("Collection(")) {
+ firstToken = rest.substring(0, rest.indexOf(')') + 1);
+ entitySetOrSingletonOrType = firstToken;
+ } else {
+ final int openParIdx = rest.indexOf('(');
+ if (openParIdx == -1) {
+ firstToken = StringUtils.substringBeforeLast(rest, "/");
+
+ entitySetOrSingletonOrType = firstToken;
+ } else {
+ firstToken = isEntity ? rest : StringUtils.substringBeforeLast(rest, ")") + ")";
+
+ final List<String> parts = new ArrayList<String>();
+ for (String split : firstToken.split("\\)/")) {
+ parts.add(split.replaceAll("\\(.*", ""));
+ }
+ entitySetOrSingletonOrType = StringUtils.join(parts, '/');
+ final int commaIdx = firstToken.indexOf(',');
+ if (commaIdx != -1) {
+ contextUrl.selectList(firstToken.substring(openParIdx + 1, firstToken.length() - 1));
+ }
+ }
+ }
+ contextUrl.entitySetOrSingletonOrType(entitySetOrSingletonOrType);
+
+ final int slashIdx = entitySetOrSingletonOrType.lastIndexOf('/');
+ if (slashIdx != -1 && entitySetOrSingletonOrType.substring(slashIdx + 1).indexOf('.') != -1) {
+ contextUrl.entitySetOrSingletonOrType(entitySetOrSingletonOrType.substring(0, slashIdx));
+ contextUrl.derivedEntity(entitySetOrSingletonOrType.substring(slashIdx + 1));
+ }
+
+ if (!firstToken.equals(rest)) {
+ final String[] pathElems = StringUtils.substringAfter(rest, "/").split("/");
+ if (pathElems.length > 0 && pathElems[0].length() > 0) {
+ if (pathElems[0].indexOf('.') == -1) {
+ contextUrl.navOrPropertyPath(pathElems[0]);
+ } else {
+ contextUrl.derivedEntity(pathElems[0]);
+ }
+
+ if (pathElems.length > 1) {
+ contextUrl.navOrPropertyPath(pathElems[1]);
+ }
+ }
+ }
+
+ return contextUrl.build();
+ }
+}