Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2013 SAP AG Licensed 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 com.sap.core.odata.core.ep.consumer;
 
 import java.util.List;
 import java.util.Map;
 
 
Xml entity (content type dependent) consumer for reading input (from content).

Author(s):
SAP AG
 
 public class XmlEntityConsumer {

  
Default used charset for writer and response content header
 
   private static final String DEFAULT_CHARSET = "utf-8";
 
   public XmlEntityConsumer() throws EntityProviderException {
     super();
   }
 
   public ODataFeed readFeed(final EdmEntitySet entitySetfinal InputStream contentfinal EntityProviderReadProperties propertiesthrows EntityProviderException {
     XMLStreamReader reader = null;
     EntityProviderException cachedException = null;
 
     try {
       reader = createStaxReader(content);
 
       EntityInfoAggregator eia = EntityInfoAggregator.create(entitySet);
       XmlFeedConsumer xfc = new XmlFeedConsumer();
       return xfc.readFeed(readereiaproperties);
     } catch (EntityProviderException e) {
       cachedException = e;
       throw cachedException;
     } catch (XMLStreamException e) {
       cachedException = new EntityProviderException(.e);
       throw cachedException;
     } finally {// NOPMD (suppress DoNotThrowExceptionInFinally)
       if (reader != null) {
         try {
           reader.close();
         } catch (XMLStreamException e) {
           if (cachedException != null) {
             throw cachedException;
           } else {
             throw new EntityProviderException(.e);
           }
         }
       }
     }
   }
 
   public ODataEntry readEntry(final EdmEntitySet entitySetfinal InputStream contentfinal EntityProviderReadProperties propertiesthrows EntityProviderException {
     XMLStreamReader reader = null;
     EntityProviderException cachedException = null;
 
     try {
       XmlEntryConsumer xec = new XmlEntryConsumer();
       reader = createStaxReader(content);
 
       EntityInfoAggregator eia = EntityInfoAggregator.create(entitySet);
       ODataEntry result = xec.readEntry(readereiaproperties);
       return result;
     } catch (EntityProviderException e) {
       cachedException = e;
       throw cachedException;
     } catch (XMLStreamException e) {
       cachedException = new EntityProviderException(.e);
       throw cachedException;
     } finally {// NOPMD (suppress DoNotThrowExceptionInFinally)
      if (reader != null) {
        try {
          reader.close();
        } catch (XMLStreamException e) {
          if (cachedException != null) {
            throw cachedException;
          } else {
            throw new EntityProviderException(.e);
          }
        }
      }
    }
  }
  public Map<StringObjectreadProperty(final EdmProperty edmPropertyfinal InputStream contentfinal EntityProviderReadProperties propertiesthrows EntityProviderException {
    XMLStreamReader reader = null;
    EntityProviderException cachedException = null;
    try {
      reader = createStaxReader(content);
      Map<StringObjectresult = xec.readProperty(readeredmPropertyproperties.getMergeSemantic(), properties.getTypeMappings());
      return result;
    } catch (XMLStreamException e) {
      cachedException = new EntityProviderException(.e);
      throw cachedException;
    } finally {// NOPMD (suppress DoNotThrowExceptionInFinally)
      if (reader != null) {
        try {
          reader.close();
        } catch (XMLStreamException e) {
          if (cachedException != null) {
            throw cachedException;
          } else {
            throw new EntityProviderException(.e);
          }
        }
      }
    }
  }
  public Object readPropertyValue(final EdmProperty edmPropertyfinal InputStream contentthrows EntityProviderException {
    return readPropertyValue(edmPropertycontentnull);
  }
  public Object readPropertyValue(final EdmProperty edmPropertyfinal InputStream contentfinal Class<?> typeMappingthrows EntityProviderException {
    try {
      final Map<StringObjectresult;
      EntityProviderReadPropertiesBuilder propertiesBuilder = EntityProviderReadProperties.init().mergeSemantic(false);
      if (typeMapping == null) {
        result = readProperty(edmPropertycontentpropertiesBuilder.build());
      } else {
        Map<StringObjecttypeMappings = new HashMap<StringObject>();
        typeMappings.put(edmProperty.getName(), typeMapping);
        result = readProperty(edmPropertycontentpropertiesBuilder.addTypeMappings(typeMappings).build());
      }
      return result.get(edmProperty.getName());
    } catch (EdmException e) {
    }
  }
  public String readLink(final EdmEntitySet entitySetfinal Object contentthrows EntityProviderException {
    XMLStreamReader reader = null;
    EntityProviderException cachedException = null;
    XmlLinkConsumer xlc = new XmlLinkConsumer();
    try {
      reader = createStaxReader(content);
      return xlc.readLink(readerentitySet);
    } catch (XMLStreamException e) {
      cachedException = new EntityProviderException(.e);
      throw cachedException;
    } finally {// NOPMD (suppress DoNotThrowExceptionInFinally)
      if (reader != null) {
        try {
          reader.close();
        } catch (XMLStreamException e) {
          if (cachedException != null) {
            throw cachedException;
          } else {
            throw new EntityProviderException(.e);
          }
        }
      }
    }
  }
  public List<StringreadLinks(final EdmEntitySet entitySetfinal Object contentthrows EntityProviderException {
    XMLStreamReader reader = null;
    EntityProviderException cachedException = null;
    XmlLinkConsumer xlc = new XmlLinkConsumer();
    try {
      reader = createStaxReader(content);
      return xlc.readLinks(readerentitySet);
    } catch (XMLStreamException e) {
      cachedException = new EntityProviderException(.e);
      throw cachedException;
    } finally {// NOPMD (suppress DoNotThrowExceptionInFinally)
      if (reader != null) {
        try {
          reader.close();
        } catch (XMLStreamException e) {
          if (cachedException != null) {
            throw cachedException;
          } else {
            throw new EntityProviderException(.e);
          }
        }
      }
    }
  }
    XMLInputFactory factory = XMLInputFactory.newInstance();
    factory.setProperty(.false);
    if (content == null) {
          .addContent("Got not supported NULL object as content to de-serialize."));
    }
    if (content instanceof InputStream) {
      XMLStreamReader streamReader = factory.createXMLStreamReader((InputStreamcontent);
      // verify charset encoding set in content is supported (if not set UTF-8 is used as defined in 'http://www.w3.org/TR/2008/REC-xml-20081126/')
      String characterEncodingInContent = streamReader.getCharacterEncodingScheme();
      if (characterEncodingInContent != null && !.equalsIgnoreCase(characterEncodingInContent)) {
        throw new EntityProviderException(..addContent(characterEncodingInContent));
      }
      return streamReader;
    }
        .addContent("Found not supported content of class '" + content.getClass() + "' to de-serialize."));
  }
New to GrepCode? Check out our FAQ X