1 /*
2 * Licensed to The Apereo Foundation under one or more contributor license
3 * agreements. See the NOTICE file distributed with this work for additional
4 * information regarding copyright ownership.
5 *
6 *
7 * The Apereo Foundation licenses this file to you under the Educational
8 * Community License, Version 2.0 (the "License"); you may not use this file
9 * except in compliance with the License. You may obtain a copy of the License
10 * at:
11 *
12 * http://opensource.org/licenses/ecl2.txt
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 * License for the specific language governing permissions and limitations under
18 * the License.
19 *
20 */
21
22 package org.opencastproject.mediapackage;
23
24 import static java.lang.String.format;
25 import static org.opencastproject.util.EqualsUtil.eq;
26 import static org.opencastproject.util.EqualsUtil.hash;
27
28 import org.opencastproject.util.RequireUtil;
29
30 import org.apache.commons.lang3.StringUtils;
31
32 import java.io.Serializable;
33 import java.util.regex.Matcher;
34 import java.util.regex.Pattern;
35
36 import javax.xml.XMLConstants;
37
38 /**
39 * An XML <dfn>Expanded Name</dfn>, cf. <a href="http://www.w3.org/TR/xml-names11/#dt-expname">W3C definition</a>.
40 * <p>
41 * Expanded names in XML consists of a namespace name (URI) and a local part. In opposite to <dfn>Qualified Names</dfn>,
42 * cf. <a href="http://www.w3.org/TR/xml-names11/#dt-qualname">W3C definition</a> - which are made from an optional
43 * prefix and the local part - expanded names are <em>not</em> subject to namespace interpretation.
44 * <p>
45 * Please see <a href="http://www.w3.org/TR/xml-names/">http://www.w3.org/TR/xml-names/</a> for a complete definition
46 * and reference.
47 */
48 public final class EName implements Serializable, Comparable<EName> {
49 private static final long serialVersionUID = -5494762745288614634L;
50
51 private final String namespaceURI;
52 private final String localName;
53
54 /**
55 * A pattern to parse strings as ENames. A String representing an EName may start with a Namespace among curly braces
56 * ("{" and "}") and then it must contain a local name *without* any curly braces.
57 */
58 private static final Pattern pattern = Pattern.compile("^(?:\\{(?<namespace>[^{}\\s]*)\\})?(?<localname>[^{}\\s]+)$");
59
60 /**
61 * Create a new expanded name.
62 *
63 * @param namespaceURI
64 * the name of the namespace this EName belongs to. If set to {@link javax.xml.XMLConstants#NULL_NS_URI},
65 * this name does not belong to any namespace. Use this option with care.
66 * @param localName
67 * the local part of the name. Must not be empty.
68 */
69 public EName(String namespaceURI, String localName) {
70 RequireUtil.notNull(namespaceURI, "namespaceURI");
71 RequireUtil.notEmpty(localName, "localName");
72
73 this.namespaceURI = namespaceURI;
74 this.localName = localName;
75 }
76
77 public static EName mk(String namespaceURI, String localName) {
78 return new EName(namespaceURI, localName);
79 }
80
81 /**
82 * Create a new expanded name which does not belong to a namespace. The namespace name is set to
83 * {@link javax.xml.XMLConstants#NULL_NS_URI}.
84 */
85 public static EName mk(String localName) {
86 return new EName(XMLConstants.NULL_NS_URI, localName);
87 }
88
89 /**
90 * Return the namespace name. Usually the name will be a URI.
91 *
92 * @return the namespace name or {@link javax.xml.XMLConstants#NULL_NS_URI} if the name does not belong to a namespace
93 */
94 public String getNamespaceURI() {
95 return namespaceURI;
96 }
97
98 /** Return the local part of the name. */
99 public String getLocalName() {
100 return localName;
101 }
102
103 /**
104 * Check, if this name belongs to a namespace, i.e. its namespace URI is not
105 * {@link javax.xml.XMLConstants#NULL_NS_URI}.
106 */
107 public boolean hasNamespace() {
108 return !XMLConstants.NULL_NS_URI.equals(namespaceURI);
109 }
110
111 @Override
112 public int hashCode() {
113 return hash(namespaceURI, localName);
114 }
115
116 @Override
117 public boolean equals(Object that) {
118 return (this == that) || (that instanceof EName && eqFields((EName) that));
119 }
120
121 private boolean eqFields(EName that) {
122 return eq(localName, that.localName) && eq(namespaceURI, that.namespaceURI);
123 }
124
125 /** Return a W3C compliant string representation <code>{namespaceURI}localname</code>. */
126 @Override
127 public String toString() {
128 return format("{%s}%s", namespaceURI, localName);
129 }
130
131 @Override
132 public int compareTo(EName o) {
133 final int r = getNamespaceURI().compareTo(o.getNamespaceURI());
134 if (r == 0) {
135 return getLocalName().compareTo(o.getLocalName());
136 } else {
137 return r;
138 }
139 }
140
141 /**
142 * Parse a W3C compliant string representation <code>{namespaceURI}localname</code>.
143 *
144 * A String representing an EName may start with a namespace among curly braces ("{" and "}") and then it must contain
145 * a local name *without* any blank characters or curly braces.
146 *
147 * This is a superset of the character restrictions defined by the XML standard, where neither namespaces nor local
148 * names may contain curly braces or spaces.
149 *
150 * Examples:
151 *
152 * <ul>
153 * <li>{http://my-namespace}mylocalname
154 * <li>{}localname-with-explicit-empty-namespace
155 * <li>localname-without-namespace
156 * </ul>
157 *
158 * Incorrect examples:
159 *
160 * <ul>
161 * <li>{namespace-only}
162 * <li>contains{curly}braces
163 * </ul>
164 *
165 * @param strEName
166 * A {@link java.lang.String} representing an {@code EName}
167 * @param defaultNameSpace
168 * A NameSpace to apply if the provided {@code String} does not have any. Please note that a explicit empty
169 * NameSpace **is** a NameSpace. If this argument is blank or {@code null}, it has no effect.
170 */
171 public static EName fromString(String strEName, String defaultNameSpace) throws IllegalArgumentException {
172 if (strEName == null) {
173 throw new IllegalArgumentException("Cannot parse 'null' as EName");
174 }
175 Matcher m = pattern.matcher(strEName);
176
177 if (m.matches()) {
178 if (StringUtils.isNotBlank(defaultNameSpace) && m.group("namespace") == null)
179 return new EName(defaultNameSpace, m.group("localname"));
180 else
181 return new EName(StringUtils.trimToEmpty(m.group("namespace")), m.group("localname"));
182 }
183 throw new IllegalArgumentException(format("Cannot parse '%s' as EName", strEName));
184 }
185
186 /**
187 * Parse a W3C compliant string representation <code>{namespaceURI}localname</code>.
188 *
189 * A String representing an EName may start with a namespace among curly braces ("{" and "}") and then it must contain
190 * a local name *without* any curly braces.
191 *
192 * This is a superset of the character restrictions defined by the XML standard, where neither namespaces nor local
193 * names may contain curly braces.
194 *
195 * Examples:
196 *
197 * <ul>
198 * <li>{http://my-namespace}mylocalname
199 * <li>localname-without-namespace
200 * </ul>
201 *
202 * Incorrect examples:
203 *
204 * <ul>
205 * <li>{namespace-only}
206 * <li>contains{curly}braces
207 * </ul>
208 *
209 * @param strEName
210 * A {@link java.lang.String} representing an {@code EName}
211 *
212 */
213 public static EName fromString(String strEName) throws IllegalArgumentException {
214 return EName.fromString(strEName, null);
215 }
216 }