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