Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/naming/CompoundName.java
38829 views
/*1* Copyright (c) 1999, 2013, 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* <h1>Compound Name Syntax</h1>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 separators 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*<h1>Multithreaded Access</h1>139* A <tt>CompoundName</tt> instance is not synchronized against concurrent140* multithreaded access. Multiple threads trying to access and modify a141* <tt>CompoundName</tt> 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.152* This field is initialized by the constructors and cannot be null.153* It should be treated as a read-only variable by subclasses.154*/155protected transient NameImpl impl;156/**157* Syntax properties for this compound name.158* This field is initialized by the constructors and cannot be null.159* It should be treated as a read-only variable by subclasses.160* Any necessary changes to mySyntax should be made within constructors161* and not after the compound name has been instantiated.162*/163protected transient Properties mySyntax;164165/**166* Constructs a new compound name instance using the components167* specified in comps and syntax. This protected method is intended to be168* to be used by subclasses of CompoundName when they override169* methods such as clone(), getPrefix(), getSuffix().170*171* @param comps A non-null enumeration of the components to add.172* Each element of the enumeration is of class String.173* The enumeration will be consumed to extract its174* elements.175* @param syntax A non-null properties that specify the syntax of176* this compound name. See class description for177* contents of properties.178*/179protected CompoundName(Enumeration<String> comps, Properties syntax) {180if (syntax == null) {181throw new NullPointerException();182}183mySyntax = syntax;184impl = new NameImpl(syntax, comps);185}186187/**188* Constructs a new compound name instance by parsing the string n189* using the syntax specified by the syntax properties supplied.190*191* @param n The non-null string to parse.192* @param syntax A non-null list of properties that specify the syntax of193* this compound name. See class description for194* contents of properties.195* @exception InvalidNameException If 'n' violates the syntax specified196* by <code>syntax</code>.197*/198public CompoundName(String n, Properties syntax) throws InvalidNameException {199if (syntax == null) {200throw new NullPointerException();201}202mySyntax = syntax;203impl = new NameImpl(syntax, n);204}205206/**207* Generates the string representation of this compound name, using208* the syntax rules of the compound name. The syntax rules209* are described in the class description.210* An empty component is represented by an empty string.211*212* The string representation thus generated can be passed to213* the CompoundName constructor with the same syntax properties214* to create a new equivalent compound name.215*216* @return A non-null string representation of this compound name.217*/218public String toString() {219return (impl.toString());220}221222/**223* Determines whether obj is syntactically equal to this compound name.224* If obj is null or not a CompoundName, false is returned.225* Two compound names are equal if each component in one is "equal"226* to the corresponding component in the other.227*<p>228* Equality is also defined in terms of the syntax of this compound name.229* The default implementation of CompoundName uses the syntax properties230* jndi.syntax.ignorecase and jndi.syntax.trimblanks when comparing231* two components for equality. If case is ignored, two strings232* with the same sequence of characters but with different cases233* are considered equal. If blanks are being trimmed, leading and trailing234* blanks are ignored for the purpose of the comparison.235*<p>236* Both compound names must have the same number of components.237*<p>238* Implementation note: Currently the syntax properties of the two compound239* names are not compared for equality. They might be in the future.240*241* @param obj The possibly null object to compare against.242* @return true if obj is equal to this compound name, false otherwise.243* @see #compareTo(java.lang.Object obj)244*/245public boolean equals(Object obj) {246// %%% check syntax too?247return (obj != null &&248obj instanceof CompoundName &&249impl.equals(((CompoundName)obj).impl));250}251252/**253* Computes the hash code of this compound name.254* The hash code is the sum of the hash codes of the "canonicalized"255* forms of individual components of this compound name.256* Each component is "canonicalized" according to the257* compound name's syntax before its hash code is computed.258* For a case-insensitive name, for example, the uppercased form of259* a name has the same hash code as its lowercased equivalent.260*261* @return An int representing the hash code of this name.262*/263public int hashCode() {264return impl.hashCode();265}266267/**268* Creates a copy of this compound name.269* Changes to the components of this compound name won't270* affect the new copy and vice versa.271* The clone and this compound name share the same syntax.272*273* @return A non-null copy of this compound name.274*/275public Object clone() {276return (new CompoundName(getAll(), mySyntax));277}278279/**280* Compares this CompoundName with the specified Object for order.281* Returns a282* negative integer, zero, or a positive integer as this Name is less283* than, equal to, or greater than the given Object.284* <p>285* If obj is null or not an instance of CompoundName, ClassCastException286* is thrown.287* <p>288* See equals() for what it means for two compound names to be equal.289* If two compound names are equal, 0 is returned.290*<p>291* Ordering of compound names depend on the syntax of the compound name.292* By default, they follow lexicographical rules for string comparison293* with the extension that this applies to all the components in the294* compound name and that comparison of individual components is295* affected by the jndi.syntax.ignorecase and jndi.syntax.trimblanks296* properties, identical to how they affect equals().297* If this compound name is "lexicographically" lesser than obj,298* a negative number is returned.299* If this compound name is "lexicographically" greater than obj,300* a positive number is returned.301*<p>302* Implementation note: Currently the syntax properties of the two compound303* names are not compared when checking order. They might be in the future.304* @param obj The non-null object to compare against.305* @return a negative integer, zero, or a positive integer as this Name306* is less than, equal to, or greater than the given Object.307* @exception ClassCastException if obj is not a CompoundName.308* @see #equals(java.lang.Object)309*/310public int compareTo(Object obj) {311if (!(obj instanceof CompoundName)) {312throw new ClassCastException("Not a CompoundName");313}314return impl.compareTo(((CompoundName)obj).impl);315}316317/**318* Retrieves the number of components in this compound name.319*320* @return The nonnegative number of components in this compound name.321*/322public int size() {323return (impl.size());324}325326/**327* Determines whether this compound name is empty.328* A compound name is empty if it has zero components.329*330* @return true if this compound name is empty, false otherwise.331*/332public boolean isEmpty() {333return (impl.isEmpty());334}335336/**337* Retrieves the components of this compound name as an enumeration338* of strings.339* The effects of updates to this compound name on this enumeration340* is undefined.341*342* @return A non-null enumeration of the components of this343* compound name. Each element of the enumeration is of class String.344*/345public Enumeration<String> getAll() {346return (impl.getAll());347}348349/**350* Retrieves a component of this compound name.351*352* @param posn The 0-based index of the component to retrieve.353* Must be in the range [0,size()).354* @return The component at index posn.355* @exception ArrayIndexOutOfBoundsException if posn is outside the356* specified range.357*/358public String get(int posn) {359return (impl.get(posn));360}361362/**363* Creates a compound name whose components consist of a prefix of the364* components in this compound name.365* The result and this compound name share the same syntax.366* Subsequent changes to367* this compound name does not affect the name that is returned and368* vice versa.369*370* @param posn The 0-based index of the component at which to stop.371* Must be in the range [0,size()].372* @return A compound name consisting of the components at indexes in373* the range [0,posn).374* @exception ArrayIndexOutOfBoundsException375* If posn is outside the specified range.376*/377public Name getPrefix(int posn) {378Enumeration<String> comps = impl.getPrefix(posn);379return (new CompoundName(comps, mySyntax));380}381382/**383* Creates a compound name whose components consist of a suffix of the384* components in this compound name.385* The result and this compound name share the same syntax.386* Subsequent changes to387* this compound name does not affect the name that is returned.388*389* @param posn The 0-based index of the component at which to start.390* Must be in the range [0,size()].391* @return A compound name consisting of the components at indexes in392* the range [posn,size()). If posn is equal to393* size(), an empty compound name is returned.394* @exception ArrayIndexOutOfBoundsException395* If posn is outside the specified range.396*/397public Name getSuffix(int posn) {398Enumeration<String> comps = impl.getSuffix(posn);399return (new CompoundName(comps, mySyntax));400}401402/**403* Determines whether a compound name is a prefix of this compound name.404* A compound name 'n' is a prefix if it is equal to405* getPrefix(n.size())--in other words, this compound name406* starts with 'n'.407* If n is null or not a compound name, false is returned.408*<p>409* Implementation note: Currently the syntax properties of n410* are not used when doing the comparison. They might be in the future.411* @param n The possibly null compound name to check.412* @return true if n is a CompoundName and413* is a prefix of this compound name, false otherwise.414*/415public boolean startsWith(Name n) {416if (n instanceof CompoundName) {417return (impl.startsWith(n.size(), n.getAll()));418} else {419return false;420}421}422423/**424* Determines whether a compound name is a suffix of this compound name.425* A compound name 'n' is a suffix if it it is equal to426* getSuffix(size()-n.size())--in other words, this427* compound name ends with 'n'.428* If n is null or not a compound name, false is returned.429*<p>430* Implementation note: Currently the syntax properties of n431* are not used when doing the comparison. They might be in the future.432* @param n The possibly null compound name to check.433* @return true if n is a CompoundName and434* is a suffix of this compound name, false otherwise.435*/436public boolean endsWith(Name n) {437if (n instanceof CompoundName) {438return (impl.endsWith(n.size(), n.getAll()));439} else {440return false;441}442}443444/**445* Adds the components of a compound name -- in order -- to the end of446* this compound name.447*<p>448* Implementation note: Currently the syntax properties of suffix449* is not used or checked. They might be in the future.450* @param suffix The non-null components to add.451* @return The updated CompoundName, not a new one. Cannot be null.452* @exception InvalidNameException If suffix is not a compound name,453* or if the addition of the components violates the syntax454* of this compound name (e.g. exceeding number of components).455*/456public Name addAll(Name suffix) throws InvalidNameException {457if (suffix instanceof CompoundName) {458impl.addAll(suffix.getAll());459return this;460} else {461throw new InvalidNameException("Not a compound name: " +462suffix.toString());463}464}465466/**467* Adds the components of a compound name -- in order -- at a specified468* position within this compound name.469* Components of this compound name at or after the index of the first470* new component are shifted up (away from index 0)471* to accommodate the new components.472*<p>473* Implementation note: Currently the syntax properties of suffix474* is not used or checked. They might be in the future.475*476* @param n The non-null components to add.477* @param posn The index in this name at which to add the new478* components. Must be in the range [0,size()].479* @return The updated CompoundName, not a new one. Cannot be null.480* @exception ArrayIndexOutOfBoundsException481* If posn is outside the specified range.482* @exception InvalidNameException If n is not a compound name,483* or if the addition of the components violates the syntax484* of this compound name (e.g. exceeding number of components).485*/486public Name addAll(int posn, Name n) throws InvalidNameException {487if (n instanceof CompoundName) {488impl.addAll(posn, n.getAll());489return this;490} else {491throw new InvalidNameException("Not a compound name: " +492n.toString());493}494}495496/**497* Adds a single component to the end of this compound name.498*499* @param comp The non-null component to add.500* @return The updated CompoundName, not a new one. Cannot be null.501* @exception InvalidNameException If adding comp at end of the name502* would violate the compound name's syntax.503*/504public Name add(String comp) throws InvalidNameException{505impl.add(comp);506return this;507}508509/**510* Adds a single component at a specified position within this511* compound name.512* Components of this compound name at or after the index of the new513* component are shifted up by one (away from index 0)514* to accommodate the new component.515*516* @param comp The non-null component to add.517* @param posn The index at which to add the new component.518* Must be in the range [0,size()].519* @exception ArrayIndexOutOfBoundsException520* If posn is outside the specified range.521* @return The updated CompoundName, not a new one. Cannot be null.522* @exception InvalidNameException If adding comp at the specified position523* would violate the compound name's syntax.524*/525public Name add(int posn, String comp) throws InvalidNameException{526impl.add(posn, comp);527return this;528}529530/**531* Deletes a component from this compound name.532* The component of this compound name at position 'posn' is removed,533* and components at indices greater than 'posn'534* are shifted down (towards index 0) by one.535*536* @param posn The index of the component to delete.537* Must be in the range [0,size()).538* @return The component removed (a String).539* @exception ArrayIndexOutOfBoundsException540* If posn is outside the specified range (includes case where541* compound name is empty).542* @exception InvalidNameException If deleting the component543* would violate the compound name's syntax.544*/545public Object remove(int posn) throws InvalidNameException {546return impl.remove(posn);547}548549/**550* Overridden to avoid implementation dependency.551* @serialData The syntax <tt>Properties</tt>, followed by552* the number of components (an <tt>int</tt>), and the individual553* components (each a <tt>String</tt>).554*/555private void writeObject(java.io.ObjectOutputStream s)556throws java.io.IOException {557s.writeObject(mySyntax);558s.writeInt(size());559Enumeration<String> comps = getAll();560while (comps.hasMoreElements()) {561s.writeObject(comps.nextElement());562}563}564565/**566* Overridden to avoid implementation dependency.567*/568private void readObject(java.io.ObjectInputStream s)569throws java.io.IOException, ClassNotFoundException {570mySyntax = (Properties)s.readObject();571impl = new NameImpl(mySyntax);572int n = s.readInt(); // number of components573try {574while (--n >= 0) {575add((String)s.readObject());576}577} catch (InvalidNameException e) {578throw (new java.io.StreamCorruptedException("Invalid name"));579}580}581582/**583* Use serialVersionUID from JNDI 1.1.1 for interoperability584*/585private static final long serialVersionUID = 3513100557083972036L;586587/*588// For testing589590public static void main(String[] args) {591Properties dotSyntax = new Properties();592dotSyntax.put("jndi.syntax.direction", "right_to_left");593dotSyntax.put("jndi.syntax.separator", ".");594dotSyntax.put("jndi.syntax.ignorecase", "true");595dotSyntax.put("jndi.syntax.escape", "\\");596// dotSyntax.put("jndi.syntax.beginquote", "\"");597// dotSyntax.put("jndi.syntax.beginquote2", "'");598599Name first = null;600try {601for (int i = 0; i < args.length; i++) {602Name name;603Enumeration e;604System.out.println("Given name: " + args[i]);605name = new CompoundName(args[i], dotSyntax);606if (first == null) {607first = name;608}609e = name.getComponents();610while (e.hasMoreElements()) {611System.out.println("Element: " + e.nextElement());612}613System.out.println("Constructed name: " + name.toString());614615System.out.println("Compare " + first.toString() + " with "616+ name.toString() + " = " + first.compareTo(name));617}618} catch (Exception ne) {619ne.printStackTrace();620}621}622*/623}624625626