You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by mi...@apache.org on 2014/06/12 13:01:30 UTC
[3/9] [OLINGO-317] Rename and move of some packages and classes
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/b15439ff/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/AtomDeserializer.java
----------------------------------------------------------------------
diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/AtomDeserializer.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/AtomDeserializer.java
new file mode 100644
index 0000000..cce71c8
--- /dev/null
+++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/AtomDeserializer.java
@@ -0,0 +1,897 @@
+/*
+ * 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.commons.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.commons.api.Constants;
+import org.apache.olingo.commons.api.data.Annotation;
+import org.apache.olingo.commons.api.data.CollectionValue;
+import org.apache.olingo.commons.api.data.DeletedEntity.Reason;
+import org.apache.olingo.commons.api.data.Delta;
+import org.apache.olingo.commons.api.data.Entity;
+import org.apache.olingo.commons.api.data.EntitySet;
+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.Value;
+import org.apache.olingo.commons.api.data.v3.LinkCollection;
+import org.apache.olingo.commons.api.domain.ODataError;
+import org.apache.olingo.commons.api.domain.ODataOperation;
+import org.apache.olingo.commons.api.domain.ODataPropertyType;
+import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
+import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion;
+import org.apache.olingo.commons.api.format.ContentType;
+import org.apache.olingo.commons.api.serialization.ODataDeserializer;
+import org.apache.olingo.commons.api.serialization.ODataDeserializerException;
+import org.apache.olingo.commons.core.data.AbstractODataObject;
+import org.apache.olingo.commons.core.data.AnnotationImpl;
+import org.apache.olingo.commons.core.data.CollectionValueImpl;
+import org.apache.olingo.commons.core.data.ComplexValueImpl;
+import org.apache.olingo.commons.core.data.DeletedEntityImpl;
+import org.apache.olingo.commons.core.data.DeltaLinkImpl;
+import org.apache.olingo.commons.core.data.EntityImpl;
+import org.apache.olingo.commons.core.data.EntitySetImpl;
+import org.apache.olingo.commons.core.data.EnumValueImpl;
+import org.apache.olingo.commons.core.data.GeospatialValueImpl;
+import org.apache.olingo.commons.core.data.LinkImpl;
+import org.apache.olingo.commons.core.data.LinkedComplexValueImpl;
+import org.apache.olingo.commons.core.data.NullValueImpl;
+import org.apache.olingo.commons.core.data.ODataErrorImpl;
+import org.apache.olingo.commons.core.data.PrimitiveValueImpl;
+import org.apache.olingo.commons.core.data.PropertyImpl;
+import org.apache.olingo.commons.core.data.v3.LinkCollectionImpl;
+import org.apache.olingo.commons.core.data.v4.DeltaImpl;
+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(final ODataServiceVersion version) {
+ super(version);
+ this.geoDeserializer = new AtomGeoValueDeserializer();
+ }
+
+ private Value fromPrimitive(final XMLEventReader reader, final StartElement start,
+ final EdmTypeInfo typeInfo) throws XMLStreamException {
+
+ Value 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(
+ version, typeInfo.getFullQualifiedName().toString());
+ value = new GeospatialValueImpl(this.geoDeserializer.deserialize(reader, event.asStartElement(), geoType));
+ }
+
+ if (event.isCharacters() && !event.asCharacters().isWhiteSpace()
+ && (typeInfo == null || !typeInfo.getPrimitiveTypeKind().isGeospatial())) {
+
+ value = new PrimitiveValueImpl(event.asCharacters().getData());
+ }
+
+ if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) {
+ foundEndProperty = true;
+ }
+ }
+
+ return value == null ? new PrimitiveValueImpl(StringUtils.EMPTY) : value;
+ }
+
+ private Value fromComplexOrEnum(final XMLEventReader reader, final StartElement start)
+ throws XMLStreamException {
+
+ Value value = null;
+
+ boolean foundEndProperty = false;
+ while (reader.hasNext() && !foundEndProperty) {
+ final XMLEvent event = reader.nextEvent();
+
+ if (event.isStartElement()) {
+ if (value == null) {
+ value = version.compareTo(ODataServiceVersion.V40) < 0
+ ? new ComplexValueImpl()
+ : new LinkedComplexValueImpl();
+ }
+
+ if (Constants.QNAME_ATOM_ELEM_LINK.equals(event.asStartElement().getName())) {
+ final LinkImpl link = new LinkImpl();
+ 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(
+ version.getNamespaceMap().get(ODataServiceVersion.NAVIGATION_LINK_REL))) {
+
+ value.asLinkedComplex().getNavigationLinks().add(link);
+ inline(reader, event.asStartElement(), link);
+ } else if (link.getRel().startsWith(
+ version.getNamespaceMap().get(ODataServiceVersion.ASSOCIATION_LINK_REL))) {
+
+ value.asLinkedComplex().getAssociationLinks().add(link);
+ }
+ } else {
+ value.asComplex().get().add(property(reader, event.asStartElement()));
+ }
+ }
+
+ if (event.isCharacters() && !event.asCharacters().isWhiteSpace()) {
+ value = new EnumValueImpl(event.asCharacters().getData());
+ }
+
+ if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) {
+ foundEndProperty = true;
+ }
+ }
+
+ return value;
+ }
+
+ private CollectionValue fromCollection(final XMLEventReader reader, final StartElement start,
+ final EdmTypeInfo typeInfo) throws XMLStreamException {
+
+ final CollectionValueImpl value = new CollectionValueImpl();
+
+ 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:
+ case ENUM:
+ value.get().add(fromComplexOrEnum(reader, event.asStartElement()));
+ break;
+
+ case PRIMITIVE:
+ value.get().add(fromPrimitive(reader, event.asStartElement(), type));
+ break;
+
+ default:
+ // do not add null or empty values
+ }
+ }
+
+ if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) {
+ foundEndProperty = true;
+ }
+ }
+
+ return value;
+ }
+
+ 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 {
+
+ final PropertyImpl property = new PropertyImpl();
+
+ if (ODataServiceVersion.V40 == version && 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 {
+
+ final Attribute nullAttr = start.getAttributeByName(this.nullQName);
+
+ Value value;
+ if (nullAttr == null) {
+ final Attribute typeAttr = start.getAttributeByName(this.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;
+
+ switch (propType) {
+ case COLLECTION:
+ value = fromCollection(reader, start, typeInfo);
+ break;
+
+ case COMPLEX:
+ value = fromComplexOrEnum(reader, start);
+ break;
+
+ case PRIMITIVE:
+ // No type specified? Defaults to Edm.String
+ if (typeInfo == null) {
+ valuable.setType(EdmPrimitiveTypeKind.String.getFullQualifiedName().toString());
+ }
+ value = fromPrimitive(reader, start, typeInfo);
+ break;
+
+ case EMPTY:
+ default:
+ value = new PrimitiveValueImpl(StringUtils.EMPTY);
+ }
+ } else {
+ value = new NullValueImpl();
+ }
+
+ valuable.setValue(value);
+ }
+
+ @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);
+ }
+ }
+
+ 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 LinkImpl link)
+ throws XMLStreamException {
+
+ boolean foundEndElement = false;
+ while (reader.hasNext() && !foundEndElement) {
+ final XMLEvent event = reader.nextEvent();
+
+ if (event.isStartElement()) {
+ if (inlineQName.equals(event.asStartElement().getName())) {
+ StartElement inline = null;
+ while (reader.hasNext() && inline == null) {
+ final XMLEvent innerEvent = reader.peek();
+ if (innerEvent.isCharacters() && innerEvent.asCharacters().isWhiteSpace()) {
+ reader.nextEvent();
+ } else if (innerEvent.isStartElement()) {
+ inline = innerEvent.asStartElement();
+ }
+ }
+ 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;
+ }
+ }
+ }
+
+ public ResWrap<Delta> delta(final InputStream input) throws XMLStreamException {
+ 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 {
+ if (!Constants.QNAME_ATOM_ELEM_FEED.equals(start.getName())) {
+ return null;
+ }
+ final DeltaImpl delta = new DeltaImpl();
+ final Attribute xmlBase = start.getAttributeByName(Constants.QNAME_ATTR_XML_BASE);
+ if (xmlBase != null) {
+ delta.setBaseURI(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.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 DeletedEntityImpl deletedEntity = new DeletedEntityImpl();
+
+ 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 DeltaLinkImpl link = new DeltaLinkImpl();
+
+ 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;
+ }
+
+ public ResWrap<LinkCollection> linkCollection(final InputStream input) throws XMLStreamException {
+ final XMLEventReader reader = getReader(input);
+ final StartElement start = skipBeforeFirstStartElement(reader);
+ return getContainer(start, linkCollection(reader));
+ }
+
+ private LinkCollection linkCollection(final XMLEventReader reader) throws XMLStreamException {
+
+ final LinkCollectionImpl linkCollection = new LinkCollectionImpl();
+
+ boolean isURI = false;
+ boolean isNext = false;
+ while (reader.hasNext()) {
+ final XMLEvent event = reader.nextEvent();
+ if (event.isStartElement()) {
+ isURI = uriQName.equals(event.asStartElement().getName());
+ isNext = nextQName.equals(event.asStartElement().getName());
+ }
+
+ if (event.isCharacters() && !event.asCharacters().isWhiteSpace()) {
+ if (isURI) {
+ linkCollection.getLinks().add(URI.create(event.asCharacters().getData()));
+ isURI = false;
+ } else if (isNext) {
+ linkCollection.setNext(URI.create(event.asCharacters().getData()));
+ isNext = false;
+ }
+ }
+ }
+
+ return linkCollection;
+ }
+
+ private void properties(final XMLEventReader reader, final StartElement start, final EntityImpl entity)
+ throws XMLStreamException {
+
+ 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 {
+
+ final Annotation annotation = new AnnotationImpl();
+
+ annotation.setTerm(start.getAttributeByName(QName.valueOf(Constants.ATOM_ATTR_TERM)).getValue());
+ valuable(annotation, reader, start);
+
+ return annotation;
+ }
+
+ private EntityImpl entityRef(final StartElement start) throws XMLStreamException {
+ final EntityImpl entity = new EntityImpl();
+
+ 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 {
+ final EntityImpl entity;
+ if (entryRefQName.equals(start.getName())) {
+ entity = entityRef(start);
+ } else if (Constants.QNAME_ATOM_ELEM_ENTRY.equals(start.getName())) {
+ entity = new EntityImpl();
+ final Attribute xmlBase = start.getAttributeByName(Constants.QNAME_ATTR_XML_BASE);
+ if (xmlBase != null) {
+ entity.setBaseURI(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 LinkImpl link = new LinkImpl();
+ 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(
+ version.getNamespaceMap().get(ODataServiceVersion.NAVIGATION_LINK_REL))) {
+
+ entity.getNavigationLinks().add(link);
+ inline(reader, event.asStartElement(), link);
+ } else if (link.getRel().startsWith(
+ version.getNamespaceMap().get(ODataServiceVersion.ASSOCIATION_LINK_REL))) {
+
+ entity.getAssociationLinks().add(link);
+ } else if (link.getRel().startsWith(
+ version.getNamespaceMap().get(ODataServiceVersion.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 ODataOperation operation = new ODataOperation();
+ 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.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);
+ return getContainer(start, entity(reader, start));
+ } catch (XMLStreamException e) {
+ throw new ODataDeserializerException(e);
+ }
+ }
+
+ private void count(final XMLEventReader reader, final StartElement start, final EntitySet 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 EntitySet entitySet(final XMLEventReader reader, final StartElement start) throws XMLStreamException {
+ if (!Constants.QNAME_ATOM_ELEM_FEED.equals(start.getName())) {
+ return null;
+ }
+ final EntitySetImpl entitySet = new EntitySetImpl();
+ final Attribute xmlBase = start.getAttributeByName(Constants.QNAME_ATTR_XML_BASE);
+ if (xmlBase != null) {
+ entitySet.setBaseURI(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.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<EntitySet> 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);
+ }
+ }
+
+ private ODataError error(final XMLEventReader reader, final StartElement start) throws XMLStreamException {
+ final ODataErrorImpl error = new ODataErrorImpl();
+
+ 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/b15439ff/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/AtomGeoValueDeserializer.java
----------------------------------------------------------------------
diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/AtomGeoValueDeserializer.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/AtomGeoValueDeserializer.java
new file mode 100644
index 0000000..0136406
--- /dev/null
+++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/AtomGeoValueDeserializer.java
@@ -0,0 +1,267 @@
+/*
+ * 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.commons.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/b15439ff/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/AtomGeoValueSerializer.java
----------------------------------------------------------------------
diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/AtomGeoValueSerializer.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/AtomGeoValueSerializer.java
new file mode 100644
index 0000000..8f913a0
--- /dev/null
+++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/AtomGeoValueSerializer.java
@@ -0,0 +1,221 @@
+/*
+ * 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.commons.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 (final Iterator<Geospatial> itor = ((GeospatialCollection) value).iterator(); itor.hasNext();) {
+ serialize(writer, itor.next());
+ }
+ writer.writeEndElement();
+ }
+
+ writer.writeEndElement();
+ break;
+
+ default:
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/b15439ff/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/AtomSerializer.java
----------------------------------------------------------------------
diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/AtomSerializer.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/AtomSerializer.java
new file mode 100644
index 0000000..fb2a041
--- /dev/null
+++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/AtomSerializer.java
@@ -0,0 +1,544 @@
+/*
+ * 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.commons.core.serialization;
+
+import java.io.Writer;
+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.commons.api.Constants;
+import org.apache.olingo.commons.api.data.Annotation;
+import org.apache.olingo.commons.api.data.CollectionValue;
+import org.apache.olingo.commons.api.data.Entity;
+import org.apache.olingo.commons.api.data.EntitySet;
+import org.apache.olingo.commons.api.data.Link;
+import org.apache.olingo.commons.api.data.Property;
+import org.apache.olingo.commons.api.data.ResWrap;
+import org.apache.olingo.commons.api.data.Value;
+import org.apache.olingo.commons.api.domain.ODataOperation;
+import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
+import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion;
+import org.apache.olingo.commons.api.format.ContentType;
+import org.apache.olingo.commons.api.serialization.ODataSerializer;
+import org.apache.olingo.commons.api.serialization.ODataSerializerException;
+import org.apache.olingo.commons.core.data.AbstractODataObject;
+import org.apache.olingo.commons.core.data.EntityImpl;
+import org.apache.olingo.commons.core.data.EntitySetImpl;
+import org.apache.olingo.commons.core.data.LinkImpl;
+import org.apache.olingo.commons.core.edm.EdmTypeInfo;
+
+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(final ODataServiceVersion version) {
+ this(version, false);
+ }
+
+ public AtomSerializer(final ODataServiceVersion version, final boolean serverMode) {
+ super(version);
+ this.geoSerializer = new AtomGeoValueSerializer();
+ this.serverMode = serverMode;
+ }
+
+ private void collection(final XMLStreamWriter writer, final CollectionValue value) throws XMLStreamException {
+ for (Value item : value.get()) {
+ if (version.compareTo(ODataServiceVersion.V40) < 0) {
+ writer.writeStartElement(Constants.PREFIX_DATASERVICES, Constants.ELEM_ELEMENT,
+ version.getNamespaceMap().get(ODataServiceVersion.NS_DATASERVICES));
+ } else {
+ writer.writeStartElement(Constants.PREFIX_METADATA, Constants.ELEM_ELEMENT,
+ version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA));
+ }
+ value(writer, item);
+ writer.writeEndElement();
+ }
+ }
+
+ private void value(final XMLStreamWriter writer, final Value value) throws XMLStreamException {
+ if (value.isPrimitive()) {
+ writer.writeCharacters(value.asPrimitive().get());
+ } else if (value.isEnum()) {
+ writer.writeCharacters(value.asEnum().get());
+ } else if (value.isGeospatial()) {
+ this.geoSerializer.serialize(writer, value.asGeospatial().get());
+ } else if (value.isCollection()) {
+ collection(writer, value.asCollection());
+ } else if (value.isComplex()) {
+ for (Property property : value.asComplex().get()) {
+ property(writer, property, false);
+ }
+ }
+ }
+
+ public void property(final XMLStreamWriter writer, final Property property, final boolean standalone)
+ throws XMLStreamException {
+
+ if (version.compareTo(ODataServiceVersion.V40) >= 0 && standalone) {
+ writer.writeStartElement(Constants.PREFIX_METADATA, Constants.VALUE,
+ version.getNamespaceMap().get(ODataServiceVersion.NS_DATASERVICES));
+ } else {
+ writer.writeStartElement(Constants.PREFIX_DATASERVICES, property.getName(),
+ version.getNamespaceMap().get(ODataServiceVersion.NS_DATASERVICES));
+ }
+
+ if (standalone) {
+ namespaces(writer);
+ }
+
+ if (StringUtils.isNotBlank(property.getType())) {
+ final EdmTypeInfo typeInfo = new EdmTypeInfo.Builder().setTypeExpression(property.getType()).build();
+ if (!EdmPrimitiveTypeKind.String.getFullQualifiedName().toString().equals(typeInfo.internal())) {
+ writer.writeAttribute(Constants.PREFIX_METADATA, version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA),
+ Constants.ATTR_TYPE, typeInfo.external(version));
+ }
+ }
+
+ if (property.getValue().isNull()) {
+ writer.writeAttribute(Constants.PREFIX_METADATA, version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA),
+ Constants.ATTR_NULL, Boolean.TRUE.toString());
+ } else {
+ value(writer, property.getValue());
+ if (property.getValue().isLinkedComplex()) {
+ links(writer, property.getValue().asLinkedComplex().getAssociationLinks());
+ links(writer, property.getValue().asLinkedComplex().getNavigationLinks());
+ }
+ }
+
+ writer.writeEndElement();
+
+ for (Annotation annotation : property.getAnnotations()) {
+ annotation(writer, annotation, property.getName());
+ }
+ }
+
+ private void property(final XMLStreamWriter writer, final Property property) throws XMLStreamException {
+ 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 {
+ 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 {
+ 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,
+ version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA));
+
+ 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 {
+ for (Property property : properties) {
+ property(writer, property, false);
+ }
+ }
+
+ private void annotation(final XMLStreamWriter writer, final Annotation annotation, final String target)
+ throws XMLStreamException {
+
+ writer.writeStartElement(Constants.PREFIX_METADATA, Constants.ANNOTATION,
+ version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA));
+
+ writer.writeAttribute(Constants.ATOM_ATTR_TERM, annotation.getTerm());
+
+ if (target != null) {
+ writer.writeAttribute(Constants.ATTR_TARGET, target);
+ }
+
+ if (StringUtils.isNotBlank(annotation.getType())) {
+ final EdmTypeInfo typeInfo = new EdmTypeInfo.Builder().setTypeExpression(annotation.getType()).build();
+ if (!EdmPrimitiveTypeKind.String.getFullQualifiedName().toString().equals(typeInfo.internal())) {
+ writer.writeAttribute(Constants.PREFIX_METADATA, version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA),
+ Constants.ATTR_TYPE, typeInfo.external(version));
+ }
+ }
+
+ if (annotation.getValue().isNull()) {
+ writer.writeAttribute(Constants.PREFIX_METADATA, version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA),
+ Constants.ATTR_NULL, Boolean.TRUE.toString());
+ } else {
+ value(writer, annotation.getValue());
+ }
+
+ writer.writeEndElement();
+ }
+
+ private void entity(final XMLStreamWriter writer, final Entity entity) throws XMLStreamException {
+ 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(
+ version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA),
+ 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, version.getNamespaceMap().get(ODataServiceVersion.NS_SCHEME));
+ if (StringUtils.isNotBlank(entity.getType())) {
+ writer.writeAttribute(Constants.ATOM_ATTR_TERM,
+ new EdmTypeInfo.Builder().setTypeExpression(entity.getType()).build().external(version));
+ }
+ writer.writeEndElement();
+
+ if (entity instanceof AbstractODataObject) {
+ common(writer, (AbstractODataObject) 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 (ODataOperation operation : entity.getOperations()) {
+ writer.writeStartElement(
+ version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA), 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(version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA), Constants.PROPERTIES);
+ properties(writer, entity.getProperties());
+ } else {
+ writer.writeAttribute(Constants.ATTR_TYPE, ContentType.APPLICATION_XML);
+ writer.writeStartElement(version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA), 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, version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA));
+ 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, version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA));
+ addContextInfo(writer, container);
+ writer.writeAttribute(Constants.ATOM_ATTR_ID, container.getPayload().getId().toASCIIString());
+ }
+
+ private void entity(final Writer outWriter, final Entity entity) throws XMLStreamException {
+ final XMLStreamWriter writer = FACTORY.createXMLStreamWriter(outWriter);
+
+ if (entity.getType() == null && entity.getProperties().isEmpty()) {
+ writer.writeStartDocument();
+ writer.setDefaultNamespace(version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA));
+
+ 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 {
+ final Entity entity = container.getPayload();
+
+ final XMLStreamWriter writer = FACTORY.createXMLStreamWriter(outWriter);
+
+ if (entity.getType() == null && entity.getProperties().isEmpty()) {
+ writer.writeStartDocument();
+ writer.setDefaultNamespace(version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA));
+
+ 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 EntitySet entitySet) throws XMLStreamException {
+ if (entitySet.getBaseURI() != null) {
+ writer.writeAttribute(XMLConstants.XML_NS_URI, Constants.ATTR_XML_BASE, entitySet.getBaseURI().toASCIIString());
+ }
+
+ if (entitySet.getCount() != null) {
+ writer.writeStartElement(
+ version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA), 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();
+ }
+
+ if (entitySet instanceof AbstractODataObject) {
+ common(writer, (AbstractODataObject) 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 LinkImpl next = new LinkImpl();
+ next.setRel(Constants.NEXT_LINK_REL);
+ next.setHref(entitySet.getNext().toASCIIString());
+
+ links(writer, Collections.<Link> singletonList(next));
+ }
+ if (entitySet.getDeltaLink() != null) {
+ final LinkImpl next = new LinkImpl();
+ next.setRel(Constants.DELTA_LINK_REL);
+ next.setHref(entitySet.getDeltaLink().toASCIIString());
+
+ links(writer, Collections.<Link> singletonList(next));
+ }
+ }
+ }
+
+ private void entitySet(final Writer outWriter, final EntitySet entitySet) throws XMLStreamException {
+ 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<EntitySet> entitySet) throws XMLStreamException {
+ 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(version.getNamespaceMap().get(ODataServiceVersion.NS_DATASERVICES));
+
+ writer.writeStartElement(Constants.ELEM_URI);
+ writer.writeCharacters(link.getHref());
+ writer.writeEndElement();
+
+ writer.writeEndElement();
+
+ writer.writeEndDocument();
+ writer.flush();
+ }
+
+ public <T> void write(final Writer writer, final T obj) throws ODataSerializerException {
+ try {
+ if (obj instanceof EntitySet) {
+ entitySet(writer, (EntitySet) 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);
+ }
+ }
+
+ @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 EntitySet) {
+ this.entitySet(writer, (ResWrap<EntitySet>) 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);
+ }
+ } catch (final XMLStreamException e) {
+ throw new ODataSerializerException(e);
+ }
+ }
+
+ private <T> void addContextInfo(
+ final XMLStreamWriter writer, final ResWrap<T> container) throws XMLStreamException {
+
+ if (container.getContextURL() != null) {
+ String base = container.getContextURL().getServiceRoot().toASCIIString();
+ if (container.getPayload() instanceof EntitySet) {
+ ((EntitySetImpl) container.getPayload()).setBaseURI(base);
+ }
+ if (container.getPayload() instanceof Entity) {
+ ((EntityImpl) container.getPayload()).setBaseURI(base);
+ }
+
+ writer.writeAttribute(
+ version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA),
+ Constants.CONTEXT,
+ container.getContextURL().getURI().toASCIIString());
+ }
+
+ if (StringUtils.isNotBlank(container.getMetadataETag())) {
+ writer.writeAttribute(
+ version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA),
+ Constants.ATOM_ATTR_METADATAETAG,
+ container.getMetadataETag());
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/b15439ff/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonDeltaDeserializer.java
----------------------------------------------------------------------
diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonDeltaDeserializer.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonDeltaDeserializer.java
new file mode 100644
index 0000000..c6df8f3
--- /dev/null
+++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonDeltaDeserializer.java
@@ -0,0 +1,102 @@
+/*
+ * 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.commons.core.serialization;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.util.Iterator;
+
+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.Delta;
+import org.apache.olingo.commons.api.data.ResWrap;
+import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion;
+import org.apache.olingo.commons.api.serialization.ODataDeserializerException;
+import org.apache.olingo.commons.core.data.DeletedEntityImpl;
+import org.apache.olingo.commons.core.data.DeltaLinkImpl;
+import org.apache.olingo.commons.core.data.v4.DeltaImpl;
+
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+public class JsonDeltaDeserializer extends JsonDeserializer {
+
+ public JsonDeltaDeserializer(final ODataServiceVersion version, final boolean serverMode) {
+ super(version, serverMode);
+ }
+
+ protected ResWrap<Delta> doDeserialize(final JsonParser parser) throws IOException {
+
+ final ObjectNode tree = parser.getCodec().readTree(parser);
+
+ final DeltaImpl delta = new DeltaImpl();
+
+ final URI contextURL = tree.hasNonNull(Constants.JSON_CONTEXT) ?
+ URI.create(tree.get(Constants.JSON_CONTEXT).textValue()) : null;
+ if (contextURL != null) {
+ delta.setBaseURI(StringUtils.substringBefore(contextURL.toASCIIString(), Constants.METADATA));
+ }
+
+ if (tree.hasNonNull(jsonCount)) {
+ delta.setCount(tree.get(jsonCount).asInt());
+ }
+ if (tree.hasNonNull(jsonNextLink)) {
+ delta.setNext(URI.create(tree.get(jsonNextLink).textValue()));
+ }
+ if (tree.hasNonNull(jsonDeltaLink)) {
+ delta.setDeltaLink(URI.create(tree.get(jsonDeltaLink).textValue()));
+ }
+
+ if (tree.hasNonNull(Constants.VALUE)) {
+ JsonEntityDeserializer entityDeserializer = new JsonEntityDeserializer(version, serverMode);
+ for (final Iterator<JsonNode> itor = tree.get(Constants.VALUE).iterator(); itor.hasNext();) {
+ final ObjectNode item = (ObjectNode) itor.next();
+ final ContextURL itemContextURL = item.hasNonNull(Constants.JSON_CONTEXT)
+ ? ContextURL.getInstance(URI.create(item.get(Constants.JSON_CONTEXT).textValue())) : null;
+ item.remove(Constants.JSON_CONTEXT);
+
+ if (itemContextURL == null || itemContextURL.isEntity()) {
+ delta.getEntities().add(entityDeserializer.doDeserialize(item.traverse(parser.getCodec())).getPayload());
+ } else if (itemContextURL.isDeltaDeletedEntity()) {
+ delta.getDeletedEntities().add(parser.getCodec().treeToValue(item, DeletedEntityImpl.class));
+ } else if (itemContextURL.isDeltaLink()) {
+ delta.getAddedLinks().add(parser.getCodec().treeToValue(item, DeltaLinkImpl.class));
+ } else if (itemContextURL.isDeltaDeletedLink()) {
+ delta.getDeletedLinks().add(parser.getCodec().treeToValue(item, DeltaLinkImpl.class));
+ }
+ }
+ }
+
+ return new ResWrap<Delta>(contextURL, null, delta);
+ }
+
+ public ResWrap<Delta> toDelta(InputStream input) throws ODataDeserializerException {
+ try {
+ JsonParser parser = new JsonFactory(new ObjectMapper()).createParser(input);
+ return doDeserialize(parser);
+ } catch (final IOException e) {
+ throw new ODataDeserializerException(e);
+ }
+ }
+}