You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by il...@apache.org on 2014/03/11 13:53:09 UTC
[07/11] [OLINGO-200] V3 (de)serializers + unit tests merged
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata4/blob/78c3eaa4/lib/client-core/src/main/java/org/apache/olingo/client/core/deserializer/StructuralPropertyImpl.java
----------------------------------------------------------------------
diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/deserializer/StructuralPropertyImpl.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/deserializer/StructuralPropertyImpl.java
deleted file mode 100644
index 59ba264..0000000
--- a/lib/client-core/src/main/java/org/apache/olingo/client/core/deserializer/StructuralPropertyImpl.java
+++ /dev/null
@@ -1,83 +0,0 @@
-/*
- * 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.deserializer;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-
-import org.apache.olingo.client.api.deserializer.StructuralProperty;
-import org.apache.olingo.client.api.deserializer.Value;
-
-public class StructuralPropertyImpl implements StructuralProperty {
-
- private final List<Value> values;
-
- private final String name;
-
- private final boolean containsCollection;
-
- public StructuralPropertyImpl(final String name, final Value value) {
- this(name, false, value);
- }
-
- public StructuralPropertyImpl(final String name, final List<Value> values) {
- // XXX: ugly -> refactor
- this(name, true, values.toArray(new Value[0]));
- }
-
- public StructuralPropertyImpl(final String name, final boolean asCollection, final Value... value) {
- if (value == null || value.length == 0) {
- throw new IllegalArgumentException("Missing or NULL value argument.");
- }
-
- containsCollection = asCollection;
- this.name = name;
- values = new ArrayList<Value>(value.length);
- for (Value v : value) {
- values.add(v);
- }
- }
-
- @Override
- public Value getValue() {
- return values.get(0);
- }
-
- @Override
- public List<Value> getValues() {
- return Collections.unmodifiableList(values);
- }
-
- @Override
- public String getName() {
- return name;
- }
-
- @Override
- public boolean containsCollection() {
- return containsCollection;
- }
-
- @Override
- public String toString() {
- return "StructuralPropertyImpl [name=" + name + ", containsCollection=" + containsCollection
- + ", values=" + values + "]";
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata4/blob/78c3eaa4/lib/client-core/src/main/java/org/apache/olingo/client/core/op/impl/AbstractODataBinder.java
----------------------------------------------------------------------
diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/op/impl/AbstractODataBinder.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/op/impl/AbstractODataBinder.java
index d6ade11..54ae185 100644
--- a/lib/client-core/src/main/java/org/apache/olingo/client/core/op/impl/AbstractODataBinder.java
+++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/op/impl/AbstractODataBinder.java
@@ -18,23 +18,48 @@
*/
package org.apache.olingo.client.core.op.impl;
+import java.io.StringWriter;
+import java.net.URI;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.commons.lang3.StringUtils;
import org.apache.olingo.client.api.ODataClient;
import org.apache.olingo.client.api.ODataConstants;
+import org.apache.olingo.client.api.data.Entry;
+import org.apache.olingo.client.api.data.Feed;
+import org.apache.olingo.client.api.data.Link;
+import org.apache.olingo.client.api.data.LinkCollection;
+import org.apache.olingo.client.api.data.Operation;
import org.apache.olingo.client.api.data.ServiceDocument;
import org.apache.olingo.client.api.data.ServiceDocumentItem;
+import org.apache.olingo.client.api.domain.ODataCollectionValue;
+import org.apache.olingo.client.api.domain.ODataComplexValue;
+import org.apache.olingo.client.api.domain.ODataEntity;
+import org.apache.olingo.client.api.domain.ODataEntitySet;
+import org.apache.olingo.client.api.domain.ODataGeospatialValue;
+import org.apache.olingo.client.api.domain.ODataInlineEntity;
+import org.apache.olingo.client.api.domain.ODataInlineEntitySet;
+import org.apache.olingo.client.api.domain.ODataJClientEdmType;
+import org.apache.olingo.client.api.domain.ODataLink;
+import org.apache.olingo.client.api.domain.ODataLinkCollection;
+import org.apache.olingo.client.api.domain.ODataPrimitiveValue;
+import org.apache.olingo.client.api.domain.ODataProperty;
+import org.apache.olingo.client.api.domain.ODataProperty.PropertyType;
import org.apache.olingo.client.api.domain.ODataServiceDocument;
+import org.apache.olingo.client.api.domain.ODataValue;
+import org.apache.olingo.client.api.format.ODataPubFormat;
import org.apache.olingo.client.api.op.ODataBinder;
import org.apache.olingo.client.api.utils.XMLUtils;
-import org.apache.olingo.client.core.uri.URIUtils;
+import org.apache.olingo.client.api.utils.URIUtils;
+import org.apache.olingo.client.core.data.LinkImpl;
import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
public abstract class AbstractODataBinder implements ODataBinder {
@@ -84,499 +109,494 @@ public abstract class AbstractODataBinder implements ODataBinder {
return serviceDocument;
}
-// @Override
-// public <T extends Feed> T getFeed(final ODataEntitySet feed, final Class<T> reference) {
-// final T feedResource = ResourceFactory.newFeed(reference);
-//
-// final List<Entry> entries = new ArrayList<Entry>();
-// feedResource.setEntries(entries);
-//
-// final URI next = feed.getNext();
-// if (next != null) {
-// feedResource.setNext(next);
-// }
-//
-// for (ODataEntity entity : feed.getEntities()) {
-// entries.add(getEntry(entity, ResourceFactory.entryClassForFeed(reference)));
-// }
-//
-// feedResource.setEntries(entries);
-//
-// return feedResource;
-// }
-//
-// @Override
-// public <T extends Entry> T getEntry(final ODataEntity entity, final Class<T> reference) {
-// return getEntry(entity, reference, true);
-// }
-//
-// @Override
-// @SuppressWarnings("unchecked")
-// public <T extends Entry> T getEntry(final ODataEntity entity, final Class<T> reference,
-// final boolean setType) {
-//
-// final T entry = ResourceFactory.newEntry(reference);
-// entry.setType(entity.getName());
-//
-// // -------------------------------------------------------------
-// // Add edit and self link
-// // -------------------------------------------------------------
-// final URI editLink = entity.getEditLink();
-// if (editLink != null) {
-// final Link entryEditLink = ResourceFactory.newLinkForEntry(reference);
-// entryEditLink.setTitle(entity.getName());
-// entryEditLink.setHref(editLink.toASCIIString());
-// entryEditLink.setRel(ODataConstants.EDIT_LINK_REL);
-// entry.setEditLink(entryEditLink);
-// }
-//
-// if (entity.isReadOnly()) {
-// final Link entrySelfLink = ResourceFactory.newLinkForEntry(reference);
-// entrySelfLink.setTitle(entity.getName());
-// entrySelfLink.setHref(entity.getLink().toASCIIString());
-// entrySelfLink.setRel(ODataConstants.SELF_LINK_REL);
-// entry.setSelfLink(entrySelfLink);
-// }
-// // -------------------------------------------------------------
-//
-// // -------------------------------------------------------------
-// // Append navigation links (handling inline entry / feed as well)
-// // -------------------------------------------------------------
-// // handle navigation links
-// for (ODataLink link : entity.getNavigationLinks()) {
-// // append link
-// LOG.debug("Append navigation link\n{}", link);
-// entry.addNavigationLink(getLinkResource(link, ResourceFactory.linkClassForEntry(reference)));
-// }
-// // -------------------------------------------------------------
-//
-// // -------------------------------------------------------------
-// // Append edit-media links
-// // -------------------------------------------------------------
-// for (ODataLink link : entity.getEditMediaLinks()) {
-// LOG.debug("Append edit-media link\n{}", link);
-// entry.addMediaEditLink(getLinkResource(link, ResourceFactory.linkClassForEntry(reference)));
-// }
-// // -------------------------------------------------------------
-//
-// // -------------------------------------------------------------
-// // Append association links
-// // -------------------------------------------------------------
-// for (ODataLink link : entity.getAssociationLinks()) {
-// LOG.debug("Append association link\n{}", link);
-// entry.addAssociationLink(getLinkResource(link, ResourceFactory.linkClassForEntry(reference)));
-// }
-// // -------------------------------------------------------------
-//
-// final Element content = newEntryContent();
-// if (entity.isMediaEntity()) {
-// entry.setMediaEntryProperties(content);
-// entry.setMediaContentSource(entity.getMediaContentSource());
-// entry.setMediaContentType(entity.getMediaContentType());
-// } else {
-// entry.setContent(content);
-// }
-//
-// for (ODataProperty prop : entity.getProperties()) {
-// content.appendChild(toDOMElement(prop, content.getOwnerDocument(), setType));
-// }
-//
-// return entry;
-// }
-//
-// @Override
-// public Element toDOMElement(final ODataProperty prop) {
-// try {
-// return toDOMElement(prop, XMLUtils.DOC_BUILDER_FACTORY.newDocumentBuilder().newDocument(), true);
-// } catch (ParserConfigurationException e) {
-// LOG.error("Error retrieving property DOM", e);
-// throw new IllegalArgumentException(e);
-// }
-// }
-//
-// @Override
-// public ODataLinkCollection getLinkCollection(final LinkCollection linkCollection) {
-// final ODataLinkCollection collection = new ODataLinkCollection(linkCollection.getNext());
-// collection.setLinks(linkCollection.getLinks());
-// return collection;
-// }
-//
-// @Override
-// public ODataEntitySet getODataEntitySet(final Feed resource) {
-// return getODataEntitySet(resource, null);
-// }
-//
-// @Override
-// public ODataEntitySet getODataEntitySet(final Feed resource, final URI defaultBaseURI) {
-// if (LOG.isDebugEnabled()) {
-// final StringWriter writer = new StringWriter();
-// client.getSerializer().feed(resource, writer);
-// writer.flush();
-// LOG.debug("FeedResource -> ODataEntitySet:\n{}", writer.toString());
-// }
-//
-// final URI base = defaultBaseURI == null ? resource.getBaseURI() : defaultBaseURI;
-//
-// final URI next = resource.getNext();
-//
-// final ODataEntitySet entitySet = next == null
-// ? client.getObjectFactory().newEntitySet()
-// : client.getObjectFactory().newEntitySet(URIUtils.getURI(base, next.toASCIIString()));
-//
-// if (resource.getCount() != null) {
-// entitySet.setCount(resource.getCount());
-// }
-//
-// for (Entry entryResource : resource.getEntries()) {
-// entitySet.addEntity(getODataEntity(entryResource));
-// }
-//
-// return entitySet;
-// }
-//
-// @Override
-// public ODataEntity getODataEntity(final Entry resource) {
-// return getODataEntity(resource, null);
-// }
-//
-// @Override
-// public ODataEntity getODataEntity(final Entry resource, final URI defaultBaseURI) {
-// if (LOG.isDebugEnabled()) {
-// final StringWriter writer = new StringWriter();
-// client.getSerializer().entry(resource, writer);
-// writer.flush();
-// LOG.debug("EntryResource -> ODataEntity:\n{}", writer.toString());
-// }
-//
-// final URI base = defaultBaseURI == null ? resource.getBaseURI() : defaultBaseURI;
-//
-// final ODataEntity entity = resource.getSelfLink() == null
-// ? client.getObjectFactory().newEntity(resource.getType())
-// : client.getObjectFactory().newEntity(resource.getType(),
-// URIUtils.getURI(base, resource.getSelfLink().getHref()));
-//
-// if (StringUtils.isNotBlank(resource.getETag())) {
-// entity.setETag(resource.getETag());
-// }
-//
-// if (resource.getEditLink() != null) {
-// entity.setEditLink(URIUtils.getURI(base, resource.getEditLink().getHref()));
-// }
-//
-// for (Link link : resource.getAssociationLinks()) {
-// entity.addLink(client.getObjectFactory().newAssociationLink(link.getTitle(), base, link.getHref()));
-// }
-//
-// for (Link link : resource.getNavigationLinks()) {
-// final Entry inlineEntry = link.getInlineEntry();
-// final Feed inlineFeed = link.getInlineFeed();
-//
-// if (inlineEntry == null && inlineFeed == null) {
-// entity.addLink(
-// client.getObjectFactory().newEntityNavigationLink(link.getTitle(), base, link.getHref()));
-// } else if (inlineFeed == null) {
-// entity.addLink(client.getObjectFactory().newInlineEntity(
-// link.getTitle(), base, link.getHref(),
-// getODataEntity(inlineEntry,
-// inlineEntry.getBaseURI() == null ? base : inlineEntry.getBaseURI())));
-// } else {
-// entity.addLink(client.getObjectFactory().newInlineEntitySet(
-// link.getTitle(), base, link.getHref(),
-// getODataEntitySet(inlineFeed,
-// inlineFeed.getBaseURI() == null ? base : inlineFeed.getBaseURI())));
-// }
-// }
-//
-// for (Link link : resource.getMediaEditLinks()) {
-// entity.addLink(client.getObjectFactory().newMediaEditLink(link.getTitle(), base, link.getHref()));
-// }
-//
-// for (ODataOperation operation : resource.getOperations()) {
-// operation.setTarget(URIUtils.getURI(base, operation.getTarget()));
-// entity.addOperation(operation);
-// }
-//
-// final Element content;
-// if (resource.isMediaEntry()) {
-// entity.setMediaEntity(true);
-// entity.setMediaContentSource(resource.getMediaContentSource());
-// entity.setMediaContentType(resource.getMediaContentType());
-// content = resource.getMediaEntryProperties();
-// } else {
-// content = resource.getContent();
-// }
-// if (content != null) {
-// for (Node property : XMLUtils.getChildNodes(content, Node.ELEMENT_NODE)) {
-// try {
-// entity.addProperty(getProperty((Element) property));
-// } catch (IllegalArgumentException e) {
-// LOG.warn("Failure retrieving EdmType for {}", property.getTextContent(), e);
-// }
-// }
-// }
-//
-// return entity;
-// }
-//
-// @Override
-// public <T extends Link> T getLinkResource(final ODataLink link, final Class<T> reference) {
-// final T linkResource = ResourceFactory.newLink(reference);
-// linkResource.setRel(link.getRel());
-// linkResource.setTitle(link.getName());
-// linkResource.setHref(link.getLink() == null ? null : link.getLink().toASCIIString());
-// linkResource.setType(link.getType().toString());
-//
-// if (link instanceof ODataInlineEntity) {
-// // append inline entity
-// final ODataEntity inlineEntity = ((ODataInlineEntity) link).getEntity();
-// LOG.debug("Append in-line entity\n{}", inlineEntity);
-//
-// linkResource.setInlineEntry(getEntry(inlineEntity, ResourceFactory.entryClassForLink(reference)));
-// } else if (link instanceof ODataInlineEntitySet) {
-// // append inline feed
-// final ODataEntitySet InlineFeed = ((ODataInlineEntitySet) link).getEntitySet();
-// LOG.debug("Append in-line feed\n{}", InlineFeed);
-//
-// linkResource.setInlineFeed(getFeed(InlineFeed, ResourceFactory.feedClassForLink(reference)));
-// }
-//
-// return linkResource;
-// }
-//
-// @Override
-// public ODataProperty getProperty(final Element property) {
-// final ODataProperty res;
-//
-// final Node nullNode = property.getAttributes().getNamedItem(ODataConstants.ATTR_NULL);
-//
-// if (nullNode == null) {
-// final EdmType edmType = StringUtils.isBlank(property.getAttribute(ODataConstants.ATTR_M_TYPE))
-// ? null
-// : newEdmType(property.getAttribute(ODataConstants.ATTR_M_TYPE));
-//
-// final PropertyType propType = edmType == null
-// ? guessPropertyType(property)
-// : edmType.isCollection()
-// ? PropertyType.COLLECTION
-// : edmType.isSimpleType()
-// ? PropertyType.PRIMITIVE
-// : PropertyType.COMPLEX;
-//
-// switch (propType) {
-// case COLLECTION:
-// res = fromCollectionPropertyElement(property, edmType);
-// break;
-//
-// case COMPLEX:
-// res = fromComplexPropertyElement(property, edmType);
-// break;
-//
-// case PRIMITIVE:
-// res = fromPrimitivePropertyElement(property, edmType);
-// break;
-//
-// case EMPTY:
-// default:
-// res = client.getObjectFactory().newPrimitiveProperty(XMLUtils.getSimpleName(property), null);
-// }
-// } else {
-// res = client.getObjectFactory().newPrimitiveProperty(XMLUtils.getSimpleName(property), null);
-// }
-//
-// return res;
-// }
-//
-// protected PropertyType guessPropertyType(final Element property) {
-// PropertyType res = null;
-//
-// if (property.hasChildNodes()) {
-// final NodeList children = property.getChildNodes();
-//
-// for (int i = 0; res == null && i < children.getLength(); i++) {
-// final Node child = children.item(i);
-//
-// if (child.getNodeType() == Node.ELEMENT_NODE
-// && !child.getNodeName().startsWith(ODataConstants.PREFIX_GML)) {
-//
-// res = ODataConstants.ELEM_ELEMENT.equals(XMLUtils.getSimpleName(child))
-// ? PropertyType.COLLECTION
-// : PropertyType.COMPLEX;
-// }
-// }
-// } else {
-// res = PropertyType.EMPTY;
-// }
-//
-// if (res == null) {
-// res = PropertyType.PRIMITIVE;
-// }
-//
-// return res;
-// }
-//
-// protected Element toDOMElement(final ODataProperty prop, final Document doc, final boolean setType) {
-// final Element element;
-//
-// if (prop.hasNullValue()) {
-// // null property handling
-// element = toNullPropertyElement(prop, doc);
-// } else if (prop.hasPrimitiveValue()) {
-// // primitive property handling
-// element = toPrimitivePropertyElement(prop, doc, setType);
-// } else if (prop.hasCollectionValue()) {
-// // collection property handling
-// element = toCollectionPropertyElement(prop, doc, setType);
-// } else {
-// // complex property handling
-// element = toComplexPropertyElement(prop, doc, setType);
-// }
-//
-// element.setAttribute(ODataConstants.XMLNS_METADATA,
-// client.getWorkingVersion().getNamespaceMap().get(ODataVersion.NS_METADATA));
-// element.setAttribute(ODataConstants.XMLNS_DATASERVICES,
-// client.getWorkingVersion().getNamespaceMap().get(ODataVersion.NS_DATASERVICES));
-// element.setAttribute(ODataConstants.XMLNS_GML, ODataConstants.NS_GML);
-// element.setAttribute(ODataConstants.XMLNS_GEORSS, ODataConstants.NS_GEORSS);
-//
-// return element;
-// }
-//
-// protected Element toNullPropertyElement(final ODataProperty prop, final Document doc) {
-// final Element element = doc.createElement(ODataConstants.PREFIX_DATASERVICES + prop.getName());
-// element.setAttribute(ODataConstants.ATTR_NULL, Boolean.toString(true));
-// return element;
-// }
-//
-// protected Element toPrimitivePropertyElement(
-// final ODataProperty prop, final Document doc, final boolean setType) {
-//
-// return toPrimitivePropertyElement(prop.getName(), prop.getPrimitiveValue(), doc, setType);
-// }
-//
-// protected Element toPrimitivePropertyElement(
-// final String name, final ODataPrimitiveValue value, final Document doc, final boolean setType) {
-//
-// final Element element = doc.createElement(ODataConstants.PREFIX_DATASERVICES + name);
-// if (setType) {
-// element.setAttribute(ODataConstants.ATTR_M_TYPE, value.getTypeName());
-// }
-//
-// if (value instanceof ODataGeospatialValue) {
-// element.appendChild(doc.importNode(((ODataGeospatialValue) value).toTree(), true));
-// } else {
-// element.setTextContent(value.toString());
-// }
-//
-// return element;
-// }
-//
-// protected Element toCollectionPropertyElement(
-// final ODataProperty prop, final Document doc, final boolean setType) {
-//
-// if (!prop.hasCollectionValue()) {
-// throw new IllegalArgumentException("Invalid property value type "
-// + prop.getValue().getClass().getSimpleName());
-// }
-//
-// final ODataCollectionValue value = prop.getCollectionValue();
-//
-// final Element element = doc.createElement(ODataConstants.PREFIX_DATASERVICES + prop.getName());
-// if (value.getTypeName() != null && setType) {
-// element.setAttribute(ODataConstants.ATTR_M_TYPE, value.getTypeName());
-// }
-//
-// for (ODataValue el : value) {
-// if (el.isPrimitive()) {
-// element.appendChild(
-// toPrimitivePropertyElement(ODataConstants.ELEM_ELEMENT, el.asPrimitive(), doc, setType));
-// } else {
-// element.appendChild(
-// toComplexPropertyElement(ODataConstants.ELEM_ELEMENT, el.asComplex(), doc, setType));
-// }
-// }
-//
-// return element;
-// }
-//
-// protected Element toComplexPropertyElement(
-// final ODataProperty prop, final Document doc, final boolean setType) {
-//
-// return toComplexPropertyElement(prop.getName(), prop.getComplexValue(), doc, setType);
-// }
-//
-// protected Element toComplexPropertyElement(
-// final String name, final ODataComplexValue value, final Document doc, final boolean setType) {
-//
-// final Element element = doc.createElement(ODataConstants.PREFIX_DATASERVICES + name);
-// if (value.getTypeName() != null && setType) {
-// element.setAttribute(ODataConstants.ATTR_M_TYPE, value.getTypeName());
-// }
-//
-// for (ODataProperty field : value) {
-// element.appendChild(toDOMElement(field, doc, true));
-// }
-// return element;
-// }
-//
-// protected ODataPrimitiveValue fromPrimitiveValueElement(final Element prop, final EdmType edmType) {
-// final ODataPrimitiveValue value;
-// if (edmType != null && edmType.getSimpleType().isGeospatial()) {
-// final Element geoProp = ODataConstants.PREFIX_GML.equals(prop.getPrefix())
-// ? prop : (Element) XMLUtils.getChildNodes(prop, Node.ELEMENT_NODE).get(0);
-// value = client.getGeospatialValueBuilder().
-// setType(edmType.getSimpleType()).setTree(geoProp).build();
-// } else {
-// value = client.getPrimitiveValueBuilder().
-// setType(edmType == null ? null : edmType.getSimpleType()).setText(prop.getTextContent()).build();
-// }
-// return value;
-// }
-//
-// protected ODataProperty fromPrimitivePropertyElement(final Element prop, final EdmType edmType) {
-// return client.getObjectFactory().newPrimitiveProperty(
-// XMLUtils.getSimpleName(prop), fromPrimitiveValueElement(prop, edmType));
-// }
-//
-// protected ODataComplexValue fromComplexValueElement(final Element prop, final EdmType edmType) {
-// final ODataComplexValue value = new ODataComplexValue(edmType == null ? null : edmType.getTypeExpression());
-//
-// for (Node child : XMLUtils.getChildNodes(prop, Node.ELEMENT_NODE)) {
-// value.add(getProperty((Element) child));
-// }
-//
-// return value;
-// }
-//
-// protected ODataProperty fromComplexPropertyElement(final Element prop, final EdmType edmType) {
-// return client.getObjectFactory().newComplexProperty(XMLUtils.getSimpleName(prop),
-// fromComplexValueElement(prop, edmType));
-// }
-//
-// protected ODataProperty fromCollectionPropertyElement(final Element prop, final EdmType edmType) {
-// final ODataCollectionValue value
-// = new ODataCollectionValue(edmType == null ? null : edmType.getTypeExpression());
-//
-// final EdmType type = edmType == null ? null : newEdmType(edmType.getBaseType());
-// final NodeList elements = prop.getChildNodes();
-//
-// for (int i = 0; i < elements.getLength(); i++) {
-// if (elements.item(i).getNodeType() != Node.TEXT_NODE) {
-// final Element child = (Element) elements.item(i);
-//
-// switch (guessPropertyType(child)) {
-// case COMPLEX:
-// value.add(fromComplexValueElement(child, type));
-// break;
-// case PRIMITIVE:
-// value.add(fromPrimitiveValueElement(child, type));
-// break;
-// default:
-// // do not add null or empty values
-// }
-// }
-// }
-//
-// return client.getObjectFactory().newCollectionProperty(XMLUtils.getSimpleName(prop), value);
-// }
-// protected abstract EdmType newEdmType(String expression);
+ @Override
+ public Feed getFeed(final ODataEntitySet feed, final Class<? extends Feed> reference) {
+ final Feed feedResource = ResourceFactory.newFeed(reference);
+
+ final URI next = feed.getNext();
+ if (next != null) {
+ feedResource.setNext(next);
+ }
+
+ for (ODataEntity entity : feed.getEntities()) {
+ feedResource.getEntries().add(getEntry(entity, ResourceFactory.entryClassForFeed(reference)));
+ }
+
+ return feedResource;
+ }
+
+ @Override
+ public Entry getEntry(final ODataEntity entity, final Class<? extends Entry> reference) {
+ return getEntry(entity, reference, true);
+ }
+
+ @Override
+ public Entry getEntry(final ODataEntity entity, final Class<? extends Entry> reference, final boolean setType) {
+ final Entry entry = ResourceFactory.newEntry(reference);
+ entry.setType(entity.getName());
+
+ // -------------------------------------------------------------
+ // Add edit and self link
+ // -------------------------------------------------------------
+ final URI editLink = entity.getEditLink();
+ if (editLink != null) {
+ final LinkImpl entryEditLink = new LinkImpl();
+ entryEditLink.setTitle(entity.getName());
+ entryEditLink.setHref(editLink.toASCIIString());
+ entryEditLink.setRel(ODataConstants.EDIT_LINK_REL);
+ entry.setEditLink(entryEditLink);
+ }
+
+ if (entity.isReadOnly()) {
+ final LinkImpl entrySelfLink = new LinkImpl();
+ entrySelfLink.setTitle(entity.getName());
+ entrySelfLink.setHref(entity.getLink().toASCIIString());
+ entrySelfLink.setRel(ODataConstants.SELF_LINK_REL);
+ entry.setSelfLink(entrySelfLink);
+ }
+ // -------------------------------------------------------------
+
+ // -------------------------------------------------------------
+ // Append navigation links (handling inline entry / feed as well)
+ // -------------------------------------------------------------
+ // handle navigation links
+ for (ODataLink link : entity.getNavigationLinks()) {
+ // append link
+ LOG.debug("Append navigation link\n{}", link);
+ entry.getNavigationLinks().add(getLink(link,
+ ResourceFactory.formatForEntryClass(reference) == ODataPubFormat.ATOM));
+ }
+ // -------------------------------------------------------------
+
+ // -------------------------------------------------------------
+ // Append edit-media links
+ // -------------------------------------------------------------
+ for (ODataLink link : entity.getEditMediaLinks()) {
+ LOG.debug("Append edit-media link\n{}", link);
+ entry.getMediaEditLinks().add(getLink(link,
+ ResourceFactory.formatForEntryClass(reference) == ODataPubFormat.ATOM));
+ }
+ // -------------------------------------------------------------
+
+ // -------------------------------------------------------------
+ // Append association links
+ // -------------------------------------------------------------
+ for (ODataLink link : entity.getAssociationLinks()) {
+ LOG.debug("Append association link\n{}", link);
+ entry.getAssociationLinks().add(getLink(link,
+ ResourceFactory.formatForEntryClass(reference) == ODataPubFormat.ATOM));
+ }
+ // -------------------------------------------------------------
+
+ final Element content = newEntryContent();
+ if (entity.isMediaEntity()) {
+ entry.setMediaEntryProperties(content);
+ entry.setMediaContentSource(entity.getMediaContentSource());
+ entry.setMediaContentType(entity.getMediaContentType());
+ } else {
+ entry.setContent(content);
+ }
+
+ for (ODataProperty prop : entity.getProperties()) {
+ content.appendChild(toDOMElement(prop, content.getOwnerDocument(), setType));
+ }
+
+ return entry;
+ }
+
+ @Override
+ public Element toDOMElement(final ODataProperty prop) {
+ try {
+ return toDOMElement(prop, XMLUtils.DOC_BUILDER_FACTORY.newDocumentBuilder().newDocument(), true);
+ } catch (ParserConfigurationException e) {
+ LOG.error("Error retrieving property DOM", e);
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ @Override
+ public ODataLinkCollection getLinkCollection(final LinkCollection linkCollection) {
+ final ODataLinkCollection collection = new ODataLinkCollection(linkCollection.getNext());
+ collection.setLinks(linkCollection.getLinks());
+ return collection;
+ }
+
+ @Override
+ public ODataEntitySet getODataEntitySet(final Feed resource) {
+ return getODataEntitySet(resource, null);
+ }
+
+ @Override
+ public ODataEntitySet getODataEntitySet(final Feed resource, final URI defaultBaseURI) {
+ if (LOG.isDebugEnabled()) {
+ final StringWriter writer = new StringWriter();
+ client.getSerializer().feed(resource, writer);
+ writer.flush();
+ LOG.debug("Feed -> ODataEntitySet:\n{}", writer.toString());
+ }
+
+ final URI base = defaultBaseURI == null ? resource.getBaseURI() : defaultBaseURI;
+
+ final URI next = resource.getNext();
+
+ final ODataEntitySet entitySet = next == null
+ ? client.getObjectFactory().newEntitySet()
+ : client.getObjectFactory().newEntitySet(URIUtils.getURI(base, next.toASCIIString()));
+
+ if (resource.getCount() != null) {
+ entitySet.setCount(resource.getCount());
+ }
+
+ for (Entry entryResource : resource.getEntries()) {
+ entitySet.addEntity(getODataEntity(entryResource));
+ }
+
+ return entitySet;
+ }
+
+ @Override
+ public ODataEntity getODataEntity(final Entry resource) {
+ return getODataEntity(resource, null);
+ }
+
+ @Override
+ public ODataEntity getODataEntity(final Entry resource, final URI defaultBaseURI) {
+ if (LOG.isDebugEnabled()) {
+ final StringWriter writer = new StringWriter();
+ client.getSerializer().entry(resource, writer);
+ writer.flush();
+ LOG.debug("EntryResource -> ODataEntity:\n{}", writer.toString());
+ }
+
+ final URI base = defaultBaseURI == null ? resource.getBaseURI() : defaultBaseURI;
+
+ final ODataEntity entity = resource.getSelfLink() == null
+ ? client.getObjectFactory().newEntity(resource.getType())
+ : client.getObjectFactory().newEntity(resource.getType(),
+ URIUtils.getURI(base, resource.getSelfLink().getHref()));
+
+ if (StringUtils.isNotBlank(resource.getETag())) {
+ entity.setETag(resource.getETag());
+ }
+
+ if (resource.getEditLink() != null) {
+ entity.setEditLink(URIUtils.getURI(base, resource.getEditLink().getHref()));
+ }
+
+ for (Link link : resource.getAssociationLinks()) {
+ entity.addLink(client.getObjectFactory().newAssociationLink(link.getTitle(), base, link.getHref()));
+ }
+
+ for (Link link : resource.getNavigationLinks()) {
+ final Entry inlineEntry = link.getInlineEntry();
+ final Feed inlineFeed = link.getInlineFeed();
+
+ if (inlineEntry == null && inlineFeed == null) {
+ entity.addLink(
+ client.getObjectFactory().newEntityNavigationLink(link.getTitle(), base, link.getHref()));
+ } else if (inlineFeed == null) {
+ entity.addLink(client.getObjectFactory().newInlineEntity(
+ link.getTitle(), base, link.getHref(),
+ getODataEntity(inlineEntry,
+ inlineEntry.getBaseURI() == null ? base : inlineEntry.getBaseURI())));
+ } else {
+ entity.addLink(client.getObjectFactory().newInlineEntitySet(
+ link.getTitle(), base, link.getHref(),
+ getODataEntitySet(inlineFeed,
+ inlineFeed.getBaseURI() == null ? base : inlineFeed.getBaseURI())));
+ }
+ }
+
+ for (Link link : resource.getMediaEditLinks()) {
+ entity.addLink(client.getObjectFactory().newMediaEditLink(link.getTitle(), base, link.getHref()));
+ }
+
+ for (Operation operation : resource.getOperations()) {
+ operation.setTarget(URIUtils.getURI(base, operation.getTarget()));
+ entity.getOperations().add(operation);
+ }
+
+ final Element content;
+ if (resource.isMediaEntry()) {
+ entity.setMediaEntity(true);
+ entity.setMediaContentSource(resource.getMediaContentSource());
+ entity.setMediaContentType(resource.getMediaContentType());
+ content = resource.getMediaEntryProperties();
+ } else {
+ content = resource.getContent();
+ }
+ if (content != null) {
+ for (Node property : XMLUtils.getChildNodes(content, Node.ELEMENT_NODE)) {
+ try {
+ entity.getProperties().add(getODataProperty((Element) property));
+ } catch (IllegalArgumentException e) {
+ LOG.warn("Failure retrieving EdmType for {}", property.getTextContent(), e);
+ }
+ }
+ }
+
+ return entity;
+ }
+
+ @Override
+ public Link getLink(final ODataLink link, boolean isXML) {
+ final Link linkResource = new LinkImpl();
+ linkResource.setRel(link.getRel());
+ linkResource.setTitle(link.getName());
+ linkResource.setHref(link.getLink() == null ? null : link.getLink().toASCIIString());
+ linkResource.setType(link.getType().toString());
+
+ if (link instanceof ODataInlineEntity) {
+ // append inline entity
+ final ODataEntity inlineEntity = ((ODataInlineEntity) link).getEntity();
+ LOG.debug("Append in-line entity\n{}", inlineEntity);
+
+ linkResource.setInlineEntry(getEntry(inlineEntity, ResourceFactory.entryClassForFormat(isXML)));
+ } else if (link instanceof ODataInlineEntitySet) {
+ // append inline feed
+ final ODataEntitySet InlineFeed = ((ODataInlineEntitySet) link).getEntitySet();
+ LOG.debug("Append in-line feed\n{}", InlineFeed);
+
+ linkResource.setInlineFeed(getFeed(InlineFeed, ResourceFactory.feedClassForFormat(isXML)));
+ }
+
+ return linkResource;
+ }
+
+ @Override
+ public ODataProperty getODataProperty(final Element property) {
+ final ODataProperty res;
+
+ final Node nullNode = property.getAttributes().getNamedItem(ODataConstants.ATTR_NULL);
+
+ if (nullNode == null) {
+ final ODataJClientEdmType edmType = StringUtils.isBlank(property.getAttribute(ODataConstants.ATTR_M_TYPE))
+ ? null
+ : new ODataJClientEdmType(property.getAttribute(ODataConstants.ATTR_M_TYPE));
+
+ final PropertyType propType = edmType == null
+ ? guessPropertyType(property)
+ : edmType.isCollection()
+ ? PropertyType.COLLECTION
+ : edmType.isSimpleType()
+ ? PropertyType.PRIMITIVE
+ : PropertyType.COMPLEX;
+
+ switch (propType) {
+ case COLLECTION:
+ res = fromCollectionPropertyElement(property, edmType);
+ break;
+
+ case COMPLEX:
+ res = fromComplexPropertyElement(property, edmType);
+ break;
+
+ case PRIMITIVE:
+ res = fromPrimitivePropertyElement(property, edmType);
+ break;
+
+ case EMPTY:
+ default:
+ res = client.getObjectFactory().newPrimitiveProperty(XMLUtils.getSimpleName(property), null);
+ }
+ } else {
+ res = client.getObjectFactory().newPrimitiveProperty(XMLUtils.getSimpleName(property), null);
+ }
+
+ return res;
+ }
+
+ protected PropertyType guessPropertyType(final Element property) {
+ PropertyType res = null;
+
+ if (property.hasChildNodes()) {
+ final NodeList children = property.getChildNodes();
+
+ for (int i = 0; res == null && i < children.getLength(); i++) {
+ final Node child = children.item(i);
+
+ if (child.getNodeType() == Node.ELEMENT_NODE
+ && !child.getNodeName().startsWith(ODataConstants.PREFIX_GML)) {
+
+ res = ODataConstants.ELEM_ELEMENT.equals(XMLUtils.getSimpleName(child))
+ ? PropertyType.COLLECTION
+ : PropertyType.COMPLEX;
+ }
+ }
+ } else {
+ res = PropertyType.EMPTY;
+ }
+
+ if (res == null) {
+ res = PropertyType.PRIMITIVE;
+ }
+
+ return res;
+ }
+
+ protected Element toDOMElement(final ODataProperty prop, final Document doc, final boolean setType) {
+ final Element element;
+
+ if (prop.hasNullValue()) {
+ // null property handling
+ element = toNullPropertyElement(prop, doc);
+ } else if (prop.hasPrimitiveValue()) {
+ // primitive property handling
+ element = toPrimitivePropertyElement(prop, doc, setType);
+ } else if (prop.hasCollectionValue()) {
+ // collection property handling
+ element = toCollectionPropertyElement(prop, doc, setType);
+ } else {
+ // complex property handling
+ element = toComplexPropertyElement(prop, doc, setType);
+ }
+
+ element.setAttribute(ODataConstants.XMLNS_METADATA,
+ client.getServiceVersion().getNamespaceMap().get(ODataServiceVersion.NS_METADATA));
+ element.setAttribute(ODataConstants.XMLNS_DATASERVICES,
+ client.getServiceVersion().getNamespaceMap().get(ODataServiceVersion.NS_DATASERVICES));
+ element.setAttribute(ODataConstants.XMLNS_GML, ODataConstants.NS_GML);
+ element.setAttribute(ODataConstants.XMLNS_GEORSS, ODataConstants.NS_GEORSS);
+
+ return element;
+ }
+
+ protected Element toNullPropertyElement(final ODataProperty prop, final Document doc) {
+ final Element element = doc.createElement(ODataConstants.PREFIX_DATASERVICES + prop.getName());
+ element.setAttribute(ODataConstants.ATTR_NULL, Boolean.toString(true));
+ return element;
+ }
+
+ protected Element toPrimitivePropertyElement(
+ final ODataProperty prop, final Document doc, final boolean setType) {
+
+ return toPrimitivePropertyElement(prop.getName(), prop.getPrimitiveValue(), doc, setType);
+ }
+
+ protected Element toPrimitivePropertyElement(
+ final String name, final ODataPrimitiveValue value, final Document doc, final boolean setType) {
+
+ final Element element = doc.createElement(ODataConstants.PREFIX_DATASERVICES + name);
+ if (setType) {
+ element.setAttribute(ODataConstants.ATTR_M_TYPE, value.getTypeName());
+ }
+
+ if (value instanceof ODataGeospatialValue) {
+ element.appendChild(doc.importNode(((ODataGeospatialValue) value).toTree(), true));
+ } else {
+ element.setTextContent(value.toString());
+ }
+
+ return element;
+ }
+
+ protected Element toCollectionPropertyElement(
+ final ODataProperty prop, final Document doc, final boolean setType) {
+
+ if (!prop.hasCollectionValue()) {
+ throw new IllegalArgumentException("Invalid property value type "
+ + prop.getValue().getClass().getSimpleName());
+ }
+
+ final ODataCollectionValue value = prop.getCollectionValue();
+
+ final Element element = doc.createElement(ODataConstants.PREFIX_DATASERVICES + prop.getName());
+ if (value.getTypeName() != null && setType) {
+ element.setAttribute(ODataConstants.ATTR_M_TYPE, value.getTypeName());
+ }
+
+ for (ODataValue el : value) {
+ if (el.isPrimitive()) {
+ element.appendChild(
+ toPrimitivePropertyElement(ODataConstants.ELEM_ELEMENT, el.asPrimitive(), doc, setType));
+ } else {
+ element.appendChild(
+ toComplexPropertyElement(ODataConstants.ELEM_ELEMENT, el.asComplex(), doc, setType));
+ }
+ }
+
+ return element;
+ }
+
+ protected Element toComplexPropertyElement(
+ final ODataProperty prop, final Document doc, final boolean setType) {
+
+ return toComplexPropertyElement(prop.getName(), prop.getComplexValue(), doc, setType);
+ }
+
+ protected Element toComplexPropertyElement(
+ final String name, final ODataComplexValue value, final Document doc, final boolean setType) {
+
+ final Element element = doc.createElement(ODataConstants.PREFIX_DATASERVICES + name);
+ if (value.getTypeName() != null && setType) {
+ element.setAttribute(ODataConstants.ATTR_M_TYPE, value.getTypeName());
+ }
+
+ for (ODataProperty field : value) {
+ element.appendChild(toDOMElement(field, doc, true));
+ }
+ return element;
+ }
+
+ protected ODataPrimitiveValue fromPrimitiveValueElement(final Element prop, final ODataJClientEdmType edmType) {
+ final ODataPrimitiveValue value;
+ if (edmType != null && edmType.getSimpleType().isGeospatial()) {
+ final Element geoProp = ODataConstants.PREFIX_GML.equals(prop.getPrefix())
+ ? prop : (Element) XMLUtils.getChildNodes(prop, Node.ELEMENT_NODE).get(0);
+ value = client.getGeospatialValueBuilder().
+ setType(edmType.getSimpleType()).setTree(geoProp).build();
+ } else {
+ value = client.getPrimitiveValueBuilder().
+ setType(edmType == null ? null : edmType.getSimpleType()).setText(prop.getTextContent()).build();
+ }
+ return value;
+ }
+
+ protected ODataProperty fromPrimitivePropertyElement(final Element prop, final ODataJClientEdmType edmType) {
+ return client.getObjectFactory().newPrimitiveProperty(
+ XMLUtils.getSimpleName(prop), fromPrimitiveValueElement(prop, edmType));
+ }
+
+ protected ODataComplexValue fromComplexValueElement(final Element prop, final ODataJClientEdmType edmType) {
+ final ODataComplexValue value = new ODataComplexValue(edmType == null ? null : edmType.getTypeExpression());
+
+ for (Node child : XMLUtils.getChildNodes(prop, Node.ELEMENT_NODE)) {
+ value.add(getODataProperty((Element) child));
+ }
+
+ return value;
+ }
+
+ protected ODataProperty fromComplexPropertyElement(final Element prop, final ODataJClientEdmType edmType) {
+ return client.getObjectFactory().newComplexProperty(XMLUtils.getSimpleName(prop),
+ fromComplexValueElement(prop, edmType));
+ }
+
+ protected ODataProperty fromCollectionPropertyElement(final Element prop, final ODataJClientEdmType edmType) {
+ final ODataCollectionValue value =
+ new ODataCollectionValue(edmType == null ? null : edmType.getTypeExpression());
+
+ final ODataJClientEdmType type = edmType == null ? null : new ODataJClientEdmType(edmType.getBaseType());
+ final NodeList elements = prop.getChildNodes();
+
+ for (int i = 0; i < elements.getLength(); i++) {
+ if (elements.item(i).getNodeType() != Node.TEXT_NODE) {
+ final Element child = (Element) elements.item(i);
+
+ switch (guessPropertyType(child)) {
+ case COMPLEX:
+ value.add(fromComplexValueElement(child, type));
+ break;
+ case PRIMITIVE:
+ value.add(fromPrimitiveValueElement(child, type));
+ break;
+ default:
+ // do not add null or empty values
+ }
+ }
+ }
+
+ return client.getObjectFactory().newCollectionProperty(XMLUtils.getSimpleName(prop), value);
+ }
+
}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata4/blob/78c3eaa4/lib/client-core/src/main/java/org/apache/olingo/client/core/op/impl/AbstractODataDeserializer.java
----------------------------------------------------------------------
diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/op/impl/AbstractODataDeserializer.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/op/impl/AbstractODataDeserializer.java
index 2c7ff22..43636b3 100644
--- a/lib/client-core/src/main/java/org/apache/olingo/client/core/op/impl/AbstractODataDeserializer.java
+++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/op/impl/AbstractODataDeserializer.java
@@ -18,161 +18,174 @@
*/
package org.apache.olingo.client.core.op.impl;
+import com.fasterxml.aalto.stax.InputFactoryImpl;
+import com.fasterxml.aalto.stax.OutputFactoryImpl;
+import com.fasterxml.jackson.dataformat.xml.JacksonXmlModule;
+import com.fasterxml.jackson.dataformat.xml.XmlFactory;
+import com.fasterxml.jackson.dataformat.xml.XmlMapper;
+
+import java.io.IOException;
import java.io.InputStream;
+import java.net.URI;
import org.apache.olingo.client.api.ODataClient;
+import org.apache.olingo.client.api.ODataConstants;
+import org.apache.olingo.client.api.data.Entry;
+import org.apache.olingo.client.api.data.Error;
+import org.apache.olingo.client.api.data.Feed;
+import org.apache.olingo.client.api.data.LinkCollection;
+import org.apache.olingo.client.api.format.ODataFormat;
+import org.apache.olingo.client.api.format.ODataPubFormat;
import org.apache.olingo.client.api.op.ODataDeserializer;
+import org.apache.olingo.client.core.data.AtomDeserializer;
+import org.apache.olingo.client.core.data.JSONEntryImpl;
+import org.apache.olingo.client.core.data.JSONErrorBundle;
+import org.apache.olingo.client.core.data.JSONFeedImpl;
+import org.apache.olingo.client.core.data.JSONLinkCollectionImpl;
+import org.apache.olingo.client.core.data.JSONPropertyImpl;
+import org.apache.olingo.client.core.data.XMLErrorImpl;
+import org.apache.olingo.client.core.data.XMLLinkCollectionImpl;
import org.apache.olingo.client.core.xml.XMLParser;
import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
public abstract class AbstractODataDeserializer extends AbstractJacksonTool implements ODataDeserializer {
private static final long serialVersionUID = -4244158979195609909L;
-// private final AtomDeserializer atomDeserializer;
+ private final AtomDeserializer atomDeserializer;
+
public AbstractODataDeserializer(final ODataClient client) {
super(client);
-// this.atomDeserializer = new AtomDeserializer(client);
- }
-
-// @Override
-// @SuppressWarnings("unchecked")
-// public <T extends Feed> T toFeed(final InputStream input, final Class<T> reference) {
-// T entry;
-//
-// if (AtomFeed.class.equals(reference)) {
-// entry = (T) toAtomFeed(input);
-// } else {
-// entry = (T) toJSONFeed(input);
-// }
-//
-// return entry;
-// }
-//
-// @Override
-// @SuppressWarnings("unchecked")
-// public <T extends Entry> T toEntry(final InputStream input, final Class<T> reference) {
-// T entry;
-//
-// if (AtomEntry.class.equals(reference)) {
-// entry = (T) toAtomEntry(input);
-//
-// } else {
-// entry = (T) toJSONEntry(input);
-// }
-//
-// return entry;
-// }
-//
-// @Override
-// public Element toPropertyDOM(final InputStream input, final ODataFormat format) {
-// return format == ODataFormat.XML
-// ? toPropertyDOMFromXML(input)
-// : toPropertyDOMFromJSON(input);
-// }
-//
-// @Override
-// public LinkCollection toLinkCollection(final InputStream input, final ODataFormat format) {
-// return format == ODataFormat.XML
-// ? toLinkCollectionFromXML(input)
-// : toLinkCollectionFromJSON(input);
-// }
-//
-// @Override
-// public ODataError toODataError(final InputStream input, final boolean isXML) {
-// return isXML
-// ? toODataErrorFromXML(input)
-// : toODataErrorFromJSON(input);
-// }
-//
+ this.atomDeserializer = new AtomDeserializer(client);
+ }
+
+ @Override
+ public Feed toFeed(final InputStream input, final ODataPubFormat format) {
+ return format == ODataPubFormat.ATOM
+ ? toAtomFeed(input)
+ : toJSONFeed(input);
+ }
+
+ @Override
+ public Entry toEntry(final InputStream input, final ODataPubFormat format) {
+ return format == ODataPubFormat.ATOM
+ ? toAtomEntry(input)
+ : toJSONEntry(input);
+ }
+
+ @Override
+ public Element toPropertyDOM(final InputStream input, final ODataFormat format) {
+ return format == ODataFormat.XML
+ ? toPropertyDOMFromXML(input)
+ : toPropertyDOMFromJSON(input);
+ }
+
+ @Override
+ public LinkCollection toLinkCollection(final InputStream input, final ODataFormat format) {
+ return format == ODataFormat.XML
+ ? toLinkCollectionFromXML(input)
+ : toLinkCollectionFromJSON(input);
+ }
+
+ @Override
+ public Error toError(final InputStream input, final boolean isXML) {
+ return isXML
+ ? toErrorFromXML(input)
+ : toErrorFromJSON(input);
+ }
+
@Override
public Element toDOM(final InputStream input) {
return XMLParser.PARSER.deserialize(input);
}
-//
-// /*
-// * ------------------ Protected methods ------------------
-// */
-// protected AtomFeed toAtomFeed(final InputStream input) {
-// try {
-// return atomDeserializer.feed(toDOM(input));
-// } catch (Exception e) {
-// throw new IllegalArgumentException("While deserializing Atom feed", e);
-// }
-// }
-//
-// protected AtomEntry toAtomEntry(final InputStream input) {
-// try {
-// return atomDeserializer.entry(toDOM(input));
-// } catch (Exception e) {
-// throw new IllegalArgumentException("While deserializing Atom entry", e);
-// }
-// }
-//
-// protected JSONFeed toJSONFeed(final InputStream input) {
-// try {
-// return getObjectMapper().readValue(input, JSONFeed.class);
-// } catch (IOException e) {
-// throw new IllegalArgumentException("While deserializing JSON feed", e);
-// }
-// }
-//
-// protected abstract AbstractJSONEntry toJSONEntry(final InputStream input);
-//
-// protected Element toPropertyDOMFromXML(final InputStream input) {
-// return toDOM(input);
-// }
-//
-// protected Element toPropertyDOMFromJSON(final InputStream input) {
-// try {
-// return getObjectMapper().readValue(input, JSONProperty.class).getContent();
-// } catch (IOException e) {
-// throw new IllegalArgumentException("While deserializing JSON property", e);
-// }
-// }
-//
-// protected XMLLinkCollection toLinkCollectionFromXML(final InputStream input) {
-// final Element root = toDOM(input);
-//
-// final NodeList uris = root.getOwnerDocument().getElementsByTagName(ODataConstants.ELEM_URI);
-//
-// final List<URI> links = new ArrayList<URI>();
-// for (int i = 0; i < uris.getLength(); i++) {
-// links.add(URI.create(uris.item(i).getTextContent()));
-// }
-//
-// final NodeList next = root.getElementsByTagName(ODataConstants.NEXT_LINK_REL);
-// final XMLLinkCollection linkCollection = next.getLength() > 0
-// ? new XMLLinkCollection(URI.create(next.item(0).getTextContent()))
-// : new XMLLinkCollection();
-// linkCollection.setLinks(links);
-//
-// return linkCollection;
-// }
-//
-// protected JSONLinkCollection toLinkCollectionFromJSON(final InputStream input) {
-// try {
-// return getObjectMapper().readValue(input, JSONLinkCollection.class);
-// } catch (IOException e) {
-// throw new IllegalArgumentException("While deserializing JSON $links", e);
-// }
-// }
-//
-// protected XMLODataError toODataErrorFromXML(final InputStream input) {
-// try {
-// final XmlMapper xmlMapper = new XmlMapper(
-// new XmlFactory(new InputFactoryImpl(), new OutputFactoryImpl()), new JacksonXmlModule());
-// return xmlMapper.readValue(input, XMLODataError.class);
-// } catch (Exception e) {
-// throw new IllegalArgumentException("While deserializing XML error", e);
-// }
-// }
-//
-// protected JSONODataError toODataErrorFromJSON(final InputStream input) {
-// try {
-// return getObjectMapper().readValue(input, JSONODataErrorBundle.class).getError();
-// } catch (IOException e) {
-// throw new IllegalArgumentException("While deserializing JSON error", e);
-// }
-// }
+
+ /*
+ * ------------------ Protected methods ------------------
+ */
+ protected Feed toAtomFeed(final InputStream input) {
+ try {
+ return atomDeserializer.feed(toDOM(input));
+ } catch (Exception e) {
+ throw new IllegalArgumentException("While deserializing Atom feed", e);
+ }
+ }
+
+ protected Entry toAtomEntry(final InputStream input) {
+ try {
+ return atomDeserializer.entry(toDOM(input));
+ } catch (Exception e) {
+ throw new IllegalArgumentException("While deserializing Atom entry", e);
+ }
+ }
+
+ protected Feed toJSONFeed(final InputStream input) {
+ try {
+ return getObjectMapper().readValue(input, JSONFeedImpl.class);
+ } catch (IOException e) {
+ throw new IllegalArgumentException("While deserializing JSON feed", e);
+ }
+ }
+
+ protected Entry toJSONEntry(final InputStream input) {
+ try {
+ return getObjectMapper().readValue(input, JSONEntryImpl.class);
+ } catch (IOException e) {
+ throw new IllegalArgumentException("While deserializing JSON entry", e);
+ }
+ }
+
+ protected Element toPropertyDOMFromXML(final InputStream input) {
+ return toDOM(input);
+ }
+
+ protected Element toPropertyDOMFromJSON(final InputStream input) {
+ try {
+ return getObjectMapper().readValue(input, JSONPropertyImpl.class).getContent();
+ } catch (IOException e) {
+ throw new IllegalArgumentException("While deserializing JSON property", e);
+ }
+ }
+
+ protected XMLLinkCollectionImpl toLinkCollectionFromXML(final InputStream input) {
+ final Element root = toDOM(input);
+
+ final NodeList uris = root.getOwnerDocument().getElementsByTagName(ODataConstants.ELEM_URI);
+
+ final NodeList next = root.getElementsByTagName(ODataConstants.NEXT_LINK_REL);
+ final XMLLinkCollectionImpl linkCollection = next.getLength() > 0
+ ? new XMLLinkCollectionImpl(URI.create(next.item(0).getTextContent()))
+ : new XMLLinkCollectionImpl();
+ for (int i = 0; i < uris.getLength(); i++) {
+ linkCollection.getLinks().add(URI.create(uris.item(i).getTextContent()));
+ }
+
+ return linkCollection;
+ }
+
+ protected JSONLinkCollectionImpl toLinkCollectionFromJSON(final InputStream input) {
+ try {
+ return getObjectMapper().readValue(input, JSONLinkCollectionImpl.class);
+ } catch (IOException e) {
+ throw new IllegalArgumentException("While deserializing JSON $links", e);
+ }
+ }
+
+ protected Error toErrorFromXML(final InputStream input) {
+ try {
+ final XmlMapper xmlMapper = new XmlMapper(
+ new XmlFactory(new InputFactoryImpl(), new OutputFactoryImpl()), new JacksonXmlModule());
+ return xmlMapper.readValue(input, XMLErrorImpl.class);
+ } catch (Exception e) {
+ throw new IllegalArgumentException("While deserializing XML error", e);
+ }
+ }
+
+ protected Error toErrorFromJSON(final InputStream input) {
+ try {
+ return getObjectMapper().readValue(input, JSONErrorBundle.class).getError();
+ } catch (IOException e) {
+ throw new IllegalArgumentException("While deserializing JSON error", e);
+ }
+ }
}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata4/blob/78c3eaa4/lib/client-core/src/main/java/org/apache/olingo/client/core/op/impl/AbstractODataReader.java
----------------------------------------------------------------------
diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/op/impl/AbstractODataReader.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/op/impl/AbstractODataReader.java
index 6cf19a3..d8a85c6 100644
--- a/lib/client-core/src/main/java/org/apache/olingo/client/core/op/impl/AbstractODataReader.java
+++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/op/impl/AbstractODataReader.java
@@ -18,10 +18,30 @@
*/
package org.apache.olingo.client.core.op.impl;
+import java.io.InputStream;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.StringUtils;
import org.apache.olingo.client.api.ODataClient;
+import org.apache.olingo.client.api.ODataConstants;
+import org.apache.olingo.client.api.data.Error;
+import org.apache.olingo.client.api.domain.ODataEntity;
+import org.apache.olingo.client.api.domain.ODataEntitySet;
+import org.apache.olingo.client.api.domain.ODataJClientEdmPrimitiveType;
+import org.apache.olingo.client.api.domain.ODataLinkCollection;
+import org.apache.olingo.client.api.domain.ODataProperty;
+import org.apache.olingo.client.api.domain.ODataServiceDocument;
+import org.apache.olingo.client.api.domain.ODataValue;
+import org.apache.olingo.client.api.edm.xml.XMLMetadata;
+import org.apache.olingo.client.api.format.ODataFormat;
+import org.apache.olingo.client.api.format.ODataPubFormat;
+import org.apache.olingo.client.api.format.ODataValueFormat;
import org.apache.olingo.client.api.op.ODataReader;
+import org.apache.olingo.client.core.data.ODataEntitySetIterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
public abstract class AbstractODataReader implements ODataReader {
@@ -38,101 +58,99 @@ public abstract class AbstractODataReader implements ODataReader {
this.client = client;
}
-// @Override
-// public ODataEntitySet readEntitySet(final InputStream input, final ODataPubFormat format) {
-// return client.getBinder().getODataEntitySet(
-// client.getDeserializer().toFeed(input, ResourceFactory.feedClassForFormat(format)));
-// }
-//
-// @Override
-// public ODataEntity readEntity(final InputStream input, final ODataPubFormat format) {
-// return client.getBinder().getODataEntity(
-// client.getDeserializer().toEntry(input, ResourceFactory.entryClassForFormat(format)));
-// }
-//
-// @Override
-// public ODataProperty readProperty(final InputStream input, final ODataFormat format) {
-// final Element property = client.getDeserializer().toPropertyDOM(input, format);
-//
-// // The ODataProperty object is used either for actual entity properties and for invoke result
-// // (when return type is neither an entity nor a collection of entities).
-// // Such formats are mostly the same except for collections: an entity property looks like
-// // <aproperty m:type="Collection(AType)">
-// // <element>....</element>
-// // </aproperty>
-// //
-// // while an invoke result with returnType="Collection(AnotherType)" looks like
-// // <functionImportName>
-// // <element m:type="AnotherType">...</element>
-// // <functionImportName>
-// //
-// // The code below is meant for "normalizing" the latter into
-// // <functionImportName m:type="Collection(AnotherType)">
-// // <element m:type="AnotherType">...</element>
-// // <functionImportName>
-// final String type = property.getAttribute(ODataConstants.ATTR_M_TYPE);
-// final NodeList elements = property.getElementsByTagName(ODataConstants.ELEM_ELEMENT);
-// if (StringUtils.isBlank(type) && elements != null && elements.getLength() > 0) {
-// final Node elementType = elements.item(0).getAttributes().getNamedItem(ODataConstants.ATTR_M_TYPE);
-// if (elementType != null) {
-// property.setAttribute(ODataConstants.ATTR_M_TYPE, "Collection(" + elementType.getTextContent() + ")");
-// }
-// }
-//
-// return client.getBinder().getProperty(property);
-// }
-//
-// @Override
-// public ODataLinkCollection readLinks(final InputStream input, final ODataFormat format) {
-// return client.getBinder().getLinkCollection(
-// client.getDeserializer().toLinkCollection(input, format));
-// }
-//
-// @Override
-// public ODataError readError(final InputStream inputStream, final boolean isXML) {
-// return client.getDeserializer().toODataError(inputStream, isXML);
-// }
-//
-// @Override
-// @SuppressWarnings("unchecked")
-// public <T> T read(final InputStream src, final String format, final Class<T> reference) {
-// Object res;
-//
-// try {
-// if (ODataEntitySetIterator.class.isAssignableFrom(reference)) {
-// res = new ODataEntitySetIterator(client, src, ODataPubFormat.fromString(format));
-// } else if (ODataEntitySet.class.isAssignableFrom(reference)) {
-// res = readEntitySet(src, ODataPubFormat.fromString(format));
-// } else if (ODataEntity.class.isAssignableFrom(reference)) {
-// res = readEntity(src, ODataPubFormat.fromString(format));
-// } else if (ODataProperty.class.isAssignableFrom(reference)) {
-// res = readProperty(src, ODataFormat.fromString(format));
-// } else if (ODataLinkCollection.class.isAssignableFrom(reference)) {
-// res = readLinks(src, ODataFormat.fromString(format));
-// } else if (ODataValue.class.isAssignableFrom(reference)) {
-// res = client.getPrimitiveValueBuilder().
-// setType(ODataValueFormat.fromString(format) == ODataValueFormat.TEXT
-// ? EdmSimpleType.String : EdmSimpleType.Stream).
-// setText(IOUtils.toString(src)).
-// build();
-// } else if (AbstractEdmMetadata.class.isAssignableFrom(reference)) {
-// res = readMetadata(src);
-// } else if (ODataServiceDocument.class.isAssignableFrom(reference)) {
-// res = readServiceDocument(src, ODataFormat.fromString(format));
-// } else if (ODataError.class.isAssignableFrom(reference)) {
-// res = readError(src, !format.toString().contains("json"));
-// } else {
-// throw new IllegalArgumentException("Invalid reference type " + reference);
-// }
-// } catch (Exception e) {
-// LOG.warn("Cast error", e);
-// res = null;
-// } finally {
-// if (!ODataEntitySetIterator.class.isAssignableFrom(reference)) {
-// IOUtils.closeQuietly(src);
-// }
-// }
-//
-// return (T) res;
-// }
+ @Override
+ public ODataEntitySet readEntitySet(final InputStream input, final ODataPubFormat format) {
+ return client.getBinder().getODataEntitySet(client.getDeserializer().toFeed(input, format));
+ }
+
+ @Override
+ public ODataEntity readEntity(final InputStream input, final ODataPubFormat format) {
+ return client.getBinder().getODataEntity(client.getDeserializer().toEntry(input, format));
+ }
+
+ @Override
+ public ODataProperty readProperty(final InputStream input, final ODataFormat format) {
+ final Element property = client.getDeserializer().toPropertyDOM(input, format);
+
+ // The ODataProperty object is used either for actual entity properties and for invoke result
+ // (when return type is neither an entity nor a collection of entities).
+ // Such formats are mostly the same except for collections: an entity property looks like
+ // <aproperty m:type="Collection(AType)">
+ // <element>....</element>
+ // </aproperty>
+ //
+ // while an invoke result with returnType="Collection(AnotherType)" looks like
+ // <functionImportName>
+ // <element m:type="AnotherType">...</element>
+ // <functionImportName>
+ //
+ // The code below is meant for "normalizing" the latter into
+ // <functionImportName m:type="Collection(AnotherType)">
+ // <element m:type="AnotherType">...</element>
+ // <functionImportName>
+ final String type = property.getAttribute(ODataConstants.ATTR_M_TYPE);
+ final NodeList elements = property.getElementsByTagName(ODataConstants.ELEM_ELEMENT);
+ if (StringUtils.isBlank(type) && elements != null && elements.getLength() > 0) {
+ final Node elementType = elements.item(0).getAttributes().getNamedItem(ODataConstants.ATTR_M_TYPE);
+ if (elementType != null) {
+ property.setAttribute(ODataConstants.ATTR_M_TYPE, "Collection(" + elementType.getTextContent() + ")");
+ }
+ }
+
+ return client.getBinder().getODataProperty(property);
+ }
+
+ @Override
+ public ODataLinkCollection readLinks(final InputStream input, final ODataFormat format) {
+ return client.getBinder().getLinkCollection(
+ client.getDeserializer().toLinkCollection(input, format));
+ }
+
+ @Override
+ public Error readError(final InputStream inputStream, final boolean isXML) {
+ return client.getDeserializer().toError(inputStream, isXML);
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public <T> T read(final InputStream src, final String format, final Class<T> reference) {
+ Object res;
+
+ try {
+ if (ODataEntitySetIterator.class.isAssignableFrom(reference)) {
+ res = new ODataEntitySetIterator(client, src, ODataPubFormat.fromString(format));
+ } else if (ODataEntitySet.class.isAssignableFrom(reference)) {
+ res = readEntitySet(src, ODataPubFormat.fromString(format));
+ } else if (ODataEntity.class.isAssignableFrom(reference)) {
+ res = readEntity(src, ODataPubFormat.fromString(format));
+ } else if (ODataProperty.class.isAssignableFrom(reference)) {
+ res = readProperty(src, ODataFormat.fromString(format));
+ } else if (ODataLinkCollection.class.isAssignableFrom(reference)) {
+ res = readLinks(src, ODataFormat.fromString(format));
+ } else if (ODataValue.class.isAssignableFrom(reference)) {
+ res = client.getPrimitiveValueBuilder().
+ setType(ODataValueFormat.fromString(format) == ODataValueFormat.TEXT
+ ? ODataJClientEdmPrimitiveType.String : ODataJClientEdmPrimitiveType.Stream).
+ setText(IOUtils.toString(src)).
+ build();
+ } else if (XMLMetadata.class.isAssignableFrom(reference)) {
+ res = readMetadata(src);
+ } else if (ODataServiceDocument.class.isAssignableFrom(reference)) {
+ res = readServiceDocument(src, ODataFormat.fromString(format));
+ } else if (Error.class.isAssignableFrom(reference)) {
+ res = readError(src, !format.toString().contains("json"));
+ } else {
+ throw new IllegalArgumentException("Invalid reference type " + reference);
+ }
+ } catch (Exception e) {
+ LOG.warn("Cast error", e);
+ res = null;
+ } finally {
+ if (!ODataEntitySetIterator.class.isAssignableFrom(reference)) {
+ IOUtils.closeQuietly(src);
+ }
+ }
+
+ return (T) res;
+ }
}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata4/blob/78c3eaa4/lib/client-core/src/main/java/org/apache/olingo/client/core/op/impl/AbstractODataSerializer.java
----------------------------------------------------------------------
diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/op/impl/AbstractODataSerializer.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/op/impl/AbstractODataSerializer.java
index d60ff19..151ff49 100644
--- a/lib/client-core/src/main/java/org/apache/olingo/client/core/op/impl/AbstractODataSerializer.java
+++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/op/impl/AbstractODataSerializer.java
@@ -18,81 +18,103 @@
*/
package org.apache.olingo.client.core.op.impl;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
+import javax.xml.parsers.DocumentBuilder;
import org.apache.olingo.client.api.ODataClient;
+import org.apache.olingo.client.api.ODataConstants;
+import org.apache.olingo.client.api.data.Entry;
+import org.apache.olingo.client.api.data.Feed;
+import org.apache.olingo.client.api.data.Link;
+import org.apache.olingo.client.api.format.ODataFormat;
import org.apache.olingo.client.api.op.ODataSerializer;
+import org.apache.olingo.client.api.utils.XMLUtils;
+import org.apache.olingo.client.core.data.AbstractPayloadObject;
+import org.apache.olingo.client.core.data.AtomEntryImpl;
+import org.apache.olingo.client.core.data.AtomFeedImpl;
+import org.apache.olingo.client.core.data.AtomSerializer;
+import org.apache.olingo.client.core.data.JSONEntryImpl;
+import org.apache.olingo.client.core.data.JSONFeedImpl;
+import org.apache.olingo.client.core.data.JSONPropertyImpl;
import org.apache.olingo.client.core.xml.XMLParser;
+import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
import org.w3c.dom.Node;
public abstract class AbstractODataSerializer extends AbstractJacksonTool implements ODataSerializer {
private static final long serialVersionUID = -357777648541325363L;
-// private final AtomSerializer atomSerializer;
+ private final AtomSerializer atomSerializer;
+
public AbstractODataSerializer(final ODataClient client) {
super(client);
-// this.atomSerializer = new AtomSerializer(client);
- }
-
-// @Override
-// public <T extends Feed> void feed(final T obj, final OutputStream out) {
-// feed(obj, new OutputStreamWriter(out));
-// }
-//
-// @Override
-// public <T extends Feed> void feed(final T obj, final Writer writer) {
-// if (obj instanceof AtomFeed) {
-// atom((AtomFeed) obj, writer);
-// } else {
-// json((JSONFeed) obj, writer);
-// }
-// }
-//
-// @Override
-// public <T extends Entry> void entry(final T obj, final OutputStream out) {
-// entry(obj, new OutputStreamWriter(out));
-// }
-//
-// @Override
-// public <T extends Entry> void entry(final T obj, final Writer writer) {
-// if (obj instanceof AtomEntry) {
-// atom((AtomEntry) obj, writer);
-// } else {
-// json((JSONEntry) obj, writer);
-// }
-// }
-//
-// @Override
-// public void property(final Element element, final ODataFormat format, final OutputStream out) {
-// property(element, format, new OutputStreamWriter(out));
-// }
-//
-// @Override
-// public void property(final Element element, final ODataFormat format, final Writer writer) {
-// if (format == ODataFormat.XML) {
-// dom(element, writer);
-// } else {
-// json(element, writer);
-// }
-// }
-//
-// @Override
-// public void link(final ODataLink link, final ODataFormat format, final OutputStream out) {
-// link(link, format, new OutputStreamWriter(out));
-// }
-//
-// @Override
-// public void link(final ODataLink link, final ODataFormat format, final Writer writer) {
-// if (format == ODataFormat.XML) {
-// xmlLink(link, writer);
-// } else {
-// jsonLink(link, writer);
-// }
-// }
-//
+ this.atomSerializer = new AtomSerializer(client);
+ }
+
+ @Override
+ public void feed(final Feed obj, final OutputStream out) {
+ feed(obj, new OutputStreamWriter(out));
+ }
+
+ @Override
+ public void feed(final Feed obj, final Writer writer) {
+ if (obj instanceof AtomFeedImpl) {
+ atom((AtomFeedImpl) obj, writer);
+ } else {
+ json((JSONFeedImpl) obj, writer);
+ }
+ }
+
+ @Override
+ public void entry(final Entry obj, final OutputStream out) {
+ entry(obj, new OutputStreamWriter(out));
+ }
+
+ @Override
+ public void entry(final Entry obj, final Writer writer) {
+ if (obj instanceof AtomEntryImpl) {
+ atom((AtomEntryImpl) obj, writer);
+ } else {
+ json((JSONEntryImpl) obj, writer);
+ }
+ }
+
+ @Override
+ public void property(final Element element, final ODataFormat format, final OutputStream out) {
+ property(element, format, new OutputStreamWriter(out));
+ }
+
+ @Override
+ public void property(final Element element, final ODataFormat format, final Writer writer) {
+ if (format == ODataFormat.XML) {
+ dom(element, writer);
+ } else {
+ json(element, writer);
+ }
+ }
+
+ @Override
+ public void link(final Link link, final ODataFormat format, final OutputStream out) {
+ link(link, format, new OutputStreamWriter(out));
+ }
+
+ @Override
+ public void link(final Link link, final ODataFormat format, final Writer writer) {
+ if (format == ODataFormat.XML) {
+ xmlLink(link, writer);
+ } else {
+ jsonLink(link, writer);
+ }
+ }
+
@Override
public void dom(final Node content, final OutputStream out) {
dom(content, new OutputStreamWriter(out));
@@ -102,60 +124,60 @@ public abstract class AbstractODataSerializer extends AbstractJacksonTool implem
public void dom(final Node content, final Writer writer) {
XMLParser.PARSER.serialize(content, writer);
}
-//
-// /*
-// * ------------------ Protected methods ------------------
-// */
-// protected <T extends AbstractPayloadObject> void atom(final T obj, final Writer writer) {
-// try {
-// dom(atomSerializer.serialize(obj), writer);
-// } catch (Exception e) {
-// throw new IllegalArgumentException("While serializing Atom object", e);
-// }
-// }
-//
-// protected <T extends AbstractPayloadObject> void json(final T obj, final Writer writer) {
-// try {
-// getObjectMapper().writeValue(writer, obj);
-// } catch (IOException e) {
-// throw new IllegalArgumentException("While serializing JSON object", e);
-// }
-// }
-//
-// protected void json(final Element element, final Writer writer) {
-// try {
-// final JSONProperty property = new JSONProperty();
-// property.setContent(element);
-// getObjectMapper().writeValue(writer, property);
-// } catch (IOException e) {
-// throw new IllegalArgumentException("While serializing JSON property", e);
-// }
-// }
-//
-// protected void xmlLink(final ODataLink link, final Writer writer) {
-// try {
-// final DocumentBuilder builder = XMLUtils.DOC_BUILDER_FACTORY.newDocumentBuilder();
-// final Document doc = builder.newDocument();
-// final Element uri = doc.createElementNS(
-// client.getWorkingVersion().getNamespaceMap().get(ODataVersion.NS_DATASERVICES),
-// ODataConstants.ELEM_URI);
-// uri.appendChild(doc.createTextNode(link.getLink().toASCIIString()));
-//
-// dom(uri, writer);
-// } catch (Exception e) {
-// throw new IllegalArgumentException("While serializing XML link", e);
-// }
-// }
-//
-// protected void jsonLink(final ODataLink link, final Writer writer) {
-// final ObjectMapper mapper = getObjectMapper();
-// final ObjectNode uri = mapper.createObjectNode();
-// uri.put(ODataConstants.JSON_URL, link.getLink().toASCIIString());
-//
-// try {
-// mapper.writeValue(writer, uri);
-// } catch (Exception e) {
-// throw new IllegalArgumentException("While serializing JSON link", e);
-// }
-// }
+
+ /*
+ * ------------------ Protected methods ------------------
+ */
+ protected <T extends AbstractPayloadObject> void atom(final T obj, final Writer writer) {
+ try {
+ dom(atomSerializer.serialize(obj), writer);
+ } catch (Exception e) {
+ throw new IllegalArgumentException("While serializing Atom object", e);
+ }
+ }
+
+ protected <T extends AbstractPayloadObject> void json(final T obj, final Writer writer) {
+ try {
+ getObjectMapper().writeValue(writer, obj);
+ } catch (IOException e) {
+ throw new IllegalArgumentException("While serializing JSON object", e);
+ }
+ }
+
+ protected void json(final Element element, final Writer writer) {
+ try {
+ final JSONPropertyImpl property = new JSONPropertyImpl();
+ property.setContent(element);
+ getObjectMapper().writeValue(writer, property);
+ } catch (IOException e) {
+ throw new IllegalArgumentException("While serializing JSON property", e);
+ }
+ }
+
+ protected void xmlLink(final Link link, final Writer writer) {
+ try {
+ final DocumentBuilder builder = XMLUtils.DOC_BUILDER_FACTORY.newDocumentBuilder();
+ final Document doc = builder.newDocument();
+ final Element uri = doc.createElementNS(
+ client.getServiceVersion().getNamespaceMap().get(ODataServiceVersion.NS_DATASERVICES),
+ ODataConstants.ELEM_URI);
+ uri.appendChild(doc.createTextNode(link.getHref()));
+
+ dom(uri, writer);
+ } catch (Exception e) {
+ throw new IllegalArgumentException("While serializing XML link", e);
+ }
+ }
+
+ protected void jsonLink(final Link link, final Writer writer) {
+ final ObjectMapper mapper = getObjectMapper();
+ final ObjectNode uri = mapper.createObjectNode();
+ uri.put(ODataConstants.JSON_URL, link.getHref());
+
+ try {
+ mapper.writeValue(writer, uri);
+ } catch (Exception e) {
+ throw new IllegalArgumentException("While serializing JSON link", e);
+ }
+ }
}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata4/blob/78c3eaa4/lib/client-core/src/main/java/org/apache/olingo/client/core/op/impl/ODataObjectFactoryImpl.java
----------------------------------------------------------------------
diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/op/impl/ODataObjectFactoryImpl.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/op/impl/ODataObjectFactoryImpl.java
new file mode 100644
index 0000000..1855135
--- /dev/null
+++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/op/impl/ODataObjectFactoryImpl.java
@@ -0,0 +1,287 @@
+/*
+ * 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.op.impl;
+
+import java.net.URI;
+import org.apache.olingo.client.api.ODataClient;
+import org.apache.olingo.client.api.data.LinkType;
+import org.apache.olingo.client.api.domain.ODataCollectionValue;
+import org.apache.olingo.client.api.domain.ODataComplexValue;
+import org.apache.olingo.client.api.domain.ODataEntity;
+import org.apache.olingo.client.api.domain.ODataEntitySet;
+import org.apache.olingo.client.api.domain.ODataInlineEntity;
+import org.apache.olingo.client.api.domain.ODataInlineEntitySet;
+import org.apache.olingo.client.api.domain.ODataLink;
+import org.apache.olingo.client.api.domain.ODataObjectFactory;
+import org.apache.olingo.client.api.domain.ODataPrimitiveValue;
+import org.apache.olingo.client.api.domain.ODataProperty;
+
+public class ODataObjectFactoryImpl implements ODataObjectFactory {
+
+ private static final long serialVersionUID = -3769695665946919447L;
+
+ protected final ODataClient client;
+
+ public ODataObjectFactoryImpl(final ODataClient client) {
+ this.client = client;
+ }
+
+ /**
+ * Instantiates a new entity set.
+ *
+ * @return entity set.
+ */
+ @Override
+ public ODataEntitySet newEntitySet() {
+ return new ODataEntitySet();
+ }
+
+ /**
+ * Instantiates a new entity set.
+ *
+ * @param next next link.
+ * @return entity set.
+ */
+ @Override
+ public ODataEntitySet newEntitySet(final URI next) {
+ return new ODataEntitySet(next);
+ }
+
+ /**
+ * Instantiates a new entity.
+ *
+ * @param name OData entity name.
+ * @return entity.
+ */
+ @Override
+ public ODataEntity newEntity(final String name) {
+ return new ODataEntity(name);
+ }
+
+ /**
+ * Instantiates a new entity.
+ *
+ * @param name OData entity name.
+ * @param link self link.
+ * @return entity.
+ */
+ @Override
+ public ODataEntity newEntity(final String name, final URI link) {
+ final ODataEntity result = new ODataEntity(name);
+ result.setLink(link);
+ return result;
+ }
+
+ /**
+ * Instantiates a new in-line entity set.
+ *
+ * @param name name.
+ * @param link edit link.
+ * @param entitySet entity set.
+ * @return in-line entity set.
+ */
+ @Override
+ public ODataInlineEntitySet newInlineEntitySet(final String name, final URI link,
+ final ODataEntitySet entitySet) {
+
+ return new ODataInlineEntitySet(client, link, LinkType.ENTITY_SET_NAVIGATION, name, entitySet);
+ }
+
+ /**
+ * Instantiates a new in-line entity set.
+ *
+ * @param name name.
+ * @param baseURI base URI.
+ * @param href href.
+ * @param entitySet entity set.
+ * @return in-line entity set.
+ */
+ @Override
+ public ODataInlineEntitySet newInlineEntitySet(final String name, final URI baseURI, final String href,
+ final ODataEntitySet entitySet) {
+
+ return new ODataInlineEntitySet(client, baseURI, href, LinkType.ENTITY_SET_NAVIGATION, name, entitySet);
+ }
+
+ /**
+ * Instantiates a new in-line entity.
+ *
+ * @param name name.
+ * @param link edit link.
+ * @param entity entity.
+ * @return in-line entity.
+ */
+ @Override
+ public ODataInlineEntity newInlineEntity(final String name, final URI link, final ODataEntity entity) {
+ return new ODataInlineEntity(client, link, LinkType.ENTITY_NAVIGATION, name, entity);
+ }
+
+ /**
+ * Instantiates a new in-line entity.
+ *
+ * @param name name.
+ * @param baseURI base URI.
+ * @param href href.
+ * @param entity entity.
+ * @return in-line entity.
+ */
+ @Override
+ public ODataInlineEntity newInlineEntity(final String name, final URI baseURI, final String href,
+ final ODataEntity entity) {
+
+ return new ODataInlineEntity(client, baseURI, href, LinkType.ENTITY_NAVIGATION, name, entity);
+ }
+
+ /**
+ * Instantiates a new entity navigation link.
+ *
+ * @param name name.
+ * @param link link.
+ * @return entity navigation link.
+ */
+ @Override
+ public ODataLink newEntityNavigationLink(final String name, final URI link) {
+ return new ODataLink(client, link, LinkType.ENTITY_NAVIGATION, name);
+ }
+
+ /**
+ * Instantiates a new entity navigation link.
+ *
+ * @param name name.
+ * @param baseURI base URI.
+ * @param href href.
+ * @return entity navigation link.
+ */
+ @Override
+ public ODataLink newEntityNavigationLink(final String name, final URI baseURI, final String href) {
+ return new ODataLink(client, baseURI, href, LinkType.ENTITY_NAVIGATION, name);
+ }
+
+ /**
+ * Instantiates a new entity set navigation link.
+ *
+ * @param name name.
+ * @param link link.
+ * @return entity set navigation link.
+ */
+ @Override
+ public ODataLink newFeedNavigationLink(final String name, final URI link) {
+ return new ODataLink(client, link, LinkType.ENTITY_SET_NAVIGATION, name);
+ }
+
+ /**
+ * Instantiates a new entity set navigation link.
+ *
+ * @param name name.
+ * @param baseURI base URI.
+ * @param href href.
+ * @return entity set navigation link.
+ */
+ @Override
+ public ODataLink newFeedNavigationLink(final String name, final URI baseURI, final String href) {
+ return new ODataLink(client, baseURI, href, LinkType.ENTITY_SET_NAVIGATION, name);
+ }
+
+ /**
+ * Instantiates a new association link.
+ *
+ * @param name name.
+ * @param link link.
+ * @return association link.
+ */
+ @Override
+ public ODataLink newAssociationLink(final String name, final URI link) {
+ return new ODataLink(client, link, LinkType.ASSOCIATION, name);
+ }
+
+ /**
+ * Instantiates a new association link.
+ *
+ * @param name name.
+ * @param baseURI base URI.
+ * @param href href.
+ * @return association link.
+ */
+ @Override
+ public ODataLink newAssociationLink(final String name, final URI baseURI, final String href) {
+ return new ODataLink(client, baseURI, href, LinkType.ASSOCIATION, name);
+ }
+
+ /**
+ * Instantiates a new media-edit link.
+ *
+ * @param name name.
+ * @param link link.
+ * @return media-edit link.
+ */
+ @Override
+ public ODataLink newMediaEditLink(final String name, final URI link) {
+ return new ODataLink(client, link, LinkType.MEDIA_EDIT, name);
+ }
+
+ /**
+ * Instantiates a new media-edit link.
+ *
+ * @param name name.
+ * @param baseURI base URI.
+ * @param href href.
+ * @return media-edit link.
+ */
+ @Override
+ public ODataLink newMediaEditLink(final String name, final URI baseURI, final String href) {
+ return new ODataLink(client, baseURI, href, LinkType.MEDIA_EDIT, name);
+ }
+
+ /**
+ * Instantiates a new primitive property.
+ *
+ * @param name name.
+ * @param value value.
+ * @return primitive property.
+ */
+ @Override
+ public ODataProperty newPrimitiveProperty(final String name, final ODataPrimitiveValue value) {
+ return new ODataProperty(name, value);
+ }
+
+ /**
+ * Instantiates a new complex property.
+ *
+ * @param name name.
+ * @param value value.
+ * @return complex property.
+ */
+ @Override
+ public ODataProperty newComplexProperty(final String name, final ODataComplexValue value) {
+ return new ODataProperty(name, value);
+ }
+
+ /**
+ * Instantiates a new collection property.
+ *
+ * @param name name.
+ * @param value value.
+ * @return collection property.
+ */
+ @Override
+ public ODataProperty newCollectionProperty(final String name, final ODataCollectionValue value) {
+ return new ODataProperty(name, value);
+ }
+
+}