Merge "Bug 1441: Fixed incorrect commas serialization of unkeyed-list."
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / codec / xml / ElementIdentityrefParser.java
1 /*
2  * Copyright (c) 2014 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.impl.codec.xml;
9
10 import com.google.common.base.Preconditions;
11
12 import java.net.URI;
13
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.data.util.AbstractStringIdentityrefCodec;
16 import org.opendaylight.yangtools.yang.model.api.Module;
17 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
18 import org.w3c.dom.Element;
19
20 final class ElementIdentityrefParser extends AbstractStringIdentityrefCodec {
21     private final SchemaContext schema;
22     private final Element element;
23
24     ElementIdentityrefParser(final SchemaContext schema, final Element element) {
25         this.element = Preconditions.checkNotNull(element);
26         this.schema = Preconditions.checkNotNull(schema);
27     }
28
29     @Override
30     protected String prefixForNamespace(final URI namespace) {
31         return element.lookupPrefix(namespace.toString());
32     }
33
34     @Override
35     protected QName createQName(final String prefix, final String localName) {
36         final String namespace = element.lookupNamespaceURI(prefix);
37         Preconditions.checkArgument(namespace != null, "Failed to lookup prefix %s", prefix);
38
39         final URI ns = URI.create(namespace);
40         final Module module = schema.findModuleByNamespaceAndRevision(ns, null);
41         Preconditions.checkArgument(module != null, "Namespace %s is not owned by a module", ns);
42         return QName.create(module.getQNameModule(), localName);
43     }
44
45 }