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
9 package org.opendaylight.yangtools.yang.data.codec.xml;
11 import com.google.common.base.Preconditions;
13 import javax.xml.namespace.NamespaceContext;
14 import javax.xml.stream.XMLStreamException;
15 import javax.xml.stream.XMLStreamWriter;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import org.opendaylight.yangtools.yang.data.util.AbstractModuleStringIdentityrefCodec;
19 import org.opendaylight.yangtools.yang.model.api.Module;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 final class XmlStringIdentityrefCodec extends AbstractModuleStringIdentityrefCodec implements XmlCodec<QName> {
24 private final SchemaContext context;
25 private final QNameModule parentModuleQname;
26 private final NamespaceContext namespaceContext;
28 XmlStringIdentityrefCodec(final SchemaContext context, final QNameModule parentModule,
29 final NamespaceContext namespaceContext) {
30 this.context = Preconditions.checkNotNull(context);
31 this.parentModuleQname = Preconditions.checkNotNull(parentModule);
32 this.namespaceContext = Preconditions.checkNotNull(namespaceContext);
36 protected Module moduleForPrefix(final String prefix) {
37 if (prefix.isEmpty()) {
38 return context.findModuleByNamespaceAndRevision(parentModuleQname.getNamespace(),
39 parentModuleQname.getRevision());
41 final String prefixedNS = namespaceContext.getNamespaceURI(prefix);
42 return context.findModuleByNamespaceAndRevision(URI.create(prefixedNS), null);
47 protected String prefixForNamespace(final URI namespace) {
48 final Module module = context.findModuleByNamespaceAndRevision(namespace, null);
49 return module == null ? null : module.getName();
53 * Serialize QName with specified XMLStreamWriter.
55 * @param writer XMLStreamWriter
59 public void serializeToWriter(final XMLStreamWriter writer, final QName value) throws XMLStreamException {
60 writer.writeCharacters(serialize(value));