Java Code Examples for javax.xml.stream.events.XMLEvent#START_ELEMENT

The following examples show how to use javax.xml.stream.events.XMLEvent#START_ELEMENT . 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: XMLStreamReaderImpl.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
/**
 * @param namespaceURI
 * @param localName
 * @return
 */
public String getAttributeValue(String namespaceURI, String localName) {
    //State should be either START_ELEMENT or ATTRIBUTE
    if( fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.ATTRIBUTE) {
        XMLAttributesImpl attributes = fScanner.getAttributeIterator();
        if (namespaceURI == null) { //sjsxp issue 70
            return attributes.getValue(attributes.getIndexByLocalName(localName)) ;
        } else {
            return fScanner.getAttributeIterator().getValue(
                    namespaceURI.length() == 0 ? null : namespaceURI, localName) ;
        }

    } else{
        throw new java.lang.IllegalStateException("Current state is not among the states "
                 + getEventTypeString(XMLEvent.START_ELEMENT) + " , "
                 + getEventTypeString(XMLEvent.ATTRIBUTE)
                 + "valid for getAttributeValue()") ;
    }

}
 
Example 2
Source File: XMLStreamReaderImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param namespaceURI
 * @param localName
 * @return
 */
public String getAttributeValue(String namespaceURI, String localName) {
    //State should be either START_ELEMENT or ATTRIBUTE
    if (fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.ATTRIBUTE) {
        XMLAttributesImpl attributes = fScanner.getAttributeIterator();
        if (namespaceURI == null) { //sjsxp issue 70
            return attributes.getValue(attributes.getIndexByLocalName(localName));
        } else {
            return fScanner.getAttributeIterator().getValue(
                    namespaceURI.length() == 0 ? null : namespaceURI, localName);
        }

    } else {
        throw new java.lang.IllegalStateException("Current state is not among the states "
                + getEventTypeString(XMLEvent.START_ELEMENT) + " , "
                + getEventTypeString(XMLEvent.ATTRIBUTE)
                + "valid for getAttributeValue()");
    }

}
 
Example 3
Source File: StreamingLoader.java    From secure-data-service with Apache License 2.0 6 votes vote down vote up
public void process(Reader reader) throws XMLStreamException {
    XMLEventReader r = XMLInputFactory.newInstance().createXMLEventReader(reader);

    XMLEvent root;
    while ((root = r.nextEvent()).getEventType() != XMLEvent.START_ELEMENT) {
        ;
    }
    String rootNodeName = extractName(root);

    StopWatch sw = new StopWatch();
    while (true) {
        XMLEvent e = r.nextEvent();

        if (e.getEventType() == XMLEvent.END_ELEMENT && extractName(e).equals(rootNodeName)) {
            break;
        } else if (e.getEventType() == XMLEvent.START_ELEMENT && tp.isComplexType(extractName(e))) {
            Map<String, Object> entity = parseMap(r, extractName(e));
            sw.start();
            repo.create(extractName(e), va.adapt(entity));
            sw.stop();
            LOG.debug("Saved entity: {}",extractName(e));
        }
    }

    LOG.info("Total mongo time:" + sw.getTotalTimeMillis());
}
 
Example 4
Source File: XMLStreamReaderImpl.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param namespaceURI
 * @param localName
 * @return
 */
public String getAttributeValue(String namespaceURI, String localName) {
    //State should be either START_ELEMENT or ATTRIBUTE
    if( fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.ATTRIBUTE) {
        XMLAttributesImpl attributes = fScanner.getAttributeIterator();
        if (namespaceURI == null) { //sjsxp issue 70
            return attributes.getValue(attributes.getIndexByLocalName(localName)) ;
        } else {
            return fScanner.getAttributeIterator().getValue(
                    namespaceURI.length() == 0 ? null : namespaceURI, localName) ;
        }

    } else{
        throw new java.lang.IllegalStateException("Current state is not among the states "
                 + getEventTypeString(XMLEvent.START_ELEMENT) + " , "
                 + getEventTypeString(XMLEvent.ATTRIBUTE)
                 + "valid for getAttributeValue()") ;
    }

}
 
Example 5
Source File: XMLStreamReaderImpl.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/** Returns the namespace of the attribute at the provided
 * index
 * @param index the position of the attribute
 * @return the namespace URI (can be null)
 * @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE
 */
public String getAttributeNamespace(int index) {
    //State should be either START_ELEMENT or ATTRIBUTE
    if( fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.ATTRIBUTE) {
        return fScanner.getAttributeIterator().getURI(index);
    } else{
        throw new java.lang.IllegalStateException("Current state is not among the states "
                 + getEventTypeString(XMLEvent.START_ELEMENT) + " , "
                 + getEventTypeString(XMLEvent.ATTRIBUTE)
                 + "valid for getAttributeNamespace()") ;
    }

}
 
Example 6
Source File: XMLStreamReaderImpl.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @return
 */
public String getValue() {
    if(fEventType == XMLEvent.PROCESSING_INSTRUCTION){
        return fScanner.getPIData().toString();
    } else if(fEventType == XMLEvent.COMMENT){
        return fScanner.getComment();
    } else if(fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.END_ELEMENT){
        return fScanner.getElementQName().localpart ;
    } else if(fEventType == XMLEvent.CHARACTERS){
        return fScanner.getCharacterData().toString();
    }
    return null;
}
 
Example 7
Source File: XMLStreamReaderImpl.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/** Returns the qname of the attribute at the provided index
 *
 * @param index the position of the attribute
 * @return the QName of the attribute
 * @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE
 */
public javax.xml.namespace.QName getAttributeQName(int index) {
    //State should be either START_ELEMENT or ATTRIBUTE
    if( fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.ATTRIBUTE) {
        // create new object at runtime..
        String localName = fScanner.getAttributeIterator().getLocalName(index) ;
        String uri = fScanner.getAttributeIterator().getURI(index) ;
        return new javax.xml.namespace.QName(uri, localName) ;
    } else{
        throw new java.lang.IllegalStateException("Current state is not among the states "
                 + getEventTypeString(XMLEvent.START_ELEMENT) + " , "
                 + getEventTypeString(XMLEvent.ATTRIBUTE)
                 + "valid for getAttributeQName()") ;
    }
}
 
Example 8
Source File: XMLStreamReaderImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
final static String getEventTypeString(int eventType) {
    switch (eventType){
        case XMLEvent.START_ELEMENT:
            return "START_ELEMENT";
        case XMLEvent.END_ELEMENT:
            return "END_ELEMENT";
        case XMLEvent.PROCESSING_INSTRUCTION:
            return "PROCESSING_INSTRUCTION";
        case XMLEvent.CHARACTERS:
            return "CHARACTERS";
        case XMLEvent.COMMENT:
            return "COMMENT";
        case XMLEvent.START_DOCUMENT:
            return "START_DOCUMENT";
        case XMLEvent.END_DOCUMENT:
            return "END_DOCUMENT";
        case XMLEvent.ENTITY_REFERENCE:
            return "ENTITY_REFERENCE";
        case XMLEvent.ATTRIBUTE:
            return "ATTRIBUTE";
        case XMLEvent.DTD:
            return "DTD";
        case XMLEvent.CDATA:
            return "CDATA";
        case XMLEvent.SPACE:
            return "SPACE";
    }
    return "UNKNOWN_EVENT_TYPE, " + String.valueOf(eventType);
}
 
Example 9
Source File: EcrfReader.java    From hmftools with GNU General Public License v3.0 5 votes vote down vote up
@NotNull
private static String getEventTypeString(int eventType) {
    switch (eventType) {
        case XMLEvent.START_ELEMENT:
            return "START_ELEMENT";
        case XMLEvent.END_ELEMENT:
            return "END_ELEMENT";
        case XMLEvent.PROCESSING_INSTRUCTION:
            return "PROCESSING_INSTRUCTION";
        case XMLEvent.CHARACTERS:
            return "CHARACTERS";
        case XMLEvent.COMMENT:
            return "COMMENT";
        case XMLEvent.START_DOCUMENT:
            return "START_DOCUMENT";
        case XMLEvent.END_DOCUMENT:
            return "END_DOCUMENT";
        case XMLEvent.ENTITY_REFERENCE:
            return "ENTITY_REFERENCE";
        case XMLEvent.ATTRIBUTE:
            return "ATTRIBUTE";
        case XMLEvent.DTD:
            return "DTD";
        case XMLEvent.CDATA:
            return "CDATA";
        case XMLEvent.SPACE:
            return "SPACE";
    }
    return "UNKNOWN_EVENT_TYPE , " + eventType;
}
 
Example 10
Source File: XMLStreamFilterImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 *
 * @throws XMLStreamException
 * @return
 */
public int nextTag() throws XMLStreamException {
    if (fStreamAdvancedByHasNext && fEventAccepted &&
            (fCurrentEvent == XMLEvent.START_ELEMENT || fCurrentEvent == XMLEvent.START_ELEMENT)) {
        fStreamAdvancedByHasNext = false;
        return fCurrentEvent;
    }

    int event = findNextTag();
    if (event != -1) {
        return event;
    }
    throw new IllegalStateException("The stream reader has reached the end of the document, or there are no more "+
                                " items to return");
}
 
Example 11
Source File: XMLStreamReaderImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
final static String getEventTypeString(int eventType) {
    switch (eventType){
        case XMLEvent.START_ELEMENT:
            return "START_ELEMENT";
        case XMLEvent.END_ELEMENT:
            return "END_ELEMENT";
        case XMLEvent.PROCESSING_INSTRUCTION:
            return "PROCESSING_INSTRUCTION";
        case XMLEvent.CHARACTERS:
            return "CHARACTERS";
        case XMLEvent.COMMENT:
            return "COMMENT";
        case XMLEvent.START_DOCUMENT:
            return "START_DOCUMENT";
        case XMLEvent.END_DOCUMENT:
            return "END_DOCUMENT";
        case XMLEvent.ENTITY_REFERENCE:
            return "ENTITY_REFERENCE";
        case XMLEvent.ATTRIBUTE:
            return "ATTRIBUTE";
        case XMLEvent.DTD:
            return "DTD";
        case XMLEvent.CDATA:
            return "CDATA";
        case XMLEvent.SPACE:
            return "SPACE";
    }
    return "UNKNOWN_EVENT_TYPE, " + String.valueOf(eventType);
}
 
Example 12
Source File: XMLStreamReaderImpl.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/** Returns the uri for the namespace declared at the
 * index.
 *
 * @param index the position of the namespace declaration
 * @return returns the namespace uri
 * @throws IllegalStateException if this is not a START_ELEMENT, END_ELEMENT or NAMESPACE
 */
public String getNamespaceURI(int index) {
    if(fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.END_ELEMENT || fEventType == XMLEvent.NAMESPACE){
        //namespaceContext is dynamic object.
        return fScanner.getNamespaceContext().getURI(fScanner.getNamespaceContext().getDeclaredPrefixAt(index));
    }
    else{
        throw new IllegalStateException("Current state " + getEventTypeString(fEventType)
         + " is not among the states " + getEventTypeString(XMLEvent.START_ELEMENT)
         + ", " + getEventTypeString(XMLEvent.END_ELEMENT) + ", "
                 + getEventTypeString(XMLEvent.NAMESPACE)
         + " valid for getNamespaceURI()." );
    }

}
 
Example 13
Source File: XMLStreamReaderImpl.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
final static String getEventTypeString(int eventType) {
    switch (eventType){
        case XMLEvent.START_ELEMENT:
            return "START_ELEMENT";
        case XMLEvent.END_ELEMENT:
            return "END_ELEMENT";
        case XMLEvent.PROCESSING_INSTRUCTION:
            return "PROCESSING_INSTRUCTION";
        case XMLEvent.CHARACTERS:
            return "CHARACTERS";
        case XMLEvent.COMMENT:
            return "COMMENT";
        case XMLEvent.START_DOCUMENT:
            return "START_DOCUMENT";
        case XMLEvent.END_DOCUMENT:
            return "END_DOCUMENT";
        case XMLEvent.ENTITY_REFERENCE:
            return "ENTITY_REFERENCE";
        case XMLEvent.ATTRIBUTE:
            return "ATTRIBUTE";
        case XMLEvent.DTD:
            return "DTD";
        case XMLEvent.CDATA:
            return "CDATA";
        case XMLEvent.SPACE:
            return "SPACE";
    }
    return "UNKNOWN_EVENT_TYPE, " + String.valueOf(eventType);
}
 
Example 14
Source File: StaxJobFactory.java    From scheduling with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Get the arguments at the given tag and return them as a string array.
 * Leave the cursor on the end 'ELEMENT_SCRIPT_ARGUMENTS' tag.
 *
 * @param cursorArgs the streamReader with the cursor on the 'ELEMENT_SCRIPT_ARGUMENTS' tag.
 * @return the arguments as a string array, null if no args.
 */
private String[] getArguments(XMLStreamReader cursorArgs) throws JobCreationException {
    if (XMLTags.SCRIPT_ARGUMENTS.matches(cursorArgs.getLocalName())) {
        ArrayList<String> args = new ArrayList<>(0);
        try {
            int eventType;
            while (cursorArgs.hasNext()) {
                eventType = cursorArgs.next();
                switch (eventType) {
                    case XMLEvent.START_ELEMENT:
                        if (XMLTags.SCRIPT_ARGUMENT.matches(cursorArgs.getLocalName())) {
                            args.add(cursorArgs.getAttributeValue(0));
                        }
                        break;
                    case XMLEvent.END_ELEMENT:
                        if (XMLTags.SCRIPT_ARGUMENTS.matches(cursorArgs.getLocalName())) {
                            return args.toArray(new String[args.size()]);
                        }
                        break;
                    default:
                        // do nothing just cope with sonarqube rule switch must have default
                }
            }
            return args.toArray(new String[args.size()]);
        } catch (Exception e) {
            String temporaryAttribute = null;
            if (cursorArgs.isStartElement() && cursorArgs.getAttributeCount() == 1) {
                temporaryAttribute = cursorArgs.getAttributeLocalName(0);
            }
            throw new JobCreationException(cursorArgs.getLocalName(), temporaryAttribute, e);
        }
    } else {
        return null;
    }
}
 
Example 15
Source File: XMLStreamReaderImpl.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/** Returns a boolean which indicates if this
 * attribute was created by default
 * @param index the position of the attribute
 * @return true if this is a default attribute
 * @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE
 */
public boolean isAttributeSpecified(int index) {
    //check that current state should be either START_ELEMENT or ATTRIBUTE
    if( (fEventType == XMLEvent.START_ELEMENT) || (fEventType == XMLEvent.ATTRIBUTE)){
        return fScanner.getAttributeIterator().isSpecified(index) ;
    } else{
        throw new IllegalStateException("Current state is not among the states "
                 + getEventTypeString(XMLEvent.START_ELEMENT) + " , "
                 + getEventTypeString(XMLEvent.ATTRIBUTE)
                 + "valid for isAttributeSpecified()")  ;
    }
}
 
Example 16
Source File: XMLStreamReaderImpl.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/** Returns the localName of the attribute at the provided
 * index
 * @param index the position of the attribute
 * @return the localName of the attribute
 * @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE
 */
public QName getAttributeName(int index) {
    //State should be either START_ELEMENT or ATTRIBUTE
    if( fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.ATTRIBUTE) {
        return convertXNIQNametoJavaxQName(fScanner.getAttributeIterator().getQualifiedName(index)) ;
    } else{
        throw new java.lang.IllegalStateException("Current state is not among the states "
                 + getEventTypeString(XMLEvent.START_ELEMENT) + " , "
                 + getEventTypeString(XMLEvent.ATTRIBUTE)
                 + "valid for getAttributeName()") ;
    }
}
 
Example 17
Source File: XMLStreamReaderImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/** this Funtion returns true if the current event has name */
public boolean hasName() {
    if(fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.END_ELEMENT) {
        return true;
    }  else {
        return false;
    }
}
 
Example 18
Source File: TsoGeneratorSpeedAutomaton.java    From powsybl-core with Mozilla Public License 2.0 4 votes vote down vote up
public static TsoGeneratorSpeedAutomaton fromXml(String contingencyId, XMLStreamReader xmlsr) throws XMLStreamException {
    LimitsXmlParsingState state = null;
    String text = null;
    List<String> onUnderSpeedDiconnectedGenerators = new ArrayList<>();
    List<String> onOverSpeedDiconnectedGenerators = new ArrayList<>();
    while (xmlsr.hasNext()) {
        int eventType = xmlsr.next();
        switch (eventType) {
            case XMLEvent.CHARACTERS:
                text = xmlsr.getText();
                break;

            case XMLEvent.START_ELEMENT:
                switch (xmlsr.getLocalName()) {
                    case ON_UNDER_SPEED_DISCONNECTED_GENERATORS:
                        state = LimitsXmlParsingState.UNDER;
                        break;

                    case ON_OVER_SPEED_DISCONNECTED_GENERATORS:
                        state = LimitsXmlParsingState.OVER;
                        break;

                    case GEN:
                    case INDEX:
                        // nothing to do
                        break;

                    default:
                        throw new AssertionError("Unexpected element: " + xmlsr.getLocalName());
                }
                break;
            case XMLEvent.END_ELEMENT:
                switch (xmlsr.getLocalName()) {
                    case ON_UNDER_SPEED_DISCONNECTED_GENERATORS:
                    case ON_OVER_SPEED_DISCONNECTED_GENERATORS:
                        state = null;
                        break;

                    case GEN:
                        LimitsXmlParsingState.addGenerator(state, text, onUnderSpeedDiconnectedGenerators, onOverSpeedDiconnectedGenerators);
                        break;

                    case INDEX:
                        return new TsoGeneratorSpeedAutomaton(contingencyId, onUnderSpeedDiconnectedGenerators, onOverSpeedDiconnectedGenerators);

                    default:
                        throw new AssertionError("Unexpected element: " + xmlsr.getLocalName());
                }
                break;

            default:
                break;
        }
    }
    throw new AssertionError("Should not happen");
}
 
Example 19
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 20
Source File: DummyEvent.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public boolean isStartElement() {
    return fEventType == XMLEvent.START_ELEMENT;
}