2 * Copyright (c) 2015 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.model.repo.api;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12 import static org.opendaylight.yangtools.yang.common.YangConstants.RFC6020_YIN_MODULE;
13 import static org.opendaylight.yangtools.yang.model.api.YangStmtMapping.MODULE;
14 import static org.opendaylight.yangtools.yang.model.api.YangStmtMapping.REVISION;
15 import static org.opendaylight.yangtools.yang.model.api.YangStmtMapping.SUBMODULE;
17 import com.google.common.base.MoreObjects;
18 import com.google.common.base.MoreObjects.ToStringHelper;
19 import javax.xml.transform.Source;
20 import javax.xml.transform.TransformerException;
21 import javax.xml.transform.TransformerFactory;
22 import javax.xml.transform.dom.DOMResult;
23 import javax.xml.transform.dom.DOMSource;
24 import org.eclipse.jdt.annotation.NonNull;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.common.Revision;
28 import org.opendaylight.yangtools.yang.common.YangConstants;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.w3c.dom.Attr;
32 import org.w3c.dom.Element;
33 import org.w3c.dom.Node;
34 import org.w3c.dom.NodeList;
37 * Utility {@link YinXmlSchemaSource} exposing a W3C {@link DOMSource} representation of YIN model.
39 public abstract class YinDomSchemaSource implements YinXmlSchemaSource {
40 private static final Logger LOG = LoggerFactory.getLogger(YinDomSchemaSource.class);
41 private static final TransformerFactory TRANSFORMER_FACTORY = TransformerFactory.newInstance();
42 private static final QName REVISION_STMT = REVISION.getStatementName();
43 private static final String MODULE_ARG = MODULE.getArgumentDefinition().get().getArgumentName().getLocalName();
44 private static final String REVISION_ARG = REVISION.getArgumentDefinition().get().getArgumentName().getLocalName();
46 YinDomSchemaSource() {
47 // Prevent outside instantiation
51 * Create a new {@link YinDomSchemaSource} using an identifier and a source.
53 * @param identifier Schema source identifier
54 * @param source W3C DOM source
55 * @return A new {@link YinDomSchemaSource} instance.
57 public static @NonNull YinDomSchemaSource create(final @NonNull SourceIdentifier identifier,
58 final @NonNull DOMSource source) {
60 final Node root = source.getNode().getFirstChild();
61 final String rootNs = root.getNamespaceURI();
63 // Let whoever is using this deal with this
64 return new Simple(identifier, source);
67 final QName qname = QName.create(rootNs, root.getLocalName());
68 checkArgument(RFC6020_YIN_MODULE.equals(qname.getModule()),
69 "Root node namepsace %s does not match %s", rootNs, YangConstants.RFC6020_YIN_NAMESPACE);
70 checkArgument(MODULE.getStatementName().equals(qname)
71 || SUBMODULE.getStatementName().equals(qname), "Root element %s is not a module nor a submodule", qname);
73 checkArgument(root instanceof Element, "Root node %s is not an element", root);
74 final Element element = (Element)root;
76 final Attr nameAttr = element.getAttributeNode(MODULE_ARG);
77 checkArgument(nameAttr != null, "No %s name argument found in %s", element.getLocalName());
79 final NodeList revisions = element.getElementsByTagNameNS(REVISION_STMT.getNamespace().toString(),
80 REVISION_STMT.getLocalName());
81 if (revisions.getLength() == 0) {
82 // FIXME: is module name important (as that may have changed)
83 return new Simple(identifier, source);
86 final Element revisionStmt = (Element) revisions.item(0);
87 final Attr dateAttr = revisionStmt.getAttributeNode(REVISION_ARG);
88 checkArgument(dateAttr != null, "No revision statement argument found in %s", revisionStmt);
90 final SourceIdentifier parsedId = RevisionSourceIdentifier.create(nameAttr.getValue(),
91 Revision.of(dateAttr.getValue()));
92 final SourceIdentifier id;
93 if (!parsedId.equals(identifier)) {
94 LOG.debug("Changed identifier from {} to {}", identifier, parsedId);
100 return new Simple(id, source);
104 * Create a {@link YinDomSchemaSource} from a {@link YinXmlSchemaSource}. If the argument is already a
105 * YinDomSchemaSource, this method returns the same instance. The source will be translated on first access,
106 * at which point an {@link IllegalStateException} may be raised.
108 * @param xmlSchemaSource Backing schema source
109 * @return A {@link YinDomSchemaSource} instance
111 public static @NonNull YinDomSchemaSource lazyTransform(final YinXmlSchemaSource xmlSchemaSource) {
112 final YinDomSchemaSource cast = castSchemaSource(xmlSchemaSource);
113 return cast != null ? cast : new Transforming(xmlSchemaSource);
117 * Create a {@link YinDomSchemaSource} from a {@link YinXmlSchemaSource}. If the argument is already a
118 * YinDomSchemaSource, this method returns the same instance. The source will be translated immediately.
120 * @param xmlSchemaSource Backing schema source
121 * @return A {@link YinDomSchemaSource} instance
122 * @throws TransformerException when the provided source fails to transform
124 public static @NonNull YinDomSchemaSource transform(final YinXmlSchemaSource xmlSchemaSource)
125 throws TransformerException {
126 final YinDomSchemaSource cast = castSchemaSource(xmlSchemaSource);
127 return cast != null ? cast :
128 create(xmlSchemaSource.getIdentifier(), transformSource(xmlSchemaSource.getSource()));
132 public abstract DOMSource getSource();
135 public final Class<? extends YinXmlSchemaSource> getType() {
136 return YinDomSchemaSource.class;
140 public final String toString() {
141 return addToStringAttributes(MoreObjects.toStringHelper(this).add("identifier", getIdentifier())).toString();
145 * Add subclass-specific attributes to the output {@link #toString()} output. Since
146 * subclasses are prevented from overriding {@link #toString()} for consistency
147 * reasons, they can add their specific attributes to the resulting string by attaching
148 * attributes to the supplied {@link ToStringHelper}.
150 * @param toStringHelper ToStringHelper onto the attributes can be added
151 * @return ToStringHelper supplied as input argument.
153 protected abstract ToStringHelper addToStringAttributes(ToStringHelper toStringHelper);
155 static @NonNull DOMSource transformSource(final Source source) throws TransformerException {
156 final DOMResult result = new DOMResult();
157 TRANSFORMER_FACTORY.newTransformer().transform(source, result);
159 return new DOMSource(result.getNode(), result.getSystemId());
162 private static @Nullable YinDomSchemaSource castSchemaSource(final YinXmlSchemaSource xmlSchemaSource) {
163 if (xmlSchemaSource instanceof YinDomSchemaSource) {
164 return (YinDomSchemaSource) xmlSchemaSource;
167 final Source source = xmlSchemaSource.getSource();
168 if (source instanceof DOMSource) {
169 return create(xmlSchemaSource.getIdentifier(), (DOMSource) source);
175 private static final class Simple extends YinDomSchemaSource {
176 private final @NonNull SourceIdentifier identifier;
177 private final @NonNull DOMSource source;
179 Simple(final @NonNull SourceIdentifier identifier, final @NonNull DOMSource source) {
180 this.identifier = requireNonNull(identifier);
181 this.source = requireNonNull(source);
185 public DOMSource getSource() {
190 public SourceIdentifier getIdentifier() {
195 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
196 return toStringHelper.add("source", source);
200 private static final class Transforming extends YinDomSchemaSource {
201 private final YinXmlSchemaSource xmlSchemaSource;
202 private volatile DOMSource source;
204 Transforming(final YinXmlSchemaSource xmlSchemaSource) {
205 this.xmlSchemaSource = requireNonNull(xmlSchemaSource);
209 public DOMSource getSource() {
210 DOMSource ret = source;
212 synchronized (this) {
216 ret = transformSource(xmlSchemaSource.getSource());
217 } catch (TransformerException e) {
218 throw new IllegalStateException("Failed to transform schema source " + xmlSchemaSource, e);
229 public SourceIdentifier getIdentifier() {
230 return xmlSchemaSource.getIdentifier();
234 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
235 return toStringHelper.add("xmlSchemaSource", xmlSchemaSource);