Example usage for javax.xml.stream XMLStreamReader next

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

Introduction

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

Prototype

public int next() throws XMLStreamException;

Source Link

Document

Get next parsing event - a processor may return all contiguous character data in a single chunk, or it may split it into several chunks.

Usage

From source file:net.xy.jcms.controller.configurations.parser.TranslationParser.java

/**
 * parses an xml configuration from an input streams. throwes
 * IllegalArgumentExceptions in case of syntax error.
 * //from w  w w.j  a va  2  s . c o  m
 * @param in
 * @return value
 * @throws XMLStreamException
 * @throws ClassNotFoundException
 *             in case there are problems with an params type converter
 */
public static TranslationRule[] parse(final InputStream in, final ClassLoader loader)
        throws XMLStreamException, ClassNotFoundException {
    @SuppressWarnings("deprecation")
    final XMLInputFactory factory = XMLInputFactory.newInstance(
            "com.sun.xml.internal.stream.XMLInputFactoryImpl", TranslationParser.class.getClassLoader());
    LOG.info("XMLInputFactory loaded: " + factory.getClass().getName());
    factory.setProperty("javax.xml.stream.isCoalescing", true);
    // not supported be the reference implementation
    // factory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.TRUE);
    final XMLStreamReader parser = factory.createXMLStreamReader(in);
    while (parser.hasNext()) {
        final int event = parser.next();
        if (event == XMLStreamConstants.START_ELEMENT && parser.getName().getLocalPart().equals("rules")) {
            return parseRules(parser, loader);
        }
    }
    throw new IllegalArgumentException("No rules section found.");
}

From source file:net.xy.jcms.controller.configurations.parser.TranslationParser.java

/**
 * parses an single file translation/*from   ww w .  j  a v a  2s  .c  o  m*/
 * 
 * @param in
 * @param loader
 * @return value
 * @throws XMLStreamException
 * @throws ClassNotFoundException
 *             in case there are problems with an params type converter
 */
public static TranslationRule parseSingle(final InputStream in, final ClassLoader loader)
        throws XMLStreamException, ClassNotFoundException {
    @SuppressWarnings("deprecation")
    final XMLInputFactory factory = XMLInputFactory.newInstance(
            "com.sun.xml.internal.stream.XMLInputFactoryImpl", TranslationParser.class.getClassLoader());
    LOG.info("XMLInputFactory loaded: " + factory.getClass().getName());
    factory.setProperty("javax.xml.stream.isCoalescing", true);
    final XMLStreamReader parser = factory.createXMLStreamReader(in);
    while (parser.hasNext()) {
        final int event = parser.next();
        if (event == XMLStreamConstants.START_ELEMENT && parser.getName().getLocalPart().equals("rule")) {
            return parseRule(parser, loader);
        }
    }
    throw new IllegalArgumentException("No rules section found.");
}

From source file:net.xy.jcms.controller.configurations.parser.TranslationParser.java

/**
 * checks for parameters <parameter name="contentgroup" group="1"
 * convert="de.jamba.ContentGroupConverter"/>
 * //from w  w  w  . j  a v  a2s . c o m
 * @param parser
 * @return value
 * @throws XMLStreamException
 * @throws ClassNotFoundException
 * @throws IllegalAccessException
 * @throws InstantiationException
 */
private static List<RuleParameter> parseParameter(final XMLStreamReader parser, final ClassLoader loader)
        throws XMLStreamException, ClassNotFoundException {
    final List<RuleParameter> params = new ArrayList<RuleParameter>();
    while (parser.nextTag() == XMLStreamConstants.START_ELEMENT) {
        String parameterName = null, converter = null;
        Integer aplicatesToGroup = null;
        if (parser.getAttributeCount() != 3 && parser.getAttributeCount() != 2) {
            throw new IllegalArgumentException("There are to much or few attributes specified for parameter.");
        }
        for (int i = 0; i < parser.getAttributeCount(); i++) {
            if (parser.getAttributeLocalName(i).equals("name")) {
                parameterName = parser.getAttributeValue(i);
            } else if (parser.getAttributeLocalName(i).equals("convert")) {
                converter = parser.getAttributeValue(i);
            } else if (parser.getAttributeLocalName(i).equals("group")) {
                aplicatesToGroup = new Integer(parser.getAttributeValue(i));
            }
        }

        boolean goEnd = true;
        Map<String, String> mappings = null;
        if (parser.next() == XMLStreamConstants.CHARACTERS) {
            final String mappingStr = parser.getText();
            // get integrated mapping body
            if (StringUtils.isNotBlank(mappingStr)) {
                mappings = new HashMap<String, String>();
                final String[] lines = mappingStr.split("\n");
                for (String line : lines) {
                    line = line.trim();
                    if (StringUtils.isBlank(line) || line.startsWith("#")) {
                        continue;
                    }
                    final String[] pair = line.split("=", 2);
                    mappings.put(pair[0].trim(), pair[1].trim());
                }
            }
        } else {
            goEnd = false; // allready on end
        }

        if (StringUtils.isBlank(parameterName)) {
            throw new IllegalArgumentException("Parameter name has to be set");
        } else if (StringUtils.isBlank(converter)) {
            throw new IllegalArgumentException("Parameter Converter has to be set");
        } else if (aplicatesToGroup == null) {
            throw new IllegalArgumentException("Applicates to regex group has to be set");
        } else {
            if (mappings != null) {
                // get special mapping converter
                params.add(new RuleParameter(parameterName, aplicatesToGroup,
                        ((InitializableController<?>) ConverterPool.get(converter, loader))
                                .initialize(mappings)));
            } else {
                // get normal converter
                params.add(new RuleParameter(parameterName, aplicatesToGroup,
                        ConverterPool.get(converter, loader)));
            }
        }
        if (goEnd) {
            parser.nextTag(); // gets endtag
        }
    }
    return params;
}

From source file:net.xy.jcms.controller.configurations.parser.UsecaseParser.java

/**
 * parses usecases out from an xml file/*from   w w  w  .j a v  a2  s  .c  o  m*/
 * 
 * @param in
 * @param loader
 *            used for retrieving configuration included resources and also
 *            for retrieving the controllers
 * @return value
 * @throws XMLStreamException
 * @throws ClassNotFoundException
 */
public static Usecase[] parse(final InputStream in, final ClassLoader loader)
        throws XMLStreamException, ClassNotFoundException {
    final XMLInputFactory factory = XMLInputFactory.newInstance();
    factory.setProperty("javax.xml.stream.isCoalescing", true);
    // not supported by the reference implementation
    // factory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.TRUE);
    final XMLStreamReader parser = factory.createXMLStreamReader(in);
    while (parser.hasNext()) {
        final int event = parser.next();
        if (event == XMLStreamConstants.START_ELEMENT && parser.getName().getLocalPart().equals("usecases")) {
            return parseUsecases(parser, loader);
        }
    }
    throw new IllegalArgumentException("No usecases section found. [" + parser.getLocation() + "]");
}

From source file:net.xy.jcms.controller.configurations.parser.UsecaseParser.java

/**
 * method for parsing single usecase xml files. one per file.
 * //w ww.  j a  va 2s. c o  m
 * @param in
 * @param loader
 * @return parsed usecase
 * @throws XMLStreamException
 * @throws ClassNotFoundException
 */
public static Usecase parseSingle(final InputStream in, final ClassLoader loader)
        throws XMLStreamException, ClassNotFoundException {
    final XMLInputFactory factory = XMLInputFactory.newInstance();
    factory.setProperty("javax.xml.stream.isCoalescing", true);
    // not supported by the reference implementation
    // factory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.TRUE);
    final XMLStreamReader parser = factory.createXMLStreamReader(in);
    while (parser.hasNext()) {
        final int event = parser.next();
        if (event == XMLStreamConstants.START_ELEMENT && parser.getName().getLocalPart().equals("usecase")) {
            return parseUsecase(parser, loader);
        }
    }
    throw new IllegalArgumentException("No usecases section found. [" + parser.getLocation() + "]");
}

From source file:net.xy.jcms.controller.configurations.parser.UsecaseParser.java

/**
 * parses the description field/* w ww.j  a v a  2  s .  c  o  m*/
 * 
 * @param parser
 * @return
 * @throws XMLStreamException
 */
private static String parseDescription(final XMLStreamReader parser) throws XMLStreamException {
    parser.next();
    final String text = parser.getText();
    parser.nextTag();
    return text;
}

From source file:net.xy.jcms.controller.configurations.parser.UsecaseParser.java

/**
 * inits an config from the xml body/* ww  w .ja va2  s . com*/
 * 
 * @param type
 * @param parser
 * @return
 * @throws XMLStreamException
 */
private static Configuration<?> getConfigurationByBody(final ConfigurationType type,
        final XMLStreamReader parser, final ClassLoader loader) throws XMLStreamException {
    parser.next();
    final String text = parser.getText();
    return Configuration.initByString(type, text, loader);
}

From source file:nl.knaw.huygens.tei.xpath.XPathUtil.java

public static Map<String, String> getNamespaceInfo(String xml) {
    Map<String, String> namespaces = Maps.newIdentityHashMap();
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    try {//  w  w  w . j  av  a  2  s. com
        XMLStreamReader xreader = inputFactory.createXMLStreamReader(IOUtils.toInputStream(xml, "UTF-8"));
        while (xreader.hasNext()) {
            if (xreader.next() == XMLStreamConstants.START_ELEMENT) {
                QName qName = xreader.getName();
                if (qName != null) {
                    addNamespace(namespaces, qName.getPrefix(), qName.getNamespaceURI());
                    for (int i = 0; i < xreader.getAttributeCount(); i++) {
                        addNamespace(namespaces, xreader.getAttributePrefix(i),
                                xreader.getAttributeNamespace(i));
                    }
                }
            }
        }
    } catch (XMLStreamException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return namespaces;
}

From source file:org.activiti.bpmn.converter.BaseBpmnXMLConverter.java

@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());
    }/*  w  ww  . java2  s. co m*/
    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()) {
            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;
}

From source file:org.activiti.bpmn.converter.BpmnXMLConverter.java

public BpmnModel convertToBpmnModel(XMLStreamReader xtr) {
    BpmnModel model = new BpmnModel();
    model.setStartEventFormTypes(startEventFormTypes);
    model.setUserTaskFormTypes(userTaskFormTypes);
    try {/*from w ww  .j  av  a  2  s. c om*/
        Process activeProcess = null;
        List<SubProcess> activeSubProcessList = new ArrayList<SubProcess>();
        while (xtr.hasNext()) {
            try {
                xtr.next();
            } catch (Exception e) {
                LOGGER.debug("Error reading XML document", e);
                throw new XMLException("Error reading XML", e);
            }

            if (xtr.isEndElement() && ELEMENT_SUBPROCESS.equals(xtr.getLocalName())) {
                activeSubProcessList.remove(activeSubProcessList.size() - 1);
            }

            if (xtr.isEndElement() && ELEMENT_TRANSACTION.equals(xtr.getLocalName())) {
                activeSubProcessList.remove(activeSubProcessList.size() - 1);
            }

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

            if (ELEMENT_DEFINITIONS.equals(xtr.getLocalName())) {
                definitionsParser.parse(xtr, model);

            } else if (ELEMENT_SIGNAL.equals(xtr.getLocalName())) {
                signalParser.parse(xtr, model);

            } else if (ELEMENT_MESSAGE.equals(xtr.getLocalName())) {
                messageParser.parse(xtr, model);

            } else if (ELEMENT_ERROR.equals(xtr.getLocalName())) {

                if (StringUtils.isNotEmpty(xtr.getAttributeValue(null, ATTRIBUTE_ID))) {
                    model.addError(xtr.getAttributeValue(null, ATTRIBUTE_ID),
                            xtr.getAttributeValue(null, ATTRIBUTE_ERROR_CODE));
                }

            } else if (ELEMENT_IMPORT.equals(xtr.getLocalName())) {
                importParser.parse(xtr, model);

            } else if (ELEMENT_ITEM_DEFINITION.equals(xtr.getLocalName())) {
                itemDefinitionParser.parse(xtr, model);

            } else if (ELEMENT_INTERFACE.equals(xtr.getLocalName())) {
                interfaceParser.parse(xtr, model);

            } else if (ELEMENT_IOSPECIFICATION.equals(xtr.getLocalName())) {
                new IOSpecificationParser().parseChildElement(xtr, activeProcess, model);

            } else if (ELEMENT_PARTICIPANT.equals(xtr.getLocalName())) {

                if (StringUtils.isNotEmpty(xtr.getAttributeValue(null, ATTRIBUTE_ID))) {
                    Pool pool = new Pool();
                    pool.setId(xtr.getAttributeValue(null, ATTRIBUTE_ID));
                    pool.setName(xtr.getAttributeValue(null, ATTRIBUTE_NAME));
                    pool.setProcessRef(xtr.getAttributeValue(null, ATTRIBUTE_PROCESS_REF));
                    BpmnXMLUtil.parseChildElements(ELEMENT_PARTICIPANT, pool, xtr, model);
                    model.getPools().add(pool);
                }

            } else if (ELEMENT_PROCESS.equals(xtr.getLocalName())) {

                Process process = processParser.parse(xtr, model);
                if (process != null) {
                    activeProcess = process;
                }

            } else if (ELEMENT_POTENTIAL_STARTER.equals(xtr.getLocalName())) {
                potentialStarterParser.parse(xtr, activeProcess);

            } else if (ELEMENT_LANE.equals(xtr.getLocalName())) {
                laneParser.parse(xtr, activeProcess, model);

            } else if (ELEMENT_DOCUMENTATION.equals(xtr.getLocalName())) {

                BaseElement parentElement = null;
                if (activeSubProcessList.size() > 0) {
                    parentElement = activeSubProcessList.get(activeSubProcessList.size() - 1);
                } else if (activeProcess != null) {
                    parentElement = activeProcess;
                }
                new DocumentationParser().parseChildElement(xtr, parentElement, model);

            } else if (activeProcess == null && ELEMENT_TEXT_ANNOTATION.equals(xtr.getLocalName())) {
                String elementId = xtr.getAttributeValue(null, ATTRIBUTE_ID);
                TextAnnotation textAnnotation = (TextAnnotation) new TextAnnotationXMLConverter()
                        .convertXMLToElement(xtr, model);
                textAnnotation.setId(elementId);
                model.getGlobalArtifacts().add(textAnnotation);

            } else if (activeProcess == null && ELEMENT_ASSOCIATION.equals(xtr.getLocalName())) {
                String elementId = xtr.getAttributeValue(null, ATTRIBUTE_ID);
                Association association = (Association) new AssociationXMLConverter().convertXMLToElement(xtr,
                        model);
                association.setId(elementId);
                model.getGlobalArtifacts().add(association);

            } else if (ELEMENT_EXTENSIONS.equals(xtr.getLocalName())) {
                new ExtensionElementsParser().parse(xtr, activeSubProcessList, activeProcess, model);

            } else if (ELEMENT_SUBPROCESS.equals(xtr.getLocalName())) {
                subProcessParser.parse(xtr, activeSubProcessList, activeProcess);

            } else if (ELEMENT_TRANSACTION.equals(xtr.getLocalName())) {
                subProcessParser.parse(xtr, activeSubProcessList, activeProcess);

            } else if (ELEMENT_DI_SHAPE.equals(xtr.getLocalName())) {
                bpmnShapeParser.parse(xtr, model);

            } else if (ELEMENT_DI_EDGE.equals(xtr.getLocalName())) {
                bpmnEdgeParser.parse(xtr, model);

            } else {

                if (activeSubProcessList.size() > 0
                        && ELEMENT_MULTIINSTANCE.equalsIgnoreCase(xtr.getLocalName())) {

                    new MultiInstanceParser().parseChildElement(xtr,
                            activeSubProcessList.get(activeSubProcessList.size() - 1), model);

                } else if (convertersToBpmnMap.containsKey(xtr.getLocalName())) {
                    if (activeProcess != null) {
                        BaseBpmnXMLConverter converter = convertersToBpmnMap.get(xtr.getLocalName());
                        converter.convertToBpmnModel(xtr, model, activeProcess, activeSubProcessList);
                    }
                }
            }
        }

        for (Process process : model.getProcesses()) {
            for (Pool pool : model.getPools()) {
                if (process.getId().equals(pool.getProcessRef())) {
                    pool.setExecutable(process.isExecutable());
                }
            }
            processFlowElements(process.getFlowElements(), process);
        }

    } catch (XMLException e) {
        throw e;

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