Example usage for javax.xml.stream XMLInputFactory newInstance

List of usage examples for javax.xml.stream XMLInputFactory newInstance

Introduction

In this page you can find the example usage for javax.xml.stream XMLInputFactory newInstance.

Prototype

public static XMLInputFactory newInstance() throws FactoryConfigurationError 

Source Link

Document

Creates a new instance of the factory in exactly the same manner as the #newFactory() method.

Usage

From source file:org.squidy.nodes.laserpointer.config.ConfigConnection.java

public void run() {
    try {/*  w  w w. java  2s  .com*/
        xmlEventReader = XMLInputFactory.newInstance().createXMLEventReader(socket.getInputStream());
        xmlEventReader.nextEvent(); // startDocument
        xmlEventReader.nextTag(); // protocol element
        identifier = "" + xmlEventReader.nextEvent();
        identifier = identifier.substring(4, identifier.length() - 3);
        connectionToConfigClient = identifier.startsWith("config");

        if (LOG.isDebugEnabled()) {
            LOG.debug("Connection from camera #" + identifier);
        }
        configManager.attachConnection(this);

        xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(socket.getOutputStream());
        xmlStreamWriter.writeStartDocument();
        xmlStreamWriter.writeStartElement(PROTOCOL_ELEM);
        xmlStreamWriter.writeComment("skipme");
        xmlStreamWriter.flush();

        // write xml for the first time
        marshaller.marshal(configManager.getConfig(), xmlStreamWriter);
        xmlStreamWriter.flush();

        if (connectionToConfigClient) {
            while (running && xmlEventReader.peek().isStartElement()) {

                if (LOG.isDebugEnabled()) {
                    LOG.debug("Receiving configuration from config client.");
                }

                // Reading configuration from config client.
                Configuration configuration = (Configuration) unmarshaller.unmarshal(xmlEventReader);

                // Send update to cameras.
                configManager.updateConfig(this, configuration);

                if (LOG.isDebugEnabled()) {
                    LOG.debug("Writing result back to config client.");
                }

                // Write result back to config client.
                marshaller.marshal(configManager.getConfig(), xmlStreamWriter);
                xmlStreamWriter.flush();
            }
            close();
        }
    } catch (Exception e) {
        if (LOG.isErrorEnabled()) {
            LOG.error(e.getMessage(), e);
        }
        close();
    }
}

From source file:org.squidy.nodes.laserpointer.configclient.service.comm.TcpIpCommService.java

private void newConnection(InputStream in, OutputStream out) throws CommException {
    try {//from   w w  w .  j a  v a  2  s.com

        xsw = XMLOutputFactory.newInstance().createXMLStreamWriter(out);
        xsw.writeStartDocument();
        xsw.writeStartElement(PROTOCOL_ELEM);
        xsw.writeComment(PROTOCOL_COMMENT); // arbitrary
        xsw.flush();

        xer = XMLInputFactory.newInstance().createXMLEventReader(in);
        xer.nextEvent(); // startDocument
        xer.nextTag(); // protocol element TODO: protocol check
        xer.nextEvent(); // arbitrary comment

    } catch (Exception e) {
        // XMLStreamException
        // FactoryConfigurationError
        String msg = "Unable to initialize protocol.";
        LOG.error(msg, e);
        throw new CommException(msg);
    }
}

From source file:org.squidy.nodes.tracking.config.ConfigConnection.java

public void run() {
    try {/*from w  w w.  jav a 2  s  . com*/
        xmlEventReader = XMLInputFactory.newInstance().createXMLEventReader(socket.getInputStream());
        xmlEventReader.nextEvent(); // startDocument
        xmlEventReader.nextTag(); // protocol element
        identifier = "" + xmlEventReader.nextEvent();
        identifier = identifier.substring(4, identifier.length() - 3);
        connectionToConfigClient = identifier.startsWith("config");

        if (LOG.isDebugEnabled()) {
            LOG.debug("Connection from camera #" + identifier);
        }
        configManager.attachConnection(this);

        xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(socket.getOutputStream());
        xmlStreamWriter.writeStartDocument();
        xmlStreamWriter.writeStartElement(PROTOCOL_ELEM);
        xmlStreamWriter.writeComment("skipme");
        xmlStreamWriter.flush();

        // write xml for the first time
        marshaller.marshal(configManager.getConfig(), xmlStreamWriter);
        xmlStreamWriter.flush();

        if (connectionToConfigClient) {
            while (running && xmlEventReader.peek().isStartElement()) {

                if (LOG.isDebugEnabled()) {
                    LOG.debug("Receiving configuration from config client.");
                }

                // Reading configuration from config client.
                Configuration configuration = (Configuration) unmarshaller.unmarshal(xmlEventReader);

                // Send update to cameras.
                configManager.updateConfig(this, configuration);

                if (LOG.isDebugEnabled()) {
                    LOG.debug("Writing result back to config client.");
                }

                // Write result back to config client.
                marshaller.marshal(configManager.getConfig(), xmlStreamWriter);
                xmlStreamWriter.flush();
            }
            close();
        }
    } catch (Exception e) {
        if (running) {
            if (LOG.isErrorEnabled()) {
                LOG.error(e.getMessage(), e);
            }
            close();
        }
    }
}

From source file:org.sweble.wikitext.dumpreader.DumpReader.java

/**
 * The xerces UTF8Reader is broken. If the xerces XML parser is given an
 * input stream, it will instantiate a reader for the encoding found in the
 * XML file, a UTF8Reader in case of an UTF8 encoded XML file. Sadly, this
 * UTF8Reader crashes for certain input (not sure why exactly).
 * /*www  .  j a  v  a  2s  .c o m*/
 * On the other hand, when given a reader, which is forced to work with a
 * certain encoding, the xerces XML parser does not have this freedom and
 * will apparently process Wikipedia dumps just fine.
 * 
 * Therefore, in case you have trouble to parse a XML file, by specifying an
 * encoding, you force the use of a Reader and can circumvent the crash.
 */
private XMLStreamReader getXmlStreamReader(Charset encoding)
        throws FactoryConfigurationError, XMLStreamException, UnsupportedEncodingException {
    XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();

    if (encoding != null) {
        InputStreamReader isr = new InputStreamReader(decompressedInputStream, encoding);
        return xmlInputFactory.createXMLStreamReader(isr);
    } else {
        return xmlInputFactory.createXMLStreamReader(decompressedInputStream);
    }
}

From source file:org.tobarsegais.webapp.data.Index.java

public static Index read(String bundle, InputStream inputStream) throws XMLStreamException {
    try {/*from  ww w  . j  a v a  2s . c o m*/
        return read(bundle, XMLInputFactory.newInstance().createXMLStreamReader(inputStream));
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
}

From source file:org.tobarsegais.webapp.data.Plugin.java

public static Plugin read(InputStream inputStream) throws XMLStreamException {
    try {/* ww  w.j a v  a  2s  .c om*/
        return read(XMLInputFactory.newInstance().createXMLStreamReader(inputStream));
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
}

From source file:org.tobarsegais.webapp.data.Toc.java

public static Toc read(InputStream inputStream) throws XMLStreamException {
    try {//from  w w w . j a  v a 2 s .  c o m
        return read(XMLInputFactory.newInstance().createXMLStreamReader(inputStream));
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
}

From source file:org.ut.biolab.medsavant.client.plugin.AppController.java

public AppDescriptor getDescriptorFromFile(File f) throws PluginVersionException {
    XMLStreamReader reader;/*  w  w w.j a  va2  s .c o  m*/

    try {
        JarFile jar = new JarFile(f);
        ZipEntry entry = jar.getEntry("plugin.xml");
        if (entry != null) {
            InputStream entryStream = jar.getInputStream(entry);
            reader = XMLInputFactory.newInstance().createXMLStreamReader(entryStream);
            String className = null;
            String id = null;
            String version = null;
            String sdkVersion = null;
            String name = null;
            String category = AppDescriptor.Category.UTILITY.toString();
            String currentElement = null;
            String currentText = "";
            do {
                switch (reader.next()) {
                case XMLStreamConstants.START_ELEMENT:
                    switch (readElement(reader)) {
                    case PLUGIN:
                        className = readAttribute(reader, AppDescriptor.PluginXMLAttribute.CLASS);

                        //category can be specified as an attribute or <property>.
                        category = readAttribute(reader, AppDescriptor.PluginXMLAttribute.CATEGORY);
                        break;

                    case ATTRIBUTE:
                        if ("sdk-version".equals(readAttribute(reader, AppDescriptor.PluginXMLAttribute.ID))) {
                            sdkVersion = readAttribute(reader, AppDescriptor.PluginXMLAttribute.VALUE);
                        }
                        break;

                    case PARAMETER:
                        if ("name".equals(readAttribute(reader, AppDescriptor.PluginXMLAttribute.ID))) {
                            name = readAttribute(reader, AppDescriptor.PluginXMLAttribute.VALUE);
                        }
                        break;

                    case PROPERTY:
                        if ("name".equals(readAttribute(reader, AppDescriptor.PluginXMLAttribute.NAME))) {
                            name = readAttribute(reader, AppDescriptor.PluginXMLAttribute.VALUE);
                            if (name == null) {
                                currentElement = "name";
                            }
                        }

                        if ("version".equals(readAttribute(reader, AppDescriptor.PluginXMLAttribute.NAME))) {
                            version = readAttribute(reader, AppDescriptor.PluginXMLAttribute.VALUE);
                            if (version == null) {
                                currentElement = "version";
                            }
                        }

                        if ("sdk-version"
                                .equals(readAttribute(reader, AppDescriptor.PluginXMLAttribute.NAME))) {
                            sdkVersion = readAttribute(reader, AppDescriptor.PluginXMLAttribute.VALUE);
                            if (sdkVersion == null) {
                                currentElement = "sdk-version";
                            }
                        }

                        if ("category".equals(readAttribute(reader, AppDescriptor.PluginXMLAttribute.NAME))) {
                            category = readAttribute(reader, AppDescriptor.PluginXMLAttribute.VALUE);
                            if (category == null) {
                                currentElement = "category";
                            }
                        }

                        break;
                    }
                    break;

                case XMLStreamConstants.CHARACTERS:
                    if (reader.isWhiteSpace()) {
                        break;
                    } else if (currentElement != null) {
                        currentText += reader.getText().trim().replace("\t", "");
                    }
                    break;

                case XMLStreamConstants.END_ELEMENT:
                    if (readElement(reader) == AppDescriptor.PluginXMLElement.PROPERTY) {
                        if (currentElement != null && currentText.length() > 0) {
                            if (currentElement.equals("name")) {
                                name = currentText;
                            } else if (currentElement.equals("sdk-version")) {
                                sdkVersion = currentText;
                            } else if (currentElement.equals("category")) {
                                category = currentText;
                            } else if (currentElement.equals("version")) {
                                version = currentText;
                            }
                        }
                        currentText = "";
                        currentElement = null;
                    }
                    break;

                case XMLStreamConstants.END_DOCUMENT:
                    reader.close();
                    reader = null;
                    break;
                }
            } while (reader != null);

            System.out.println(className + " " + name + " " + version);

            if (className != null && name != null && version != null) {
                return new AppDescriptor(className, version, name, sdkVersion, category, f);
            }
        }
    } catch (Exception x) {
        LOG.error("Error parsing plugin.xml from " + f.getAbsolutePath() + ": " + x);
    }
    throw new PluginVersionException(f.getName() + " did not contain a valid plugin");
}

From source file:org.ut.biolab.medsavant.client.plugin.PluginIndex.java

public PluginIndex(URL url) throws IOException {
    urls = new HashMap<String, URL>();
    try {/*from  w w  w  .  ja v  a2  s. c  om*/

        XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(
                ClientNetworkUtils.openStream(url, ClientNetworkUtils.NONCRITICAL_CONNECT_TIMEOUT,
                        ClientNetworkUtils.NONCRITICAL_READ_TIMEOUT));
        if (reader.getVersion() == null) {
            throw new XMLStreamException("Invalid XML at URL " + url);
        }
        boolean done = false;
        String id = null;
        do {
            if (reader.hasNext()) {
                int t = reader.next();
                switch (t) {
                case XMLStreamConstants.START_ELEMENT:
                    String elemName = reader.getLocalName();
                    if (elemName.equals("leaf")) {
                        id = reader.getAttributeValue(null, "id");
                    } else if (elemName.equals("url")) {
                        if (id != null) {
                            try {
                                urls.put(id, new URL(reader.getElementText()));
                            } catch (MalformedURLException x) {
                                LOG.warn(String.format("Unable to parse \"%s\" as a plugin URL.",
                                        reader.getElementText()));
                            }
                            id = null;
                        }
                    }
                    break;
                case XMLStreamConstants.END_DOCUMENT:
                    reader.close();
                    done = true;
                    break;
                }
            } else {
                throw new XMLStreamException("Malformed XML at " + url);
            }
        } while (!done);
    } catch (XMLStreamException x) {
        throw new IOException("Unable to get version number from web-site.", x);
    }
}

From source file:org.wso2.am.admin.clients.endpoint.EndPointAdminClient.java

public boolean addEndPoint(DataHandler dh)
        throws EndpointAdminEndpointAdminException, IOException, XMLStreamException {
    XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(dh.getInputStream());
    //create the builder
    StAXOMBuilder builder = new StAXOMBuilder(parser);
    OMElement endPointElem = builder.getDocumentElement();
    return endpointAdminStub.addEndpoint(endPointElem.toString());
}