Example usage for javax.xml.stream XMLStreamReader hasNext

List of usage examples for javax.xml.stream XMLStreamReader hasNext

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamReader hasNext.

Prototype

public boolean hasNext() throws XMLStreamException;

Source Link

Document

Returns true if there are more parsing events and false if there are no more events.

Usage

From source file:org.activiti.designer.eclipse.bpmn.BpmnParser.java

private BoundaryEventModel parseBoundaryEvent(XMLStreamReader xtr) {
    BoundaryEvent boundaryEvent = new BoundaryEvent();
    boundaryEvent.setName(xtr.getAttributeValue(null, "name"));

    if (xtr.getAttributeValue(null, "cancelActivity") != null) {
        String cancelActivity = xtr.getAttributeValue(null, "cancelActivity");
        if ("true".equalsIgnoreCase(cancelActivity)) {
            boundaryEvent.setCancelActivity(true);
        } else {//from  www  .  jav a  2  s  .  co  m
            boundaryEvent.setCancelActivity(false);
        }
    }

    BoundaryEventModel model = new BoundaryEventModel();
    model.boundaryEvent = boundaryEvent;
    model.attachedRef = xtr.getAttributeValue(null, "attachedToRef");
    try {
        while (xtr.hasNext()) {
            xtr.next();
            if (xtr.isStartElement() && "timerEventDefinition".equalsIgnoreCase(xtr.getLocalName())) {
                model.type = BoundaryEventModel.TIMEEVENT;
                boundaryEvent.getEventDefinitions().add(parseTimerEventDefinition(xtr));
                break;

            } else if (xtr.isStartElement() && "errorEventDefinition".equalsIgnoreCase(xtr.getLocalName())) {
                model.type = BoundaryEventModel.ERROREVENT;
                boundaryEvent.getEventDefinitions().add(parseErrorEventDefinition(xtr));
                break;

            } else if (xtr.isStartElement() && "signalEventDefinition".equalsIgnoreCase(xtr.getLocalName())) {
                model.type = BoundaryEventModel.SIGNALEVENT;
                boundaryEvent.getEventDefinitions().add(parseSignalEventDefinition(xtr));
                break;

            } else if (xtr.isEndElement() && "boundaryEvent".equalsIgnoreCase(xtr.getLocalName())) {
                break;
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return model;
}

From source file:org.activiti.designer.eclipse.bpmn.BpmnParser.java

private IntermediateCatchEvent parseIntermediateCatchEvent(XMLStreamReader xtr) {
    IntermediateCatchEvent catchEvent = new IntermediateCatchEvent();

    try {/*from ww  w . j  a  v a2  s  .  c om*/
        while (xtr.hasNext()) {
            xtr.next();
            if (xtr.isStartElement() && "timerEventDefinition".equalsIgnoreCase(xtr.getLocalName())) {
                catchEvent.getEventDefinitions().add(parseTimerEventDefinition(xtr));
                break;

            } else if (xtr.isStartElement() && "signalEventDefinition".equalsIgnoreCase(xtr.getLocalName())) {
                catchEvent.getEventDefinitions().add(parseSignalEventDefinition(xtr));
                break;

            } else if (xtr.isEndElement() && "intermediateCatchEvent".equalsIgnoreCase(xtr.getLocalName())) {
                break;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return catchEvent;
}

From source file:org.activiti.designer.eclipse.bpmn.BpmnParser.java

private ThrowEvent parseIntermediateThrowEvent(XMLStreamReader xtr) {
    ThrowEvent throwEvent = new ThrowEvent();
    try {//from   w w w.  j a va2 s . c o  m
        while (xtr.hasNext()) {
            xtr.next();
            if (xtr.isStartElement() && "signalEventDefinition".equalsIgnoreCase(xtr.getLocalName())) {
                throwEvent.getEventDefinitions().add(parseSignalEventDefinition(xtr));
                break;

            } else if (xtr.isEndElement() && "intermediateThrowEvent".equalsIgnoreCase(xtr.getLocalName())) {
                break;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return throwEvent;
}

From source file:org.activiti.designer.eclipse.bpmn.BpmnParser.java

private TimerEventDefinition parseTimerEventDefinition(XMLStreamReader xtr) {
    TimerEventDefinition eventDefinition = new TimerEventDefinition();
    try {/*from w ww. ja  v  a 2  s .  c o  m*/
        while (xtr.hasNext()) {
            xtr.next();
            if (xtr.isStartElement() && "timeDuration".equalsIgnoreCase(xtr.getLocalName())) {
                eventDefinition.setTimeDuration(xtr.getElementText());
                break;

            } else if (xtr.isStartElement() && "timeDate".equalsIgnoreCase(xtr.getLocalName())) {
                eventDefinition.setTimeDate(xtr.getElementText());
                break;

            } else if (xtr.isStartElement() && "timeCycle".equalsIgnoreCase(xtr.getLocalName())) {
                eventDefinition.setTimeCycle(xtr.getElementText());
                break;

            } else if (xtr.isEndElement() && "timerEventDefinition".equalsIgnoreCase(xtr.getLocalName())) {
                break;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return eventDefinition;
}

From source file:org.activiti.dmn.converter.util.DmnXMLUtil.java

public static void parseChildElements(String elementName, DmnElement parentElement, XMLStreamReader xtr,
        Map<String, BaseChildElementParser> childParsers, DmnDefinition model) throws Exception {

    Map<String, BaseChildElementParser> localParserMap = new HashMap<String, BaseChildElementParser>(
            genericChildParserMap);/*from  www .j  a  v  a  2s.c om*/
    if (childParsers != null) {
        localParserMap.putAll(childParsers);
    }

    boolean inExtensionElements = false;
    boolean readyWithChildElements = false;
    while (readyWithChildElements == false && xtr.hasNext()) {
        xtr.next();

        if (xtr.isStartElement()) {

            if (ELEMENT_EXTENSIONS.equals(xtr.getLocalName())) {
                inExtensionElements = true;
            } else if (localParserMap.containsKey(xtr.getLocalName())) {
                BaseChildElementParser childParser = localParserMap.get(xtr.getLocalName());
                //if we're into an extension element but the current element is not accepted by this parentElement then is read as a custom extension element
                if (inExtensionElements && !childParser.accepts(parentElement)) {
                    DmnExtensionElement extensionElement = parseExtensionElement(xtr);
                    parentElement.addExtensionElement(extensionElement);
                    continue;
                }
                localParserMap.get(xtr.getLocalName()).parseChildElement(xtr, parentElement, model);
            } else if (inExtensionElements) {
                DmnExtensionElement extensionElement = parseExtensionElement(xtr);
                parentElement.addExtensionElement(extensionElement);
            }

        } else if (xtr.isEndElement()) {
            if (ELEMENT_EXTENSIONS.equals(xtr.getLocalName())) {
                inExtensionElements = false;
            } else if (elementName.equalsIgnoreCase(xtr.getLocalName())) {
                readyWithChildElements = true;
            }
        }
    }
}

From source file:org.activiti.dmn.converter.util.DmnXMLUtil.java

public static DmnExtensionElement parseExtensionElement(XMLStreamReader xtr) throws Exception {
    DmnExtensionElement extensionElement = new DmnExtensionElement();
    extensionElement.setName(xtr.getLocalName());
    if (StringUtils.isNotEmpty(xtr.getNamespaceURI())) {
        extensionElement.setNamespace(xtr.getNamespaceURI());
    }//from   www  .  ja  v  a2  s .  c om
    if (StringUtils.isNotEmpty(xtr.getPrefix())) {
        extensionElement.setNamespacePrefix(xtr.getPrefix());
    }

    for (int i = 0; i < xtr.getAttributeCount(); i++) {
        DmnExtensionAttribute extensionAttribute = new DmnExtensionAttribute();
        extensionAttribute.setName(xtr.getAttributeLocalName(i));
        extensionAttribute.setValue(xtr.getAttributeValue(i));
        if (StringUtils.isNotEmpty(xtr.getAttributeNamespace(i))) {
            extensionAttribute.setNamespace(xtr.getAttributeNamespace(i));
        }
        if (StringUtils.isNotEmpty(xtr.getAttributePrefix(i))) {
            extensionAttribute.setNamespacePrefix(xtr.getAttributePrefix(i));
        }
        extensionElement.addAttribute(extensionAttribute);
    }

    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()) {
            DmnExtensionElement childExtensionElement = parseExtensionElement(xtr);
            extensionElement.addChildElement(childExtensionElement);
        } else if (xtr.isEndElement() && extensionElement.getName().equalsIgnoreCase(xtr.getLocalName())) {
            readyWithExtensionElement = true;
        }
    }
    return extensionElement;
}

From source file:org.activiti.dmn.xml.converter.DmnXMLConverter.java

public DmnDefinition convertToDmnModel(XMLStreamReader xtr) {
    DmnDefinition model = new DmnDefinition();
    DmnElement parentElement = null;/*w  ww  . ja  va2 s  .c  o m*/

    try {
        while (xtr.hasNext()) {
            try {
                xtr.next();
            } catch (Exception e) {
                LOGGER.debug("Error reading XML document", e);
                throw new DmnXMLException("Error reading XML", e);
            }

            if (xtr.isStartElement() == false) {
                continue;
            }

            if (ELEMENT_DEFINITIONS.equals(xtr.getLocalName())) {
                model.setId(xtr.getAttributeValue(null, ATTRIBUTE_ID));
                model.setName(xtr.getAttributeValue(null, ATTRIBUTE_NAME));
                model.setNamespace(MODEL_NAMESPACE);
                parentElement = model;
            } else if (ELEMENT_DECISION.equals(xtr.getLocalName())) {
                Decision decision = new Decision();
                model.addDrgElement(decision);
                decision.setId(xtr.getAttributeValue(null, ATTRIBUTE_ID));
                decision.setName(xtr.getAttributeValue(null, ATTRIBUTE_NAME));
                parentElement = decision;
            } else if (ELEMENT_DECISION_TABLE.equals(xtr.getLocalName())) {
                DecisionTable currentDecisionTable = new DecisionTable();
                currentDecisionTable.setId(xtr.getAttributeValue(null, ATTRIBUTE_ID));

                if (xtr.getAttributeValue(null, ATTRIBUTE_HIT_POLICY) != null) {
                    currentDecisionTable
                            .setHitPolicy(HitPolicy.valueOf(xtr.getAttributeValue(null, ATTRIBUTE_HIT_POLICY)));
                } else {
                    currentDecisionTable.setHitPolicy(HitPolicy.FIRST);
                }

                model.getDrgElements().get(model.getDrgElements().size() - 1)
                        .setDecisionTable(currentDecisionTable);
                model.setCurrentDecisionTable(currentDecisionTable);
                parentElement = currentDecisionTable;
            } else if (ELEMENT_OUTPUT_CLAUSE.equals(xtr.getLocalName())) {
                OutputClause outputClause = new OutputClause();
                model.getCurrentDecisionTable().addOutput(outputClause);
                outputClause.setId(xtr.getAttributeValue(null, ATTRIBUTE_ID));
                outputClause.setLabel(xtr.getAttributeValue(null, ATTRIBUTE_LABEL));
                outputClause.setName(xtr.getAttributeValue(null, ATTRIBUTE_NAME));
                outputClause.setTypeRef(xtr.getAttributeValue(null, ATTRIBUTE_TYPE_REF));
                parentElement = outputClause;
            } else if (ELEMENT_DESCRIPTION.equals(xtr.getLocalName())) {
                parentElement.setDescription(xtr.getElementText());
            } else if (ELEMENT_EXTENSIONS.equals(xtr.getLocalName())) {
                while (xtr.hasNext()) {
                    xtr.next();
                    if (xtr.isStartElement()) {
                        DmnExtensionElement extensionElement = DmnXMLUtil.parseExtensionElement(xtr);
                        parentElement.addExtensionElement(extensionElement);
                    } else if (xtr.isEndElement()) {
                        if (ELEMENT_EXTENSIONS.equals(xtr.getLocalName())) {
                            break;
                        }
                    }
                }

            } else if (convertersToDmnMap.containsKey(xtr.getLocalName())) {
                BaseDmnXMLConverter converter = convertersToDmnMap.get(xtr.getLocalName());
                converter.convertToDmnModel(xtr, model);
            }
        }

        processDmnElements(model.getCurrentDecisionTable());

    } catch (DmnXMLException e) {
        throw e;

    } catch (Exception e) {
        LOGGER.error("Error processing DMN document", e);
        throw new DmnXMLException("Error processing DMN document", e);
    }
    return model;
}

From source file:org.apache.axiom.om.impl.serialize.StreamingOMSerializer.java

protected void serializeNode(XMLStreamReader reader, XMLStreamWriter writer, boolean startAtNext)
        throws XMLStreamException {
    // TODO We get the StAXWriter at this point and uses it hereafter 
    // assuming that this is the only entry point to this class.
    // If there can be other classes calling methodes of this we might 
    // need to change methode signatures to OMOutputer

    // If not startAtNext, then seed the processing with the current element.
    boolean useCurrentEvent = !startAtNext;

    while (reader.hasNext() || useCurrentEvent) {
        int event = 0;
        OMDataSource ds = null;//from   www.  j a  v  a  2 s  .c o m
        if (useCurrentEvent) {
            event = reader.getEventType();
            useCurrentEvent = false;
        } else {
            event = reader.next();
        }

        // If the reader is exposing a DataSourc
        // for this event, then simply serialize the
        // DataSource
        if (reader instanceof OMStAXWrapper) {
            ds = ((OMStAXWrapper) reader).getDataSource();
        }
        if (ds != null) {
            ds.serialize(writer);
        } else {
            switch (event) {
            case START_ELEMENT:
                serializeElement(reader, writer);
                depth++;
                break;
            case ATTRIBUTE:
                serializeAttributes(reader, writer);
                break;
            case CHARACTERS:
                if (dataHandlerReader != null && dataHandlerReader.isBinary()) {
                    serializeDataHandler();
                    break;
                }
                // Fall through
            case SPACE:
                serializeText(reader, writer);
                break;
            case COMMENT:
                serializeComment(reader, writer);
                break;
            case CDATA:
                serializeCData(reader, writer);
                break;
            case PROCESSING_INSTRUCTION:
                serializeProcessingInstruction(reader, writer);
                break;
            case END_ELEMENT:
                serializeEndElement(writer);
                depth--;
                break;
            case START_DOCUMENT:
                depth++; //if a start document is found then increment the depth
                break;
            case END_DOCUMENT:
                if (depth != 0)
                    depth--; //for the end document - reduce the depth
                try {
                    serializeEndElement(writer);
                } catch (Exception e) {
                    //TODO: log exceptions
                }
            }
        }
        if (depth == 0) {
            break;
        }
    }
}

From source file:org.apache.axis2.jaxws.context.listener.ContextListenerUtils.java

public static int skipEventsTo(int targetEvent, XMLStreamReader parser) throws XMLStreamException {
    int eventType = 0;
    while (parser.hasNext()) {
        eventType = parser.next();//from  ww  w  . j  ava2s  .c o m
        if (eventType == targetEvent)
            return eventType;
    }
    return eventType; // return END_DOCUMENT;
}

From source file:org.apache.axis2.jaxws.context.listener.ParserInputStreamCustomBuilder.java

public OMElement create(String namespace, String localPart, OMContainer parent, XMLStreamReader reader,
        OMFactory factory) throws OMException {

    if (log.isDebugEnabled()) {
        log.debug("create namespace = " + namespace);
        log.debug("  localPart = " + localPart);
        log.debug("  reader = " + reader.getClass());
    }/* w w  w .j  a va 2s .c om*/

    if (!shouldUnmarshal(namespace, localPart)) {
        if (log.isDebugEnabled()) {
            log.debug("This element won't be unmarshalled with the custom builder");
        }
        return null;
    }

    /*
     * 1) Use the the parser to fetch the inputStream
     * 2) Use the inputStream to create a DataSource, delay reading of content as much as you can.
     * 3) Use the OMFactory to create OMSourcedElement, OMSourcedElement is backed by ParsedEntityDataSource.
     */
    try {
        ParsedEntityReaderFactory perf = (ParsedEntityReaderFactory) FactoryRegistry
                .getFactory(ParsedEntityReaderFactory.class);
        ParsedEntityReader entityReader = perf.getParsedEntityReader();
        if (log.isDebugEnabled()) {
            log.debug("ParsedEntityReader = " + entityReader);
        }
        //Do not user custom builder if Parser does not have ability to read sub content.
        if (!entityReader.isParsedEntityStreamAvailable()) {
            if (log.isDebugEnabled()) {
                log.debug("ParsedEntityStream is not available, defaulting to normal build");
            }
            return null;
        }
        // Create an OMSourcedElement backed by the ParsedData
        InputStream parsedStream = getPayloadContent(reader, entityReader);
        if (parsedStream == null) {
            //cant read content from EntityReader, returning null.
            if (log.isDebugEnabled()) {
                log.debug("Unable to read content from the entity reader, defaulting to normal build");
            }
            return null;
        }
        HashMap<String, String> nsElementDecls = getElementNamespaceDeclarations(reader);
        HashMap<String, String> attrElementDecls = getElementAttributeDeclarations(reader);

        //read the payload. Lets move the parser forward.
        if (reader.hasNext()) {
            reader.next();
        }
        if (namespace == null) {
            //lets look for ns in reader
            namespace = reader.getNamespaceURI();
            if (namespace == null) {
                //still cant find the namespace, just set it to "";
                namespace = "";
            }
        }
        OMNamespace ns = factory.createOMNamespace(namespace, reader.getPrefix());
        InputStream payload = ContextListenerUtils.createPayloadElement(parsedStream, ns, localPart, parent,
                nsElementDecls, attrElementDecls);

        ParserInputStreamDataSource ds = new ParserInputStreamDataSource(payload, encoding);
        OMSourcedElement om = null;
        if (parent instanceof SOAPHeader && factory instanceof SOAPFactory) {
            om = ((SOAPFactory) factory).createSOAPHeaderBlock(localPart, ns, ds);
        } else {
            om = factory.createOMElement(ds, localPart, ns);
        }
        //Add the new OMSourcedElement ot the parent
        parent.addChild(om);
        /*
        //Lets Mark the body as complete so Serialize calls dont fetch data from parser for body content.
        if(parent instanceof SOAPBodyImpl){
        ((SOAPBodyImpl)parent).setComplete(true);
        }
        */
        return om;
    } catch (OMException e) {
        throw e;
    } catch (Throwable t) {
        throw new OMException(t);
    }
}