|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
Location.java | 83.3% | 91.7% | 100% | 89.8% |
|
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | * | |
17 | * $Id: Location.java 551434 2007-06-28 03:25:05Z vgritsenko $ | |
18 | */ | |
19 | ||
20 | package org.apache.xindice.webadmin; | |
21 | ||
22 | import org.apache.xindice.core.Collection; | |
23 | import org.apache.xindice.core.DBException; | |
24 | import org.apache.xindice.core.Database; | |
25 | ||
26 | /** | |
27 | * Helper class for parsing path information to get database, collection | |
28 | * and resource name. | |
29 | * | |
30 | * @version $Revision: 551434 $, $Date: 2007-06-27 20:25:05 -0700 (Wed, 27 Jun 2007) $ | |
31 | */ | |
32 | public class Location { | |
33 | private Collection collection; | |
34 | private String name; | |
35 | private boolean isRoot; | |
36 | ||
37 | /** | |
38 | * Create Location instance out of path information. There are several | |
39 | * possible outcomes: | |
40 | * | |
41 | * <ul> | |
42 | * <li>Path is empty or "/" - both collection and name are null, isRoot | |
43 | * equals true; | |
44 | * <li>Path matches some collection's canonical name - collection is | |
45 | * set to that collection, name is null, isRoot equals false; | |
46 | * <li>Path matches some document's canonical name - collection is set | |
47 | * to the document's collection, name is document name, isRoot equals | |
48 | * false; | |
49 | * <li>Path could not be recognized, no database/collection match found - | |
50 | * both collection and name are null, isRoot equals false | |
51 | * </ul> | |
52 | * | |
53 | * In case there are collection and document that have the same name, | |
54 | * collection has a precedence over the document. | |
55 | * For example, if path is /db/col1/test, and collection col1 contains | |
56 | * child collection test and a document test, collection test will be | |
57 | * returned, not a document. | |
58 | * | |
59 | * @param path Path string | |
60 | * @throws DBException | |
61 | */ | |
62 | 58 | public Location(String path) throws DBException { |
63 | 58 | if (path == null || path.length() == 0 || path.equals("/")) { |
64 | 1 | isRoot = true; |
65 | 1 | return; |
66 | } | |
67 | ||
68 | 57 | Database db = getDatabase(path); |
69 | 57 | if (db == null) { |
70 | 6 | collection = null; |
71 | 6 | name = null; |
72 | 6 | return; |
73 | } | |
74 | ||
75 | 51 | StringBuffer buf = new StringBuffer(path); |
76 | ||
77 | // strip database name | |
78 | 51 | String dbName = db.getName(); |
79 | 51 | buf.delete(0, dbName.length() + 1); |
80 | ||
81 | 51 | Collection col; |
82 | 51 | if (buf.length() == 0 || (buf.length() == 1 && buf.charAt(0) == '/')) { |
83 | 1 | col = db; |
84 | } else { | |
85 | 50 | col = db.getCollection(buf.toString()); |
86 | } | |
87 | ||
88 | 51 | String resourceName = null; |
89 | 51 | if (col == null) { // no collection found -> try resource |
90 | 32 | if (buf.charAt(buf.length() - 1) == '/') { |
91 | 0 | buf.deleteCharAt(buf.length() - 1); |
92 | } | |
93 | ||
94 | 32 | int split = buf.lastIndexOf("/"); |
95 | 32 | resourceName = buf.substring(split + 1, buf.length()); |
96 | 32 | String colPath = buf.substring(0, split); |
97 | ||
98 | 32 | if (colPath.length() > 0) { |
99 | 32 | col = db.getCollection(colPath); |
100 | } else { | |
101 | 0 | col = db; |
102 | } | |
103 | } | |
104 | ||
105 | 51 | collection = col; |
106 | 51 | if (collection != null) { |
107 | 48 | this.name = resourceName; |
108 | } | |
109 | } | |
110 | ||
111 | 57 | private Database getDatabase(String path) { |
112 | 57 | if (path.charAt(0) != '/') { |
113 | 0 | return null; |
114 | } | |
115 | ||
116 | 57 | int end = path.indexOf('/', 1); |
117 | 57 | String dbName = (end != -1) ? path.substring(1, end) : path.substring(1); |
118 | ||
119 | 57 | return Database.getDatabase(dbName); |
120 | } | |
121 | ||
122 | 67 | public Collection getCollection() { |
123 | 67 | return collection; |
124 | } | |
125 | ||
126 | 60 | public String getName() { |
127 | 60 | return name; |
128 | } | |
129 | ||
130 | 58 | public boolean isRoot() { |
131 | 58 | return isRoot; |
132 | } | |
133 | } |
|