Search in sources :

Example 1 with XmlParserStream

use of org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream in project bgpcep by opendaylight.

the class ConfigLoaderImpl method parseDefaultConfigFile.

private NormalizedNode<?, ?> parseDefaultConfigFile(final ConfigFileProcessor config, final String filename) throws IOException, XMLStreamException {
    final NormalizedNodeResult result = new NormalizedNodeResult();
    final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
    final File newFile = new File(this.path, filename);
    FileChannel channel = new RandomAccessFile(newFile, READ).getChannel();
    FileLock lock = null;
    final Stopwatch stopwatch = Stopwatch.createStarted();
    while (lock == null || stopwatch.elapsed(TimeUnit.SECONDS) > TIMEOUT_SECONDS) {
        try {
            lock = channel.tryLock();
        } catch (final IllegalStateException e) {
        // Ignore
        }
        if (lock == null) {
            try {
                Thread.sleep(100L);
            } catch (InterruptedException e) {
                LOG.warn("Failed to lock xml", e);
            }
        }
    }
    try (InputStream resourceAsStream = new FileInputStream(newFile)) {
        final XMLInputFactory factory = XMLInputFactory.newInstance();
        final XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream);
        final SchemaNode schemaNode = SchemaContextUtil.findDataSchemaNode(this.schemaContext, config.getSchemaPath());
        try (XmlParserStream xmlParser = XmlParserStream.create(streamWriter, this.schemaContext, schemaNode)) {
            xmlParser.parse(reader);
        } catch (final URISyntaxException | XMLStreamException | IOException | ParserConfigurationException | SAXException e) {
            LOG.warn("Failed to parse xml", e);
        } finally {
            reader.close();
            channel.close();
        }
    }
    return result.getResult();
}
Also used : XmlParserStream(org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream) XMLStreamReader(javax.xml.stream.XMLStreamReader) FileChannel(java.nio.channels.FileChannel) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Stopwatch(com.google.common.base.Stopwatch) ImmutableNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) SAXException(org.xml.sax.SAXException) SchemaNode(org.opendaylight.yangtools.yang.model.api.SchemaNode) RandomAccessFile(java.io.RandomAccessFile) XMLStreamException(javax.xml.stream.XMLStreamException) NormalizedNodeResult(org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult) FileLock(java.nio.channels.FileLock) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 2 with XmlParserStream

use of org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream in project controller by opendaylight.

the class BindingContext method parseDataElement.

public NormalizedNode<?, ?> parseDataElement(final Element element, final DataSchemaNode dataSchema, final SchemaContext schemaContext) throws XMLStreamException, IOException, ParserConfigurationException, SAXException, URISyntaxException {
    final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
    final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
    final XmlParserStream xmlParser = XmlParserStream.create(writer, schemaContext, dataSchema);
    xmlParser.traverse(new DOMSource(element));
    final NormalizedNode<?, ?> result = resultHolder.getResult();
    if (result instanceof MapNode) {
        final MapNode mapNode = (MapNode) result;
        final MapEntryNode mapEntryNode = mapNode.getValue().iterator().next();
        return mapEntryNode;
    }
    return result;
}
Also used : XmlParserStream(org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream) DOMSource(javax.xml.transform.dom.DOMSource) NormalizedNodeResult(org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult) ImmutableNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) MapNode(org.opendaylight.yangtools.yang.data.api.schema.MapNode) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode)

Aggregations

NormalizedNodeStreamWriter (org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter)2 XmlParserStream (org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream)2 ImmutableNormalizedNodeStreamWriter (org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter)2 NormalizedNodeResult (org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult)2 Stopwatch (com.google.common.base.Stopwatch)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 RandomAccessFile (java.io.RandomAccessFile)1 URISyntaxException (java.net.URISyntaxException)1 FileChannel (java.nio.channels.FileChannel)1 FileLock (java.nio.channels.FileLock)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 XMLInputFactory (javax.xml.stream.XMLInputFactory)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 DOMSource (javax.xml.transform.dom.DOMSource)1 MapEntryNode (org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode)1 MapNode (org.opendaylight.yangtools.yang.data.api.schema.MapNode)1