org.apache.olingo.odata2.api.edm.provider.AnnotationAttribute Java Examples

The following examples show how to use org.apache.olingo.odata2.api.edm.provider.AnnotationAttribute. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XmlMetadataProducer.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private static void writeAnnotationAttributes(final Collection<AnnotationAttribute> annotationAttributes,
    final Map<String, String> predefinedNamespaces, ArrayList<String> setNamespaces,
    final XMLStreamWriter xmlStreamWriter) throws XMLStreamException {
  if (annotationAttributes != null) {
    if (setNamespaces == null) {
      setNamespaces = new ArrayList<String>();
    }
    for (AnnotationAttribute annotationAttribute : annotationAttributes) {
      if (annotationAttribute.getNamespace() != null) {
        xmlStreamWriter.writeAttribute(annotationAttribute.getPrefix(), annotationAttribute.getNamespace(),
            annotationAttribute.getName(), annotationAttribute.getText());
        if (setNamespaces.contains(annotationAttribute.getNamespace()) == false
            && predefinedNamespaces.containsValue(annotationAttribute.getNamespace()) == false) {
          xmlStreamWriter.writeNamespace(annotationAttribute.getPrefix(), annotationAttribute.getNamespace());
          setNamespaces.add(annotationAttribute.getNamespace());
        }
      } else {
        xmlStreamWriter.writeAttribute(annotationAttribute.getName(), annotationAttribute.getText());
      }
    }
  }
}
 
Example #2
Source File: EdmAnnotationsImplProvTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Before
public void getEdmEntityContainerImpl() throws Exception {

  List<AnnotationAttribute> annotationAttributes = new ArrayList<AnnotationAttribute>();
  AnnotationAttribute attribute =
      new AnnotationAttribute().setName("attributeName").setNamespace("namespace").setPrefix("prefix")
          .setText("Text");
  annotationAttributes.add(attribute);

  List<AnnotationElement> annotationElements = new ArrayList<AnnotationElement>();
  AnnotationElement element =
      new AnnotationElement().setName("elementName").setNamespace("namespace").setPrefix("prefix").setText("xmlData");
  annotationElements.add(element);

  annotationsProvider = new EdmAnnotationsImplProv(annotationAttributes, annotationElements);

  annotationsProviderWithNullEementAndAttributes = new EdmAnnotationsImplProv(null, null);
}
 
Example #3
Source File: XmlMetadataConsumer.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private List<AnnotationAttribute> readAnnotationAttribute(final XMLStreamReader reader) {
  List<AnnotationAttribute> annotationAttributes = new ArrayList<AnnotationAttribute>();
  for (int i = 0; i < reader.getAttributeCount(); i++) {
    String attributeNamespace = reader.getAttributeNamespace(i);
    if (attributeNamespace != null && !isDefaultNamespace(attributeNamespace)
        && !mandatoryNamespaces.containsValue(attributeNamespace)
        && !edmNamespaces.contains(attributeNamespace)) {
      annotationAttributes.add(new AnnotationAttribute().setName(reader.getAttributeLocalName(i)).
          setPrefix(reader.getAttributePrefix(i)).setNamespace(attributeNamespace).setText(
              reader.getAttributeValue(i)));
    }
  }
  if (annotationAttributes.isEmpty()) {
    return null;
  }
  return annotationAttributes;
}
 
Example #4
Source File: XmlMetadataConsumer.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private Key readEntityTypeKey(final XMLStreamReader reader) throws XMLStreamException, EntityProviderException {
  reader.require(XMLStreamConstants.START_ELEMENT, edmNamespace, XmlMetadataConstants.EDM_ENTITY_TYPE_KEY);
  List<PropertyRef> keys = new ArrayList<PropertyRef>();
  List<AnnotationElement> annotationElements = new ArrayList<AnnotationElement>();
  List<AnnotationAttribute> annotationAttributes = readAnnotationAttribute(reader);
  while (reader.hasNext()
      && !(reader.isEndElement() && edmNamespace.equals(reader.getNamespaceURI())
      && XmlMetadataConstants.EDM_ENTITY_TYPE_KEY.equals(reader.getLocalName()))) {
    reader.next();
    if (reader.isStartElement()) {
      extractNamespaces(reader);
      currentHandledStartTagName = reader.getLocalName();
      if (XmlMetadataConstants.EDM_PROPERTY_REF.equals(currentHandledStartTagName)) {
        reader.require(XMLStreamConstants.START_ELEMENT, edmNamespace, XmlMetadataConstants.EDM_PROPERTY_REF);
        keys.add(readPropertyRef(reader));
      } else {
        annotationElements.add(readAnnotationElement(reader));
      }
    }
  }
  Key key = new Key().setKeys(keys).setAnnotationAttributes(annotationAttributes);
  if (!annotationElements.isEmpty()) {
    key.setAnnotationElements(annotationElements);
  }
  return key;
}
 
Example #5
Source File: JPAEdmExtension.java    From odata-boilerplate with MIT License 6 votes vote down vote up
@Override
public void extendJPAEdmSchema(JPAEdmSchemaView view) {
	ResourceBundle i18n = ODataContextUtil.getResourceBundle("i18n");
	final Schema edmSchema = view.getEdmSchema();
	
	for (EntityType entityType : edmSchema.getEntityTypes()) {
		for (Property property : entityType.getProperties()) {
			String label = null;
			if (i18n != null) { try { label = i18n.getString(entityType.getName() + "." + property.getName()); } catch (Exception e) {} }
			List<AnnotationAttribute> annotationAttributeList = new ArrayList<AnnotationAttribute>();
			if (label != null) {
				annotationAttributeList.add(new AnnotationAttribute()
						.setNamespace(SAP_NAMESPACE)
						.setPrefix(SAP_PREFIX)
						.setName(LABEL).setText(label));
			}
			annotationAttributeList.addAll(getSapPropertyAnnotations(entityType, property));
			property.setAnnotationAttributes(annotationAttributeList); 
		}
	}
	
	addSmartAnnotations(edmSchema);
}
 
Example #6
Source File: EdmAnnotationsImplProv.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
public EdmAnnotationsImplProv(final List<AnnotationAttribute> annotationAttributes,
    final List<AnnotationElement> annotationElements) {
  if (annotationAttributes != null) {
    this.annotationAttributes = new ArrayList<EdmAnnotationAttribute>();
    this.annotationAttributes.addAll(annotationAttributes);
  }
  if (annotationElements != null) {
    this.annotationElements = new ArrayList<EdmAnnotationElement>();
    for (AnnotationElement element : annotationElements) {
      EdmAnnotationElement edmElement = new EdmAnnotationElementImplProv(element);
      this.annotationElements.add(edmElement);
    }
  }
}
 
Example #7
Source File: XmlMetadataProducerTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void writeValidMetadata4() throws Exception {

  List<Schema> schemas = new ArrayList<Schema>();

  List<AnnotationAttribute> attributesElement1 = new ArrayList<AnnotationAttribute>();
  attributesElement1.add(new AnnotationAttribute().setName("rel").setText("self"));
  attributesElement1.add(new AnnotationAttribute().setName("href").setText("link"));

  List<AnnotationElement> schemaElements = new ArrayList<AnnotationElement>();
  schemaElements.add(new AnnotationElement().setName("schemaElementTest1").setPrefix("atom").setNamespace(
      "http://www.w3.org/2005/Atom").setAttributes(attributesElement1));
  schemaElements.add(new AnnotationElement().setName("schemaElementTest2").setPrefix("atom").setNamespace(
      "http://www.w3.org/2005/Atom").setAttributes(attributesElement1));

  Schema schema = new Schema().setAnnotationElements(schemaElements);
  schema.setNamespace("http://namespace.com");
  schemas.add(schema);

  DataServices data = new DataServices().setSchemas(schemas).setDataServiceVersion(ODataServiceVersion.V20);
  OutputStreamWriter writer = null;
  CircleStreamBuffer csb = new CircleStreamBuffer();
  writer = new OutputStreamWriter(csb.getOutputStream(), "UTF-8");
  XMLStreamWriter xmlStreamWriter = xmlStreamWriterFactory.createXMLStreamWriter(writer);
  XmlMetadataProducer.writeMetadata(data, xmlStreamWriter, null);
  String metadata = StringHelper.inputStreamToString(csb.getInputStream());

  Map<String, String> prefixMap = new HashMap<String, String>();
  prefixMap.put("edmx", "http://schemas.microsoft.com/ado/2007/06/edmx");
  prefixMap.put("a", "http://schemas.microsoft.com/ado/2008/09/edm");
  prefixMap.put("atom", "http://www.w3.org/2005/Atom");

  NamespaceContext ctx = new SimpleNamespaceContext(prefixMap);
  XMLUnit.setXpathNamespaceContext(ctx);

  assertXpathExists("/edmx:Edmx/edmx:DataServices/a:Schema/atom:schemaElementTest1", metadata);
  assertXpathExists("/edmx:Edmx/edmx:DataServices/a:Schema/atom:schemaElementTest2", metadata);
}
 
Example #8
Source File: XmlMetadataProducerTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void writeValidMetadata5() throws Exception {

  List<Schema> schemas = new ArrayList<Schema>();

  List<AnnotationAttribute> attributesElement1 = new ArrayList<AnnotationAttribute>();
  attributesElement1.add(new AnnotationAttribute().setName("rel").setText("self").setPrefix("atom").setNamespace(
      "http://www.w3.org/2005/Atom"));
  attributesElement1.add(new AnnotationAttribute().setName("href").setText("link").setPrefix("atom").setNamespace(
      "http://www.w3.org/2005/Atom"));

  List<AnnotationElement> schemaElements = new ArrayList<AnnotationElement>();
  schemaElements.add(new AnnotationElement().setName("schemaElementTest1").setPrefix("atom").setNamespace(
      "http://www.w3.org/2005/Atom").setAttributes(attributesElement1));
  schemaElements.add(new AnnotationElement().setName("schemaElementTest2").setPrefix("atom").setNamespace(
      "http://www.w3.org/2005/Atom").setAttributes(attributesElement1));

  Schema schema = new Schema().setAnnotationElements(schemaElements);
  schema.setNamespace("http://namespace.com");
  schemas.add(schema);

  DataServices data = new DataServices().setSchemas(schemas).setDataServiceVersion(ODataServiceVersion.V20);
  OutputStreamWriter writer = null;
  CircleStreamBuffer csb = new CircleStreamBuffer();
  writer = new OutputStreamWriter(csb.getOutputStream(), "UTF-8");
  XMLStreamWriter xmlStreamWriter = xmlStreamWriterFactory.createXMLStreamWriter(writer);
  XmlMetadataProducer.writeMetadata(data, xmlStreamWriter, null);
  String metadata = StringHelper.inputStreamToString(csb.getInputStream());

  Map<String, String> prefixMap = new HashMap<String, String>();
  prefixMap.put("edmx", "http://schemas.microsoft.com/ado/2007/06/edmx");
  prefixMap.put("a", "http://schemas.microsoft.com/ado/2008/09/edm");
  prefixMap.put("atom", "http://www.w3.org/2005/Atom");

  NamespaceContext ctx = new SimpleNamespaceContext(prefixMap);
  XMLUnit.setXpathNamespaceContext(ctx);

  assertXpathExists("/edmx:Edmx/edmx:DataServices/a:Schema/atom:schemaElementTest1", metadata);
  assertXpathExists("/edmx:Edmx/edmx:DataServices/a:Schema/atom:schemaElementTest2", metadata);
}
 
Example #9
Source File: XmlMetadataConsumerTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private AnnotationElement createElementWithInclude() {
  List<AnnotationAttribute> childAttributes = new ArrayList<AnnotationAttribute>();
  childAttributes.add(new AnnotationAttribute().setName("Namespace").setText("Org.OData.Core.V1"));
  childAttributes.add(new AnnotationAttribute().setName("Alias").setText("UI"));
  List<AnnotationElement> childElements = new ArrayList<AnnotationElement>();
  childElements.add(new AnnotationElement().setName("Include").setNamespace(
      "http://docs.oasis-open.org/odata/ns/edmx").setPrefix("edmx").setAttributes(childAttributes));
  List<AnnotationAttribute> referenceAttributes = new ArrayList<AnnotationAttribute>();
  referenceAttributes.add(new AnnotationAttribute().setName("Uri").setText("http://someurl2.com"));
  return new AnnotationElement().setName("Reference").setPrefix("edmx").setNamespace(
      "http://docs.oasis-open.org/odata/ns/edmx").setAttributes(referenceAttributes).setChildElements(childElements);
}
 
Example #10
Source File: BasicProviderTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private AnnotationElement createElementWithInclude() {
  List<AnnotationAttribute> childAttributes = new ArrayList<AnnotationAttribute>();
  childAttributes.add(new AnnotationAttribute().setName("Namespace").setText("Org.OData.Core.V1"));
  childAttributes.add(new AnnotationAttribute().setName("Alias").setText("UI"));
  List<AnnotationElement> childElements = new ArrayList<AnnotationElement>();
  childElements.add(new AnnotationElement().setName("Include").setNamespace(
      "http://docs.oasis-open.org/odata/ns/edmx").setPrefix("edmx").setAttributes(childAttributes));
  List<AnnotationAttribute> referenceAttributes = new ArrayList<AnnotationAttribute>();
  referenceAttributes.add(new AnnotationAttribute().setName("Uri").setText("http://someurl2.com"));
  return new AnnotationElement().setName("Reference").setPrefix("edmx").setNamespace(
      "http://docs.oasis-open.org/odata/ns/edmx").setAttributes(referenceAttributes).setChildElements(childElements);
}
 
Example #11
Source File: XmlMetadataConsumer.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private AnnotationElement readAnnotationElement(final XMLStreamReader reader) throws XMLStreamException {
  AnnotationElement aElement = new AnnotationElement();
  List<AnnotationElement> annotationElements = new ArrayList<AnnotationElement>();
  List<AnnotationAttribute> annotationAttributes = new ArrayList<AnnotationAttribute>();
  aElement.setName(reader.getLocalName());
  String elementNamespace = reader.getNamespaceURI();
  if (!edmNamespaces.contains(elementNamespace)) {
    aElement.setPrefix(reader.getPrefix());
    aElement.setNamespace(elementNamespace);
  }
  for (int i = 0; i < reader.getAttributeCount(); i++) {
    AnnotationAttribute annotationAttribute = new AnnotationAttribute();
    annotationAttribute.setText(reader.getAttributeValue(i));
    annotationAttribute.setName(reader.getAttributeLocalName(i));
    annotationAttribute.setPrefix(reader.getAttributePrefix(i));
    String namespace = reader.getAttributeNamespace(i);
    if (namespace != null && !isDefaultNamespace(namespace)) {
      annotationAttribute.setNamespace(namespace);
    }
    annotationAttributes.add(annotationAttribute);
  }
  if (!annotationAttributes.isEmpty()) {
    aElement.setAttributes(annotationAttributes);
  }

  boolean justRead = false;
  if (reader.hasNext()) {
    reader.next();
    justRead = true;
  }

  while (justRead && !(reader.isEndElement() && aElement.getName() != null
      && aElement.getName().equals(reader.getLocalName()))) {
    justRead = false;
    if (reader.isStartElement()) {
      annotationElements.add(readAnnotationElement(reader));
      if (reader.hasNext()) {
        reader.next();
        justRead = true;
      }
    } else if (reader.isCharacters()) {
      String elementText = "";
      do {
        justRead = false;
        elementText = elementText + reader.getText();
        if (reader.hasNext()) {
          reader.next();
          justRead = true;
        }
      } while (justRead && reader.isCharacters());
      aElement.setText(elementText);
    }
  }
  if (!annotationElements.isEmpty()) {
    aElement.setChildElements(annotationElements);
  }
  return aElement;
}
 
Example #12
Source File: XmlMetadataProducerTest.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Test
public void writeValidMetadata2() throws Exception {
  List<Schema> schemas = new ArrayList<Schema>();

  List<AnnotationElement> childElements = new ArrayList<AnnotationElement>();
  childElements
      .add(new AnnotationElement().setName("schemaElementTest2").setText("text2").setNamespace("namespace1"));

  List<AnnotationAttribute> elementAttributes = new ArrayList<AnnotationAttribute>();
  elementAttributes.add(new AnnotationAttribute().setName("rel").setText("self"));
  elementAttributes.add(new AnnotationAttribute().setName("href").setText("http://google.com").setPrefix("pre")
      .setNamespace("namespaceForAnno"));

  List<AnnotationElement> element3List = new ArrayList<AnnotationElement>();
  element3List.add(new AnnotationElement().setName("schemaElementTest4").setText("text4").setAttributes(
      elementAttributes));
  childElements.add(new AnnotationElement().setName("schemaElementTest3").setText("text3").setPrefix("prefix")
      .setNamespace("namespace2").setChildElements(element3List));

  List<AnnotationElement> schemaElements = new ArrayList<AnnotationElement>();
  schemaElements.add(new AnnotationElement().setName("schemaElementTest1").setText("text1").setChildElements(
      childElements));

  schemaElements.add(new AnnotationElement().setName("test"));
  Schema schema = new Schema().setAnnotationElements(schemaElements);
  schema.setNamespace("http://namespace.com");
  schemas.add(schema);

  DataServices data = new DataServices().setSchemas(schemas).setDataServiceVersion(ODataServiceVersion.V20);
  OutputStreamWriter writer = null;
  CircleStreamBuffer csb = new CircleStreamBuffer();
  writer = new OutputStreamWriter(csb.getOutputStream(), "UTF-8");
  XMLStreamWriter xmlStreamWriter = xmlStreamWriterFactory.createXMLStreamWriter(writer);
  XmlMetadataProducer.writeMetadata(data, xmlStreamWriter, null);

  Map<String, String> prefixMap = new HashMap<String, String>();
  prefixMap.put("edmx", "http://schemas.microsoft.com/ado/2007/06/edmx");
  prefixMap.put("a", "http://schemas.microsoft.com/ado/2008/09/edm");
  prefixMap.put("b", "namespace1");
  prefixMap.put("prefix", "namespace2");
  prefixMap.put("pre", "namespaceForAnno");

  NamespaceContext ctx = new SimpleNamespaceContext(prefixMap);
  XMLUnit.setXpathNamespaceContext(ctx);

  String metadata = StringHelper.inputStreamToString(csb.getInputStream());
  assertXpathExists("/edmx:Edmx/edmx:DataServices/a:Schema/a:test", metadata);
  assertXpathExists("/edmx:Edmx/edmx:DataServices/a:Schema/a:schemaElementTest1", metadata);
  assertXpathExists("/edmx:Edmx/edmx:DataServices/a:Schema/a:schemaElementTest1/b:schemaElementTest2", metadata);
  assertXpathExists("/edmx:Edmx/edmx:DataServices/a:Schema/a:schemaElementTest1/prefix:schemaElementTest3", metadata);
  assertXpathExists(
      "/edmx:Edmx/edmx:DataServices/a:Schema/a:schemaElementTest1/prefix:schemaElementTest3/" +
          "a:schemaElementTest4[@rel=\"self\" and @pre:href=\"http://google.com\"]",
      metadata);

}
 
Example #13
Source File: XmlMetadataProducerTest.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Test
public void writeValidMetadata6() throws Exception {

  List<Schema> schemas = new ArrayList<Schema>();

  List<AnnotationAttribute> attributesElement1 = new ArrayList<AnnotationAttribute>();
  attributesElement1.add(new AnnotationAttribute().setName("rel").setText("self").setPrefix("atom").setNamespace(
      "http://www.w3.org/2005/Atom"));
  attributesElement1.add(new AnnotationAttribute().setName("href").setText("link").setPrefix("atom").setNamespace(
      "http://www.w3.org/2005/Atom"));

  List<AnnotationElement> elementElements = new ArrayList<AnnotationElement>();
  elementElements.add(new AnnotationElement().setName("schemaElementTest2").setPrefix("atom").setNamespace(
      "http://www.w3.org/2005/Atom").setAttributes(attributesElement1));
  elementElements.add(new AnnotationElement().setName("schemaElementTest3").setPrefix("atom").setNamespace(
      "http://www.w3.org/2005/Atom").setAttributes(attributesElement1));

  List<AnnotationElement> schemaElements = new ArrayList<AnnotationElement>();
  schemaElements.add(new AnnotationElement().setName("schemaElementTest1").setPrefix("atom").setNamespace(
      "http://www.w3.org/2005/Atom").setAttributes(attributesElement1).setChildElements(elementElements));

  Schema schema = new Schema().setAnnotationElements(schemaElements);
  schema.setNamespace("http://namespace.com");
  schemas.add(schema);

  DataServices data = new DataServices().setSchemas(schemas).setDataServiceVersion(ODataServiceVersion.V20);
  OutputStreamWriter writer = null;
  CircleStreamBuffer csb = new CircleStreamBuffer();
  writer = new OutputStreamWriter(csb.getOutputStream(), "UTF-8");
  XMLStreamWriter xmlStreamWriter = xmlStreamWriterFactory.createXMLStreamWriter(writer);
  XmlMetadataProducer.writeMetadata(data, xmlStreamWriter, null);
  String metadata = StringHelper.inputStreamToString(csb.getInputStream());

  Map<String, String> prefixMap = new HashMap<String, String>();
  prefixMap.put("edmx", "http://schemas.microsoft.com/ado/2007/06/edmx");
  prefixMap.put("a", "http://schemas.microsoft.com/ado/2008/09/edm");
  prefixMap.put("atom", "http://www.w3.org/2005/Atom");

  NamespaceContext ctx = new SimpleNamespaceContext(prefixMap);
  XMLUnit.setXpathNamespaceContext(ctx);

  assertXpathExists("/edmx:Edmx/edmx:DataServices/a:Schema/atom:schemaElementTest1", metadata);
  assertXpathExists("/edmx:Edmx/edmx:DataServices/a:Schema/atom:schemaElementTest1/atom:schemaElementTest2",
      metadata);
  assertXpathExists("/edmx:Edmx/edmx:DataServices/a:Schema/atom:schemaElementTest1/atom:schemaElementTest3",
      metadata);
}
 
Example #14
Source File: XmlMetadataProducerTest.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Test
public void writeWithPredefinedNamespaces() throws Exception {
  // prepare
  List<Schema> schemas = new ArrayList<Schema>();

  List<AnnotationAttribute> attributesElement1 = new ArrayList<AnnotationAttribute>();
  attributesElement1.add(new AnnotationAttribute().setName("rel").setText("self").setPrefix("foo").setNamespace(
      "http://www.foo.bar/Protocols/Data"));
  attributesElement1.add(new AnnotationAttribute().setName("href").setText("link").setPrefix("foo").setNamespace(
      "http://www.foo.bar/Protocols/Data"));

  List<AnnotationElement> elementElements = new ArrayList<AnnotationElement>();
  elementElements.add(new AnnotationElement().setName("schemaElementTest2").setPrefix("foo").setNamespace(
      "http://www.foo.bar/Protocols/Data").setAttributes(attributesElement1));
  elementElements.add(new AnnotationElement().setName("schemaElementTest3").setPrefix("foo").setNamespace(
      "http://www.foo.bar/Protocols/Data").setAttributes(attributesElement1));

  List<AnnotationElement> schemaElements = new ArrayList<AnnotationElement>();
  schemaElements.add(new AnnotationElement().setName("schemaElementTest1").setPrefix("foo").setNamespace(
      "http://www.foo.bar/Protocols/Data").setAttributes(attributesElement1).setChildElements(elementElements));

  Schema schema = new Schema().setAnnotationElements(schemaElements);
  schema.setNamespace("http://namespace.com");
  schemas.add(schema);

  // Execute
  Map<String, String> predefinedNamespaces = new HashMap<String, String>();
  predefinedNamespaces.put("foo", "http://www.foo.bar/Protocols/Data");
  DataServices data = new DataServices().setSchemas(schemas).setDataServiceVersion(ODataServiceVersion.V20);
  OutputStreamWriter writer = null;
  CircleStreamBuffer csb = new CircleStreamBuffer();
  writer = new OutputStreamWriter(csb.getOutputStream(), "UTF-8");
  XMLStreamWriter xmlStreamWriter = xmlStreamWriterFactory.createXMLStreamWriter(writer);
  XmlMetadataProducer.writeMetadata(data, xmlStreamWriter, predefinedNamespaces);
  String metadata = StringHelper.inputStreamToString(csb.getInputStream());

  // Verify
  Map<String, String> prefixMap = new HashMap<String, String>();
  prefixMap.put("edmx", "http://schemas.microsoft.com/ado/2007/06/edmx");
  prefixMap.put("a", "http://schemas.microsoft.com/ado/2008/09/edm");
  prefixMap.put("foo", "http://www.foo.bar/Protocols/Data");

  NamespaceContext ctx = new SimpleNamespaceContext(prefixMap);
  XMLUnit.setXpathNamespaceContext(ctx);

  assertXpathExists("/edmx:Edmx/edmx:DataServices/a:Schema/foo:schemaElementTest1", metadata);
  assertXpathExists("/edmx:Edmx/edmx:DataServices/a:Schema/foo:schemaElementTest1/foo:schemaElementTest2", metadata);
  assertXpathExists("/edmx:Edmx/edmx:DataServices/a:Schema/foo:schemaElementTest1/foo:schemaElementTest3", metadata);
}
 
Example #15
Source File: XmlMetadataConsumerTest.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Test
public void testAnnotations() throws XMLStreamException, EntityProviderException {
  final String xmlWithAnnotations =
      "<edmx:Edmx Version=\"1.0\" xmlns:edmx=\""
          + Edm.NAMESPACE_EDMX_2007_06
          + "\" xmlns:annoPrefix=\"http://annoNamespace\">"
          + "<edmx:DataServices m:DataServiceVersion=\"2.0\" xmlns:m=\""
          + Edm.NAMESPACE_M_2007_08
          + "\">"
          + "<Schema Namespace=\""
          + NAMESPACE
          + "\" xmlns=\""
          + Edm.NAMESPACE_EDM_2008_09
          + "\">"
          + "<EntityType Name= \"Employee\" prefix1:href=\"http://foo\" xmlns:prefix1=\"namespaceForAnno\">"
          + "<Key><PropertyRef Name=\"EmployeeId\"/></Key>"
          + "<Property Name=\"EmployeeId\" Type=\"Edm.String\" Nullable=\"false\"/>"
          + "<Property Name=\"EmployeeName\" Type=\"Edm.String\" m:FC_TargetPath=\"SyndicationTitle\" " +
          "annoPrefix:annoName=\"annoText\">"
          + "<annoPrefix:propertyAnnoElement>text</annoPrefix:propertyAnnoElement>"
          + "<annoPrefix:propertyAnnoElement2 />"
          + "</Property>"
          + "</EntityType>"
          + "<annoPrefix:schemaElementTest1>"
          + "<prefix:schemaElementTest2 xmlns:prefix=\"namespace\">text3"
          + "</prefix:schemaElementTest2>"
          + "<annoPrefix:schemaElementTest3 rel=\"self\" pre:href=\"http://foo\" " +
          "xmlns:pre=\"namespaceForAnno\">text4</annoPrefix:schemaElementTest3>"
          + " </annoPrefix:schemaElementTest1>" + "</Schema>"
          + "</edmx:DataServices>" + "</edmx:Edmx>";
  XmlMetadataConsumer parser = new XmlMetadataConsumer();
  XMLStreamReader reader = createStreamReader(xmlWithAnnotations);
  DataServices result = parser.readMetadata(reader, false);
  for (Schema schema : result.getSchemas()) {
    assertEquals(1, schema.getAnnotationElements().size());
    for (AnnotationElement annoElement : schema.getAnnotationElements()) {
      for (AnnotationElement childAnnoElement : annoElement.getChildElements()) {
        if ("schemaElementTest2".equals(childAnnoElement.getName())) {
          assertEquals("prefix", childAnnoElement.getPrefix());
          assertEquals("namespace", childAnnoElement.getNamespace());
          assertEquals("text3", childAnnoElement.getText());
        } else if ("schemaElementTest3".equals(childAnnoElement.getName())) {
          assertEquals("text4", childAnnoElement.getText());
          assertEquals("rel", childAnnoElement.getAttributes().get(0).getName());
          assertEquals("self", childAnnoElement.getAttributes().get(0).getText());
          assertEquals("", childAnnoElement.getAttributes().get(0).getPrefix());
          assertNull(childAnnoElement.getAttributes().get(0).getNamespace());
          assertEquals("href", childAnnoElement.getAttributes().get(1).getName());
          assertEquals("pre", childAnnoElement.getAttributes().get(1).getPrefix());
          assertEquals("namespaceForAnno", childAnnoElement.getAttributes().get(1).getNamespace());
          assertEquals("http://foo", childAnnoElement.getAttributes().get(1).getText());
        } else {
          throw new EntityProviderException(null, "xmlWithAnnotations");
        }
      }
    }
    for (EntityType entityType : schema.getEntityTypes()) {
      assertEquals(1, entityType.getAnnotationAttributes().size());
      AnnotationAttribute attr = entityType.getAnnotationAttributes().get(0);
      assertEquals("href", attr.getName());
      assertEquals("prefix1", attr.getPrefix());
      assertEquals("namespaceForAnno", attr.getNamespace());
      assertEquals("http://foo", attr.getText());
      for (Property property : entityType.getProperties()) {
        if ("EmployeeName".equals(property.getName())) {
          assertEquals(2, property.getAnnotationElements().size());
          for (AnnotationElement anElement : property.getAnnotationElements()) {
            if ("propertyAnnoElement".equals(anElement.getName())) {
              assertEquals("text", anElement.getText());
            }
          }
          for (AnnotationAttribute anAttribute : property.getAnnotationAttributes()) {
            assertEquals("annoName", anAttribute.getName());
            assertEquals("annoPrefix", anAttribute.getPrefix());
            assertEquals("annoText", anAttribute.getText());
            assertEquals("http://annoNamespace", anAttribute.getNamespace());
          }
        }
      }
    }
  }
}
 
Example #16
Source File: XmlMetadataConsumerTest.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private AnnotationElement createElementWithoutInclude() {
  List<AnnotationAttribute> referenceAttributes = new ArrayList<AnnotationAttribute>();
  referenceAttributes.add(new AnnotationAttribute().setName("Uri").setText("http://someurl.com"));
  return new AnnotationElement().setName("Reference").setPrefix("edmx").setNamespace(
      "http://docs.oasis-open.org/odata/ns/edmx").setAttributes(referenceAttributes);
}
 
Example #17
Source File: BasicProviderTest.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private AnnotationElement createElementWithoutInclude() {
  List<AnnotationAttribute> referenceAttributes = new ArrayList<AnnotationAttribute>();
  referenceAttributes.add(new AnnotationAttribute().setName("Uri").setText("http://someurl.com"));
  return new AnnotationElement().setName("Reference").setPrefix("edmx").setNamespace(
      "http://docs.oasis-open.org/odata/ns/edmx").setAttributes(referenceAttributes);
}