6b106af50a2e7bad8cebdf409c08fa80e9e58479
[yangtools.git] / codec / yang-data-codec-xml / src / main / java / org / opendaylight / yangtools / yang / data / codec / xml / IdentityrefXmlCodec.java
1 /*
2  * Copyright (c) 2016 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.data.codec.xml;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.base.MoreObjects;
14 import javax.xml.namespace.NamespaceContext;
15 import javax.xml.stream.XMLStreamException;
16 import javax.xml.stream.XMLStreamWriter;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.common.QNameModule;
20 import org.opendaylight.yangtools.yang.common.XMLNamespace;
21 import org.opendaylight.yangtools.yang.data.util.codec.IdentityCodecUtil;
22 import org.opendaylight.yangtools.yang.data.util.codec.QNameCodecUtil;
23 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
24
25 final class IdentityrefXmlCodec implements XmlCodec<QName> {
26     private final @NonNull EffectiveModelContext context;
27     private final @NonNull PreferredPrefixes pref;
28     private final @NonNull QNameModule parentModule;
29
30     IdentityrefXmlCodec(final EffectiveModelContext context, final PreferredPrefixes pref,
31             final QNameModule parentModule) {
32         this.context = requireNonNull(context);
33         this.pref = requireNonNull(pref);
34         this.parentModule = requireNonNull(parentModule);
35     }
36
37     @Override
38     public Class<QName> getDataType() {
39         return QName.class;
40     }
41
42     @Override
43     public QName parseValue(final NamespaceContext ctx, final String str) {
44         // FIXME: YANGTOOLS-1523: do not trim()
45         return IdentityCodecUtil.parseIdentity(str.trim(), context, prefix -> {
46             if (prefix.isEmpty()) {
47                 return parentModule;
48             }
49
50             final var prefixedNS = ctx.getNamespaceURI(prefix);
51             checkArgument(prefixedNS != null, "Failed to resolve prefix %s", prefix);
52
53             final var modules = context.findModuleStatements(XMLNamespace.of(prefixedNS)).iterator();
54             checkArgument(modules.hasNext(), "Could not find module for namespace %s", prefixedNS);
55             return modules.next().localQNameModule();
56         }).getQName();
57     }
58
59     @Override
60     public void writeValue(final XMLStreamWriter ctx, final QName value) throws XMLStreamException {
61         final var prefixes = new NamespacePrefixes(pref, ctx.getNamespaceContext());
62         final var str = QNameCodecUtil.encodeQName(value, uri -> prefixes.encodePrefix(uri.getNamespace()));
63
64         for (var e : prefixes.emittedPrefixes()) {
65             ctx.writeNamespace(e.getValue(), e.getKey().toString());
66         }
67         ctx.writeCharacters(str);
68     }
69
70     @Override
71     public String toString() {
72         return MoreObjects.toStringHelper(this).add("module", parentModule).toString();
73     }
74 }