1 /*******************************************************************************
2 * Copyright (c) 2013 University of Stuttgart.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * and the Apache License 2.0 which both accompany this distribution,
6 * and are available at http://www.eclipse.org/legal/epl-v10.html
7 * and http://www.apache.org/licenses/LICENSE-2.0
10 * Oliver Kopp - initial API and implementation
11 *******************************************************************************/
12 package org.eclipse.winery.common.ids.definitions;
14 import javax.xml.namespace.QName;
16 import org.eclipse.winery.common.ids.GenericId;
17 import org.eclipse.winery.common.ids.Namespace;
18 import org.eclipse.winery.common.ids.XMLId;
21 * Identifies a TOSCA component. Each component is required to be identified
22 * subclasses this class
24 * A TOSCAcomponentId has a namespace and an id within that namespace. In XML,
25 * the ID might be serialized as NCName (in the case of EntityTypes and
26 * EntityTemplates) and as xs:id (in the case of EntityTypeImplementations)
28 * Components are elements, which may appear directly nested in TDefinitions:
30 * <li>ServiceTemplates,</li>
31 * <li>EntityTypes,</li
32 * <li>EntityTypeImplementations,</li>
33 * <li>EntityTemplates</li>
36 public abstract class TOSCAComponentId extends GenericId {
38 private final Namespace namespace;
41 public TOSCAComponentId(Namespace namespace, XMLId xmlId) {
43 this.namespace = namespace;
47 * Creates a new id based on strings. This constructor is required for
48 * {@link AbstractComponentsResource}
50 * @param ns the namespace to be used
51 * @param id the id to be used
52 * @param URLencoded true: both Strings are URLencoded, false: both Strings
55 public TOSCAComponentId(String ns, String id, boolean URLencoded) {
56 this(new Namespace(ns, URLencoded), new XMLId(id, URLencoded));
59 public TOSCAComponentId(QName qname) {
60 this(qname.getNamespaceURI(), qname.getLocalPart(), false);
63 public QName getQName() {
64 QName qname = new QName(this.getNamespace().getDecoded(), this.getXmlId().getDecoded());
68 public Namespace getNamespace() {
69 return this.namespace;
73 public int hashCode() {
74 return this.namespace.hashCode() ^ this.getXmlId().hashCode();
78 public boolean equals(Object obj) {
82 if (!(obj instanceof TOSCAComponentId)) {
85 TOSCAComponentId other = (TOSCAComponentId) obj;
86 return this.getXmlId().equals(other.getXmlId()) && this.namespace.equals(other.namespace);
91 public String toString() {
92 QName qn = this.getQName();
93 return this.getClass().toString() + " / " + qn.toString();
97 public GenericId getParent() {
102 public int compareTo(GenericId o1) {
103 if (o1 instanceof TOSCAComponentId) {
104 TOSCAComponentId o = (TOSCAComponentId) o1;
105 int res = this.getXmlId().compareTo(o.getXmlId());
107 res = this.getNamespace().compareTo(o.getNamespace());
111 // comparing TOSCAcomponentIDs with non-TOSCAcomponentIDs is not
113 throw new IllegalStateException();