Example usage for javax.xml.stream XMLInputFactory createXMLStreamReader

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

Introduction

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

Prototype

public abstract XMLStreamReader createXMLStreamReader(String systemId, java.io.Reader reader)
        throws XMLStreamException;

Source Link

Document

Create a new XMLStreamReader from a java.io.InputStream

Usage

From source file:org.deegree.tools.alkis.BackReferenceFixer.java

public static void main(String[] args) {
    Options opts = initOptions();//from   w  w w  .  j a v  a  2s.  c  om
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
        CommandLine line = new PosixParser().parse(opts, args);
        String input = line.getOptionValue('i');
        String output = line.getOptionValue('o');
        String schema = line.getOptionValue('s');
        fis = new FileInputStream(input);
        fos = new FileOutputStream(output);
        XMLInputFactory xifac = XMLInputFactory.newInstance();
        XMLOutputFactory xofac = XMLOutputFactory.newInstance();
        XMLStreamReader xreader = xifac.createXMLStreamReader(input, fis);
        IndentingXMLStreamWriter xwriter = new IndentingXMLStreamWriter(xofac.createXMLStreamWriter(fos));
        GMLStreamReader reader = GMLInputFactory.createGMLStreamReader(GMLVersion.GML_32, xreader);

        AppSchema appSchema = new GMLAppSchemaReader(null, null, schema).extractAppSchema();
        reader.setApplicationSchema(appSchema);

        GMLStreamWriter writer = GMLOutputFactory.createGMLStreamWriter(GMLVersion.GML_32, xwriter);
        XlinkedObjectsHandler handler = new XlinkedObjectsHandler(true, null, new GmlXlinkOptions());
        writer.setReferenceResolveStrategy(handler);

        QName prop = new QName(ns601, "dientZurDarstellungVon");

        Map<String, List<String>> refs = new HashMap<String, List<String>>();
        Map<String, List<String>> types = new HashMap<String, List<String>>();
        Map<String, String> bindings = null;

        for (Feature f : reader.readFeatureCollectionStream()) {
            if (bindings == null) {
                bindings = f.getType().getSchema().getNamespaceBindings();
            }
            for (Property p : f.getProperties(prop)) {
                FeatureReference ref = (FeatureReference) p.getValue();
                List<String> list = refs.get(ref.getId());
                if (list == null) {
                    list = new ArrayList<String>();
                    refs.put(ref.getId(), list);
                }
                list.add(f.getId());
                list = types.get(ref.getId());
                if (list == null) {
                    list = new ArrayList<String>();
                    types.put(ref.getId(), list);
                }
                list.add("inversZu_dientZurDarstellungVon_" + f.getType().getName().getLocalPart());
            }
        }

        QName[] inversePropNames = new QName[] {
                new QName(ns601, "inversZu_dientZurDarstellungVon_AP_Darstellung"),
                new QName(ns601, "inversZu_dientZurDarstellungVon_AP_LTO"),
                new QName(ns601, "inversZu_dientZurDarstellungVon_AP_PTO"),
                new QName(ns601, "inversZu_dientZurDarstellungVon_AP_FPO"),
                new QName(ns601, "inversZu_dientZurDarstellungVon_AP_KPO_3D"),
                new QName(ns601, "inversZu_dientZurDarstellungVon_AP_LPO"),
                new QName(ns601, "inversZu_dientZurDarstellungVon_AP_PPO") };

        reader.close();
        fis.close();
        writer.setNamespaceBindings(bindings);

        fis = new FileInputStream(input);
        xreader = xifac.createXMLStreamReader(input, fis);
        reader = GMLInputFactory.createGMLStreamReader(GMLVersion.GML_32, xreader);
        reader.setApplicationSchema(appSchema);

        if (bindings != null) {
            for (Map.Entry<String, String> e : bindings.entrySet()) {
                if (!e.getKey().isEmpty()) {
                    xwriter.setPrefix(e.getValue(), e.getKey());
                }
            }
        }
        xwriter.writeStartDocument();
        xwriter.setPrefix("gml", "http://www.opengis.net/gml/3.2");
        xwriter.writeStartElement("http://www.opengis.net/gml/3.2", "FeatureCollection");
        xwriter.writeNamespace("gml", "http://www.opengis.net/gml/3.2");

        GmlDocumentIdContext ctx = new GmlDocumentIdContext(GMLVersion.GML_32);

        for (Feature f : reader.readFeatureCollectionStream()) {
            if (refs.containsKey(f.getId())) {
                List<Property> props = new ArrayList<Property>(f.getProperties());
                ListIterator<Property> iter = props.listIterator();
                String name = iter.next().getName().getLocalPart();
                while (name.equals("lebenszeitintervall") || name.equals("modellart") || name.equals("anlass")
                        || name.equals("zeigtAufExternes") || name.equals("istTeilVon")
                        || name.equals("identifier")) {
                    if (iter.hasNext()) {
                        name = iter.next().getName().getLocalPart();
                    } else {
                        break;
                    }
                }
                if (iter.hasPrevious()) {
                    iter.previous();
                }
                for (QName propName : inversePropNames) {
                    Iterator<String> idIter = refs.get(f.getId()).iterator();
                    Iterator<String> typeIter = types.get(f.getId()).iterator();
                    while (idIter.hasNext()) {
                        String id = idIter.next();
                        if (typeIter.next().equals(propName.getLocalPart())) {
                            PropertyType pt = f.getType().getPropertyDeclaration(propName);
                            Property p = new GenericProperty(pt, new FeatureReference(ctx, "#" + id, null));
                            iter.add(p);
                        }
                    }
                }
                f.setProperties(props);

            }
            xwriter.writeStartElement("http://www.opengis.net/gml/3.2", "featureMember");
            writer.write(f);
            xwriter.writeEndElement();
        }

        xwriter.writeEndElement();
        xwriter.close();
    } catch (Throwable e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(fis);
        IOUtils.closeQuietly(fos);
    }
}

From source file:org.jbpm.designer.web.profile.impl.JbpmProfileImpl.java

private void initializeLocalPlugins(ServletContext context) {
    Map<String, IDiagramPlugin> registry = PluginServiceImpl.getLocalPluginsRegistry(context);
    FileInputStream fileStream = null;
    try {/*ww w  .j  a v  a  2s. co  m*/
        try {
            fileStream = new FileInputStream(new StringBuilder(context.getRealPath("/")).append("/")
                    .append(ConfigurationProvider.getInstance().getDesignerContext()).append("profiles")
                    .append("/").append("jbpm.xml").toString());
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLStreamReader reader = factory.createXMLStreamReader(fileStream, "UTF-8");
        while (reader.hasNext()) {
            if (reader.next() == XMLStreamReader.START_ELEMENT) {
                if ("profile".equals(reader.getLocalName())) {
                    for (int i = 0; i < reader.getAttributeCount(); i++) {
                        if ("stencilset".equals(reader.getAttributeLocalName(i))) {
                            _stencilSet = reader.getAttributeValue(i);
                        }
                    }
                } else if ("plugin".equals(reader.getLocalName())) {
                    String name = null;
                    for (int i = 0; i < reader.getAttributeCount(); i++) {
                        if ("name".equals(reader.getAttributeLocalName(i))) {
                            name = reader.getAttributeValue(i);
                        }
                    }
                    _plugins.put(name, registry.get(name));
                } else if ("localhistory".equals(reader.getLocalName())) {
                    for (int i = 0; i < reader.getAttributeCount(); i++) {
                        if ("enabled".equals(reader.getAttributeLocalName(i))) {
                            String localhistoryenabled = reader.getAttributeValue(i);
                            if (!isEmpty(localhistoryenabled)) {
                                _localHistoryEnabled = localhistoryenabled;
                            } else {
                                _logger.info("Invalid local history enabled");
                            }
                        }
                        if ("timeout".equals(reader.getAttributeLocalName(i))) {
                            String localhistorytimeout = reader.getAttributeValue(i);
                            if (!isEmpty(localhistorytimeout)) {
                                _localHistoryTimeout = localhistorytimeout;
                            } else {
                                _logger.info("Invalid local history timeout");
                            }
                        }
                    }
                } else if ("storesvgonsave".equals(reader.getLocalName())) {
                    for (int i = 0; i < reader.getAttributeCount(); i++) {
                        if ("enabled".equals(reader.getAttributeLocalName(i))) {
                            String storesvgonsaveenabled = reader.getAttributeValue(i);
                            if (!isEmpty(storesvgonsaveenabled)) {
                                _storeSVGonSaveOption = storesvgonsaveenabled;
                            } else {
                                _logger.info("Invalid store svg on save enabled");
                            }
                        }
                    }
                }
            }
        }
    } catch (XMLStreamException e) {
        _logger.error(e.getMessage(), e);
        throw new RuntimeException(e); // stop initialization
    } finally {
        if (fileStream != null) {
            try {
                fileStream.close();
            } catch (IOException e) {
            }
        }
        ;
    }
}

From source file:org.jbpm.designer.web.server.ProcessDiffServiceServlet.java

private List<String> getAssetVersions(String packageName, String assetName, String uuid,
        IDiagramProfile profile) {/*from  ww  w . j av a2s . c om*/
    try {
        String assetVersionURL = RepositoryInfo.getRepositoryProtocol(profile) + "://"
                + RepositoryInfo.getRepositoryHost(profile) + "/"
                + RepositoryInfo.getRepositorySubdomain(profile).substring(0,
                        RepositoryInfo.getRepositorySubdomain(profile).indexOf("/"))
                + "/rest/packages/" + URLEncoder.encode(packageName, "UTF-8") + "/assets/" + assetName
                + "/versions/";
        List<String> versionList = new ArrayList<String>();

        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLStreamReader reader = factory.createXMLStreamReader(
                ServletUtil.getInputStreamForURL(assetVersionURL, "GET", profile), "UTF-8");
        boolean isFirstTitle = true;
        String title = "";
        while (reader.hasNext()) {
            int next = reader.next();
            if (next == XMLStreamReader.START_ELEMENT) {
                if ("title".equals(reader.getLocalName())) {
                    if (isFirstTitle) {
                        isFirstTitle = false;
                    } else {
                        versionList.add(reader.getElementText());
                    }
                }
            }
        }
        return versionList;
    } catch (Exception e) {
        _logger.error(e.getMessage());
        return null;
    }
}

From source file:org.sonar.plugins.plsqltoad.PlSqlToadResultsParser.java

private void parse(File codeXpertXml) {
    LOG.info("Parsing Toad's result XML file {}..", codeXpertXml.getAbsolutePath());

    currentFileAbsolutePath = null;/*from ww  w.j  ava  2  s  .c om*/

    try {
        XMLInputFactory xmlFactory = XMLInputFactory2.newInstance();
        InputStream is = new AsciiFilterInputStream(new FileInputStream(codeXpertXml));
        SMHierarchicCursor rootCursor = SMInputFactory
                .rootElementCursor(xmlFactory.createXMLStreamReader(is, Charsets.US_ASCII.name()));

        rootCursor.advance();

        try {
            SMInputCursor objectCursor = rootCursor.childElementCursor("object");
            while (objectCursor.getNext() != null) {
                processFile(objectCursor.descendantElementCursor());
            }
        } finally {
            rootCursor.getStreamReader().closeCompletely();
        }
    } catch (FileNotFoundException e) {
        throw new PlSqlToadException(
                "Unable to find the Toad's result XML file at " + codeXpertXml.getAbsolutePath(), e);
    } catch (XMLStreamException e) {
        throw new PlSqlToadException("Error while parsing Toad's result XML file at "
                + codeXpertXml.getAbsolutePath() + ", current source file = "
                + (currentFileAbsolutePath == null ? "[unknown]" : currentFileAbsolutePath) + "\n"
                + e.getMessage(), e);
    }
}