Java Code Examples for javax.xml.stream.XMLStreamReader#require()

The following examples show how to use javax.xml.stream.XMLStreamReader#require() . 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: 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 2
Source File: XmlLinkConsumer.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
/**
 * Reads multiple links with format
 * <pre> {@code
 * <links>
 *  <uri>http://somelink</uri>
 *  <uri>http://anotherLink</uri>
 *  <uri>http://somelink/yetAnotherLink</uri>
 * </links>
 * } </pre>
 * @param reader
 * @param entitySet
 * @return list of string based links
 * @throws EntityProviderException
 */
public List<String> readLinks(final XMLStreamReader reader, final EdmEntitySet entitySet)
    throws EntityProviderException {
  try {
    List<String> links = new ArrayList<String>();
    reader.nextTag();
    reader.require(XMLStreamConstants.START_ELEMENT, Edm.NAMESPACE_D_2007_08, FormatXml.D_LINKS);
    reader.nextTag();
    while (!reader.isEndElement()) {
      if (reader.getLocalName().equals(FormatXml.M_COUNT)) {
        readTag(reader, Edm.NAMESPACE_M_2007_08, FormatXml.M_COUNT);
      } else {
        final String link = readLink(reader);
        links.add(link);
      }
      reader.nextTag();
    }

    reader.require(XMLStreamConstants.END_ELEMENT, Edm.NAMESPACE_D_2007_08, FormatXml.D_LINKS);
    return links;
  } catch (final XMLStreamException e) {
    throw new EntityProviderException(EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass()
        .getSimpleName()), e);
  }
}
 
Example 3
Source File: XmlEntryConsumer.java    From cloud-odata-java with Apache License 2.0 5 votes vote down vote up
private void validateFeedTags(final XMLStreamReader reader, final ContentType cType) throws XMLStreamException, EntityProviderException {
  if (FormatXml.ATOM_FEED.equals(cType.getParameters().get(FormatXml.ATOM_TYPE))) {
    int next = reader.nextTag();
    if (XMLStreamConstants.START_ELEMENT == next) {
      reader.require(XMLStreamConstants.START_ELEMENT, Edm.NAMESPACE_ATOM_2005, FormatXml.ATOM_FEED);
    } else {
      reader.require(XMLStreamConstants.END_ELEMENT, Edm.NAMESPACE_M_2007_08, FormatXml.M_INLINE);
    }
  } else {
    throw new EntityProviderException(EntityProviderException.INVALID_INLINE_CONTENT.addContent("feed"));
  }
}
 
Example 4
Source File: XmlMetadataConsumer.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private EntitySet readEntitySet(final XMLStreamReader reader) throws XMLStreamException, EntityProviderException {
  reader.require(XMLStreamConstants.START_ELEMENT, edmNamespace, XmlMetadataConstants.EDM_ENTITY_SET);
  EntitySet entitySet = new EntitySet();
  List<AnnotationElement> annotationElements = new ArrayList<AnnotationElement>();
  entitySet.setName(reader.getAttributeValue(null, XmlMetadataConstants.EDM_NAME));
  String entityType = reader.getAttributeValue(null, XmlMetadataConstants.EDM_ENTITY_TYPE);
  if (entityType != null) {
    FullQualifiedName fqName = extractFQName(entityType);
    entitySet.setEntityType(fqName);
  } else {
    throw new EntityProviderException(EntityProviderException.MISSING_ATTRIBUTE
        .addContent(XmlMetadataConstants.EDM_ENTITY_TYPE).addContent(XmlMetadataConstants.EDM_ENTITY_SET));
  }
  entitySet.setAnnotationAttributes(readAnnotationAttribute(reader));
  while (reader.hasNext()
      && !(reader.isEndElement() && edmNamespace.equals(reader.getNamespaceURI())
      && XmlMetadataConstants.EDM_ENTITY_SET.equals(reader.getLocalName()))) {
    reader.next();
    if (reader.isStartElement()) {
      extractNamespaces(reader);
      annotationElements.add(readAnnotationElement(reader));
    }
  }
  if (!annotationElements.isEmpty()) {
    entitySet.setAnnotationElements(annotationElements);
  }
  return entitySet;
}
 
Example 5
Source File: AtomServiceDocumentConsumer.java    From cloud-odata-java with Apache License 2.0 5 votes vote down vote up
private CategoryImpl parseCategory(final XMLStreamReader reader) throws XMLStreamException {
  reader.require(XMLStreamConstants.START_ELEMENT, Edm.NAMESPACE_ATOM_2005, FormatXml.ATOM_CATEGORY);
  CategoryImpl category = new CategoryImpl();
  category.setScheme(reader.getAttributeValue(null, FormatXml.ATOM_CATEGORY_SCHEME));
  category.setTerm(reader.getAttributeValue(null, FormatXml.ATOM_CATEGORY_TERM));
  category.setLabel(reader.getAttributeValue(null, FormatXml.ATOM_CATEGORY_LABEL));
  CommonAttributesImpl attributes = parseCommonAttribute(reader);
  return category.setCommonAttributes(attributes);
}
 
Example 6
Source File: XmlMetadataConsumer.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private FunctionImportParameter readFunctionImportParameter(final XMLStreamReader reader)
    throws EntityProviderException, XMLStreamException {
  reader.require(XMLStreamConstants.START_ELEMENT, edmNamespace, XmlMetadataConstants.EDM_FUNCTION_PARAMETER);
  FunctionImportParameter functionParameter = new FunctionImportParameter();
  List<AnnotationElement> annotationElements = new ArrayList<AnnotationElement>();

  functionParameter.setName(reader.getAttributeValue(null, XmlMetadataConstants.EDM_NAME));
  functionParameter.setMode(reader.getAttributeValue(null, XmlMetadataConstants.EDM_FUNCTION_PARAMETER_MODE));
  String type = reader.getAttributeValue(null, XmlMetadataConstants.EDM_TYPE);
  if (type == null) {
    throw new EntityProviderException(EntityProviderException.MISSING_ATTRIBUTE
        .addContent(XmlMetadataConstants.EDM_TYPE).addContent(XmlMetadataConstants.EDM_FUNCTION_PARAMETER));
  }
  functionParameter.setType(EdmSimpleTypeKind.valueOf(extractFQName(type).getName()));
  functionParameter.setFacets(readFacets(reader));
  functionParameter.setAnnotationAttributes(readAnnotationAttribute(reader));
  while (reader.hasNext()
      && !(reader.isEndElement() && edmNamespace.equals(reader.getNamespaceURI())
      && XmlMetadataConstants.EDM_FUNCTION_PARAMETER.equals(reader.getLocalName()))) {
    reader.next();
    if (reader.isStartElement()) {
      extractNamespaces(reader);
      annotationElements.add(readAnnotationElement(reader));
    }
  }
  if (!annotationElements.isEmpty()) {
    functionParameter.setAnnotationElements(annotationElements);
  }
  return functionParameter;
}
 
Example 7
Source File: XmlMetadataDeserializer.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private EdmReferentialConstraint readReferentialConstraint(final XMLStreamReader reader) throws XMLStreamException,
    EntityProviderException, EdmException {
  reader.require(XMLStreamConstants.START_ELEMENT, edmNamespace, XmlMetadataConstants.EDM_ASSOCIATION_CONSTRAINT);
  EdmReferentialConstraintImpl refConstraint = new EdmReferentialConstraintImpl();
  List<EdmAnnotationElement> annotationElements = new ArrayList<EdmAnnotationElement>();
  EdmAnnotationsImpl annotations = new EdmAnnotationsImpl();
  annotations.setAnnotationAttributes(readAnnotationAttribute(reader));
  while (reader.hasNext()
      && !(reader.isEndElement() && edmNamespace.equals(reader.getNamespaceURI())
          && XmlMetadataConstants.EDM_ASSOCIATION_CONSTRAINT.equals(reader.getLocalName()))) {
    reader.next();
    if (reader.isStartElement()) {
      extractNamespaces(reader);
      currentHandledStartTagName = reader.getLocalName();
      if (XmlMetadataConstants.EDM_ASSOCIATION_PRINCIPAL.equals(currentHandledStartTagName)) {
        reader
            .require(XMLStreamConstants.START_ELEMENT, edmNamespace, XmlMetadataConstants.EDM_ASSOCIATION_PRINCIPAL);
        refConstraint.setPrincipal(readReferentialConstraintRole(reader));
      } else if (XmlMetadataConstants.EDM_ASSOCIATION_DEPENDENT.equals(currentHandledStartTagName)) {
        reader
            .require(XMLStreamConstants.START_ELEMENT, edmNamespace, XmlMetadataConstants.EDM_ASSOCIATION_DEPENDENT);
        refConstraint.setDependent(readReferentialConstraintRole(reader));
      } else {
        annotationElements.add(readAnnotationElement(reader));
      }
    }
  }
  if (!annotationElements.isEmpty()) {
    annotations.setAnnotationElements(annotationElements);
  }
  refConstraint.setAnnotations(annotations);
  return refConstraint;
}
 
Example 8
Source File: XmlMetadataDeserializer.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private EdmFunctionImportParameter readFunctionImportParameter(final XMLStreamReader reader)
    throws EntityProviderException, XMLStreamException, EdmException {
  reader.require(XMLStreamConstants.START_ELEMENT, edmNamespace, XmlMetadataConstants.EDM_FUNCTION_PARAMETER);
  EdmFunctionImportParameter functionParameter = new EdmFunctionImportParameter();
  EdmAnnotationsImpl annotations = new EdmAnnotationsImpl();
  List<EdmAnnotationElement> annotationElements = new ArrayList<EdmAnnotationElement>();

  functionParameter.setName(reader.getAttributeValue(null, XmlMetadataConstants.EDM_NAME));
  functionParameter.setMode(reader.getAttributeValue(null, XmlMetadataConstants.EDM_FUNCTION_PARAMETER_MODE));
  String type = reader.getAttributeValue(null, XmlMetadataConstants.EDM_TYPE);
  if (type == null) {
    throw new EntityProviderException(EntityProviderException.MISSING_ATTRIBUTE
        .addContent(XmlMetadataConstants.EDM_TYPE).addContent(XmlMetadataConstants.EDM_FUNCTION_PARAMETER));
  }
  functionParameter.setType(EdmSimpleTypeKind.valueOf(extractFQName(type).getName()));
  EdmFacets facets = readFacets(reader);
  functionParameter.setFacets(facets);
  annotations.setAnnotationAttributes(readAnnotationAttribute(reader));
  while (reader.hasNext()
      && !(reader.isEndElement() && edmNamespace.equals(reader.getNamespaceURI())
          && XmlMetadataConstants.EDM_FUNCTION_PARAMETER.equals(reader.getLocalName()))) {
    reader.next();
    if (reader.isStartElement()) {
      extractNamespaces(reader);
      annotationElements.add(readAnnotationElement(reader));
    }
  }
  if (!annotationElements.isEmpty()) {
    annotations.setAnnotationElements(annotationElements);
  }
  functionParameter.setAnnotations(annotations);
  return functionParameter;
}
 
Example 9
Source File: AtomServiceDocumentConsumer.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private WorkspaceImpl parseWorkspace(final XMLStreamReader reader)
    throws XMLStreamException, EntityProviderException {
  reader.require(XMLStreamConstants.START_ELEMENT, Edm.NAMESPACE_APP_2007, FormatXml.APP_WORKSPACE);

  TitleImpl title = null;
  List<Collection> collections = new ArrayList<Collection>();
  List<ExtensionElement> extElements = new ArrayList<ExtensionElement>();
  CommonAttributesImpl attributes = parseCommonAttribute(reader);
  while (reader.hasNext()
      && !(reader.isEndElement() && Edm.NAMESPACE_APP_2007.equals(reader.getNamespaceURI()) && FormatXml.APP_WORKSPACE
          .equals(reader.getLocalName()))) {
    reader.next();
    if (reader.isStartElement()) {
      currentHandledStartTagName = reader.getLocalName();
      if (FormatXml.APP_COLLECTION.equals(currentHandledStartTagName)) {
        collections.add(parseCollection(reader));
      } else if (FormatXml.ATOM_TITLE.equals(currentHandledStartTagName)) {
        title = parseTitle(reader);
      } else {
        extElements.add(parseExtensionSansTitleElement(reader));
      }
    }
  }
  if (title == null) {
    throw new EntityProviderException(EntityProviderException.INVALID_STATE
        .addContent("Missing element title for workspace"));
  }
  return new WorkspaceImpl().setTitle(title).setCollections(collections).setAttributes(attributes)
      .setExtesionElements(extElements);
}
 
Example 10
Source File: XmlMetadataConsumer.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private Property readProperty(final XMLStreamReader reader) throws XMLStreamException, EntityProviderException {
  reader.require(XMLStreamConstants.START_ELEMENT, edmNamespace, XmlMetadataConstants.EDM_PROPERTY);
  Property property;
  List<AnnotationElement> annotationElements = new ArrayList<AnnotationElement>();
  String type = reader.getAttributeValue(null, XmlMetadataConstants.EDM_TYPE);
  if (type == null) {
    throw new EntityProviderException(EntityProviderException.MISSING_ATTRIBUTE
        .addContent(XmlMetadataConstants.EDM_TYPE).addContent(XmlMetadataConstants.EDM_PROPERTY));
  }
  FullQualifiedName fqName = extractFQName(type);

  if (EdmSimpleType.EDM_NAMESPACE.equals(fqName.getNamespace())) {
    property = readSimpleProperty(reader, fqName);
  } else {
    property = readComplexProperty(reader, fqName);
  }
  property.setFacets(readFacets(reader));
  property.setCustomizableFeedMappings(readCustomizableFeedMappings(reader));
  property.setMimeType(reader.getAttributeValue(Edm.NAMESPACE_M_2007_08, XmlMetadataConstants.M_MIMETYPE));
  property.setAnnotationAttributes(readAnnotationAttribute(reader));
  while (reader.hasNext() && !(reader.isEndElement() && edmNamespace.equals(reader.getNamespaceURI())
      && XmlMetadataConstants.EDM_PROPERTY.equals(reader.getLocalName()))) {
    reader.next();
    if (reader.isStartElement()) {
      extractNamespaces(reader);
      annotationElements.add(readAnnotationElement(reader));
    }
  }
  if (!annotationElements.isEmpty()) {
    property.setAnnotationElements(annotationElements);
  }
  return property;
}
 
Example 11
Source File: StaEDIXMLStreamReaderTest.java    From staedi with Apache License 2.0 5 votes vote down vote up
@Test
void testRequire() throws Exception {
    XMLStreamReader xmlReader = getXmlReader(DUMMY_X12);
    XMLStreamException thrown;

    assertEquals(XMLStreamConstants.START_DOCUMENT, xmlReader.getEventType());
    thrown = assertThrows(XMLStreamException.class,
                          () -> xmlReader.require(XMLStreamConstants.START_DOCUMENT, EDINamespaces.LOOPS, "INTERCHANGE"));
    assertTrue(thrown.getMessage().endsWith("does not have a corresponding name"));
    thrown = assertThrows(XMLStreamException.class,
                          () -> xmlReader.require(XMLStreamConstants.START_ELEMENT, EDINamespaces.LOOPS, "INTERCHANGE"));
    assertTrue(thrown.getMessage().contains("does not match required type"));
    xmlReader.next();
    // Happy Path
    xmlReader.require(XMLStreamConstants.START_ELEMENT, EDINamespaces.LOOPS, "INTERCHANGE");
    xmlReader.require(XMLStreamConstants.START_ELEMENT, EDINamespaces.LOOPS, null);
    xmlReader.require(XMLStreamConstants.START_ELEMENT, null, "INTERCHANGE");
    xmlReader.require(XMLStreamConstants.START_ELEMENT, null, null);

    thrown = assertThrows(XMLStreamException.class,
                          () -> xmlReader.require(XMLStreamConstants.START_ELEMENT, EDINamespaces.LOOPS, "GROUP"));
    assertTrue(thrown.getMessage().contains("does not match required localName"));

    thrown = assertThrows(XMLStreamException.class,
                          () -> xmlReader.require(XMLStreamConstants.START_ELEMENT, EDINamespaces.SEGMENTS, "INTERCHANGE"));
    assertTrue(thrown.getMessage().contains("does not match required namespaceURI"));

}
 
Example 12
Source File: StaEDIXMLStreamReaderTest.java    From staedi with Apache License 2.0 5 votes vote down vote up
private static void assertSegmentBoundaries(XMLStreamReader xmlReader, String tag, int elementCount)
        throws XMLStreamException {
    assertEquals(XMLStreamConstants.START_ELEMENT, xmlReader.next());
    assertEquals(tag, xmlReader.getLocalName());
    xmlReader.require(XMLStreamConstants.START_ELEMENT, null, tag);
    skipEvents(xmlReader, 3 * elementCount);
    assertEquals(XMLStreamConstants.END_ELEMENT, xmlReader.next());
    assertEquals(tag, xmlReader.getLocalName());
    xmlReader.require(XMLStreamConstants.END_ELEMENT, null, tag);
}
 
Example 13
Source File: XmlEntryConsumer.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private void readCustomElement(final XMLStreamReader reader, final String tagName, final EntityInfoAggregator eia,
    final EntityProviderReadProperties readProperties)
    throws EdmException, EntityProviderException, XMLStreamException {
  EntityPropertyInfo targetPathInfo = eia.getTargetPathInfo(tagName);
  NamespaceContext nsctx = reader.getNamespaceContext();

  boolean skipTag = true;
  if (!Edm.NAMESPACE_ATOM_2005.equals(reader.getName().getNamespaceURI())) {

    if (targetPathInfo != null) {
      final String customPrefix = targetPathInfo.getCustomMapping().getFcNsPrefix();
      final String customNamespaceURI = targetPathInfo.getCustomMapping().getFcNsUri();

      if (customPrefix != null && customNamespaceURI != null) {
        String xmlPrefix = nsctx.getPrefix(customNamespaceURI);
        String xmlNamespaceUri = reader.getNamespaceURI(customPrefix);

        if (customNamespaceURI.equals(xmlNamespaceUri) && customPrefix.equals(xmlPrefix)) {
          skipTag = false;
          reader.require(XMLStreamConstants.START_ELEMENT, customNamespaceURI, tagName);
          final String text = reader.getElementText();
          reader.require(XMLStreamConstants.END_ELEMENT, customNamespaceURI, tagName);

          final EntityPropertyInfo propertyInfo = getValidatedPropertyInfo(eia, tagName);
          final Class<?> typeMapping = typeMappings.getMappingClass(propertyInfo.getName());
          final EdmSimpleType type = (EdmSimpleType) propertyInfo.getType();
          final Object value = type.valueOfString(text, EdmLiteralKind.DEFAULT,
              readProperties == null || readProperties.isValidatingFacets() ? propertyInfo.getFacets() : null,
              typeMapping == null ? type.getDefaultType() : typeMapping);
          properties.put(tagName, value);
        }
      }
    } else {
      throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY.addContent(tagName));
    }
  }

  if (skipTag) {
    skipStartedTag(reader);
  }
}
 
Example 14
Source File: XmlMetadataConsumer.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private Schema readSchema(final XMLStreamReader reader) throws XMLStreamException, EntityProviderException {
  reader.require(XMLStreamConstants.START_ELEMENT, edmNamespace, XmlMetadataConstants.EDM_SCHEMA);

  Schema schema = new Schema();
  List<Using> usings = new ArrayList<Using>();
  List<ComplexType> complexTypes = new ArrayList<ComplexType>();
  List<EntityType> entityTypes = new ArrayList<EntityType>();
  List<Association> associations = new ArrayList<Association>();
  List<EntityContainer> entityContainers = new ArrayList<EntityContainer>();
  List<AnnotationElement> annotationElements = new ArrayList<AnnotationElement>();

  schema.setNamespace(reader.getAttributeValue(null, XmlMetadataConstants.EDM_SCHEMA_NAMESPACE));
  inscopeMap.put(schema.getNamespace(), new HashSet<String>());
  schema.setAlias(reader.getAttributeValue(null, XmlMetadataConstants.EDM_SCHEMA_ALIAS));
  schema.setAnnotationAttributes(readAnnotationAttribute(reader));
  currentNamespace = schema.getNamespace();
  while (reader.hasNext()
      && !(reader.isEndElement() && edmNamespace.equals(reader.getNamespaceURI())
      && XmlMetadataConstants.EDM_SCHEMA.equals(reader.getLocalName()))) {
    reader.next();
    if (reader.isStartElement()) {
      extractNamespaces(reader);
      currentHandledStartTagName = reader.getLocalName();
      if (XmlMetadataConstants.EDM_USING.equals(currentHandledStartTagName)) {
        usings.add(readUsing(reader, schema.getNamespace()));
      } else if (XmlMetadataConstants.EDM_ENTITY_TYPE.equals(currentHandledStartTagName)) {
        entityTypes.add(readEntityType(reader));
      } else if (XmlMetadataConstants.EDM_COMPLEX_TYPE.equals(currentHandledStartTagName)) {
        complexTypes.add(readComplexType(reader));
      } else if (XmlMetadataConstants.EDM_ASSOCIATION.equals(currentHandledStartTagName)) {
        associations.add(readAssociation(reader));
      } else if (XmlMetadataConstants.EDM_ENTITY_CONTAINER.equals(currentHandledStartTagName)) {
        entityContainers.add(readEntityContainer(reader));
      } else {
        annotationElements.add(readAnnotationElement(reader));
      }
    }
  }
  if (schema.getAlias() != null) {
    aliasNamespaceMap.put(schema.getAlias(), schema.getNamespace());
  }
  if (!annotationElements.isEmpty()) {
    schema.setAnnotationElements(annotationElements);
  }
  schema.setUsings(usings).setEntityTypes(entityTypes).setComplexTypes(complexTypes).setAssociations(associations)
      .setEntityContainers(entityContainers);
  return schema;
}
 
Example 15
Source File: XmlMetadataDeserializer.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private EdmSchema readSchema(final XMLStreamReader reader, EdmImpl edm) //NOSONAR
    throws XMLStreamException, EntityProviderException, EdmException { 
  reader.require(XMLStreamConstants.START_ELEMENT, edmNamespace, XmlMetadataConstants.EDM_SCHEMA);
  final EdmSchemaImpl schemaImpl = new EdmSchemaImpl();
  final List<EdmUsing> usings = new ArrayList<EdmUsing>();
  final List<EdmComplexType> complexTypes = new ArrayList<EdmComplexType>();
  final List<EdmEntityType> entityTypes = new ArrayList<EdmEntityType>();
  final List<EdmAssociation> associations = new ArrayList<EdmAssociation>();
  final List<EdmEntityContainer> entityContainers = new ArrayList<EdmEntityContainer>();
  final List<EdmAnnotationElement> annotationElements = new ArrayList<EdmAnnotationElement>();
  schemaImpl.setNamespace(reader.getAttributeValue(null, XmlMetadataConstants.EDM_SCHEMA_NAMESPACE));
  inscopeMap.put(schemaImpl.getNamespace(), new HashSet<String>());
  schemaImpl.setAlias(reader.getAttributeValue(null, XmlMetadataConstants.EDM_SCHEMA_ALIAS));
  schemaImpl.setAnnotationAttributes(readAnnotationAttribute(reader));
  currentNamespace = schemaImpl.getNamespace();
  while (reader.hasNext()
      && !(reader.isEndElement() && edmNamespace.equals(reader.getNamespaceURI())
          && XmlMetadataConstants.EDM_SCHEMA.equals(reader.getLocalName()))) {
    reader.next();
    if (reader.isStartElement()) {
      extractNamespaces(reader);
      currentHandledStartTagName = reader.getLocalName();
      if (XmlMetadataConstants.EDM_USING.equals(currentHandledStartTagName)) {
        usings.add(readUsing(reader, schemaImpl.getNamespace()));
      } else if (XmlMetadataConstants.EDM_ENTITY_TYPE.equals(currentHandledStartTagName)) {
        entityTypes.add(readEntityType(reader, edm));
      } else if (XmlMetadataConstants.EDM_COMPLEX_TYPE.equals(currentHandledStartTagName)) {
        complexTypes.add(readComplexType(reader, edm));
      } else if (XmlMetadataConstants.EDM_ASSOCIATION.equals(currentHandledStartTagName)) {
        associations.add(readAssociation(reader, edm));
      } else if (XmlMetadataConstants.EDM_ENTITY_CONTAINER.equals(currentHandledStartTagName)) {
        entityContainers.add(readEntityContainer(reader, (EdmImpl) edm));
      } else {
        annotationElements.add(readAnnotationElement(reader));
      }
    }
  }
  if (schemaImpl.getAlias() != null) {
    aliasNamespaceMap.put(schemaImpl.getAlias(), schemaImpl.getNamespace());
    edm.setAliasToNamespaceInfo(aliasNamespaceMap);
  }
  if (!annotationElements.isEmpty()) {
    schemaImpl.setAnnotationElements(annotationElements);
  }
  schemaImpl
      .setUsings(usings)
      .setEntityTypes(entityTypes)
      .setComplexTypes(complexTypes)
      .setAssociations(associations)
      .setEntityContainers(entityContainers);

  edm.setAliasToNamespaceInfo(aliasNamespaceMap)
      .setEdmAssociations(associationsMap)
      .setEdmComplexTypes(complexTypesMap)
      .setEdmEntityContainers(containerMap)
      .setDefaultEntityContainer(defaultEdmEntityContainer)
      .setEdmEntitySets(edmEntitySetList)
      .setEdmEntityTypes(entityTypesMap)
      .setEdmFunctionImports(edmFunctionImportList);


  return schemaImpl;
}
 
Example 16
Source File: DocumentStaxUtils.java    From gate-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Processes the TextWithNodes element from this XMLStreamReader,
 * returning the text content of the document. The supplied map is
 * updated with the offset of each Node element encountered. The
 * reader must be positioned on the starting TextWithNodes tag and
 * will be returned positioned on the corresponding closing tag.
 * 
 * @param xsr
 * @param nodeIdToOffsetMap
 * @return the text content of the document
 */
public static String readTextWithNodes(XMLStreamReader xsr,
        Map<Integer, Long> nodeIdToOffsetMap) throws XMLStreamException {
  StringBuffer textBuf = new StringBuffer(20480);
  int eventType;
  while((eventType = xsr.next()) != XMLStreamConstants.END_ELEMENT) {
    switch(eventType) {
      case XMLStreamConstants.CHARACTERS:
      case XMLStreamConstants.CDATA:
        textBuf.append(xsr.getTextCharacters(), xsr.getTextStart(), xsr
                .getTextLength());
        break;

      case XMLStreamConstants.START_ELEMENT:
        // only Node elements allowed
        xsr.require(XMLStreamConstants.START_ELEMENT, null, "Node");
        String idString = xsr.getAttributeValue(null, "id");
        if(idString == null) {
          throw new XMLStreamException("Node element has no id", xsr
                  .getLocation());
        }
        try {
          Integer id = Integer.valueOf(idString);
          Long offset = Long.valueOf(textBuf.length());
          nodeIdToOffsetMap.put(id, offset);
        }
        catch(NumberFormatException nfe) {
          throw new XMLStreamException("Node element must have "
                  + "integer id", xsr.getLocation());
        }

        // Node element must be empty
        if(xsr.next() != XMLStreamConstants.END_ELEMENT) {
          throw new XMLStreamException("Node element within TextWithNodes "
                  + "must be empty.", xsr.getLocation());
        }
        break;

      default:
        // do nothing - ignore comments, PIs...
    }
  }
  return textBuf.toString();
}
 
Example 17
Source File: XmlPropertyDeserializer.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
protected Object readStartedElement(XMLStreamReader reader, final String name, //NOSONAR
    final EntityPropertyInfo propertyInfo, 
    final EntityTypeMapping typeMappings, final DeserializerProperties readProperties)
    throws EntityProviderException, EdmException { 
  Object result = null;

  try {
    reader.require(XMLStreamConstants.START_ELEMENT, Edm.NAMESPACE_D_2007_08, name);
    final String nullAttribute = reader.getAttributeValue(Edm.NAMESPACE_M_2007_08, FormatXml.M_NULL);

    if (!(nullAttribute == null || TRUE.equals(nullAttribute) || FALSE.equals(nullAttribute))) {
      throw new EntityProviderException(EntityProviderException.COMMON);
    }

    if (TRUE.equals(nullAttribute)) {
      if ((readProperties == null || readProperties.isValidatingFacets()) && propertyInfo.isMandatory()) {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE.addContent(name));
      }
      reader.nextTag();
    } else if (propertyInfo.isComplex()) {
      final String typeAttribute = reader.getAttributeValue(Edm.NAMESPACE_M_2007_08, FormatXml.M_TYPE);
      if (typeAttribute != null) {
        final String expectedTypeAttributeValue =
            propertyInfo.getType().getNamespace() + Edm.DELIMITER + propertyInfo.getType().getName();
        if (!expectedTypeAttributeValue.equals(typeAttribute)) { //NOSONAR
          throw new EntityProviderException(EntityProviderException.INVALID_COMPLEX_TYPE.addContent(
              expectedTypeAttributeValue).addContent(typeAttribute));
        }
      }

      reader.nextTag();
      Map<String, Object> name2Value = new HashMap<String, Object>();
      while (reader.hasNext() && !reader.isEndElement()) {
        final String childName = reader.getLocalName();
        final EntityPropertyInfo childProperty =
            ((EntityComplexPropertyInfo) propertyInfo).getPropertyInfo(childName);
        if (childProperty == null) { //NOSONAR
          throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY.addContent(childName));
        }
        final Object value = readStartedElement(reader, childName, childProperty,
            typeMappings.getEntityTypeMapping(name), readProperties);
        name2Value.put(childName, value);
        reader.nextTag();
      }
      result = name2Value;
    } else {
      result = convert(propertyInfo, reader.getElementText(), typeMappings.getMappingClass(name), readProperties);
    }
    reader.require(XMLStreamConstants.END_ELEMENT, Edm.NAMESPACE_D_2007_08, name);

    return result;
  } catch (XMLStreamException e) {
    throw new EntityProviderException(EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass()
        .getSimpleName()), e);
  }
}
 
Example 18
Source File: AtomServiceDocumentConsumer.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private TitleImpl parseTitle(final XMLStreamReader reader) throws XMLStreamException {
  reader.require(XMLStreamConstants.START_ELEMENT, Edm.NAMESPACE_ATOM_2005, FormatXml.ATOM_TITLE);
  String text = reader.getElementText();
  reader.require(XMLStreamConstants.END_ELEMENT, Edm.NAMESPACE_ATOM_2005, FormatXml.ATOM_TITLE);
  return new TitleImpl().setText(text);
}
 
Example 19
Source File: XmlMetadataConsumer.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private ComplexType readComplexType(final XMLStreamReader reader) throws XMLStreamException, EntityProviderException {
  reader.require(XMLStreamConstants.START_ELEMENT, edmNamespace, XmlMetadataConstants.EDM_COMPLEX_TYPE);

  ComplexType complexType = new ComplexType();
  List<Property> properties = new ArrayList<Property>();
  List<AnnotationElement> annotationElements = new ArrayList<AnnotationElement>();
  complexType.setName(reader.getAttributeValue(null, XmlMetadataConstants.EDM_NAME));
  String baseType = reader.getAttributeValue(null, XmlMetadataConstants.EDM_BASE_TYPE);
  if (baseType != null) {
    complexType.setBaseType(extractFQName(baseType));
  }
  if (reader.getAttributeValue(null, XmlMetadataConstants.EDM_TYPE_ABSTRACT) != null) {
    complexType.setAbstract("true".equalsIgnoreCase(reader.getAttributeValue(null,
        XmlMetadataConstants.EDM_TYPE_ABSTRACT)));
  }
  complexType.setAnnotationAttributes(readAnnotationAttribute(reader));
  while (reader.hasNext()
      && !(reader.isEndElement() && edmNamespace.equals(reader.getNamespaceURI())
      && XmlMetadataConstants.EDM_COMPLEX_TYPE.equals(reader.getLocalName()))) {
    reader.next();
    if (reader.isStartElement()) {
      extractNamespaces(reader);
      currentHandledStartTagName = reader.getLocalName();
      if (XmlMetadataConstants.EDM_PROPERTY.equals(currentHandledStartTagName)) {
        properties.add(readProperty(reader));
      } else {
        annotationElements.add(readAnnotationElement(reader));
      }
    }
  }
  if (!annotationElements.isEmpty()) {
    complexType.setAnnotationElements(annotationElements);
  }
  complexType.setProperties(properties);
  if (complexType.getName() != null) {
    FullQualifiedName fqName = new FullQualifiedName(currentNamespace, complexType.getName());
    complexTypesMap.put(fqName, complexType);
  } else {
    throw new EntityProviderException(EntityProviderException.MISSING_ATTRIBUTE.addContent("Name"));
  }
  return complexType;

}
 
Example 20
Source File: XmlMetadataDeserializer.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private EdmFunctionImport readFunctionImport(final XMLStreamReader reader, EdmImpl edm)
    throws XMLStreamException, EntityProviderException, EdmException {
  reader.require(XMLStreamConstants.START_ELEMENT, edmNamespace, XmlMetadataConstants.EDM_FUNCTION_IMPORT);
  EdmFunctionImportImpl function = new EdmFunctionImportImpl();
  ArrayList<EdmFunctionImportParameter> functionImportParameters = new ArrayList<EdmFunctionImportParameter>();
  Map<String, ArrayList<EdmFunctionImportParameter>> functionParameters =
      new HashMap<String, ArrayList<EdmFunctionImportParameter>>();
  List<EdmAnnotationElement> annotationElements = new ArrayList<EdmAnnotationElement>();
  EdmAnnotationsImpl annotations = new EdmAnnotationsImpl();
  Map<String, EdmParameter> edmParamMap = new HashMap<String, EdmParameter>();
  List<String> parametersList =  new ArrayList<String>();
  FullQualifiedName fqName ;
  function.setName(reader.getAttributeValue(null, XmlMetadataConstants.EDM_NAME));
  function.setHttpMethod(reader.getAttributeValue(Edm.NAMESPACE_M_2007_08,
      XmlMetadataConstants.EDM_FUNCTION_IMPORT_HTTP_METHOD));
  String entitySet = reader.getAttributeValue(null, XmlMetadataConstants.EDM_ENTITY_SET);
  function.setEntitySet(entitySet);

  String returnTypeString = reader.getAttributeValue(null, XmlMetadataConstants.EDM_FUNCTION_IMPORT_RETURN);
  EdmTypedImpl returnType = new EdmTypedImpl();
  if (returnTypeString != null) {
    if (returnTypeString.startsWith("Collection") || returnTypeString.startsWith("collection")) {
      returnTypeString = returnTypeString.substring(returnTypeString.indexOf("(") + 1, returnTypeString.length() - 1);
      returnType.setMultiplicity(EdmMultiplicity.MANY);
    } else {
      returnType.setMultiplicity(EdmMultiplicity.ONE);
    }
    fqName = extractFQName(returnTypeString);
    returnType.setTypeName(fqName);
    ((EdmNamedImpl) returnType).setName(fqName.getName());
    ((EdmNamedImpl) returnType).setEdm(edm);
    function.setReturnType(returnType);
  }
  annotations.setAnnotationAttributes(readAnnotationAttribute(reader));
  while (reader.hasNext()
      && !(reader.isEndElement() && edmNamespace.equals(reader.getNamespaceURI())
          && XmlMetadataConstants.EDM_FUNCTION_IMPORT.equals(reader.getLocalName()))) {
    reader.next();
    if (reader.isStartElement()) {
      extractNamespaces(reader);
      currentHandledStartTagName = reader.getLocalName();
      if (XmlMetadataConstants.EDM_FUNCTION_PARAMETER.equals(currentHandledStartTagName)) {
        EdmFunctionImportParameter edmFunctionImportParameter = readFunctionImportParameter(reader);
        functionImportParameters.add(edmFunctionImportParameter);
        EdmParameterImpl edmParamImpl = new EdmParameterImpl();
        edmParamImpl.setEdm(edm);
        edmParamImpl.setName(edmFunctionImportParameter.getName());
        edmParamImpl.setFacets(edmFunctionImportParameter.getFacets());
        edmParamImpl.setMapping(edmFunctionImportParameter.getMapping());
        edmParamImpl.setMultiplicity(returnType.getMultiplicity());
        edmParamImpl.setParameter(edmFunctionImportParameter);
        edmParamImpl.setTypeName(edmFunctionImportParameter.getType().getFullQualifiedName());
        edmParamImpl.setAnnotations(edmFunctionImportParameter.getAnnotations());
        parametersList.add(edmParamImpl.getName());
        edmParamMap.put(edmFunctionImportParameter.getName(), edmParamImpl);
      } else {
        annotationElements.add(readAnnotationElement(reader));
      }
    }
  }
  if (!annotationElements.isEmpty()) {
    annotations.setAnnotationElements(annotationElements);
  }
  function.setAnnotations(annotations);
  functionParameters.put(function.getName(), functionImportParameters);
  function.setParameters(functionParameters);
  function.setEdmParameters(edmParamMap);
  function.setParametersList(parametersList);
  return function;
}