KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > webdav > util > UriHandler


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/util/UriHandler.java,v 1.33 2004/08/05 14:43:30 dflorey Exp $
3  * $Revision: 1.33 $
4  * $Date: 2004/08/05 14:43:30 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.webdav.util;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Set JavaDoc;
31 import java.util.StringTokenizer JavaDoc;
32
33 import org.apache.slide.common.Domain;
34 import org.apache.slide.common.NamespaceAccessToken;
35 import org.apache.slide.common.ServiceAccessException;
36 import org.apache.slide.common.SlideToken;
37 import org.apache.slide.content.Content;
38 import org.apache.slide.content.NodeProperty;
39 import org.apache.slide.content.NodeRevisionDescriptor;
40 import org.apache.slide.content.NodeRevisionDescriptors;
41 import org.apache.slide.content.NodeRevisionNumber;
42 import org.apache.slide.content.RevisionAlreadyExistException;
43 import org.apache.slide.content.RevisionDescriptorNotFoundException;
44 import org.apache.slide.content.RevisionNotFoundException;
45 import org.apache.slide.content.NodeProperty.NamespaceCache;
46 import org.apache.slide.event.VetoException;
47 import org.apache.slide.lock.ObjectLockedException;
48 import org.apache.slide.security.AccessDeniedException;
49 import org.apache.slide.structure.LinkedObjectNotFoundException;
50 import org.apache.slide.structure.ObjectAlreadyExistsException;
51 import org.apache.slide.structure.ObjectNotFoundException;
52 import org.apache.slide.structure.Structure;
53 import org.apache.slide.structure.SubjectNode;
54
55
56     /**
57  * Helper class for the handling of URIs
58      */

59 public class UriHandler implements DeltavConstants, AclConstants, DaslConstants {
60
61     /**
62      * Determines whether the hack that restricts the size of collections in
63      * the history collection is configured to be used.
64      */

65     protected static boolean useHistoryCollectionHack = false;
66     
67     /**
68      * Sets whether the hack that restricts the size of collections in
69      * the history collection is configured to be used.
70      */

71     public static void setGloballyUseHistoryCollectionHack(boolean useHistoryCollectionHack) {
72         // XXX it is truely ugly to do things this way, it would be better to have an Uri factory
73
// that gets initialized with this value
74
UriHandler.useHistoryCollectionHack = useHistoryCollectionHack;
75     }
76     
77     /**
78      * Factory method.
79      */

80     public static UriHandler getUriHandler( String JavaDoc resourcePath ) {
81         return new UriHandler( resourcePath );
82         }
83
84     /**
85      * Factory method.
86      */

87     public static UriHandler
88         getUriHandler( NodeRevisionDescriptors nrds, NodeRevisionDescriptor nrd ) {
89
90         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
91         String JavaDoc uri = nrds.getUri();
92         UriHandler uriHandler = UriHandler.getUriHandler( uri );
93         if ( ! uriHandler.isHistoryUri() ) {
94             // any resource
95
b.append( uri );
96         }
97         else {
98             if( (NodeRevisionNumber.HIDDEN_0_0).equals(nrd.getRevisionNumber()) ) {
99                 // history resource
100
b.append( uri );
101             }
102             else {
103                 // version resource
104
b.append( uri );
105                 if( !uri.endsWith("/") ) {
106                     b.append( "/" );
107                 }
108                 b.append( nrd.getRevisionNumber().toString() );
109             }
110         }
111         return new UriHandler( b.toString() );
112     }
113
114     /**
115      * Generates the next available history URI and returns an URI handler for it.
116      * @param sToken the Slide token
117      * @param nsaToken the namespace access token
118      * @param uh the URI handler of the resource being set under version control
119      */

120     public static UriHandler
121     createNextHistoryUri( SlideToken sToken, NamespaceAccessToken nsaToken, UriHandler uh )
122     throws ObjectNotFoundException, AccessDeniedException, ObjectLockedException,
123     LinkedObjectNotFoundException, ServiceAccessException,
124     RevisionDescriptorNotFoundException, RevisionNotFoundException, VetoException {
125         
126         UriHandler result = null;
127         String JavaDoc nsName = nsaToken.getName();
128         UriHandler hpathHandler =
129             HistoryPathHandler.getResolvedHistoryPathHandler( nsName, uh );
130         Content content = nsaToken.getContentHelper();
131         String JavaDoc hpath = hpathHandler.toString();
132         
133         Structure structure = nsaToken.getStructureHelper();
134         String JavaDoc uniqueUri = structure.generateUniqueUri(sToken, hpath);
135
136         if (uniqueUri != null) {
137             if (UriHandler.useHistoryCollectionHack) {
138                 // XXX this is a bit silly: first compose it, now analyze it...
139
String JavaDoc nextHnStr = uniqueUri.substring(uniqueUri.lastIndexOf('/') + 1);
140                 // XXX start with (at least) 10 to
141
// assure no resources are created directly in history folder
142
long nextHnLong = Long.parseLong(nextHnStr) + 10;
143                 nextHnStr = String.valueOf(nextHnLong);
144
145                 // make hpath/1/2/3 out of 1234
146
StringBuffer JavaDoc pathBuffer = new StringBuffer JavaDoc(hpath);
147                 for (int i = 0; i < nextHnStr.length() - 1; i++) {
148                     pathBuffer.append('/');
149                     pathBuffer.append(nextHnStr.charAt(i));
150                     // as we have no information how this number may look like we will have to assure the full
151
// path is created
152
String JavaDoc dir = pathBuffer.toString();
153                     try {
154                         structure.retrieve(sToken, dir);
155                     } catch (ObjectNotFoundException onfe) {
156                         try {
157                             structure.create(sToken, new SubjectNode(), dir);
158                             content.create(sToken, dir, new NodeRevisionDescriptor(0), null);
159                         } catch (ObjectAlreadyExistsException oae) {
160                             Domain.warn("Object " + dir + " already exists.");
161                         } catch (RevisionAlreadyExistException rae) {
162                             Domain.warn("Revision " + dir + " already exists.");
163                         }
164                     }
165                 }
166
167                 // make hpath/1/2/3/h4 out of 1234
168
pathBuffer.append("/h").append(nextHnStr.charAt(nextHnStr.length() - 1));
169                 String JavaDoc fullPath = pathBuffer.toString();
170                 
171                 return new UriHandler(fullPath);
172                 
173             } else {
174                 return new UriHandler(uniqueUri);
175             }
176         } else {
177
178             NodeRevisionDescriptors hpathNrds = content.retrieve(sToken, hpath);
179             NodeRevisionDescriptor hpathNrd = content.retrieve(sToken, hpathNrds);
180             NodeProperty nextHnProp = hpathNrd.getProperty(I_NEXT_HISTORY_NAME, NamespaceCache.SLIDE_URI);
181
182             if (UriHandler.useHistoryCollectionHack) {
183
184                 if (nextHnProp == null || nextHnProp.getValue() == null) {
185                     // XXX start with historyCollectionHackChildren to assure no
186
// resources are created directly in history folder
187
nextHnProp = new NodeProperty(I_NEXT_HISTORY_NAME, "10", NamespaceCache.SLIDE_URI);
188                     nextHnProp.setKind(NodeProperty.Kind.PROTECTED);
189                     hpathNrd.setProperty(nextHnProp);
190                 }
191
192                 String JavaDoc nextHnStr = (String JavaDoc) nextHnProp.getValue();
193                 long nextHnLong = Long.parseLong(nextHnStr);
194
195                 if (nextHnLong % 10 == 0) {
196                     // create parent collection
197
long dirNum = nextHnLong / 10;
198                     char dirChar[] = Long.toString(dirNum).toCharArray();
199                     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
200                     for (int i = 0; i < dirChar.length - 1; i++) {
201                         buf.append(dirChar[i]);
202                         buf.append('/');
203                     }
204                     buf.append(dirChar[dirChar.length - 1]);
205                     String JavaDoc dirPath = hpath + "/" + buf.toString();
206
207                     try {
208                         structure.create(sToken, new SubjectNode(), dirPath);
209                         //content.create(sToken,dirPath,true);
210
//NodeRevisionDescriptors dnrds =
211
// structure.retrieve(stoken,dirPath);
212
content.create(sToken, dirPath, new NodeRevisionDescriptor(0), null);
213                     } catch (ObjectAlreadyExistsException oae) {
214                         Domain.warn("Object " + dirPath + " already exists.");
215                     } catch (RevisionAlreadyExistException rae) {
216                         Domain.warn("Revision " + dirPath + " already exists.");
217                     }
218                 }
219
220                 StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
221                 char nextHnChar[] = nextHnStr.toCharArray();
222                 for (int i = 0; i < nextHnChar.length - 1; i++) {
223                     buf.append(nextHnChar[i]);
224                     buf.append('/');
225                 }
226                 buf.append('h');
227                 buf.append(nextHnChar[nextHnChar.length - 1]);
228
229                 result = new UriHandler(hpath + "/" + buf.toString());
230                 
231                 nextHnProp = new NodeProperty(I_NEXT_HISTORY_NAME, String.valueOf(nextHnLong + 1),
232                         NamespaceCache.SLIDE_URI);
233                 hpathNrd.setProperty(nextHnProp);
234
235             } else {
236
237                 if (nextHnProp == null) {
238                     // convert to slide namespace if this property is still
239
// in DAV: namespace
240
nextHnProp = hpathNrd.getProperty(I_NEXT_HISTORY_NAME);
241                     if (nextHnProp != null) {
242                         hpathNrd.removeProperty(nextHnProp);
243                         nextHnProp = new NodeProperty(I_NEXT_HISTORY_NAME, nextHnProp.getValue(),
244                                 NamespaceCache.SLIDE_URI);
245                         nextHnProp.setKind(NodeProperty.Kind.PROTECTED);
246                         hpathNrd.setProperty(nextHnProp);
247                     }
248                 }
249                 if (nextHnProp == null || nextHnProp.getValue() == null) {
250                     nextHnProp = new NodeProperty(I_NEXT_HISTORY_NAME, I_INITIAL_HISTORY_NAME, NamespaceCache.SLIDE_URI);
251                     nextHnProp.setKind(NodeProperty.Kind.PROTECTED);
252                     hpathNrd.setProperty(nextHnProp);
253                 }
254
255                 String JavaDoc nextHnStr = (String JavaDoc) nextHnProp.getValue();
256                 result = new UriHandler(hpath + "/" + nextHnStr);
257
258                 long nextHnLong = Long.parseLong(nextHnStr);
259                 nextHnProp = new NodeProperty(I_NEXT_HISTORY_NAME, String.valueOf(nextHnLong + 1),
260                         NamespaceCache.SLIDE_URI);
261                 hpathNrd.setProperty(nextHnProp);
262
263             }
264             content.store(sToken, hpath, hpathNrd, null); //revisionContent = null
265
return result;
266         }
267     }
268
269     
270     /**
271      * Generates the next available workingresource URI and returns an URI handler for it.
272      * @param sToken the Slide token
273      * @param nsaToken the namespace access token
274      * @param uh the URI handler of the version resource being checked-out
275      */

276     public static UriHandler
277     createNextWorkingresourceUri( SlideToken sToken, NamespaceAccessToken nsaToken, UriHandler uh )
278     throws ObjectNotFoundException, AccessDeniedException, ObjectLockedException,
279     LinkedObjectNotFoundException, ServiceAccessException,
280     RevisionDescriptorNotFoundException, RevisionNotFoundException, VetoException {
281         
282         UriHandler result = null;
283         String JavaDoc nsName = nsaToken.getName();
284         UriHandler wrpathHandler =
285             WorkingresourcePathHandler.getResolvedWorkingresourcePathHandler( nsName, uh );
286         Content content = nsaToken.getContentHelper();
287         String JavaDoc wrpath = wrpathHandler.toString();
288         
289         Structure structure = nsaToken.getStructureHelper();
290         String JavaDoc uniqueUri = structure.generateUniqueUri(sToken, wrpath);
291
292         if (uniqueUri != null) {
293             result = new UriHandler(uniqueUri);
294         } else {
295             NodeRevisionDescriptors wrpathNrds =
296                 content.retrieve( sToken, wrpath );
297             
298             NodeRevisionDescriptor wrpathNrd =
299                 content.retrieve( sToken, wrpathNrds );
300             
301             NodeProperty nextWrnProp = wrpathNrd.getProperty(I_NEXT_WORKINGRESOURCE_NAME,
302                                                              NamespaceCache.SLIDE_URI);
303             if (nextWrnProp == null) {
304                 // convert to slide namespace if this property is still
305
// in DAV: namespace
306
nextWrnProp = wrpathNrd.getProperty( I_NEXT_WORKINGRESOURCE_NAME );
307                 if (nextWrnProp != null) {
308                     wrpathNrd.removeProperty(nextWrnProp);
309                     nextWrnProp = new NodeProperty(I_NEXT_WORKINGRESOURCE_NAME,
310                                                    nextWrnProp.getValue(),
311                                                    NamespaceCache.SLIDE_URI);
312                     nextWrnProp.setKind( NodeProperty.Kind.PROTECTED );
313                     wrpathNrd.setProperty( nextWrnProp );
314                 }
315             }
316             
317             if( nextWrnProp == null || nextWrnProp.getValue() == null ) {
318                 nextWrnProp =
319                     new NodeProperty(I_NEXT_WORKINGRESOURCE_NAME,
320                                      I_INITIAL_WORKINGRESOURCE_NAME,
321                                      NamespaceCache.SLIDE_URI );
322                 nextWrnProp.setKind( NodeProperty.Kind.PROTECTED );
323                 wrpathNrd.setProperty( nextWrnProp );
324             }
325             
326             String JavaDoc nextWrnStr = (String JavaDoc)nextWrnProp.getValue();
327             result = new UriHandler( wrpath+"/"+nextWrnStr );
328             
329             long nextWrnLong = Long.parseLong( nextWrnStr );
330             nextWrnProp = new NodeProperty(I_NEXT_WORKINGRESOURCE_NAME,
331                                            String.valueOf(nextWrnLong + 1),
332                                            NamespaceCache.SLIDE_URI );
333             wrpathNrd.setProperty( nextWrnProp );
334             
335             content.store( sToken, wrpath, wrpathNrd, null ); //revisionContent = null
336
}
337         return result;
338     }
339     
340     /**
341      * Creates a VR URI for the specified version in the specified history and
342      * returns an URI handler for it.
343      */

344     public static UriHandler
345         createVersionUri( UriHandler vhrUri, String JavaDoc version ) {
346         return new UriHandler( vhrUri, version );
347     }
348     
349     /**
350      * Registerd stores: namespaceName -> (scope -> store)
351      */

352     protected static Map JavaDoc registeredStores = new HashMap JavaDoc();
353     protected static Set JavaDoc allScopes = new HashSet JavaDoc();
354     protected static Set JavaDoc allStoreNames = new HashSet JavaDoc();
355     
356     /**
357      * Called when a Namespace registers a Store
358      */

359     public static void notifyStoreCreated( String JavaDoc namespaceName, String JavaDoc scope, String JavaDoc storeName ) {
360         Map JavaDoc storesInNamespace = (Map JavaDoc)registeredStores.get( namespaceName );
361         if( storesInNamespace == null ) {
362             storesInNamespace = new HashMap JavaDoc();
363             registeredStores.put( namespaceName, storesInNamespace );
364         }
365         UriHandler scopeUh = new UriHandler(scope);
366         storesInNamespace.put( scopeUh, storeName );
367         allScopes.add( scopeUh );
368         allStoreNames.add( storeName );
369     }
370     
371     /**
372      * Return the best matching scope for the specified URI in the specified namespace
373      */

374     protected static UriHandler bestMatchingScope( String JavaDoc namespaceName, UriHandler uh ) {
375         UriHandler result = null;
376         Map JavaDoc storesInNamespace = (Map JavaDoc)registeredStores.get( namespaceName );
377         Iterator JavaDoc i = storesInNamespace.keySet().iterator();
378         int matchSize = 0;
379         while( i.hasNext() ) {
380             UriHandler scopeUh = (UriHandler)i.next();
381             if( scopeUh.isAncestorOf(uh) && scopeUh.length() > matchSize ) {
382                 matchSize = scopeUh.length();
383                 result = scopeUh;
384             }
385         }
386         return result;
387     }
388     
389     private String JavaDoc uri;
390     private String JavaDoc[] uriTokens = null;
391     
392     
393     /**
394      * Protected constructor
395      */

396     protected UriHandler( String JavaDoc uri ) {
397         this.uri = uri;
398         
399         StringTokenizer JavaDoc t = new StringTokenizer JavaDoc( uri, "/" );
400         int n = t.countTokens();
401         this.uriTokens = new String JavaDoc[n];
402         for( int i = 0; i < n; i++ )
403             uriTokens[i] = t.nextToken();
404     }
405     
406     /**
407      * Protected constructor
408      */

409     protected UriHandler( String JavaDoc[] uriTokens, int number ) {
410         String JavaDoc[] t = new String JavaDoc[number];
411         for( int i = 0; i < number; i++ )
412             t[i] = uriTokens[i];
413         this.uriTokens = t;
414     }
415     
416     /**
417      * Protected constructor for VR URIs
418      */

419     protected UriHandler( UriHandler vhrUri, String JavaDoc versionName ) {
420         int n = vhrUri.length();
421         this.uriTokens = new String JavaDoc[n + 1];
422         for( int i = 0; i < n; i++ )
423             uriTokens[i] = vhrUri.getUriTokens()[i];
424         uriTokens[n] = versionName;
425     }
426     
427     /**
428      * Get the URI as string
429      */

430     public String JavaDoc getUri() {
431         if( uri == null )
432             uri = toString();
433             return uri;
434     }
435     
436     /**
437      * Get the name (last token)
438      */

439     public String JavaDoc getName() {
440         if( uriTokens.length == 0 )
441             return null;
442         return uriTokens[uriTokens.length - 1];
443     }
444     
445     /**
446      * Get the UriHandler for the parent URI, null if this URI is root.
447      */

448     public UriHandler getParentUriHandler() {
449         if( uriTokens.length == 0 )
450             return null;
451         return new UriHandler( uriTokens, uriTokens.length - 1 );
452     }
453     
454     /**
455      * Return true, if this handler's tokens are the initial tokens of the specified handler.
456      * Example: /a/b is ancestor of /a/b/x/y/z
457      */

458     public boolean isAncestorOf( UriHandler uh ) {
459         if( uriTokens.length > uh.length() )
460                     return false;
461         for( int i = 0; i < uriTokens.length; i++ )
462             if( !uriTokens[i].equals(uh.getUriTokens()[i]) )
463         return false;
464         return true;
465     }
466     
467     /**
468      * Get the tokens
469      */

470     public String JavaDoc[] getUriTokens() {
471         return uriTokens;
472     }
473     
474     /**
475      * Return true, if this URI determines the root.
476      */

477     public boolean isRootUri() {
478         return (uriTokens.length == 0);
479     }
480     
481     /**
482      * Return true, if this URI determines the history path.
483      */

484     public boolean isHistoryPathUri() {
485         HistoryPathHandler hpathHandler = HistoryPathHandler.getHistoryPathHandler();
486         return hpathHandler.isHistoryPathUri( this );
487     }
488     
489     /**
490      * Return true, if this is a history URI
491      */

492     public boolean isHistoryUri() {
493         HistoryPathHandler hpathHandler = HistoryPathHandler.getHistoryPathHandler();
494
495         if (UriHandler.useHistoryCollectionHack) {
496             if (hpathHandler.isAncestorOf(this)) {
497                 String JavaDoc[] hpathTokens = hpathHandler.getUriTokens();
498                 if (uriTokens.length < hpathTokens.length + 1) {
499                     return false;
500                 }
501                 String JavaDoc h = uriTokens[uriTokens.length - 1];
502                 return (h.charAt(0) == 'h');
503             } else {
504                 return false;
505             }
506         } else {
507             String JavaDoc[] hpathTokens = hpathHandler.getUriTokens();
508
509             if ((hpathTokens.length + 1) == uriTokens.length) {
510                 if (hpathHandler.isHistoryPathUri(getParentUriHandler())) {
511                     try {
512                         Integer.parseInt(uriTokens[uriTokens.length - 1]);
513                         return true;
514                     } catch (NumberFormatException JavaDoc x) {
515                     }
516                     ;
517                 }
518             }
519             return false;
520         }
521     }
522     
523     /**
524      * Return true, if this is a version URI
525      */

526     public boolean isVersionUri() {
527         HistoryPathHandler hpathHandler = HistoryPathHandler.getHistoryPathHandler();
528
529         if (UriHandler.useHistoryCollectionHack) {
530             /*
531              * Version Uris look like /my/history/path/1/2/3/h9/1.0
532              */

533
534             if (hpathHandler.isAncestorOf(this)) {
535                 String JavaDoc[] hpathTokens = hpathHandler.getUriTokens();
536                 if (uriTokens.length < hpathTokens.length + 2) {
537                     return false;
538                 }
539                 String JavaDoc h = uriTokens[uriTokens.length - 2];
540                 return (h.charAt(0) == 'h');
541             } else {
542                 return false;
543             }
544         } else {
545
546             String JavaDoc[] hpathTokens = hpathHandler.getUriTokens();
547
548             if ((hpathTokens.length + 2) == uriTokens.length) {
549                 if (getParentUriHandler().isHistoryUri())
550                     return true;
551             }
552             return false;
553         }
554     }
555     
556     /**
557      * Return true, if this URI determines the workspace path.
558      */

559     public boolean isWorkspacePathUri() {
560         WorkspacePathHandler wspathHandler = WorkspacePathHandler.getWorkspacePathHandler();
561         return wspathHandler.isWorkspacePathUri( this );
562     }
563         
564     /**
565      * Return true, if this is a valid workspace URI
566      */

567     public boolean isWorkspaceUri() {
568         WorkspacePathHandler wspathHandler = WorkspacePathHandler.getWorkspacePathHandler();
569         String JavaDoc[] wspathTokens = wspathHandler.getUriTokens();
570         
571         if( (wspathTokens.length + 1) == uriTokens.length ) {
572             if( wspathHandler.isWorkspacePathUri(getParentUriHandler()) ) {
573                 return true;
574             }
575         }
576                     return false;
577             }
578     
579     /**
580      * Return true, if this is an URI of a resource in a workspace
581      */

582     public boolean isResourceInWorkspaceUri() {
583         WorkspacePathHandler wspathHandler = WorkspacePathHandler.getWorkspacePathHandler();
584         String JavaDoc[] wspathTokens = wspathHandler.getUriTokens();
585         
586         if( (wspathTokens.length + 1) < uriTokens.length ) {
587             UriHandler p = new UriHandler( uriTokens, wspathTokens.length + 1 );
588             if( p.isWorkspaceUri() ) {
589             return true;
590         }
591         }
592         return false;
593     }
594     
595     /**
596      * Return true, if this URI determines the workingresource path.
597      */

598     public boolean isWorkingresourcePathUri() {
599         WorkingresourcePathHandler wrpathHandler = WorkingresourcePathHandler.getWorkingresourcePathHandler();
600         return wrpathHandler.isWorkingresourcePathUri( this );
601     }
602     
603     /**
604      * Return true, if this is a valid workingresource URI
605      */

606     public boolean isWorkingresourceUri() {
607         WorkingresourcePathHandler wrpathHandler = WorkingresourcePathHandler.getWorkingresourcePathHandler();
608         String JavaDoc[] wrpathTokens = wrpathHandler.getUriTokens();
609         
610         if( (wrpathTokens.length + 1) == uriTokens.length ) {
611             if( wrpathHandler.isWorkingresourcePathUri(getParentUriHandler()) ) {
612                 try {
613                     Integer.parseInt( uriTokens[uriTokens.length - 1] );
614             return true;
615         }
616                 catch( NumberFormatException JavaDoc x ) {};
617     }
618         }
619         return false; }
620     
621     /**
622      * Returns the workspace identifier from the uri
623      * @pre isWorkspaceUri()
624      */

625     public String JavaDoc getWorkspaceName() {
626         UriHandler wspathHandler = WorkspacePathHandler.getWorkspacePathHandler();
627         String JavaDoc[] wspathTokens = wspathHandler.getUriTokens();
628         
629         if( !isWorkspaceUri() )
630             throw new IllegalStateException JavaDoc(
631                 "URI "+uri+" is not a workspace URI" );
632         
633         return uriTokens[wspathTokens.length];
634     }
635     
636     /**
637      * Returns the history identifier from the uri
638      * @pre isHistoryUri() || isVersionUri()
639      */

640     public String JavaDoc getHistoryName() {
641         UriHandler hpathHandler = HistoryPathHandler.getHistoryPathHandler();
642         String JavaDoc[] hpathTokens = hpathHandler.getUriTokens();
643
644         if (UriHandler.useHistoryCollectionHack) {
645             if (isHistoryUri()) {
646                 return getName();
647             } else if (isVersionUri()) {
648                 return uriTokens[uriTokens.length - 2];
649             } else {
650                 throw new IllegalStateException JavaDoc("URI " + toString() + " is neither history nor version URI");
651             }
652         } else {
653             if (!isHistoryUri() && !isVersionUri())
654                 throw new IllegalStateException JavaDoc("URI " + uri + " is neither history nor version URI");
655
656             return uriTokens[hpathTokens.length];
657         }
658     }
659     
660     /**
661      * Returns the version name from the uri
662      *
663      * @pre isVersionUri()
664      */

665     public String JavaDoc getVersionName() {
666         if (!isVersionUri())
667             throw new IllegalStateException JavaDoc("URI " + uri + " is not a version URI");
668
669         if (UriHandler.useHistoryCollectionHack) {
670             return getName();
671         } else {
672             UriHandler hpathHandler = HistoryPathHandler.getHistoryPathHandler();
673             String JavaDoc[] hpathTokens = hpathHandler.getUriTokens();
674
675             return uriTokens[hpathTokens.length + 1];
676         }
677     }
678     
679     /**
680      * Returns the associated history URI of this version URI
681      *
682      * @pre isVersionUri()
683      */

684     public String JavaDoc getAssociatedHistoryUri() {
685         if( !isVersionUri() )
686             throw new IllegalStateException JavaDoc(
687                 "URI "+uri+" is not a version URI" );
688         
689         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
690         for( int i = 0; i < (uriTokens.length - 1); i++ ) {
691             b.append( "/" );
692             b.append( uriTokens[i] );
693         }
694         return b.toString();
695     }
696
697     /**
698      * Returns the associated workspace URI if this URI represents a resource
699      * in a workspace; if the resource is a workspace itself, its own URI is returned.
700      * @return the associated workspace URI; null, if the resource is neither a workspace itself
701      * nor contained in a workspace
702      */

703     public String JavaDoc getAssociatedWorkspaceUri() {
704         if( !isWorkspaceUri() && !isResourceInWorkspaceUri() )
705             return null;
706         
707         UriHandler wspathHandler = WorkspacePathHandler.getWorkspacePathHandler();
708         String JavaDoc[] wspathTokens = wspathHandler.getUriTokens();
709         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
710         for( int i = 0; i < (wspathTokens.length + 1); i++ ) {
711             b.append( "/" );
712             b.append( uriTokens[i] );
713         }
714         return b.toString();
715     }
716     
717     /**
718      * Returns the associated <b>base</b> store name.
719      * Examples (assume historyPath is parameterized: /history/${store}):
720      * uri=/history/mycoll/1 ---> result=mycoll // history URI
721      * uri=/history/files/1/1.17 ---> result=files // version URI
722      * uri=/files/foo/bar.xml ---> result=files // best matching scope
723      * If the URI is e.g. a version URI but historyPath is not parameterized, no associated
724      * base store can be determined.
725      *
726      * @return the associated base store name.
727      */

728     public String JavaDoc getAssociatedBaseStoreName( String JavaDoc namespaceName ) {
729         String JavaDoc result = "";
730         
731         if( isHistoryPathUri() || isHistoryUri() || isVersionUri() ) {
732             if( HistoryPathHandler.parameterized ) {
733                 result = extractStoreName( HistoryPathHandler.historyPathHandler );
734             }
735             else {
736                 Domain.info( "Cannot determine base store name for URI "+this );
737             }
738         }
739         else if( isWorkspacePathUri() || isWorkspaceUri() || isResourceInWorkspaceUri() ) {
740             if( WorkspacePathHandler.parameterized ) {
741                 result = extractStoreName( WorkspacePathHandler.workspacePathHandler );
742             }
743             else {
744                 Domain.info( "Cannot determine base store name for URI "+this );
745             }
746         }
747         else if( isWorkingresourcePathUri() || isWorkingresourceUri() ) {
748             if( WorkingresourcePathHandler.parameterized ) {
749                 result = extractStoreName( WorkingresourcePathHandler.workingresourcePathHandler );
750             }
751             else {
752                 Domain.info( "Cannot determine base store name for URI "+this );
753             }
754         }
755         else {
756             UriHandler scopeUh = bestMatchingScope( namespaceName, this );
757             Map JavaDoc storesInNamespace = (Map JavaDoc)registeredStores.get( namespaceName );
758             result = (String JavaDoc)storesInNamespace.get( scopeUh );
759         }
760         return result;
761     }
762     
763     /**
764      * Extract the store name from a parameterized path.
765      */

766     private String JavaDoc extractStoreName( UriHandler pUh ) {
767         String JavaDoc result = "";
768         String JavaDoc[] pTokens = pUh.getUriTokens();
769         int ptIndex = -1;
770         for( int i = 0; i < pTokens.length; i++ ) {
771             if( pTokens[i].indexOf(I_STORE_PLACE_HOLDER_IN_PATH) >= 0 ) {
772                 ptIndex = i;
773                 break;
774             }
775         }
776         if( ptIndex >= 0 ) {
777             result = extractStoreName( pTokens[ptIndex], uriTokens[ptIndex] );
778         }
779         return result;
780     }
781     
782     /**
783      * Extract the store name from a parameterized path.
784      */

785     private String JavaDoc extractStoreName( String JavaDoc prmStr, String JavaDoc srcStr ) {
786         String JavaDoc tail = null;
787         int start = prmStr.indexOf( I_STORE_PLACE_HOLDER_IN_PATH );
788         int endP = start + I_STORE_PLACE_HOLDER_IN_PATH.length();
789         int endS = -1;
790         
791         if( endP < prmStr.length() ) {
792             tail = prmStr.substring( endP );
793             endS = srcStr.lastIndexOf( tail );
794         }
795         else
796             endS = srcStr.length();
797         
798         return srcStr.substring( start, endS );
799     }
800     
801     /**
802      * Returns <code>true</code> if the usage of this URI
803      * is restricted by the server, means the server may refuse to
804      * create resource at this URI due to a client request like <code>PUT</code>.
805      * (e.g. an URI that determines a resource in a history folder).
806      *
807      * @return <code>true</code> if the usage of the URI is restricted.
808      */

809     public boolean isRestrictedUri() {
810         if( isRootUri() )
811             return true;
812         if( isVersionUri() )
813             return true;
814         if( isHistoryUri() )
815             return true;
816         if( isHistoryPathUri() )
817             return true;
818         if( isWorkspaceUri() )
819             return true;
820         if( isWorkspacePathUri() )
821             return true;
822         if( isWorkingresourceUri() )
823             return true;
824         if( isWorkingresourcePathUri() )
825             return true;
826         
827         UriHandler p = getParentUriHandler();
828         if( p != null ) {
829             if( p.isRootUri() )
830                 return true;
831             if( p.isHistoryPathUri() )
832                 return true;
833             if( p.isWorkingresourcePathUri() )
834                 return true;
835         }
836         return false;
837     }
838
839     /**
840      * Overwrites Object.equals()
841      */

842     public boolean equals( Object JavaDoc o ) {
843         if( !(o instanceof UriHandler) )
844             return false;
845         String JavaDoc[] oTokens = ((UriHandler)o).getUriTokens();
846         if( oTokens.length != uriTokens.length )
847             return false;
848         for( int i = 0; i < uriTokens.length; i++ )
849             if( !oTokens[i].equals(uriTokens[i]) )
850                 return false;
851         return true;
852     }
853
854     /**
855      * Overwrites Object.hashCode()
856      */

857     public int hashCode() {
858         return uriTokens.length;
859     }
860
861     /**
862      * Get the length of this handler.
863      */

864     public int length() {
865         return uriTokens.length;
866     }
867     
868     /**
869      * Return string representation of the object
870      */

871     public String JavaDoc toString() {
872         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
873         if( uriTokens.length == 0 )
874             b.append("/");
875         else
876             for( int i = 0; i < uriTokens.length; i++ )
877             b.append("/").append( uriTokens[i] );
878         return b.toString();
879     }
880 }
881
882
Popular Tags