01: /******************************************************************************
02: * JBoss, a division of Red Hat *
03: * Copyright 2006, Red Hat Middleware, LLC, and individual *
04: * contributors as indicated by the @authors tag. See the *
05: * copyright.txt in the distribution for a full listing of *
06: * individual contributors. *
07: * *
08: * This is free software; you can redistribute it and/or modify it *
09: * under the terms of the GNU Lesser General Public License as *
10: * published by the Free Software Foundation; either version 2.1 of *
11: * the License, or (at your option) any later version. *
12: * *
13: * This software is distributed in the hope that it will be useful, *
14: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16: * Lesser General Public License for more details. *
17: * *
18: * You should have received a copy of the GNU Lesser General Public *
19: * License along with this software; if not, write to the Free *
20: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
21: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
22: ******************************************************************************/package org.jboss.portal.server.servlet;
23:
24: /**
25: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
26: * @version $Revision: 8784 $
27: */
28: public class PathParser {
29:
30: public PathMappingResult map(PathMapping mapping, String path)
31: throws IllegalArgumentException {
32: if (mapping == null) {
33: throw new IllegalArgumentException(
34: "No null mapping allowed");
35: }
36: if (path == null) {
37: throw new IllegalArgumentException(
38: "No null request path allowed");
39: }
40: if (path.length() == 0) {
41: throw new IllegalArgumentException(
42: "No empty request path allowed");
43: }
44: Object target = mapping.getRoot();
45: if (target == null) {
46: return new PathMappingResult(target, null, path);
47: }
48: if ("/".equals(path)) {
49: return new PathMappingResult(target, "", "/");
50: } else {
51: int previousSlashPos = 0;
52: StringBuffer matchedPath = new StringBuffer();
53: while (true) {
54: int nextSlashPos = path.indexOf('/',
55: previousSlashPos + 1);
56: if (nextSlashPos == -1) {
57: String atom = path.substring(previousSlashPos + 1);
58: Object o = mapping.getChild(target, atom);
59: if (o != null) {
60: target = o;
61: matchedPath.append('/').append(atom);
62: String remainingPath = "";
63: return new PathMappingResult(target,
64: matchedPath.toString(), remainingPath);
65: } else {
66: String remainingPath = "/" + atom;
67: return new PathMappingResult(target,
68: matchedPath.toString(), remainingPath);
69: }
70: } else if (nextSlashPos == previousSlashPos + 1) {
71: previousSlashPos = nextSlashPos;
72: } else {
73: String atom = path.substring(previousSlashPos + 1,
74: nextSlashPos);
75: Object o = mapping.getChild(target, atom);
76: if (o != null) {
77: target = o;
78: matchedPath.append('/').append(atom);
79: previousSlashPos = nextSlashPos;
80: } else {
81:
82: String remainingPath = path
83: .substring(previousSlashPos);
84: return new PathMappingResult(target,
85: matchedPath.toString(), remainingPath);
86: }
87: }
88: }
89: }
90: }
91: }
|