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

The following examples show how to use javax.xml.stream.XMLStreamReader#isStartElement() . 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: PolicyWSDLParserExtension.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private boolean readExternalFile(final String fileUrl) {
    InputStream ios = null;
    XMLStreamReader reader = null;
    try {
        final URL xmlURL = new URL(fileUrl);
        ios = xmlURL.openStream();
        reader = XmlUtil.newXMLInputFactory(true).createXMLStreamReader(ios);
        while (reader.hasNext()) {
            if (reader.isStartElement() && NamespaceVersion.resolveAsToken(reader.getName()) == XmlToken.Policy) {
                readSinglePolicy(policyReader.readPolicyElement(reader, fileUrl), false);
            }
            reader.next();
        }
        return true;
    } catch (IOException ioe) {
        return false;
    } catch (XMLStreamException xmlse) {
        return false;
    } finally {
        PolicyUtils.IO.closeResource(reader);
        PolicyUtils.IO.closeResource(ios);
    }
}
 
Example 2
Source File: XmlEntryConsumer.java    From cloud-odata-java with Apache License 2.0 6 votes vote down vote up
private void readContent(final XMLStreamReader reader, final EntityInfoAggregator eia) throws EntityProviderException, XMLStreamException, EdmException {
  reader.require(XMLStreamConstants.START_ELEMENT, Edm.NAMESPACE_ATOM_2005, FormatXml.ATOM_CONTENT);

  extractNamespacesFromTag(reader);

  checkAllMandatoryNamespacesAvailable();

  final String contentType = reader.getAttributeValue(null, FormatXml.ATOM_TYPE);
  final String sourceLink = reader.getAttributeValue(null, FormatXml.ATOM_SRC);

  reader.nextTag();

  if (reader.isStartElement() && reader.getLocalName().equals(FormatXml.M_PROPERTIES)) {
    readProperties(reader, eia);
  } else if (reader.isEndElement()) {
    reader.require(XMLStreamConstants.END_ELEMENT, Edm.NAMESPACE_ATOM_2005, FormatXml.ATOM_CONTENT);
  } else {
    throw new EntityProviderException(EntityProviderException.INVALID_STATE
        .addContent("Expected closing 'content' or starting 'properties' but found '" + reader.getLocalName() + "'."));
  }

  mediaMetadata.setContentType(contentType);
  mediaMetadata.setSourceLink(sourceLink);
}
 
Example 3
Source File: EdmParser.java    From cloud-odata-java with Apache License 2.0 6 votes vote down vote up
private ReferentialConstraintRole readReferentialConstraintRole(final XMLStreamReader reader) throws EntityProviderException, XMLStreamException {
  ReferentialConstraintRole rcRole = new ReferentialConstraintRole();
  rcRole.setRole(reader.getAttributeValue(null, EdmParserConstants.EDM_ROLE));
  List<PropertyRef> propertyRefs = new ArrayList<PropertyRef>();
  List<AnnotationElement> annotationElements = new ArrayList<AnnotationElement>();
  rcRole.setAnnotationAttributes(readAnnotationAttribute(reader));
  while (reader.hasNext() && !(reader.isEndElement() && Edm.NAMESPACE_EDM_2008_09.equals(reader.getNamespaceURI())
      && (EdmParserConstants.EDM_ASSOCIATION_PRINCIPAL.equals(reader.getLocalName()) || EdmParserConstants.EDM_ASSOCIATION_DEPENDENT.equals(reader.getLocalName())))) {
    reader.next();
    if (reader.isStartElement()) {
      extractNamespaces(reader);
      currentHandledStartTagName = reader.getLocalName();
      if (EdmParserConstants.EDM_PROPERTY_REF.equals(currentHandledStartTagName)) {
        propertyRefs.add(readPropertyRef(reader));
      } else {
        annotationElements.add(readAnnotationElement(reader));
      }
    }
  }
  rcRole.setPropertyRefs(propertyRefs).setAnnotationElements(annotationElements);
  return rcRole;
}
 
Example 4
Source File: PolicyWSDLParserExtension.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private boolean readExternalFile(final String fileUrl) {
    InputStream ios = null;
    XMLStreamReader reader = null;
    try {
        final URL xmlURL = new URL(fileUrl);
        ios = xmlURL.openStream();
        reader = XmlUtil.newXMLInputFactory(true).createXMLStreamReader(ios);
        while (reader.hasNext()) {
            if (reader.isStartElement() && NamespaceVersion.resolveAsToken(reader.getName()) == XmlToken.Policy) {
                readSinglePolicy(policyReader.readPolicyElement(reader, fileUrl), false);
            }
            reader.next();
        }
        return true;
    } catch (IOException ioe) {
        return false;
    } catch (XMLStreamException xmlse) {
        return false;
    } finally {
        PolicyUtils.IO.closeResource(reader);
        PolicyUtils.IO.closeResource(ios);
    }
}
 
Example 5
Source File: ItemDefinitionXMLConverter.java    From activiti6-boot2 with Apache License 2.0 6 votes vote down vote up
@Override
protected DmnElement convertXMLToElement(XMLStreamReader xtr, DmnDefinition model, DecisionTable decisionTable) throws Exception {
    ItemDefinition itemDefinition = new ItemDefinition();
    itemDefinition.setId(xtr.getAttributeValue(null, ATTRIBUTE_ID));
    itemDefinition.setName(xtr.getAttributeValue(null, ATTRIBUTE_NAME));

    boolean readyWithItemDefinition = false;
    try {
        while (readyWithItemDefinition == false && xtr.hasNext()) {
            xtr.next();
            if (xtr.isStartElement() && ELEMENT_TYPE_DEFINITION.equalsIgnoreCase(xtr.getLocalName())) {
                itemDefinition.setTypeDefinition(xtr.getElementText());
            }
            else if (xtr.isEndElement() && getXMLElementName().equalsIgnoreCase(xtr.getLocalName())) {
                readyWithItemDefinition = true;
            }
        }
    } catch (Exception e) {
        LOGGER.warn("Error parsing input expression", e);
    }

    return itemDefinition;
}
 
Example 6
Source File: AtomServiceDocumentConsumer.java    From cloud-odata-java with Apache License 2.0 6 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 7
Source File: XmlMetadataConsumer.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private PropertyRef readPropertyRef(final XMLStreamReader reader) throws XMLStreamException, EntityProviderException {
  reader.require(XMLStreamConstants.START_ELEMENT, edmNamespace, XmlMetadataConstants.EDM_PROPERTY_REF);
  PropertyRef propertyRef = new PropertyRef();
  propertyRef.setName(reader.getAttributeValue(null, XmlMetadataConstants.EDM_NAME));
  List<AnnotationElement> annotationElements = new ArrayList<AnnotationElement>();
  propertyRef.setAnnotationAttributes(readAnnotationAttribute(reader));
  while (reader.hasNext() && !(reader.isEndElement() && edmNamespace.equals(reader.getNamespaceURI())
      && XmlMetadataConstants.EDM_PROPERTY_REF.equals(reader.getLocalName()))) {
    reader.next();
    if (reader.isStartElement()) {
      extractNamespaces(reader);
      annotationElements.add(readAnnotationElement(reader));
    }
  }
  if (!annotationElements.isEmpty()) {
    propertyRef.setAnnotationElements(annotationElements);
  }
  return propertyRef;
}
 
Example 8
Source File: XmlMetadataConsumer.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private NavigationProperty readNavigationProperty(final XMLStreamReader reader) throws XMLStreamException,
    EntityProviderException {
  reader.require(XMLStreamConstants.START_ELEMENT, edmNamespace, XmlMetadataConstants.EDM_NAVIGATION_PROPERTY);

  NavigationProperty navProperty = new NavigationProperty();
  List<AnnotationElement> annotationElements = new ArrayList<AnnotationElement>();
  navProperty.setName(reader.getAttributeValue(null, XmlMetadataConstants.EDM_NAME));
  String relationship = reader.getAttributeValue(null, XmlMetadataConstants.EDM_NAVIGATION_RELATIONSHIP);
  if (relationship != null) {
    FullQualifiedName fqName = extractFQName(relationship);
    navProperty.setRelationship(fqName);

  } else {
    throw new EntityProviderException(EntityProviderException.MISSING_ATTRIBUTE
        .addContent(XmlMetadataConstants.EDM_NAVIGATION_RELATIONSHIP).addContent(
            XmlMetadataConstants.EDM_NAVIGATION_PROPERTY));
  }

  navProperty.setFromRole(reader.getAttributeValue(null, XmlMetadataConstants.EDM_NAVIGATION_FROM_ROLE));
  navProperty.setToRole(reader.getAttributeValue(null, XmlMetadataConstants.EDM_NAVIGATION_TO_ROLE));
  navProperty.setAnnotationAttributes(readAnnotationAttribute(reader));
  while (reader.hasNext() && !(reader.isEndElement() && edmNamespace.equals(reader.getNamespaceURI())
      && XmlMetadataConstants.EDM_NAVIGATION_PROPERTY.equals(reader.getLocalName()))) {
    reader.next();
    if (reader.isStartElement()) {
      extractNamespaces(reader);
      annotationElements.add(readAnnotationElement(reader));
    }
  }
  if (!annotationElements.isEmpty()) {
    navProperty.setAnnotationElements(annotationElements);
  }
  navProperties.add(navProperty);
  return navProperty;
}
 
Example 9
Source File: StaxJobFactory.java    From scheduling with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Add the dependencies to the current task.
 * Leave this method at the end of the 'ELEMENT_TASK_DEPENDENCES' tag.
 *
 * @param cursorDepends the streamReader with the cursor on the 'ELEMENT_TASK_DEPENDENCES' tag.
 * @param t             the task on which to apply the dependencies.
 */
private Map<String, ArrayList<String>> createDependences(XMLStreamReader cursorDepends, Task t)
        throws JobCreationException {
    try {
        Map<String, ArrayList<String>> dependencies = new HashMap<>();

        ArrayList<String> depends = new ArrayList<>(0);
        int eventType;
        while (cursorDepends.hasNext()) {
            eventType = cursorDepends.next();
            switch (eventType) {
                case XMLEvent.START_ELEMENT:
                    if (XMLTags.TASK_DEPENDENCES_TASK.matches(cursorDepends.getLocalName())) {
                        depends.add(cursorDepends.getAttributeValue(0));
                    }
                    break;
                case XMLEvent.END_ELEMENT:
                    if (XMLTags.TASK_DEPENDENCES.matches(cursorDepends.getLocalName())) {
                        dependencies.put(t.getName(), depends);
                        return dependencies;
                    }

                    break;
                default:
                    // do nothing just cope with sonarqube rule switch must have default
            }
        }
        return dependencies;
    } catch (Exception e) {
        String attrtmp = null;
        if (cursorDepends.isStartElement() && cursorDepends.getAttributeCount() == 1) {
            attrtmp = cursorDepends.getAttributeLocalName(0);
        }
        throw new JobCreationException(cursorDepends.getLocalName(), attrtmp, e);
    }
}
 
Example 10
Source File: XMLStreamReaderToolsTest.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
@Test
public void testSkipElement() throws FileNotFoundException, XMLStreamException {
    File file = new File(getAbsPath("sample.xml"));
    InputStream stream = new BufferedInputStream(new FileInputStream(file));
    XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(stream);

    while (!reader.isStartElement()) {
        reader.next();
    }
    XMLStreamReaderTools.skipElement(reader); // skip everything inside
    assertTrue(reader.isEndElement());
}
 
Example 11
Source File: EdmParser.java    From cloud-odata-java with Apache License 2.0 5 votes vote down vote up
private PropertyRef readPropertyRef(final XMLStreamReader reader) throws XMLStreamException, EntityProviderException {
  reader.require(XMLStreamConstants.START_ELEMENT, Edm.NAMESPACE_EDM_2008_09, EdmParserConstants.EDM_PROPERTY_REF);
  PropertyRef propertyRef = new PropertyRef();
  propertyRef.setName(reader.getAttributeValue(null, EdmParserConstants.EDM_NAME));
  List<AnnotationElement> annotationElements = new ArrayList<AnnotationElement>();
  propertyRef.setAnnotationAttributes(readAnnotationAttribute(reader));
  while (reader.hasNext() && !(reader.isEndElement() && Edm.NAMESPACE_EDM_2008_09.equals(reader.getNamespaceURI()) && EdmParserConstants.EDM_PROPERTY_REF.equals(reader.getLocalName()))) {
    reader.next();
    if (reader.isStartElement()) {
      extractNamespaces(reader);
      annotationElements.add(readAnnotationElement(reader));
    }
  }
  return propertyRef.setAnnotationElements(annotationElements);
}
 
Example 12
Source File: BaseBpmnXMLConverter.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
protected ExtensionElement parseExtensionElement(XMLStreamReader xtr) throws Exception {
  ExtensionElement extensionElement = new ExtensionElement();
  extensionElement.setName(xtr.getLocalName());
  if (StringUtils.isNotEmpty(xtr.getNamespaceURI())) {
    extensionElement.setNamespace(xtr.getNamespaceURI());
  }
  if (StringUtils.isNotEmpty(xtr.getPrefix())) {
    extensionElement.setNamespacePrefix(xtr.getPrefix());
  }

  BpmnXMLUtil.addCustomAttributes(xtr, extensionElement, defaultElementAttributes);

  boolean readyWithExtensionElement = false;
  while (readyWithExtensionElement == false && xtr.hasNext()) {
    xtr.next();
    if (xtr.isCharacters() || XMLStreamReader.CDATA == xtr.getEventType()) {
      if (StringUtils.isNotEmpty(xtr.getText().trim())) {
        extensionElement.setElementText(xtr.getText().trim());
      }
    } else if (xtr.isStartElement()) {
      ExtensionElement childExtensionElement = parseExtensionElement(xtr);
      extensionElement.addChildElement(childExtensionElement);
    } else if (xtr.isEndElement() && extensionElement.getName().equalsIgnoreCase(xtr.getLocalName())) {
      readyWithExtensionElement = true;
    }
  }
  return extensionElement;
}
 
Example 13
Source File: BaseChildElementParser.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
protected void parseChildElements(XMLStreamReader xtr, BaseElement parentElement, BpmnModel model, BaseChildElementParser parser) throws Exception {
  boolean readyWithChildElements = false;
  while (readyWithChildElements == false && xtr.hasNext()) {
    xtr.next();
    if (xtr.isStartElement()) {
      if (parser.getElementName().equals(xtr.getLocalName())) {
        parser.parseChildElement(xtr, parentElement, model);
      }

    } else if (xtr.isEndElement() && getElementName().equalsIgnoreCase(xtr.getLocalName())) {
      readyWithChildElements = true;
    }
  }
}
 
Example 14
Source File: XmlMetadataConsumer.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private EntityContainer readEntityContainer(final XMLStreamReader reader)
    throws XMLStreamException, EntityProviderException {
  reader.require(XMLStreamConstants.START_ELEMENT, edmNamespace, XmlMetadataConstants.EDM_ENTITY_CONTAINER);
  EntityContainer container = new EntityContainer();
  List<EntitySet> entitySets = new ArrayList<EntitySet>();
  List<AssociationSet> associationSets = new ArrayList<AssociationSet>();
  List<FunctionImport> functionImports = new ArrayList<FunctionImport>();
  List<AnnotationElement> annotationElements = new ArrayList<AnnotationElement>();

  container.setName(reader.getAttributeValue(null, XmlMetadataConstants.EDM_NAME));
  if (reader.getAttributeValue(Edm.NAMESPACE_M_2007_08, XmlMetadataConstants.EDM_CONTAINER_IS_DEFAULT) != null) {
    container.setDefaultEntityContainer("true".equalsIgnoreCase(reader.getAttributeValue(Edm.NAMESPACE_M_2007_08,
        "IsDefaultEntityContainer")));
  }
  container.setExtendz(reader.getAttributeValue(null, XmlMetadataConstants.EDM_CONTAINER_EXTENDZ));
  container.setAnnotationAttributes(readAnnotationAttribute(reader));

  while (reader.hasNext()
      && !(reader.isEndElement() && edmNamespace.equals(reader.getNamespaceURI())
      && XmlMetadataConstants.EDM_ENTITY_CONTAINER.equals(reader.getLocalName()))) {
    reader.next();
    if (reader.isStartElement()) {
      extractNamespaces(reader);
      currentHandledStartTagName = reader.getLocalName();
      if (XmlMetadataConstants.EDM_ENTITY_SET.equals(currentHandledStartTagName)) {
        entitySets.add(readEntitySet(reader));
      } else if (XmlMetadataConstants.EDM_ASSOCIATION_SET.equals(currentHandledStartTagName)) {
        associationSets.add(readAssociationSet(reader));
      } else if (XmlMetadataConstants.EDM_FUNCTION_IMPORT.equals(currentHandledStartTagName)) {
        functionImports.add(readFunctionImport(reader));
      } else {
        annotationElements.add(readAnnotationElement(reader));
      }
    }
  }
  if (!annotationElements.isEmpty()) {
    container.setAnnotationElements(annotationElements);
  }
  container.setEntitySets(entitySets).setAssociationSets(associationSets).setFunctionImports(functionImports);
  edmFunctionImportList.addAll(functionImports);
  containerMap.put(new FullQualifiedName(currentNamespace, container.getName()), container);
  return container;
}
 
Example 15
Source File: FieldExtensionParser.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
@Override
public void parseChildElement(XMLStreamReader xtr, BaseElement parentElement, BpmnModel model) throws Exception {

    if (!accepts(parentElement))
        return;

    FieldExtension extension = new FieldExtension();
    BpmnXMLUtil.addXMLLocation(extension, xtr);
    extension.setFieldName(xtr.getAttributeValue(null, ATTRIBUTE_FIELD_NAME));

    if (StringUtils.isNotEmpty(xtr.getAttributeValue(null, ATTRIBUTE_FIELD_STRING))) {
        extension.setStringValue(xtr.getAttributeValue(null, ATTRIBUTE_FIELD_STRING));

    } else if (StringUtils.isNotEmpty(xtr.getAttributeValue(null, ATTRIBUTE_FIELD_EXPRESSION))) {
        extension.setExpression(xtr.getAttributeValue(null, ATTRIBUTE_FIELD_EXPRESSION));

    } else {
        boolean readyWithFieldExtension = false;
        try {
            while (!readyWithFieldExtension && xtr.hasNext()) {
                xtr.next();
                if (xtr.isStartElement() && ELEMENT_FIELD_STRING.equalsIgnoreCase(xtr.getLocalName())) {
                    extension.setStringValue(xtr.getElementText().trim());

                } else if (xtr.isStartElement() && ATTRIBUTE_FIELD_EXPRESSION.equalsIgnoreCase(xtr.getLocalName())) {
                    extension.setExpression(xtr.getElementText().trim());

                } else if (xtr.isEndElement() && getElementName().equalsIgnoreCase(xtr.getLocalName())) {
                    readyWithFieldExtension = true;
                }
            }
        } catch (Exception e) {
            LOGGER.warn("Error parsing field extension child elements", e);
        }
    }

    if (parentElement instanceof FlowableListener) {
        ((FlowableListener) parentElement).getFieldExtensions().add(extension);
    } else if (parentElement instanceof ServiceTask) {
        ((ServiceTask) parentElement).getFieldExtensions().add(extension);
    } else if (parentElement instanceof SendTask) {
        ((SendTask) parentElement).getFieldExtensions().add(extension);
    } else {
        ((AbstractFlowableHttpHandler) parentElement).getFieldExtensions().add(extension);
    }
}
 
Example 16
Source File: StaxJobFactory.java    From scheduling with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Add the Java Executable to this java Task.
 * The cursor is currently at the beginning of the 'ELEMENT_JAVA_EXECUTABLE' tag.
 *
 * @param javaTask   the task in which to add the Java Executable.
 * @param cursorExec the streamReader with the cursor on the 'ELEMENT_JAVA_EXECUTABLE' tag.
 */
private void setJavaExecutable(JavaTask javaTask, XMLStreamReader cursorExec, Map<String, String> variables)
        throws JobCreationException {
    int i = 0;
    String current = cursorExec.getLocalName();
    try {
        //parsing executable attributes
        int attrCount = cursorExec.getAttributeCount();
        for (i = 0; i < attrCount; i++) {
            String attrName = cursorExec.getAttributeLocalName(i);
            if (XMLAttributes.TASK_CLASS_NAME.matches(attrName)) {
                javaTask.setExecutableClassName(cursorExec.getAttributeValue(i));
            }
        }
        //parsing executable tags
        int eventType;
        while (cursorExec.hasNext()) {
            eventType = cursorExec.next();
            switch (eventType) {
                case XMLEvent.START_ELEMENT:
                    current = cursorExec.getLocalName();
                    if (XMLTags.FORK_ENVIRONMENT.matches(current)) {
                        ForkEnvironment forkEnv = createForkEnvironment(cursorExec, variables);
                        javaTask.setForkEnvironment(forkEnv);
                    } else if (XMLTags.TASK_PARAMETER.matches(current)) {
                        Map<String, String> attributesAsMap = getAttributesAsMap(cursorExec, variables);

                        String name = attributesAsMap.get(XMLAttributes.VARIABLE_NAME.getXMLName());
                        String value = attributesAsMap.get(XMLAttributes.VARIABLE_VALUE.getXMLName());

                        javaTask.addArgument(replace(name, variables), value);
                    }
                    break;
                case XMLEvent.END_ELEMENT:
                    if (XMLTags.JAVA_EXECUTABLE.matches(cursorExec.getLocalName())) {
                        return;
                    }
                    break;
                default:
                    // do nothing just cope with sonarqube rule switch must have default
            }
        }
    } catch (JobCreationException jce) {
        jce.pushTag(current);
        throw jce;
    } catch (Exception e) {
        String attrtmp = null;
        if (cursorExec.isStartElement() && cursorExec.getAttributeCount() > 0) {
            attrtmp = cursorExec.getAttributeLocalName(i);
        }
        throw new JobCreationException(current, attrtmp, e);
    }
}
 
Example 17
Source File: XmlMetadataConsumer.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private AssociationEnd readAssociationEnd(final XMLStreamReader reader) throws EntityProviderException,
    XMLStreamException {
  reader.require(XMLStreamConstants.START_ELEMENT, edmNamespace, XmlMetadataConstants.EDM_ASSOCIATION_END);

  AssociationEnd associationEnd = new AssociationEnd();
  List<AnnotationElement> annotationElements = new ArrayList<AnnotationElement>();
  associationEnd.setRole(reader.getAttributeValue(null, XmlMetadataConstants.EDM_ROLE));
  associationEnd.setMultiplicity(EdmMultiplicity.fromLiteral(reader.getAttributeValue(null,
      XmlMetadataConstants.EDM_ASSOCIATION_MULTIPLICITY)));
  String type = reader.getAttributeValue(null, XmlMetadataConstants.EDM_TYPE);
  if (type == null) {
    throw new EntityProviderException(EntityProviderException.MISSING_ATTRIBUTE
        .addContent(XmlMetadataConstants.EDM_TYPE).addContent(XmlMetadataConstants.EDM_ASSOCIATION_END));
  }
  associationEnd.setType(extractFQName(type));
  associationEnd.setAnnotationAttributes(readAnnotationAttribute(reader));
  while (reader.hasNext() && !(reader.isEndElement() && edmNamespace.equals(reader.getNamespaceURI())
      && XmlMetadataConstants.EDM_ASSOCIATION_END.equals(reader.getLocalName()))) {
    reader.next();
    if (reader.isStartElement()) {
      extractNamespaces(reader);
      currentHandledStartTagName = reader.getLocalName();
      if (XmlMetadataConstants.EDM_ASSOCIATION_ONDELETE.equals(currentHandledStartTagName)) {
        OnDelete onDelete = new OnDelete();
        for (int i = 0; i < EdmAction.values().length; i++) {
          if (EdmAction.values()[i].name().equalsIgnoreCase(
              reader.getAttributeValue(null, XmlMetadataConstants.EDM_ONDELETE_ACTION))) {
            onDelete.setAction(EdmAction.values()[i]);
          }
        }
        associationEnd.setOnDelete(onDelete);
      } else {
        annotationElements.add(readAnnotationElement(reader));
      }
    }
  }
  if (!annotationElements.isEmpty()) {
    associationEnd.setAnnotationElements(annotationElements);
  }
  return associationEnd;
}
 
Example 18
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 19
Source File: XmlMetadataDeserializer.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private EdmAnnotationElement readAnnotationElement(final XMLStreamReader reader) throws XMLStreamException {
  EdmAnnotationElementImpl elementImpl = new EdmAnnotationElementImpl();
  List<EdmAnnotationElement> annotationElements = new ArrayList<EdmAnnotationElement>();
  List<EdmAnnotationAttribute> annotationAttributes = new ArrayList<EdmAnnotationAttribute>();
  elementImpl.setName(reader.getLocalName());
  String elementNamespace = reader.getNamespaceURI();
  if (!edmNamespaces.contains(elementNamespace)) {
    elementImpl.setPrefix(reader.getPrefix());
    elementImpl.setNamespace(elementNamespace);
  }
  for (int i = 0; i < reader.getAttributeCount(); i++) {
    EdmAnnotationAttributeImpl annotationAttribute = new EdmAnnotationAttributeImpl();
    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()) {
    elementImpl.setAttributes(annotationAttributes);
  }

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

  while (justRead && !(reader.isEndElement() && elementImpl.getName() != null
      && elementImpl.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());
      elementImpl.setText(elementText);
    }
  }
  if (!annotationElements.isEmpty()) {
    elementImpl.setChildElements(annotationElements);
  }
  return elementImpl;
}
 
Example 20
Source File: Base64Type.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Override
public Object readObject(MessageReader mreader, Context context) throws DatabindingException {
    XMLStreamReader reader = mreader.getXMLStreamReader();

    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    try {
        reader.next();
        while (!reader.isCharacters() && !reader.isEndElement() && !reader.isStartElement()) {
            reader.next();
        }

        if (reader.isStartElement() && reader.getName().equals(AbstractXOPType.XOP_INCLUDE)) {
            return optimizedType.readMtoM(mreader, context);
        }

        if (reader.isEndElement()) {
            reader.next();
            return new byte[0];
        }

        CharArrayWriter writer = new CharArrayWriter(2048);
        while (reader.isCharacters()) {
            writer.write(reader.getTextCharacters(),
                         reader.getTextStart(),
                         reader.getTextLength());
            reader.next();
        }
        Base64Utility.decode(writer.toCharArray(), 0, writer.size(), bos);

        while (reader.getEventType() != XMLStreamConstants.END_ELEMENT) {
            reader.next();
        }

        // Advance just past the end element
        reader.next();

        return bos.toByteArray();
    } catch (Base64Exception | XMLStreamException e) {
        throw new DatabindingException("Could not parse base64Binary data.", e);
    }
}