Propagate @Nonnull and @Nullable annotations
[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 import java.net.URI;
12 import javax.annotation.Nonnull;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.data.util.AbstractStringIdentityrefCodec;
15 import org.opendaylight.yangtools.yang.model.api.Module;
16 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
17 import org.w3c.dom.Element;
18
19 final class ElementIdentityrefParser extends AbstractStringIdentityrefCodec {
20     private final SchemaContext schema;
21     private final Element element;
22
23     ElementIdentityrefParser(final SchemaContext schema, final Element element) {
24         this.element = Preconditions.checkNotNull(element);
25         this.schema = Preconditions.checkNotNull(schema);
26     }
27
28     @Override
29     protected String prefixForNamespace(@Nonnull final URI namespace) {
30         return element.lookupPrefix(namespace.toString());
31     }
32
33     @Override
34     protected QName createQName(@Nonnull final String prefix, @Nonnull final String localName) {
35         final String namespace = element.lookupNamespaceURI(!prefix.isEmpty() ? prefix : null);
36         Preconditions.checkArgument(namespace != null, "Failed to lookup prefix %s", prefix);
37
38         final URI ns = URI.create(namespace);
39         final Module module = schema.findModuleByNamespaceAndRevision(ns, null);
40         Preconditions.checkArgument(module != null, "Namespace %s is not owned by a module", ns);
41         return QName.create(module.getQNameModule(), localName);
42     }
43
44 }