Path: blob/master/src/java.naming/share/classes/javax/naming/CompoundName.java
41152 views
/*1* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.naming;2627import java.util.Enumeration;28import java.util.Properties;2930/**31* This class represents a compound name -- a name from32* a hierarchical name space.33* Each component in a compound name is an atomic name.34* <p>35* The components of a compound name are numbered. The indexes of a36* compound name with N components range from 0 up to, but not including, N.37* This range may be written as [0,N).38* The most significant component is at index 0.39* An empty compound name has no components.40*41* <h2>Compound Name Syntax</h2>42* The syntax of a compound name is specified using a set of properties:43*<dl>44* <dt>jndi.syntax.direction45* <dd>Direction for parsing ("right_to_left", "left_to_right", "flat").46* If unspecified, defaults to "flat", which means the namespace is flat47* with no hierarchical structure.48*49* <dt>jndi.syntax.separator50* <dd>Separator between atomic name components.51* Required unless direction is "flat".52*53* <dt>jndi.syntax.ignorecase54* <dd>If present, "true" means ignore the case when comparing name55* components. If its value is not "true", or if the property is not56* present, case is considered when comparing name components.57*58* <dt>jndi.syntax.escape59* <dd>If present, specifies the escape string for overriding separator,60* escapes and quotes.61*62* <dt>jndi.syntax.beginquote63* <dd>If present, specifies the string delimiting start of a quoted string.64*65* <dt>jndi.syntax.endquote66* <dd>String delimiting end of quoted string.67* If present, specifies the string delimiting the end of a quoted string.68* If not present, use syntax.beginquote as end quote.69* <dt>jndi.syntax.beginquote270* <dd>Alternative set of begin/end quotes.71*72* <dt>jndi.syntax.endquote273* <dd>Alternative set of begin/end quotes.74*75* <dt>jndi.syntax.trimblanks76* <dd>If present, "true" means trim any leading and trailing whitespaces77* in a name component for comparison purposes. If its value is not78* "true", or if the property is not present, blanks are significant.79* <dt>jndi.syntax.separator.ava80* <dd>If present, specifies the string that separates81* attribute-value-assertions when specifying multiple attribute/value82* pairs. (e.g. "," in age=65,gender=male).83* <dt>jndi.syntax.separator.typeval84* <dd>If present, specifies the string that separates attribute85* from value (e.g. "=" in "age=65")86*</dl>87* These properties are interpreted according to the following rules:88*<ol>89*<li>90* In a string without quotes or escapes, any instance of the91* separator delimits two atomic names. Each atomic name is referred92* to as a <em>component</em>.93*<li>94* A separator, quote or escape is escaped if preceded immediately95* (on the left) by the escape.96*<li>97* If there are two sets of quotes, a specific begin-quote must be matched98* by its corresponding end-quote.99*<li>100* A non-escaped begin-quote which precedes a component must be101* matched by a non-escaped end-quote at the end of the component.102* A component thus quoted is referred to as a103* <em>quoted component</em>. It is parsed by104* removing the being- and end- quotes, and by treating the intervening105* characters as ordinary characters unless one of the rules involving106* quoted components listed below applies.107*<li>108* Quotes embedded in non-quoted components are treated as ordinary strings109* and need not be matched.110*<li>111* A separator that is escaped or appears between non-escaped112* quotes is treated as an ordinary string and not a separator.113*<li>114* An escape string within a quoted component acts as an escape only when115* followed by the corresponding end-quote string.116* This can be used to embed an escaped quote within a quoted component.117*<li>118* An escaped escape string is not treated as an escape string.119*<li>120* An escape string that does not precede a meta string (quotes or separator)121* and is not at the end of a component is treated as an ordinary string.122*<li>123* A leading separator (the compound name string begins with124* a separator) denotes a leading empty atomic component (consisting125* of an empty string).126* A trailing separator (the compound name string ends with127* a separator) denotes a trailing empty atomic component.128* Adjacent separators denote an empty atomic component.129*</ol>130* <p>131* The string form of the compound name follows the syntax described above.132* When the components of the compound name are turned into their133* string representation, the reserved syntax rules described above are134* applied (e.g. embedded separators are escaped or quoted)135* so that when the same string is parsed, it will yield the same components136* of the original compound name.137*138*<h2>Multithreaded Access</h2>139* A {@code CompoundName} instance is not synchronized against concurrent140* multithreaded access. Multiple threads trying to access and modify a141* {@code CompoundName} should lock the object.142*143* @author Rosanna Lee144* @author Scott Seligman145* @since 1.3146*/147148public class CompoundName implements Name {149150/**151* Implementation of this compound name. This field is initialized by the152* constructors and cannot be null.153*/154private transient NameImpl impl;155/**156* Syntax properties for this compound name.157* This field is initialized by the constructors and cannot be null.158* It should be treated as a read-only variable by subclasses.159* Any necessary changes to mySyntax should be made within constructors160* and not after the compound name has been instantiated.161*/162protected transient Properties mySyntax;163164/**165* Constructs a new compound name instance using the components166* specified in comps and syntax. This protected method is intended167* to be used by subclasses of CompoundName when they override168* methods such as clone(), getPrefix(), getSuffix().169*170* @param comps A non-null enumeration of the components to add.171* Each element of the enumeration is of class String.172* The enumeration will be consumed to extract its173* elements.174* @param syntax A non-null properties that specify the syntax of175* this compound name. See class description for176* contents of properties.177*/178protected CompoundName(Enumeration<String> comps, Properties syntax) {179if (syntax == null) {180throw new NullPointerException();181}182mySyntax = syntax;183impl = new NameImpl(syntax, comps);184}185186/**187* Constructs a new compound name instance by parsing the string n188* using the syntax specified by the syntax properties supplied.189*190* @param n The non-null string to parse.191* @param syntax A non-null list of properties that specify the syntax of192* this compound name. See class description for193* contents of properties.194* @throws InvalidNameException If 'n' violates the syntax specified195* by {@code syntax}.196*/197public CompoundName(String n, Properties syntax) throws InvalidNameException {198if (syntax == null) {199throw new NullPointerException();200}201mySyntax = syntax;202impl = new NameImpl(syntax, n);203}204205/**206* Generates the string representation of this compound name, using207* the syntax rules of the compound name. The syntax rules208* are described in the class description.209* An empty component is represented by an empty string.210*211* The string representation thus generated can be passed to212* the CompoundName constructor with the same syntax properties213* to create a new equivalent compound name.214*215* @return A non-null string representation of this compound name.216*/217public String toString() {218return (impl.toString());219}220221/**222* Determines whether obj is syntactically equal to this compound name.223* If obj is null or not a CompoundName, false is returned.224* Two compound names are equal if each component in one is "equal"225* to the corresponding component in the other.226*<p>227* Equality is also defined in terms of the syntax of this compound name.228* The default implementation of CompoundName uses the syntax properties229* jndi.syntax.ignorecase and jndi.syntax.trimblanks when comparing230* two components for equality. If case is ignored, two strings231* with the same sequence of characters but with different cases232* are considered equal. If blanks are being trimmed, leading and trailing233* blanks are ignored for the purpose of the comparison.234*<p>235* Both compound names must have the same number of components.236*<p>237* Implementation note: Currently the syntax properties of the two compound238* names are not compared for equality. They might be in the future.239*240* @param obj The possibly null object to compare against.241* @return true if obj is equal to this compound name, false otherwise.242* @see #compareTo(java.lang.Object obj)243*/244public boolean equals(Object obj) {245// %%% check syntax too?246return (obj != null &&247obj instanceof CompoundName &&248impl.equals(((CompoundName)obj).impl));249}250251/**252* Computes the hash code of this compound name.253* The hash code is the sum of the hash codes of the "canonicalized"254* forms of individual components of this compound name.255* Each component is "canonicalized" according to the256* compound name's syntax before its hash code is computed.257* For a case-insensitive name, for example, the uppercased form of258* a name has the same hash code as its lowercased equivalent.259*260* @return An int representing the hash code of this name.261*/262public int hashCode() {263return impl.hashCode();264}265266/**267* Creates a copy of this compound name.268* Changes to the components of this compound name won't269* affect the new copy and vice versa.270* The clone and this compound name share the same syntax.271*272* @return A non-null copy of this compound name.273*/274public Object clone() {275return (new CompoundName(getAll(), mySyntax));276}277278/**279* Compares this CompoundName with the specified Object for order.280* Returns a281* negative integer, zero, or a positive integer as this Name is less282* than, equal to, or greater than the given Object.283* <p>284* If obj is null or not an instance of CompoundName, ClassCastException285* is thrown.286* <p>287* See equals() for what it means for two compound names to be equal.288* If two compound names are equal, 0 is returned.289*<p>290* Ordering of compound names depend on the syntax of the compound name.291* By default, they follow lexicographical rules for string comparison292* with the extension that this applies to all the components in the293* compound name and that comparison of individual components is294* affected by the jndi.syntax.ignorecase and jndi.syntax.trimblanks295* properties, identical to how they affect equals().296* If this compound name is "lexicographically" lesser than obj,297* a negative number is returned.298* If this compound name is "lexicographically" greater than obj,299* a positive number is returned.300*<p>301* Implementation note: Currently the syntax properties of the two compound302* names are not compared when checking order. They might be in the future.303* @param obj The non-null object to compare against.304* @return a negative integer, zero, or a positive integer as this Name305* is less than, equal to, or greater than the given Object.306* @throws ClassCastException if obj is not a CompoundName.307* @see #equals(java.lang.Object)308*/309public int compareTo(Object obj) {310if (!(obj instanceof CompoundName)) {311throw new ClassCastException("Not a CompoundName");312}313return impl.compareTo(((CompoundName)obj).impl);314}315316/**317* Retrieves the number of components in this compound name.318*319* @return The nonnegative number of components in this compound name.320*/321public int size() {322return (impl.size());323}324325/**326* Determines whether this compound name is empty.327* A compound name is empty if it has zero components.328*329* @return true if this compound name is empty, false otherwise.330*/331public boolean isEmpty() {332return (impl.isEmpty());333}334335/**336* Retrieves the components of this compound name as an enumeration337* of strings.338* The effects of updates to this compound name on this enumeration339* is undefined.340*341* @return A non-null enumeration of the components of this342* compound name. Each element of the enumeration is of class String.343*/344public Enumeration<String> getAll() {345return (impl.getAll());346}347348/**349* Retrieves a component of this compound name.350*351* @param posn The 0-based index of the component to retrieve.352* Must be in the range [0,size()).353* @return The component at index posn.354* @throws ArrayIndexOutOfBoundsException if posn is outside the355* specified range.356*/357public String get(int posn) {358return (impl.get(posn));359}360361/**362* Creates a compound name whose components consist of a prefix of the363* components in this compound name.364* The result and this compound name share the same syntax.365* Subsequent changes to366* this compound name do not affect the name that is returned and367* vice versa.368*369* @param posn The 0-based index of the component at which to stop.370* Must be in the range [0,size()].371* @return A compound name consisting of the components at indexes in372* the range [0,posn).373* @throws ArrayIndexOutOfBoundsException374* If posn is outside the specified range.375*/376public Name getPrefix(int posn) {377Enumeration<String> comps = impl.getPrefix(posn);378return (new CompoundName(comps, mySyntax));379}380381/**382* Creates a compound name whose components consist of a suffix of the383* components in this compound name.384* The result and this compound name share the same syntax.385* Subsequent changes to386* this compound name do not affect the name that is returned.387*388* @param posn The 0-based index of the component at which to start.389* Must be in the range [0,size()].390* @return A compound name consisting of the components at indexes in391* the range [posn,size()). If posn is equal to392* size(), an empty compound name is returned.393* @throws ArrayIndexOutOfBoundsException394* If posn is outside the specified range.395*/396public Name getSuffix(int posn) {397Enumeration<String> comps = impl.getSuffix(posn);398return (new CompoundName(comps, mySyntax));399}400401/**402* Determines whether a compound name is a prefix of this compound name.403* A compound name 'n' is a prefix if it is equal to404* getPrefix(n.size())--in other words, this compound name405* starts with 'n'.406* If n is null or not a compound name, false is returned.407*<p>408* Implementation note: Currently the syntax properties of n409* are not used when doing the comparison. They might be in the future.410* @param n The possibly null compound name to check.411* @return true if n is a CompoundName and412* is a prefix of this compound name, false otherwise.413*/414public boolean startsWith(Name n) {415if (n instanceof CompoundName) {416return (impl.startsWith(n.size(), n.getAll()));417} else {418return false;419}420}421422/**423* Determines whether a compound name is a suffix of this compound name.424* A compound name 'n' is a suffix if it is equal to425* getSuffix(size()-n.size())--in other words, this426* compound name ends with 'n'.427* If n is null or not a compound name, false is returned.428*<p>429* Implementation note: Currently the syntax properties of n430* are not used when doing the comparison. They might be in the future.431* @param n The possibly null compound name to check.432* @return true if n is a CompoundName and433* is a suffix of this compound name, false otherwise.434*/435public boolean endsWith(Name n) {436if (n instanceof CompoundName) {437return (impl.endsWith(n.size(), n.getAll()));438} else {439return false;440}441}442443/**444* Adds the components of a compound name -- in order -- to the end of445* this compound name.446*<p>447* Implementation note: Currently the syntax properties of suffix448* is not used or checked. They might be in the future.449* @param suffix The non-null components to add.450* @return The updated CompoundName, not a new one. Cannot be null.451* @throws InvalidNameException If suffix is not a compound name,452* or if the addition of the components violates the syntax453* of this compound name (e.g. exceeding number of components).454*/455public Name addAll(Name suffix) throws InvalidNameException {456if (suffix instanceof CompoundName) {457impl.addAll(suffix.getAll());458return this;459} else {460throw new InvalidNameException("Not a compound name: " +461suffix.toString());462}463}464465/**466* Adds the components of a compound name -- in order -- at a specified467* position within this compound name.468* Components of this compound name at or after the index of the first469* new component are shifted up (away from index 0)470* to accommodate the new components.471*<p>472* Implementation note: Currently the syntax properties of suffix473* is not used or checked. They might be in the future.474*475* @param n The non-null components to add.476* @param posn The index in this name at which to add the new477* components. Must be in the range [0,size()].478* @return The updated CompoundName, not a new one. Cannot be null.479* @throws ArrayIndexOutOfBoundsException480* If posn is outside the specified range.481* @throws InvalidNameException If n is not a compound name,482* or if the addition of the components violates the syntax483* of this compound name (e.g. exceeding number of components).484*/485public Name addAll(int posn, Name n) throws InvalidNameException {486if (n instanceof CompoundName) {487impl.addAll(posn, n.getAll());488return this;489} else {490throw new InvalidNameException("Not a compound name: " +491n.toString());492}493}494495/**496* Adds a single component to the end of this compound name.497*498* @param comp The non-null component to add.499* @return The updated CompoundName, not a new one. Cannot be null.500* @throws InvalidNameException If adding comp at end of the name501* would violate the compound name's syntax.502*/503public Name add(String comp) throws InvalidNameException{504impl.add(comp);505return this;506}507508/**509* Adds a single component at a specified position within this510* compound name.511* Components of this compound name at or after the index of the new512* component are shifted up by one (away from index 0)513* to accommodate the new component.514*515* @param comp The non-null component to add.516* @param posn The index at which to add the new component.517* Must be in the range [0,size()].518* @throws ArrayIndexOutOfBoundsException519* If posn is outside the specified range.520* @return The updated CompoundName, not a new one. Cannot be null.521* @throws InvalidNameException If adding comp at the specified position522* would violate the compound name's syntax.523*/524public Name add(int posn, String comp) throws InvalidNameException{525impl.add(posn, comp);526return this;527}528529/**530* Deletes a component from this compound name.531* The component of this compound name at position 'posn' is removed,532* and components at indices greater than 'posn'533* are shifted down (towards index 0) by one.534*535* @param posn The index of the component to delete.536* Must be in the range [0,size()).537* @return The component removed (a String).538* @throws ArrayIndexOutOfBoundsException539* If posn is outside the specified range (includes case where540* compound name is empty).541* @throws InvalidNameException If deleting the component542* would violate the compound name's syntax.543*/544public Object remove(int posn) throws InvalidNameException {545return impl.remove(posn);546}547548/**549* The writeObject method is called to save the state of the550* {@code CompoundName} to a stream.551*552* @serialData The syntax {@code Properties}, followed by553* the number of components (an {@code int}), and the individual554* components (each a {@code String}).555*556* @param s the {@code ObjectOutputStream} to write to557* @throws java.io.IOException if an I/O error occurs558*/559@java.io.Serial560private void writeObject(java.io.ObjectOutputStream s)561throws java.io.IOException {562// Overridden to avoid implementation dependency563s.writeObject(mySyntax);564s.writeInt(size());565Enumeration<String> comps = getAll();566while (comps.hasMoreElements()) {567s.writeObject(comps.nextElement());568}569}570571/**572* The readObject method is called to restore the state of573* the {@code CompoundName} from a stream.574*575* See {@code writeObject} for a description of the serial form.576*577* @param s the {@code ObjectInputStream} to read from578* @throws java.io.IOException if an I/O error occurs579* @throws ClassNotFoundException if the class of a serialized object580* could not be found581*/582@java.io.Serial583private void readObject(java.io.ObjectInputStream s)584throws java.io.IOException, ClassNotFoundException {585// Overridden to avoid implementation dependency.586mySyntax = (Properties)s.readObject();587impl = new NameImpl(mySyntax);588int n = s.readInt(); // number of components589try {590while (--n >= 0) {591add((String)s.readObject());592}593} catch (InvalidNameException e) {594throw (new java.io.StreamCorruptedException("Invalid name"));595}596}597598/**599* Use serialVersionUID from JNDI 1.1.1 for interoperability600*/601@java.io.Serial602private static final long serialVersionUID = 3513100557083972036L;603604/*605// For testing606607public static void main(String[] args) {608Properties dotSyntax = new Properties();609dotSyntax.put("jndi.syntax.direction", "right_to_left");610dotSyntax.put("jndi.syntax.separator", ".");611dotSyntax.put("jndi.syntax.ignorecase", "true");612dotSyntax.put("jndi.syntax.escape", "\\");613// dotSyntax.put("jndi.syntax.beginquote", "\"");614// dotSyntax.put("jndi.syntax.beginquote2", "'");615616Name first = null;617try {618for (int i = 0; i < args.length; i++) {619Name name;620Enumeration e;621System.out.println("Given name: " + args[i]);622name = new CompoundName(args[i], dotSyntax);623if (first == null) {624first = name;625}626e = name.getComponents();627while (e.hasMoreElements()) {628System.out.println("Element: " + e.nextElement());629}630System.out.println("Constructed name: " + name.toString());631632System.out.println("Compare " + first.toString() + " with "633+ name.toString() + " = " + first.compareTo(name));634}635} catch (Exception ne) {636ne.printStackTrace();637}638}639*/640}641642643