2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.parser.rfc7950.repo;
10 import com.google.common.annotations.Beta;
11 import com.google.common.util.concurrent.Futures;
12 import java.io.IOException;
13 import javax.xml.parsers.SAXParser;
14 import javax.xml.transform.dom.DOMSource;
15 import org.opendaylight.yangtools.util.xml.UntrustedXML;
16 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
17 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
18 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceTransformer;
19 import org.opendaylight.yangtools.yang.model.spi.source.YinDomSource;
20 import org.opendaylight.yangtools.yang.model.spi.source.YinTextSource;
21 import org.w3c.dom.Document;
22 import org.xml.sax.SAXException;
23 import org.xml.sax.helpers.DefaultHandler;
26 * A {@link SchemaSourceTransformer} which handles translation of models from {@link YinTextSource} representation into
27 * {@link YinDomSource}.
30 public final class YinTextToDomTransformer extends SchemaSourceTransformer<YinTextSource, YinDomSource> {
31 private YinTextToDomTransformer(final SchemaRepository provider, final SchemaSourceRegistry consumer) {
32 super(provider, YinTextSource.class, consumer, YinDomSource.class,
33 input -> Futures.immediateFuture(transformSource(input)));
36 public static YinTextToDomTransformer create(final SchemaRepository provider, final SchemaSourceRegistry consumer) {
37 return new YinTextToDomTransformer(provider, consumer);
40 public static YinDomSource transformSource(final YinTextSource source) throws SAXException, IOException {
41 final Document doc = UntrustedXML.newDocumentBuilder().newDocument();
42 final SAXParser parser = UntrustedXML.newSAXParser();
43 final DefaultHandler handler = new StatementSourceReferenceHandler(doc, null);
44 parser.parse(source.openStream(), handler);
45 return YinDomSource.create(source.sourceId(), new DOMSource(doc), source.symbolicName());