Remove RevisionSourceIdentifier
[yangtools.git] / yang / yang-repo-api / src / main / java / org / opendaylight / yangtools / yang / model / repo / api / YinDomSchemaSource.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.model.repo.api;
9
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;
16
17 import com.google.common.base.MoreObjects;
18 import com.google.common.base.MoreObjects.ToStringHelper;
19 import java.util.Optional;
20 import javax.xml.transform.Source;
21 import javax.xml.transform.TransformerException;
22 import javax.xml.transform.TransformerFactory;
23 import javax.xml.transform.dom.DOMResult;
24 import javax.xml.transform.dom.DOMSource;
25 import org.eclipse.jdt.annotation.NonNull;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.opendaylight.yangtools.yang.common.QName;
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;
35
36 /**
37  * Utility {@link YinXmlSchemaSource} exposing a W3C {@link DOMSource} representation of YIN model.
38  */
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();
45
46     YinDomSchemaSource() {
47         // Prevent outside instantiation
48     }
49
50     /**
51      * Create a new {@link YinDomSchemaSource} using an identifier and a source.
52      *
53      * @param identifier Schema source identifier
54      * @param source W3C DOM source
55      * @param symbolicName Source symbolic name
56      * @return A new {@link YinDomSchemaSource} instance.
57      */
58     public static @NonNull YinDomSchemaSource create(final @NonNull SourceIdentifier identifier,
59             final @NonNull DOMSource source, final @Nullable String symbolicName) {
60
61         final Node root = source.getNode().getFirstChild();
62         final String rootNs = root.getNamespaceURI();
63         if (rootNs == null) {
64             // Let whoever is using this deal with this
65             return new Simple(identifier, source, symbolicName);
66         }
67
68         final QName qname = QName.create(rootNs, root.getLocalName());
69         checkArgument(RFC6020_YIN_MODULE.equals(qname.getModule()),
70             "Root node namepsace %s does not match %s", rootNs, YangConstants.RFC6020_YIN_NAMESPACE);
71         checkArgument(MODULE.getStatementName().equals(qname)
72             || SUBMODULE.getStatementName().equals(qname), "Root element %s is not a module nor a submodule", qname);
73
74         checkArgument(root instanceof Element, "Root node %s is not an element", root);
75         final Element element = (Element)root;
76
77         final Attr nameAttr = element.getAttributeNode(MODULE_ARG);
78         checkArgument(nameAttr != null, "No %s name argument found in %s", element.getLocalName());
79
80         final NodeList revisions = element.getElementsByTagNameNS(REVISION_STMT.getNamespace().toString(),
81             REVISION_STMT.getLocalName());
82         if (revisions.getLength() == 0) {
83             // FIXME: is module name important (as that may have changed)
84             return new Simple(identifier, source, symbolicName);
85         }
86
87         final Element revisionStmt = (Element) revisions.item(0);
88         final Attr dateAttr = revisionStmt.getAttributeNode(REVISION_ARG);
89         checkArgument(dateAttr != null, "No revision statement argument found in %s", revisionStmt);
90
91         final SourceIdentifier parsedId = new SourceIdentifier(nameAttr.getValue(), dateAttr.getValue());
92         final SourceIdentifier id;
93         if (!parsedId.equals(identifier)) {
94             LOG.debug("Changed identifier from {} to {}", identifier, parsedId);
95             id = parsedId;
96         } else {
97             id = identifier;
98         }
99
100         return new Simple(id, source, symbolicName);
101     }
102
103     /**
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.
107      *
108      * @param xmlSchemaSource Backing schema source
109      * @return A {@link YinDomSchemaSource} instance
110      */
111     public static @NonNull YinDomSchemaSource lazyTransform(final YinXmlSchemaSource xmlSchemaSource) {
112         final YinDomSchemaSource cast = castSchemaSource(xmlSchemaSource);
113         return cast != null ? cast : new Transforming(xmlSchemaSource);
114     }
115
116     /**
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.
119      *
120      * @param xmlSchemaSource Backing schema source
121      * @return A {@link YinDomSchemaSource} instance
122      * @throws TransformerException when the provided source fails to transform
123      */
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()),
129                 xmlSchemaSource.getSymbolicName().orElse(null));
130     }
131
132     @Override
133     public abstract DOMSource getSource();
134
135     @Override
136     public final Class<? extends YinXmlSchemaSource> getType() {
137         return YinDomSchemaSource.class;
138     }
139
140     @Override
141     public final String toString() {
142         return addToStringAttributes(MoreObjects.toStringHelper(this).add("identifier", getIdentifier())).toString();
143     }
144
145     /**
146      * Add subclass-specific attributes to the output {@link #toString()} output. Since
147      * subclasses are prevented from overriding {@link #toString()} for consistency
148      * reasons, they can add their specific attributes to the resulting string by attaching
149      * attributes to the supplied {@link ToStringHelper}.
150      *
151      * @param toStringHelper ToStringHelper onto the attributes can be added
152      * @return ToStringHelper supplied as input argument.
153      */
154     protected abstract ToStringHelper addToStringAttributes(ToStringHelper toStringHelper);
155
156     static @NonNull DOMSource transformSource(final Source source) throws TransformerException {
157         final DOMResult result = new DOMResult();
158         TRANSFORMER_FACTORY.newTransformer().transform(source, result);
159
160         return new DOMSource(result.getNode(), result.getSystemId());
161     }
162
163     private static @Nullable YinDomSchemaSource castSchemaSource(final YinXmlSchemaSource xmlSchemaSource) {
164         if (xmlSchemaSource instanceof YinDomSchemaSource) {
165             return (YinDomSchemaSource) xmlSchemaSource;
166         }
167
168         final Source source = xmlSchemaSource.getSource();
169         if (source instanceof DOMSource) {
170             return create(xmlSchemaSource.getIdentifier(), (DOMSource) source,
171                 xmlSchemaSource.getSymbolicName().orElse(null));
172         }
173
174         return null;
175     }
176
177     private static final class Simple extends YinDomSchemaSource {
178         private final @NonNull SourceIdentifier identifier;
179         private final @NonNull DOMSource source;
180         private final String symbolicName;
181
182         Simple(final @NonNull SourceIdentifier identifier, final @NonNull DOMSource source,
183                 final @Nullable String symbolicName) {
184             this.identifier = requireNonNull(identifier);
185             this.source = requireNonNull(source);
186             this.symbolicName = symbolicName;
187         }
188
189         @Override
190         public DOMSource getSource() {
191             return source;
192         }
193
194         @Override
195         public SourceIdentifier getIdentifier() {
196             return identifier;
197         }
198
199         @Override
200         public Optional<String> getSymbolicName() {
201             return Optional.ofNullable(symbolicName);
202         }
203
204         @Override
205         protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
206             return toStringHelper.add("source", source);
207         }
208     }
209
210     private static final class Transforming extends YinDomSchemaSource {
211         private final YinXmlSchemaSource xmlSchemaSource;
212         private volatile DOMSource source;
213
214         Transforming(final YinXmlSchemaSource xmlSchemaSource) {
215             this.xmlSchemaSource = requireNonNull(xmlSchemaSource);
216         }
217
218         @Override
219         public DOMSource getSource() {
220             DOMSource ret = source;
221             if (ret == null) {
222                 synchronized (this) {
223                     ret = source;
224                     if (ret == null) {
225                         try {
226                             ret = transformSource(xmlSchemaSource.getSource());
227                         } catch (TransformerException e) {
228                             throw new IllegalStateException("Failed to transform schema source " + xmlSchemaSource, e);
229                         }
230                         source = ret;
231                     }
232                 }
233             }
234
235             return ret;
236         }
237
238         @Override
239         public SourceIdentifier getIdentifier() {
240             return xmlSchemaSource.getIdentifier();
241         }
242
243         @Override
244         public Optional<String> getSymbolicName() {
245             return xmlSchemaSource.getSymbolicName();
246         }
247
248         @Override
249         protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
250             return toStringHelper.add("xmlSchemaSource", xmlSchemaSource);
251         }
252     }
253 }